blob: 42dee703e8bac5d2c723ac1652fd4f01d0a9a68a [file] [log] [blame]
Thiébaud Weksteen90a68252020-10-27 15:05:33 +01001//
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//
17
18#include <stdio.h>
19
Yabin Cui9698eff2023-07-25 10:06:16 -070020#include <optional>
21
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010022#include <android-base/logging.h>
Yabin Cui3ee97272020-10-28 09:23:24 -070023#include <android-base/properties.h>
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010024
Yabin Cui9698eff2023-07-25 10:06:16 -070025#include "command.h"
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010026#include "event_attr.h"
27#include "event_fd.h"
28#include "event_type.h"
Yabin Cui9698eff2023-07-25 10:06:16 -070029#include "record_file.h"
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010030#include "test_util.h"
31
Yabin Cui9698eff2023-07-25 10:06:16 -070032#if defined(__linux__)
33
34static std::optional<bool> CanSampleRegsFor32BitABI() {
35 std::vector<std::unique_ptr<Workload>> workloads;
36 CreateProcesses(1, &workloads);
37 std::string pid = std::to_string(workloads[0]->GetPid());
38 std::unique_ptr<Command> cmd = CreateCommandInstance("record");
39 TemporaryFile tmpfile;
40 if (!cmd->Run({"-p", pid, "--call-graph", "dwarf,8", "--no-unwind", "-e", "cpu-clock:u",
41 "--duration", "3", "-o", tmpfile.path})) {
42 return std::nullopt;
43 }
44 auto reader = RecordFileReader::CreateInstance(tmpfile.path);
45 if (!reader) {
46 return std::nullopt;
47 }
48 for (const std::unique_ptr<Record>& record : reader->DataSection()) {
49 if (record->type() == PERF_RECORD_SAMPLE) {
50 auto sample = static_cast<const SampleRecord*>(record.get());
51 if (sample->regs_user_data.abi == PERF_SAMPLE_REGS_ABI_32) {
52 return true;
53 }
54 }
55 }
56 return false;
57}
58
Yabin Cui00994672023-07-26 10:59:42 -070059std::optional<bool> IsInNativeAbi() {
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010060 static int in_native_abi = -1;
61 if (in_native_abi == -1) {
62 FILE* fp = popen("uname -m", "re");
63 char buf[40];
64 memset(buf, '\0', sizeof(buf));
65 CHECK_EQ(fgets(buf, sizeof(buf), fp), buf);
66 pclose(fp);
67 std::string s = buf;
68 in_native_abi = 1;
Yabin Cui38076912021-08-16 16:59:09 -070069 if (GetTargetArch() == ARCH_X86_32 || GetTargetArch() == ARCH_X86_64) {
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010070 if (s.find("86") == std::string::npos) {
71 in_native_abi = 0;
72 }
Yabin Cui38076912021-08-16 16:59:09 -070073 } else if (GetTargetArch() == ARCH_ARM || GetTargetArch() == ARCH_ARM64) {
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010074 if (s.find("arm") == std::string::npos && s.find("aarch64") == std::string::npos) {
75 in_native_abi = 0;
76 }
Yabin Cui9698eff2023-07-25 10:06:16 -070077 if (GetTargetArch() == ARCH_ARM) {
78 // If we can't get ARM registers in samples, probably we are running with a 32-bit
Yabin Cui00994672023-07-26 10:59:42 -070079 // translator on 64-bit only CPUs. Then we should make in_native_abi = 0.
80 if (auto result = CanSampleRegsFor32BitABI(); result.has_value()) {
81 in_native_abi = result.value() ? 1 : 0;
82 } else {
83 in_native_abi = 2;
Yabin Cui9698eff2023-07-25 10:06:16 -070084 }
85 }
haocheng.zy1808a5b2022-10-12 22:59:45 +080086 } else if (GetTargetArch() == ARCH_RISCV64) {
87 if (s.find("riscv") == std::string::npos) {
88 in_native_abi = 0;
89 }
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010090 }
91 }
Yabin Cui00994672023-07-26 10:59:42 -070092 if (in_native_abi == 2) {
93 return std::nullopt;
94 }
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010095 return in_native_abi == 1;
96}
97
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010098// Check if we can get a non-zero instruction event count by monitoring current thread.
99static bool HasNonZeroInstructionEventCount() {
100 const simpleperf::EventType* type = simpleperf::FindEventTypeByName("instructions", false);
101 if (type == nullptr) {
102 return false;
103 }
104 perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
105 std::unique_ptr<EventFd> event_fd =
106 EventFd::OpenEventFile(attr, gettid(), -1, nullptr, type->name, false);
107 if (!event_fd) {
108 return false;
109 }
110 // do some cpu work.
111 for (volatile int i = 0; i < 100000; ++i) {
112 }
113 PerfCounter counter;
114 if (event_fd->ReadCounter(&counter)) {
115 return counter.value != 0;
116 }
117 return false;
118}
Thiébaud Weksteen90a68252020-10-27 15:05:33 +0100119
Yabin Cuib68f0f92024-04-15 16:34:00 -0700120bool IsInEmulator() {
121 std::string fingerprint = android::base::GetProperty("ro.system.build.fingerprint", "");
122 return android::base::StartsWith(fingerprint, "google/sdk_gphone") ||
123 android::base::StartsWith(fingerprint, "google/sdk_gpc") ||
124 android::base::StartsWith(fingerprint, "generic/cf") ||
125 android::base::StartsWith(fingerprint, "generic/aosp_cf");
126}
127
Thiébaud Weksteen90a68252020-10-27 15:05:33 +0100128bool HasHardwareCounter() {
129 static int has_hw_counter = -1;
130 if (has_hw_counter == -1) {
Yabin Cui1f6f51a2021-07-22 13:33:24 -0700131 has_hw_counter = 1;
Yabin Cui8c426d92021-10-27 14:45:08 -0700132 auto arch = GetTargetArch();
Yabin Cuib68f0f92024-04-15 16:34:00 -0700133
Yabin Cui00994672023-07-26 10:59:42 -0700134 bool in_native_abi = IsInNativeAbi() == std::optional(true);
Yabin Cui8c426d92021-10-27 14:45:08 -0700135
Yabin Cuib68f0f92024-04-15 16:34:00 -0700136 if (arch == ARCH_X86_64 || arch == ARCH_X86_32 || !in_native_abi || IsInEmulator()) {
Yabin Cui924e56e2023-06-09 15:38:10 -0700137 // On x86 and x86_64, or when we are not in native abi, it's likely to run on an emulator or
138 // vm without hardware perf counters. It's hard to enumerate them all. So check the support
139 // at runtime.
Yabin Cui8c426d92021-10-27 14:45:08 -0700140 const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false);
141 CHECK(type != nullptr);
142 perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
143 has_hw_counter = IsEventAttrSupported(attr, "cpu-cycles") ? 1 : 0;
144 } else if (arch == ARCH_ARM) {
145 // For arm32 devices, external non-invasive debug signal controls PMU counters. Once it is
146 // disabled for security reason, we always get zero values for PMU counters. And we want to
147 // skip hardware counter tests once we detect it.
148 has_hw_counter &= HasNonZeroInstructionEventCount() ? 1 : 0;
149 }
Thiébaud Weksteen90a68252020-10-27 15:05:33 +0100150 }
151 return has_hw_counter == 1;
152}
153
Yabin Cui8c426d92021-10-27 14:45:08 -0700154#else // !defined(__linux__)
155bool HasHardwareCounter() {
156 return false;
157}
158#endif // !defined(__linux__)
159
Thiébaud Weksteen90a68252020-10-27 15:05:33 +0100160bool HasPmuCounter() {
161 static int has_pmu_counter = -1;
162 if (has_pmu_counter == -1) {
163 has_pmu_counter = 0;
164 auto callback = [&](const simpleperf::EventType& event_type) {
165 if (event_type.IsPmuEvent()) {
166 has_pmu_counter = 1;
167 return false;
168 }
169 return true;
170 };
171 simpleperf::EventTypeManager::Instance().ForEachType(callback);
172 }
173 return has_pmu_counter == 1;
174}
175
176bool HasTracepointEvents() {
177 static int has_tracepoint_events = -1;
178 if (has_tracepoint_events == -1) {
Yabin Cuif41d0fd2024-04-16 17:02:15 -0700179 has_tracepoint_events = 0;
180 if (const char* dir = GetTraceFsDir(); dir != nullptr) {
181 if (IsDir(std::string(dir) + "/events/sched/sched_switch")) {
182 has_tracepoint_events = 1;
183 }
184 }
Thiébaud Weksteen90a68252020-10-27 15:05:33 +0100185 }
186 return has_tracepoint_events == 1;
187}