xref: /aosp_15_r20/system/extras/simpleperf/profcollect.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <time.h>
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
20*288bf522SAndroid Build Coastguard Worker #include <android-base/properties.h>
21*288bf522SAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
22*288bf522SAndroid Build Coastguard Worker #include <android-base/strings.h>
23*288bf522SAndroid Build Coastguard Worker 
24*288bf522SAndroid Build Coastguard Worker #include <wakelock/wakelock.h>
25*288bf522SAndroid Build Coastguard Worker #include <include/simpleperf_profcollect.hpp>
26*288bf522SAndroid Build Coastguard Worker 
27*288bf522SAndroid Build Coastguard Worker #include "ETMRecorder.h"
28*288bf522SAndroid Build Coastguard Worker #include "command.h"
29*288bf522SAndroid Build Coastguard Worker #include "event_attr.h"
30*288bf522SAndroid Build Coastguard Worker #include "event_fd.h"
31*288bf522SAndroid Build Coastguard Worker #include "event_selection_set.h"
32*288bf522SAndroid Build Coastguard Worker #include "event_type.h"
33*288bf522SAndroid Build Coastguard Worker 
34*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
35*288bf522SAndroid Build Coastguard Worker 
36*288bf522SAndroid Build Coastguard Worker namespace {
37*288bf522SAndroid Build Coastguard Worker 
38*288bf522SAndroid Build Coastguard Worker class CommandRegister {
39*288bf522SAndroid Build Coastguard Worker  public:
CommandRegister()40*288bf522SAndroid Build Coastguard Worker   CommandRegister() {
41*288bf522SAndroid Build Coastguard Worker     RegisterRecordCommand();
42*288bf522SAndroid Build Coastguard Worker     RegisterInjectCommand();
43*288bf522SAndroid Build Coastguard Worker   }
44*288bf522SAndroid Build Coastguard Worker };
45*288bf522SAndroid Build Coastguard Worker 
46*288bf522SAndroid Build Coastguard Worker CommandRegister command_register;
47*288bf522SAndroid Build Coastguard Worker 
48*288bf522SAndroid Build Coastguard Worker }  // namespace
49*288bf522SAndroid Build Coastguard Worker 
IsETMDriverAvailable()50*288bf522SAndroid Build Coastguard Worker bool IsETMDriverAvailable() {
51*288bf522SAndroid Build Coastguard Worker   bool result = ETMRecorder::GetInstance().IsETMDriverAvailable();
52*288bf522SAndroid Build Coastguard Worker   LOG(INFO) << "HasDriverSupport result " << result;
53*288bf522SAndroid Build Coastguard Worker   return result;
54*288bf522SAndroid Build Coastguard Worker }
55*288bf522SAndroid Build Coastguard Worker 
IsETMDeviceAvailable()56*288bf522SAndroid Build Coastguard Worker bool IsETMDeviceAvailable() {
57*288bf522SAndroid Build Coastguard Worker   auto result = ETMRecorder::GetInstance().CheckEtmSupport();
58*288bf522SAndroid Build Coastguard Worker   if (!result.ok()) {
59*288bf522SAndroid Build Coastguard Worker     LOG(INFO) << "HasDeviceSupport check failed: " << result.error();
60*288bf522SAndroid Build Coastguard Worker     return false;
61*288bf522SAndroid Build Coastguard Worker   }
62*288bf522SAndroid Build Coastguard Worker   const EventType* type = FindEventTypeByName("cs-etm", false);
63*288bf522SAndroid Build Coastguard Worker   if (type == nullptr) {
64*288bf522SAndroid Build Coastguard Worker     LOG(INFO) << "HasDeviceSupport check failed: no etm event";
65*288bf522SAndroid Build Coastguard Worker     return false;
66*288bf522SAndroid Build Coastguard Worker   }
67*288bf522SAndroid Build Coastguard Worker   bool ret = IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name);
68*288bf522SAndroid Build Coastguard Worker   LOG(INFO) << "HasDeviceSupport result " << ret;
69*288bf522SAndroid Build Coastguard Worker   return ret;
70*288bf522SAndroid Build Coastguard Worker }
71*288bf522SAndroid Build Coastguard Worker 
IsLBRAvailable()72*288bf522SAndroid Build Coastguard Worker bool IsLBRAvailable() {
73*288bf522SAndroid Build Coastguard Worker   return IsBranchSamplingSupported();
74*288bf522SAndroid Build Coastguard Worker }
75*288bf522SAndroid Build Coastguard Worker 
ConvertArgs(const char ** args,int arg_count)76*288bf522SAndroid Build Coastguard Worker static std::vector<std::string> ConvertArgs(const char** args, int arg_count) {
77*288bf522SAndroid Build Coastguard Worker   std::vector<std::string> cmd_args(arg_count);
78*288bf522SAndroid Build Coastguard Worker   for (int i = 0; i < arg_count; ++i) {
79*288bf522SAndroid Build Coastguard Worker     cmd_args[i] = args[i];
80*288bf522SAndroid Build Coastguard Worker   }
81*288bf522SAndroid Build Coastguard Worker   return cmd_args;
82*288bf522SAndroid Build Coastguard Worker }
83*288bf522SAndroid Build Coastguard Worker 
RunRecordCmd(const char ** args,int arg_count)84*288bf522SAndroid Build Coastguard Worker bool RunRecordCmd(const char** args, int arg_count) {
85*288bf522SAndroid Build Coastguard Worker   std::vector<std::string> cmd_args = ConvertArgs(args, arg_count);
86*288bf522SAndroid Build Coastguard Worker   LOG(INFO) << "Record " << android::base::Join(cmd_args, " ");
87*288bf522SAndroid Build Coastguard Worker   // The kernel may panic when trying to hibernate or hotplug CPUs while collecting
88*288bf522SAndroid Build Coastguard Worker   // ETM data. So get wakelock to keep the CPUs on.
89*288bf522SAndroid Build Coastguard Worker   auto wakelock = android::wakelock::WakeLock::tryGet("profcollectd");
90*288bf522SAndroid Build Coastguard Worker   if (!wakelock) {
91*288bf522SAndroid Build Coastguard Worker     LOG(ERROR) << "Record failed: Failed to request wakelock.";
92*288bf522SAndroid Build Coastguard Worker     return false;
93*288bf522SAndroid Build Coastguard Worker   }
94*288bf522SAndroid Build Coastguard Worker   bool result = CreateCommandInstance("record")->Run(cmd_args);
95*288bf522SAndroid Build Coastguard Worker   LOG(INFO) << "Record result " << result;
96*288bf522SAndroid Build Coastguard Worker   return result;
97*288bf522SAndroid Build Coastguard Worker }
98*288bf522SAndroid Build Coastguard Worker 
RunInjectCmd(const char ** args,int arg_count)99*288bf522SAndroid Build Coastguard Worker bool RunInjectCmd(const char** args, int arg_count) {
100*288bf522SAndroid Build Coastguard Worker   std::vector<std::string> cmd_args = ConvertArgs(args, arg_count);
101*288bf522SAndroid Build Coastguard Worker   LOG(INFO) << "Inject " << android::base::Join(cmd_args, " ");
102*288bf522SAndroid Build Coastguard Worker   bool result = CreateCommandInstance("inject")->Run(cmd_args);
103*288bf522SAndroid Build Coastguard Worker   LOG(INFO) << "Inject result " << result;
104*288bf522SAndroid Build Coastguard Worker   return result;
105*288bf522SAndroid Build Coastguard Worker }
106*288bf522SAndroid Build Coastguard Worker 
107*288bf522SAndroid Build Coastguard Worker static android::base::unique_fd log_fd;
108*288bf522SAndroid Build Coastguard Worker static android::base::LogFunction saved_log_func;
109*288bf522SAndroid Build Coastguard Worker 
FileLogger(android::base::LogId id,android::base::LogSeverity severity,const char * tag,const char * file,unsigned int line,const char * message)110*288bf522SAndroid Build Coastguard Worker static void FileLogger(android::base::LogId id, android::base::LogSeverity severity,
111*288bf522SAndroid Build Coastguard Worker                        const char* tag, const char* file, unsigned int line, const char* message) {
112*288bf522SAndroid Build Coastguard Worker   if (log_fd.ok()) {
113*288bf522SAndroid Build Coastguard Worker     static const char log_characters[] = "VDIWEFF";
114*288bf522SAndroid Build Coastguard Worker     char severity_char = log_characters[severity];
115*288bf522SAndroid Build Coastguard Worker     struct tm now;
116*288bf522SAndroid Build Coastguard Worker     time_t t = time(nullptr);
117*288bf522SAndroid Build Coastguard Worker     localtime_r(&t, &now);
118*288bf522SAndroid Build Coastguard Worker     char timestamp[32];
119*288bf522SAndroid Build Coastguard Worker     strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
120*288bf522SAndroid Build Coastguard Worker     std::string s = android::base::StringPrintf("%s %c %s %s:%u] %s\n", tag, severity_char,
121*288bf522SAndroid Build Coastguard Worker                                                 timestamp, file, line, message);
122*288bf522SAndroid Build Coastguard Worker     WriteStringToFd(s, log_fd);
123*288bf522SAndroid Build Coastguard Worker   }
124*288bf522SAndroid Build Coastguard Worker   saved_log_func(id, severity, tag, file, line, message);
125*288bf522SAndroid Build Coastguard Worker }
126*288bf522SAndroid Build Coastguard Worker 
SetLogFile(const char * filename)127*288bf522SAndroid Build Coastguard Worker void SetLogFile(const char* filename) {
128*288bf522SAndroid Build Coastguard Worker   int fd = TEMP_FAILURE_RETRY(open(filename, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, 0600));
129*288bf522SAndroid Build Coastguard Worker   if (fd == -1) {
130*288bf522SAndroid Build Coastguard Worker     PLOG(ERROR) << "failed to open " << filename;
131*288bf522SAndroid Build Coastguard Worker     return;
132*288bf522SAndroid Build Coastguard Worker   }
133*288bf522SAndroid Build Coastguard Worker   log_fd.reset(fd);
134*288bf522SAndroid Build Coastguard Worker   saved_log_func = SetLogger(FileLogger);
135*288bf522SAndroid Build Coastguard Worker }
136*288bf522SAndroid Build Coastguard Worker 
ResetLogFile()137*288bf522SAndroid Build Coastguard Worker void ResetLogFile() {
138*288bf522SAndroid Build Coastguard Worker   log_fd.reset();
139*288bf522SAndroid Build Coastguard Worker   SetLogger(std::move(saved_log_func));
140*288bf522SAndroid Build Coastguard Worker }
141