blob: 411dc6588b355f2bdfcc326df737be64c564f211 [file] [log] [blame]
Yi Konga86c7e32020-06-03 23:24:36 +08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuif1d91d22023-04-27 12:50:59 -070017#include <time.h>
18
19#include <android-base/file.h>
Yabin Cui6e03c482024-03-15 16:18:28 -070020#include <android-base/properties.h>
Yabin Cuif1d91d22023-04-27 12:50:59 -070021#include <android-base/stringprintf.h>
Yabin Cuiffc25f82023-12-13 11:04:40 -080022#include <android-base/strings.h>
Yabin Cuif1d91d22023-04-27 12:50:59 -070023
Yabin Cui0d539c12022-02-17 09:57:42 -080024#include <wakelock/wakelock.h>
Yi Kongf8abfdb2021-02-18 16:23:37 +080025#include <include/simpleperf_profcollect.hpp>
Yi Konga86c7e32020-06-03 23:24:36 +080026
Yabin Cui0b2673c2020-07-15 14:09:34 -070027#include "ETMRecorder.h"
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020028#include "command.h"
Yi Konga86c7e32020-06-03 23:24:36 +080029#include "event_attr.h"
30#include "event_fd.h"
Yabin Cuiffc25f82023-12-13 11:04:40 -080031#include "event_selection_set.h"
Yi Konga86c7e32020-06-03 23:24:36 +080032#include "event_type.h"
33
Yi Kongf8abfdb2021-02-18 16:23:37 +080034using namespace simpleperf;
Yi Konga86c7e32020-06-03 23:24:36 +080035
Yabin Cui34227b22023-07-20 19:40:28 +000036namespace {
37
38class CommandRegister {
39 public:
40 CommandRegister() {
41 RegisterRecordCommand();
42 RegisterInjectCommand();
43 }
44};
45
46CommandRegister command_register;
47
48} // namespace
49
Yabin Cuiffc25f82023-12-13 11:04:40 -080050bool IsETMDriverAvailable() {
Yabin Cuif1d91d22023-04-27 12:50:59 -070051 bool result = ETMRecorder::GetInstance().IsETMDriverAvailable();
52 LOG(INFO) << "HasDriverSupport result " << result;
53 return result;
Yabin Cuif158a752022-01-10 15:35:59 -080054}
55
Yabin Cuiffc25f82023-12-13 11:04:40 -080056bool IsETMDeviceAvailable() {
Yabin Cuif158a752022-01-10 15:35:59 -080057 auto result = ETMRecorder::GetInstance().CheckEtmSupport();
58 if (!result.ok()) {
Yabin Cuif1d91d22023-04-27 12:50:59 -070059 LOG(INFO) << "HasDeviceSupport check failed: " << result.error();
Yabin Cui0b2673c2020-07-15 14:09:34 -070060 return false;
61 }
Yi Kong937d0452020-07-14 16:50:44 +080062 const EventType* type = FindEventTypeByName("cs-etm", false);
63 if (type == nullptr) {
Yabin Cuif1d91d22023-04-27 12:50:59 -070064 LOG(INFO) << "HasDeviceSupport check failed: no etm event";
Yi Kong937d0452020-07-14 16:50:44 +080065 return false;
66 }
Yabin Cuif1d91d22023-04-27 12:50:59 -070067 bool ret = IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name);
68 LOG(INFO) << "HasDeviceSupport result " << ret;
69 return ret;
Yi Konga86c7e32020-06-03 23:24:36 +080070}
71
Yabin Cuiffc25f82023-12-13 11:04:40 -080072bool IsLBRAvailable() {
73 return IsBranchSamplingSupported();
74}
75
76static std::vector<std::string> ConvertArgs(const char** args, int arg_count) {
77 std::vector<std::string> cmd_args(arg_count);
78 for (int i = 0; i < arg_count; ++i) {
79 cmd_args[i] = args[i];
80 }
81 return cmd_args;
82}
83
84bool RunRecordCmd(const char** args, int arg_count) {
85 std::vector<std::string> cmd_args = ConvertArgs(args, arg_count);
86 LOG(INFO) << "Record " << android::base::Join(cmd_args, " ");
Yabin Cui0d539c12022-02-17 09:57:42 -080087 // The kernel may panic when trying to hibernate or hotplug CPUs while collecting
88 // ETM data. So get wakelock to keep the CPUs on.
89 auto wakelock = android::wakelock::WakeLock::tryGet("profcollectd");
90 if (!wakelock) {
Yabin Cuif1d91d22023-04-27 12:50:59 -070091 LOG(ERROR) << "Record failed: Failed to request wakelock.";
Yabin Cui0d539c12022-02-17 09:57:42 -080092 return false;
93 }
Yabin Cuiffc25f82023-12-13 11:04:40 -080094 bool result = CreateCommandInstance("record")->Run(cmd_args);
Yabin Cuif1d91d22023-04-27 12:50:59 -070095 LOG(INFO) << "Record result " << result;
96 return result;
Yi Konga86c7e32020-06-03 23:24:36 +080097}
98
Yabin Cuiffc25f82023-12-13 11:04:40 -080099bool RunInjectCmd(const char** args, int arg_count) {
100 std::vector<std::string> cmd_args = ConvertArgs(args, arg_count);
101 LOG(INFO) << "Inject " << android::base::Join(cmd_args, " ");
102 bool result = CreateCommandInstance("inject")->Run(cmd_args);
Yabin Cuif1d91d22023-04-27 12:50:59 -0700103 LOG(INFO) << "Inject result " << result;
104 return result;
105}
106
107static android::base::unique_fd log_fd;
108static android::base::LogFunction saved_log_func;
109
110static void FileLogger(android::base::LogId id, android::base::LogSeverity severity,
111 const char* tag, const char* file, unsigned int line, const char* message) {
112 if (log_fd.ok()) {
113 static const char log_characters[] = "VDIWEFF";
114 char severity_char = log_characters[severity];
115 struct tm now;
116 time_t t = time(nullptr);
117 localtime_r(&t, &now);
118 char timestamp[32];
119 strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
120 std::string s = android::base::StringPrintf("%s %c %s %s:%u] %s\n", tag, severity_char,
121 timestamp, file, line, message);
122 WriteStringToFd(s, log_fd);
123 }
124 saved_log_func(id, severity, tag, file, line, message);
125}
126
127void SetLogFile(const char* filename) {
128 int fd = TEMP_FAILURE_RETRY(open(filename, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, 0600));
129 if (fd == -1) {
130 PLOG(ERROR) << "failed to open " << filename;
131 return;
132 }
133 log_fd.reset(fd);
134 saved_log_func = SetLogger(FileLogger);
135}
136
137void ResetLogFile() {
138 log_fd.reset();
139 SetLogger(std::move(saved_log_func));
Yi Konga86c7e32020-06-03 23:24:36 +0800140}