xref: /aosp_15_r20/system/extras/simpleperf/test_util.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 
18*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <optional>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <android-base/logging.h>
23*288bf522SAndroid Build Coastguard Worker #include <android-base/properties.h>
24*288bf522SAndroid Build Coastguard Worker 
25*288bf522SAndroid Build Coastguard Worker #include "command.h"
26*288bf522SAndroid Build Coastguard Worker #include "event_attr.h"
27*288bf522SAndroid Build Coastguard Worker #include "event_fd.h"
28*288bf522SAndroid Build Coastguard Worker #include "event_type.h"
29*288bf522SAndroid Build Coastguard Worker #include "record_file.h"
30*288bf522SAndroid Build Coastguard Worker #include "test_util.h"
31*288bf522SAndroid Build Coastguard Worker 
32*288bf522SAndroid Build Coastguard Worker #if defined(__linux__)
33*288bf522SAndroid Build Coastguard Worker 
CanSampleRegsFor32BitABI()34*288bf522SAndroid Build Coastguard Worker static std::optional<bool> CanSampleRegsFor32BitABI() {
35*288bf522SAndroid Build Coastguard Worker   std::vector<std::unique_ptr<Workload>> workloads;
36*288bf522SAndroid Build Coastguard Worker   CreateProcesses(1, &workloads);
37*288bf522SAndroid Build Coastguard Worker   std::string pid = std::to_string(workloads[0]->GetPid());
38*288bf522SAndroid Build Coastguard Worker   std::unique_ptr<Command> cmd = CreateCommandInstance("record");
39*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmpfile;
40*288bf522SAndroid Build Coastguard Worker   if (!cmd->Run({"-p", pid, "--call-graph", "dwarf,8", "--no-unwind", "-e", "cpu-clock:u",
41*288bf522SAndroid Build Coastguard Worker                  "--duration", "3", "-o", tmpfile.path})) {
42*288bf522SAndroid Build Coastguard Worker     return std::nullopt;
43*288bf522SAndroid Build Coastguard Worker   }
44*288bf522SAndroid Build Coastguard Worker   auto reader = RecordFileReader::CreateInstance(tmpfile.path);
45*288bf522SAndroid Build Coastguard Worker   if (!reader) {
46*288bf522SAndroid Build Coastguard Worker     return std::nullopt;
47*288bf522SAndroid Build Coastguard Worker   }
48*288bf522SAndroid Build Coastguard Worker   for (const std::unique_ptr<Record>& record : reader->DataSection()) {
49*288bf522SAndroid Build Coastguard Worker     if (record->type() == PERF_RECORD_SAMPLE) {
50*288bf522SAndroid Build Coastguard Worker       auto sample = static_cast<const SampleRecord*>(record.get());
51*288bf522SAndroid Build Coastguard Worker       if (sample->regs_user_data.abi == PERF_SAMPLE_REGS_ABI_32) {
52*288bf522SAndroid Build Coastguard Worker         return true;
53*288bf522SAndroid Build Coastguard Worker       }
54*288bf522SAndroid Build Coastguard Worker     }
55*288bf522SAndroid Build Coastguard Worker   }
56*288bf522SAndroid Build Coastguard Worker   return false;
57*288bf522SAndroid Build Coastguard Worker }
58*288bf522SAndroid Build Coastguard Worker 
IsInNativeAbi()59*288bf522SAndroid Build Coastguard Worker std::optional<bool> IsInNativeAbi() {
60*288bf522SAndroid Build Coastguard Worker   static int in_native_abi = -1;
61*288bf522SAndroid Build Coastguard Worker   if (in_native_abi == -1) {
62*288bf522SAndroid Build Coastguard Worker     FILE* fp = popen("uname -m", "re");
63*288bf522SAndroid Build Coastguard Worker     char buf[40];
64*288bf522SAndroid Build Coastguard Worker     memset(buf, '\0', sizeof(buf));
65*288bf522SAndroid Build Coastguard Worker     CHECK_EQ(fgets(buf, sizeof(buf), fp), buf);
66*288bf522SAndroid Build Coastguard Worker     pclose(fp);
67*288bf522SAndroid Build Coastguard Worker     std::string s = buf;
68*288bf522SAndroid Build Coastguard Worker     in_native_abi = 1;
69*288bf522SAndroid Build Coastguard Worker     if (GetTargetArch() == ARCH_X86_32 || GetTargetArch() == ARCH_X86_64) {
70*288bf522SAndroid Build Coastguard Worker       if (s.find("86") == std::string::npos) {
71*288bf522SAndroid Build Coastguard Worker         in_native_abi = 0;
72*288bf522SAndroid Build Coastguard Worker       }
73*288bf522SAndroid Build Coastguard Worker     } else if (GetTargetArch() == ARCH_ARM || GetTargetArch() == ARCH_ARM64) {
74*288bf522SAndroid Build Coastguard Worker       if (s.find("arm") == std::string::npos && s.find("aarch64") == std::string::npos) {
75*288bf522SAndroid Build Coastguard Worker         in_native_abi = 0;
76*288bf522SAndroid Build Coastguard Worker       }
77*288bf522SAndroid Build Coastguard Worker       if (GetTargetArch() == ARCH_ARM) {
78*288bf522SAndroid Build Coastguard Worker         // If we can't get ARM registers in samples, probably we are running with a 32-bit
79*288bf522SAndroid Build Coastguard Worker         // translator on 64-bit only CPUs. Then we should make in_native_abi = 0.
80*288bf522SAndroid Build Coastguard Worker         if (auto result = CanSampleRegsFor32BitABI(); result.has_value()) {
81*288bf522SAndroid Build Coastguard Worker           in_native_abi = result.value() ? 1 : 0;
82*288bf522SAndroid Build Coastguard Worker         } else {
83*288bf522SAndroid Build Coastguard Worker           in_native_abi = 2;
84*288bf522SAndroid Build Coastguard Worker         }
85*288bf522SAndroid Build Coastguard Worker       }
86*288bf522SAndroid Build Coastguard Worker     } else if (GetTargetArch() == ARCH_RISCV64) {
87*288bf522SAndroid Build Coastguard Worker       if (s.find("riscv") == std::string::npos) {
88*288bf522SAndroid Build Coastguard Worker         in_native_abi = 0;
89*288bf522SAndroid Build Coastguard Worker       }
90*288bf522SAndroid Build Coastguard Worker     }
91*288bf522SAndroid Build Coastguard Worker   }
92*288bf522SAndroid Build Coastguard Worker   if (in_native_abi == 2) {
93*288bf522SAndroid Build Coastguard Worker     return std::nullopt;
94*288bf522SAndroid Build Coastguard Worker   }
95*288bf522SAndroid Build Coastguard Worker   return in_native_abi == 1;
96*288bf522SAndroid Build Coastguard Worker }
97*288bf522SAndroid Build Coastguard Worker 
98*288bf522SAndroid Build Coastguard Worker // Check if we can get a non-zero instruction event count by monitoring current thread.
HasNonZeroInstructionEventCount()99*288bf522SAndroid Build Coastguard Worker static bool HasNonZeroInstructionEventCount() {
100*288bf522SAndroid Build Coastguard Worker   const simpleperf::EventType* type = simpleperf::FindEventTypeByName("instructions", false);
101*288bf522SAndroid Build Coastguard Worker   if (type == nullptr) {
102*288bf522SAndroid Build Coastguard Worker     return false;
103*288bf522SAndroid Build Coastguard Worker   }
104*288bf522SAndroid Build Coastguard Worker   perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
105*288bf522SAndroid Build Coastguard Worker   std::unique_ptr<EventFd> event_fd =
106*288bf522SAndroid Build Coastguard Worker       EventFd::OpenEventFile(attr, gettid(), -1, nullptr, type->name, false);
107*288bf522SAndroid Build Coastguard Worker   if (!event_fd) {
108*288bf522SAndroid Build Coastguard Worker     return false;
109*288bf522SAndroid Build Coastguard Worker   }
110*288bf522SAndroid Build Coastguard Worker   // do some cpu work.
111*288bf522SAndroid Build Coastguard Worker   for (volatile int i = 0; i < 100000; ++i) {
112*288bf522SAndroid Build Coastguard Worker   }
113*288bf522SAndroid Build Coastguard Worker   PerfCounter counter;
114*288bf522SAndroid Build Coastguard Worker   if (event_fd->ReadCounter(&counter)) {
115*288bf522SAndroid Build Coastguard Worker     return counter.value != 0;
116*288bf522SAndroid Build Coastguard Worker   }
117*288bf522SAndroid Build Coastguard Worker   return false;
118*288bf522SAndroid Build Coastguard Worker }
119*288bf522SAndroid Build Coastguard Worker 
IsInEmulator()120*288bf522SAndroid Build Coastguard Worker bool IsInEmulator() {
121*288bf522SAndroid Build Coastguard Worker   std::string fingerprint = android::base::GetProperty("ro.system.build.fingerprint", "");
122*288bf522SAndroid Build Coastguard Worker   return android::base::StartsWith(fingerprint, "google/sdk_gphone") ||
123*288bf522SAndroid Build Coastguard Worker          android::base::StartsWith(fingerprint, "google/sdk_gpc") ||
124*288bf522SAndroid Build Coastguard Worker          android::base::StartsWith(fingerprint, "generic/cf") ||
125*288bf522SAndroid Build Coastguard Worker          android::base::StartsWith(fingerprint, "generic/aosp_cf");
126*288bf522SAndroid Build Coastguard Worker }
127*288bf522SAndroid Build Coastguard Worker 
HasHardwareCounter()128*288bf522SAndroid Build Coastguard Worker bool HasHardwareCounter() {
129*288bf522SAndroid Build Coastguard Worker   static int has_hw_counter = -1;
130*288bf522SAndroid Build Coastguard Worker   if (has_hw_counter == -1) {
131*288bf522SAndroid Build Coastguard Worker     has_hw_counter = 1;
132*288bf522SAndroid Build Coastguard Worker     auto arch = GetTargetArch();
133*288bf522SAndroid Build Coastguard Worker 
134*288bf522SAndroid Build Coastguard Worker     bool in_native_abi = IsInNativeAbi() == std::optional(true);
135*288bf522SAndroid Build Coastguard Worker 
136*288bf522SAndroid Build Coastguard Worker     if (arch == ARCH_X86_64 || arch == ARCH_X86_32 || !in_native_abi || IsInEmulator()) {
137*288bf522SAndroid Build Coastguard Worker       // On x86 and x86_64, or when we are not in native abi, it's likely to run on an emulator or
138*288bf522SAndroid Build Coastguard Worker       // vm without hardware perf counters. It's hard to enumerate them all. So check the support
139*288bf522SAndroid Build Coastguard Worker       // at runtime.
140*288bf522SAndroid Build Coastguard Worker       const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false);
141*288bf522SAndroid Build Coastguard Worker       CHECK(type != nullptr);
142*288bf522SAndroid Build Coastguard Worker       perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
143*288bf522SAndroid Build Coastguard Worker       has_hw_counter = IsEventAttrSupported(attr, "cpu-cycles") ? 1 : 0;
144*288bf522SAndroid Build Coastguard Worker     } else if (arch == ARCH_ARM) {
145*288bf522SAndroid Build Coastguard Worker       // For arm32 devices, external non-invasive debug signal controls PMU counters. Once it is
146*288bf522SAndroid Build Coastguard Worker       // disabled for security reason, we always get zero values for PMU counters. And we want to
147*288bf522SAndroid Build Coastguard Worker       // skip hardware counter tests once we detect it.
148*288bf522SAndroid Build Coastguard Worker       has_hw_counter &= HasNonZeroInstructionEventCount() ? 1 : 0;
149*288bf522SAndroid Build Coastguard Worker     }
150*288bf522SAndroid Build Coastguard Worker   }
151*288bf522SAndroid Build Coastguard Worker   return has_hw_counter == 1;
152*288bf522SAndroid Build Coastguard Worker }
153*288bf522SAndroid Build Coastguard Worker 
154*288bf522SAndroid Build Coastguard Worker #else   // !defined(__linux__)
HasHardwareCounter()155*288bf522SAndroid Build Coastguard Worker bool HasHardwareCounter() {
156*288bf522SAndroid Build Coastguard Worker   return false;
157*288bf522SAndroid Build Coastguard Worker }
158*288bf522SAndroid Build Coastguard Worker #endif  // !defined(__linux__)
159*288bf522SAndroid Build Coastguard Worker 
HasPmuCounter()160*288bf522SAndroid Build Coastguard Worker bool HasPmuCounter() {
161*288bf522SAndroid Build Coastguard Worker   static int has_pmu_counter = -1;
162*288bf522SAndroid Build Coastguard Worker   if (has_pmu_counter == -1) {
163*288bf522SAndroid Build Coastguard Worker     has_pmu_counter = 0;
164*288bf522SAndroid Build Coastguard Worker     auto callback = [&](const simpleperf::EventType& event_type) {
165*288bf522SAndroid Build Coastguard Worker       if (event_type.IsPmuEvent()) {
166*288bf522SAndroid Build Coastguard Worker         has_pmu_counter = 1;
167*288bf522SAndroid Build Coastguard Worker         return false;
168*288bf522SAndroid Build Coastguard Worker       }
169*288bf522SAndroid Build Coastguard Worker       return true;
170*288bf522SAndroid Build Coastguard Worker     };
171*288bf522SAndroid Build Coastguard Worker     simpleperf::EventTypeManager::Instance().ForEachType(callback);
172*288bf522SAndroid Build Coastguard Worker   }
173*288bf522SAndroid Build Coastguard Worker   return has_pmu_counter == 1;
174*288bf522SAndroid Build Coastguard Worker }
175*288bf522SAndroid Build Coastguard Worker 
HasTracepointEvents()176*288bf522SAndroid Build Coastguard Worker bool HasTracepointEvents() {
177*288bf522SAndroid Build Coastguard Worker   static int has_tracepoint_events = -1;
178*288bf522SAndroid Build Coastguard Worker   if (has_tracepoint_events == -1) {
179*288bf522SAndroid Build Coastguard Worker     has_tracepoint_events = 0;
180*288bf522SAndroid Build Coastguard Worker     if (const char* dir = GetTraceFsDir(); dir != nullptr) {
181*288bf522SAndroid Build Coastguard Worker       if (IsDir(std::string(dir) + "/events/sched/sched_switch")) {
182*288bf522SAndroid Build Coastguard Worker         has_tracepoint_events = 1;
183*288bf522SAndroid Build Coastguard Worker       }
184*288bf522SAndroid Build Coastguard Worker     }
185*288bf522SAndroid Build Coastguard Worker   }
186*288bf522SAndroid Build Coastguard Worker   return has_tracepoint_events == 1;
187*288bf522SAndroid Build Coastguard Worker }
188