blob: 6dae04cdab8075d58ad6b5d153f73a842c948999 [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
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010020#include <android-base/logging.h>
Yabin Cui3ee97272020-10-28 09:23:24 -070021#include <android-base/properties.h>
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010022
23#include "event_attr.h"
24#include "event_fd.h"
25#include "event_type.h"
26#include "test_util.h"
27
28bool IsInNativeAbi() {
29 static int in_native_abi = -1;
30 if (in_native_abi == -1) {
31 FILE* fp = popen("uname -m", "re");
32 char buf[40];
33 memset(buf, '\0', sizeof(buf));
34 CHECK_EQ(fgets(buf, sizeof(buf), fp), buf);
35 pclose(fp);
36 std::string s = buf;
37 in_native_abi = 1;
Yabin Cui38076912021-08-16 16:59:09 -070038 if (GetTargetArch() == ARCH_X86_32 || GetTargetArch() == ARCH_X86_64) {
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010039 if (s.find("86") == std::string::npos) {
40 in_native_abi = 0;
41 }
Yabin Cui38076912021-08-16 16:59:09 -070042 } else if (GetTargetArch() == ARCH_ARM || GetTargetArch() == ARCH_ARM64) {
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010043 if (s.find("arm") == std::string::npos && s.find("aarch64") == std::string::npos) {
44 in_native_abi = 0;
45 }
46 }
47 }
48 return in_native_abi == 1;
49}
50
Yabin Cui8c426d92021-10-27 14:45:08 -070051#if defined(__linux__)
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010052// Check if we can get a non-zero instruction event count by monitoring current thread.
53static bool HasNonZeroInstructionEventCount() {
54 const simpleperf::EventType* type = simpleperf::FindEventTypeByName("instructions", false);
55 if (type == nullptr) {
56 return false;
57 }
58 perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
59 std::unique_ptr<EventFd> event_fd =
60 EventFd::OpenEventFile(attr, gettid(), -1, nullptr, type->name, false);
61 if (!event_fd) {
62 return false;
63 }
64 // do some cpu work.
65 for (volatile int i = 0; i < 100000; ++i) {
66 }
67 PerfCounter counter;
68 if (event_fd->ReadCounter(&counter)) {
69 return counter.value != 0;
70 }
71 return false;
72}
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010073
74bool HasHardwareCounter() {
75 static int has_hw_counter = -1;
76 if (has_hw_counter == -1) {
Yabin Cui1f6f51a2021-07-22 13:33:24 -070077 has_hw_counter = 1;
Yabin Cui8c426d92021-10-27 14:45:08 -070078 auto arch = GetTargetArch();
79 std::string fingerprint = android::base::GetProperty("ro.system.build.fingerprint", "");
80 bool is_emulator = android::base::StartsWith(fingerprint, "google/sdk_gphone") ||
81 android::base::StartsWith(fingerprint, "generic/cf");
82
83 if (arch == ARCH_X86_64 || arch == ARCH_X86_32 || is_emulator) {
84 // On x86 and x86_64, it's likely to run on an emulator or vm without hardware perf
85 // counters. It's hard to enumerate them all. So check the support at runtime.
86 const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false);
87 CHECK(type != nullptr);
88 perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
89 has_hw_counter = IsEventAttrSupported(attr, "cpu-cycles") ? 1 : 0;
90 } else if (arch == ARCH_ARM) {
91 // For arm32 devices, external non-invasive debug signal controls PMU counters. Once it is
92 // disabled for security reason, we always get zero values for PMU counters. And we want to
93 // skip hardware counter tests once we detect it.
94 has_hw_counter &= HasNonZeroInstructionEventCount() ? 1 : 0;
95 }
Thiébaud Weksteen90a68252020-10-27 15:05:33 +010096 }
97 return has_hw_counter == 1;
98}
99
Yabin Cui8c426d92021-10-27 14:45:08 -0700100#else // !defined(__linux__)
101bool HasHardwareCounter() {
102 return false;
103}
104#endif // !defined(__linux__)
105
Thiébaud Weksteen90a68252020-10-27 15:05:33 +0100106bool HasPmuCounter() {
107 static int has_pmu_counter = -1;
108 if (has_pmu_counter == -1) {
109 has_pmu_counter = 0;
110 auto callback = [&](const simpleperf::EventType& event_type) {
111 if (event_type.IsPmuEvent()) {
112 has_pmu_counter = 1;
113 return false;
114 }
115 return true;
116 };
117 simpleperf::EventTypeManager::Instance().ForEachType(callback);
118 }
119 return has_pmu_counter == 1;
120}
121
122bool HasTracepointEvents() {
123 static int has_tracepoint_events = -1;
124 if (has_tracepoint_events == -1) {
125 has_tracepoint_events = (GetTraceFsDir() != nullptr) ? 1 : 0;
126 }
127 return has_tracepoint_events == 1;
128}