blob: 9cffbddc569d588b1fa0d383c90452b2fa80aa54 [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 Cui9eb30932023-04-27 12:50:59 -070017#include <time.h>
18
19#include <android-base/file.h>
20#include <android-base/stringprintf.h>
21
Yabin Cui0d539c12022-02-17 09:57:42 -080022#include <wakelock/wakelock.h>
Yi Kongf8abfdb2021-02-18 16:23:37 +080023#include <include/simpleperf_profcollect.hpp>
Yi Konga86c7e32020-06-03 23:24:36 +080024
Yabin Cui0b2673c2020-07-15 14:09:34 -070025#include "ETMRecorder.h"
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020026#include "command.h"
Yi Konga86c7e32020-06-03 23:24:36 +080027#include "event_attr.h"
28#include "event_fd.h"
29#include "event_type.h"
30
Yi Kongf8abfdb2021-02-18 16:23:37 +080031using namespace simpleperf;
Yi Konga86c7e32020-06-03 23:24:36 +080032
Yabin Cuif158a752022-01-10 15:35:59 -080033bool HasDriverSupport() {
Yabin Cui9eb30932023-04-27 12:50:59 -070034 bool result = ETMRecorder::GetInstance().IsETMDriverAvailable();
35 LOG(INFO) << "HasDriverSupport result " << result;
36 return result;
Yabin Cuif158a752022-01-10 15:35:59 -080037}
38
39bool HasDeviceSupport() {
40 auto result = ETMRecorder::GetInstance().CheckEtmSupport();
41 if (!result.ok()) {
Yabin Cui9eb30932023-04-27 12:50:59 -070042 LOG(INFO) << "HasDeviceSupport check failed: " << result.error();
Yabin Cui0b2673c2020-07-15 14:09:34 -070043 return false;
44 }
Yi Kong937d0452020-07-14 16:50:44 +080045 const EventType* type = FindEventTypeByName("cs-etm", false);
46 if (type == nullptr) {
Yabin Cui9eb30932023-04-27 12:50:59 -070047 LOG(INFO) << "HasDeviceSupport check failed: no etm event";
Yi Kong937d0452020-07-14 16:50:44 +080048 return false;
49 }
Yabin Cui9eb30932023-04-27 12:50:59 -070050 bool ret = IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name);
51 LOG(INFO) << "HasDeviceSupport result " << ret;
52 return ret;
Yi Konga86c7e32020-06-03 23:24:36 +080053}
54
Yabin Cui4b467902023-05-02 15:12:32 -070055bool Record(const char* event_name, const char* output, float duration, const char* binary_filter) {
56 LOG(INFO) << "Record " << event_name << ", duration " << duration << ", output " << output
57 << ", binary_filter " << binary_filter;
Yabin Cui0d539c12022-02-17 09:57:42 -080058 // The kernel may panic when trying to hibernate or hotplug CPUs while collecting
59 // ETM data. So get wakelock to keep the CPUs on.
60 auto wakelock = android::wakelock::WakeLock::tryGet("profcollectd");
61 if (!wakelock) {
Yabin Cui9eb30932023-04-27 12:50:59 -070062 LOG(ERROR) << "Record failed: Failed to request wakelock.";
Yabin Cui0d539c12022-02-17 09:57:42 -080063 return false;
64 }
Yi Konga86c7e32020-06-03 23:24:36 +080065 auto recordCmd = CreateCommandInstance("record");
Yabin Cui4b467902023-05-02 15:12:32 -070066 std::vector<std::string> args = {"-a",
67 "-e",
68 event_name,
69 "--duration",
70 std::to_string(duration),
71 "--decode-etm",
72 "--exclude-perf",
73 "--binary",
74 binary_filter,
75 "-o",
76 output};
Yabin Cui9eb30932023-04-27 12:50:59 -070077 bool result = recordCmd->Run(args);
78 LOG(INFO) << "Record result " << result;
79 return result;
Yi Konga86c7e32020-06-03 23:24:36 +080080}
81
Yi Kong49eb6ff2021-11-17 00:09:02 +080082bool Inject(const char* traceInput, const char* profileOutput, const char* binary_filter) {
Yabin Cui9eb30932023-04-27 12:50:59 -070083 LOG(INFO) << "Inject traceInput " << traceInput << ", profileOutput " << profileOutput
84 << ", binary_filter " << binary_filter;
Yi Konga86c7e32020-06-03 23:24:36 +080085 auto injectCmd = CreateCommandInstance("inject");
Yabin Cui4b467902023-05-02 15:12:32 -070086 std::vector<std::string> args = {"-i", traceInput, "-o", profileOutput,
87 "--output", "branch-list", "--binary", binary_filter};
Yabin Cui9eb30932023-04-27 12:50:59 -070088 bool result = injectCmd->Run(args);
89 LOG(INFO) << "Inject result " << result;
90 return result;
91}
92
93static android::base::unique_fd log_fd;
94static android::base::LogFunction saved_log_func;
95
96static void FileLogger(android::base::LogId id, android::base::LogSeverity severity,
97 const char* tag, const char* file, unsigned int line, const char* message) {
98 if (log_fd.ok()) {
99 static const char log_characters[] = "VDIWEFF";
100 char severity_char = log_characters[severity];
101 struct tm now;
102 time_t t = time(nullptr);
103 localtime_r(&t, &now);
104 char timestamp[32];
105 strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
106 std::string s = android::base::StringPrintf("%s %c %s %s:%u] %s\n", tag, severity_char,
107 timestamp, file, line, message);
108 WriteStringToFd(s, log_fd);
109 }
110 saved_log_func(id, severity, tag, file, line, message);
111}
112
113void SetLogFile(const char* filename) {
114 int fd = TEMP_FAILURE_RETRY(open(filename, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, 0600));
115 if (fd == -1) {
116 PLOG(ERROR) << "failed to open " << filename;
117 return;
118 }
119 log_fd.reset(fd);
120 saved_log_func = SetLogger(FileLogger);
121}
122
123void ResetLogFile() {
124 log_fd.reset();
125 SetLogger(std::move(saved_log_func));
Yi Konga86c7e32020-06-03 23:24:36 +0800126}