summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2021-07-22 13:33:24 -0700
committerYabin Cui <yabinc@google.com>2021-07-30 22:16:57 +0000
commitee3097ce79889c5e0bbaec34cda8becebcc9f64f (patch)
treedd72a84514306c5cc0c9025a1fb9e9e3976ced32
parentd09092fd6d2632e6e2787af06ad6fdb69099966c (diff)
downloadextras-android12-dev.tar.gz
simpleperf: replace InCloudAndroid() with runtime check.android12-dev
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 if hardware perf counters are available at runtime. Bug: 191277482 Test: run simpleperf_unit_test Change-Id: I38cc89bc7f81f8d6d6165a4b8f344c0660c10364 (cherry picked from commit 1f6f51aee77235b1fb374a917f0b46c9c09f0933)
-rw-r--r--simpleperf/test_util.cpp41
1 files changed, 14 insertions, 27 deletions
diff --git a/simpleperf/test_util.cpp b/simpleperf/test_util.cpp
index c2a21960..547eaf90 100644
--- a/simpleperf/test_util.cpp
+++ b/simpleperf/test_util.cpp
@@ -48,29 +48,6 @@ bool IsInNativeAbi() {
return in_native_abi == 1;
}
-static bool InCloudAndroid() {
-#if defined(__i386__) || defined(__x86_64__)
-#if defined(__ANDROID__)
- std::string prop_value = android::base::GetProperty("ro.build.flavor", "");
- if (android::base::StartsWith(prop_value, "cf_x86_phone") ||
- android::base::StartsWith(prop_value, "aosp_cf_x86_phone") ||
- android::base::StartsWith(prop_value, "cf_x86_64_phone") ||
- android::base::StartsWith(prop_value, "aosp_cf_x86_64_phone")) {
- return true;
- }
- // aosp_x86* builds may also run on cloud Android. Detect it by checking
- /// if cpu-cycles isn't supported.
- if (android::base::StartsWith(prop_value, "aosp_x86")) {
- const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false);
- CHECK(type != nullptr);
- perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
- return !IsEventAttrSupported(attr, "cpu-cycles");
- }
-#endif
-#endif
- return false;
-}
-
#if defined(__arm__)
// Check if we can get a non-zero instruction event count by monitoring current thread.
static bool HasNonZeroInstructionEventCount() {
@@ -96,18 +73,28 @@ static bool HasNonZeroInstructionEventCount() {
#endif // defined(__arm__)
bool HasHardwareCounter() {
+#if defined(__linux__)
static int has_hw_counter = -1;
if (has_hw_counter == -1) {
- // Cloud Android doesn't have hardware counters.
- has_hw_counter = InCloudAndroid() ? 0 : 1;
-#if defined(__arm__)
+ 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
+#endif // defined(__arm__)
}
return has_hw_counter == 1;
+#else // defined(__linux__)
+ return false;
+#endif // defined(__linux__)
}
bool HasPmuCounter() {