summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2021-10-27 14:45:08 -0700
committerYabin Cui <yabinc@google.com>2021-10-28 10:45:06 -0700
commitd87a4a1514e6cdb6e5eec2fda1ff7778d9d1cea5 (patch)
tree666705ad840b627c1be062d4b9696820d22882ca
parent3c385fb5115c2608280836046faeea691805813e (diff)
downloadextras-d87a4a1514e6cdb6e5eec2fda1ff7778d9d1cea5.tar.gz
simpleperf: detect hardware counters on emulators.
We may run arm64 tests on emulators, which may not have hardware counters. So use fingerprint to check if we are running on an emulator, and dynamically detect if hardware counters are available. Bug: 202840384 Bug: 191278807 Test: run CtsSimpleperfTestCases on emulators. Change-Id: I3b6ea439e72188d5b4cf23662b0ee00bfa3b28cd (cherry picked from commit 8c426d91c940ddfdb480dec9369a21f1c6907bc9)
-rw-r--r--simpleperf/test_util.cpp42
1 files changed, 24 insertions, 18 deletions
diff --git a/simpleperf/test_util.cpp b/simpleperf/test_util.cpp
index 547eaf90..d85294f5 100644
--- a/simpleperf/test_util.cpp
+++ b/simpleperf/test_util.cpp
@@ -48,7 +48,7 @@ bool IsInNativeAbi() {
return in_native_abi == 1;
}
-#if defined(__arm__)
+#if defined(__linux__)
// Check if we can get a non-zero instruction event count by monitoring current thread.
static bool HasNonZeroInstructionEventCount() {
const simpleperf::EventType* type = simpleperf::FindEventTypeByName("instructions", false);
@@ -70,32 +70,38 @@ static bool HasNonZeroInstructionEventCount() {
}
return false;
}
-#endif // defined(__arm__)
bool HasHardwareCounter() {
-#if defined(__linux__)
static int has_hw_counter = -1;
if (has_hw_counter == -1) {
has_hw_counter = 1;
-#if defined(__x86__) || defined(__x86_64__)
- // On x86 and x86_64, it's likely to run on an emulator or vm without hardware perf counters.
- // It's hard to enumerate them all. So check the support at runtime.
- const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false);
- CHECK(type != nullptr);
- perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
- has_hw_counter = IsEventAttrSupported(attr, "cpu-cycles") ? 1 : 0;
-#elif defined(__arm__)
- // For arm32 devices, external non-invasive debug signal controls PMU counters. Once it is
- // disabled for security reason, we always get zero values for PMU counters. And we want to
- // skip hardware counter tests once we detect it.
- has_hw_counter &= HasNonZeroInstructionEventCount() ? 1 : 0;
-#endif // defined(__arm__)
+ auto arch = GetBuildArch();
+ std::string fingerprint = android::base::GetProperty("ro.system.build.fingerprint", "");
+ bool is_emulator = android::base::StartsWith(fingerprint, "google/sdk_gphone") ||
+ android::base::StartsWith(fingerprint, "generic/cf");
+
+ if (arch == ARCH_X86_64 || arch == ARCH_X86_32 || is_emulator) {
+ // On x86 and x86_64, it's likely to run on an emulator or vm without hardware perf
+ // counters. It's hard to enumerate them all. So check the support at runtime.
+ const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false);
+ CHECK(type != nullptr);
+ perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
+ has_hw_counter = IsEventAttrSupported(attr, "cpu-cycles") ? 1 : 0;
+ } else if (arch == ARCH_ARM) {
+ // For arm32 devices, external non-invasive debug signal controls PMU counters. Once it is
+ // disabled for security reason, we always get zero values for PMU counters. And we want to
+ // skip hardware counter tests once we detect it.
+ has_hw_counter &= HasNonZeroInstructionEventCount() ? 1 : 0;
+ }
}
return has_hw_counter == 1;
-#else // defined(__linux__)
+}
+
+#else // !defined(__linux__)
+bool HasHardwareCounter() {
return false;
-#endif // defined(__linux__)
}
+#endif // !defined(__linux__)
bool HasPmuCounter() {
static int has_pmu_counter = -1;