Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 1 | // |
| 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 Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Yabin Cui | 3ee9727 | 2020-10-28 09:23:24 -0700 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 22 | |
| 23 | #include "event_attr.h" |
| 24 | #include "event_fd.h" |
| 25 | #include "event_type.h" |
| 26 | #include "test_util.h" |
| 27 | |
| 28 | bool 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 Cui | 3807691 | 2021-08-16 16:59:09 -0700 | [diff] [blame] | 38 | if (GetTargetArch() == ARCH_X86_32 || GetTargetArch() == ARCH_X86_64) { |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 39 | if (s.find("86") == std::string::npos) { |
| 40 | in_native_abi = 0; |
| 41 | } |
Yabin Cui | 3807691 | 2021-08-16 16:59:09 -0700 | [diff] [blame] | 42 | } else if (GetTargetArch() == ARCH_ARM || GetTargetArch() == ARCH_ARM64) { |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 43 | if (s.find("arm") == std::string::npos && s.find("aarch64") == std::string::npos) { |
| 44 | in_native_abi = 0; |
| 45 | } |
haocheng.zy | 1808a5b | 2022-10-12 22:59:45 +0800 | [diff] [blame] | 46 | } else if (GetTargetArch() == ARCH_RISCV64) { |
| 47 | if (s.find("riscv") == std::string::npos) { |
| 48 | in_native_abi = 0; |
| 49 | } |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | return in_native_abi == 1; |
| 53 | } |
| 54 | |
Yabin Cui | 8c426d9 | 2021-10-27 14:45:08 -0700 | [diff] [blame] | 55 | #if defined(__linux__) |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 56 | // Check if we can get a non-zero instruction event count by monitoring current thread. |
| 57 | static bool HasNonZeroInstructionEventCount() { |
| 58 | const simpleperf::EventType* type = simpleperf::FindEventTypeByName("instructions", false); |
| 59 | if (type == nullptr) { |
| 60 | return false; |
| 61 | } |
| 62 | perf_event_attr attr = CreateDefaultPerfEventAttr(*type); |
| 63 | std::unique_ptr<EventFd> event_fd = |
| 64 | EventFd::OpenEventFile(attr, gettid(), -1, nullptr, type->name, false); |
| 65 | if (!event_fd) { |
| 66 | return false; |
| 67 | } |
| 68 | // do some cpu work. |
| 69 | for (volatile int i = 0; i < 100000; ++i) { |
| 70 | } |
| 71 | PerfCounter counter; |
| 72 | if (event_fd->ReadCounter(&counter)) { |
| 73 | return counter.value != 0; |
| 74 | } |
| 75 | return false; |
| 76 | } |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 77 | |
| 78 | bool HasHardwareCounter() { |
| 79 | static int has_hw_counter = -1; |
| 80 | if (has_hw_counter == -1) { |
Yabin Cui | 1f6f51a | 2021-07-22 13:33:24 -0700 | [diff] [blame] | 81 | has_hw_counter = 1; |
Yabin Cui | 8c426d9 | 2021-10-27 14:45:08 -0700 | [diff] [blame] | 82 | auto arch = GetTargetArch(); |
| 83 | std::string fingerprint = android::base::GetProperty("ro.system.build.fingerprint", ""); |
| 84 | bool is_emulator = android::base::StartsWith(fingerprint, "google/sdk_gphone") || |
Daichi Hirono | fa815d0 | 2022-09-07 16:24:57 +0900 | [diff] [blame] | 85 | android::base::StartsWith(fingerprint, "google/sdk_gpc") || |
Yabin Cui | 8c426d9 | 2021-10-27 14:45:08 -0700 | [diff] [blame] | 86 | android::base::StartsWith(fingerprint, "generic/cf"); |
| 87 | |
Yabin Cui | 924e56e | 2023-06-09 15:38:10 -0700 | [diff] [blame] | 88 | if (arch == ARCH_X86_64 || arch == ARCH_X86_32 || !IsInNativeAbi() || is_emulator) { |
| 89 | // On x86 and x86_64, or when we are not in native abi, it's likely to run on an emulator or |
| 90 | // vm without hardware perf counters. It's hard to enumerate them all. So check the support |
| 91 | // at runtime. |
Yabin Cui | 8c426d9 | 2021-10-27 14:45:08 -0700 | [diff] [blame] | 92 | const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false); |
| 93 | CHECK(type != nullptr); |
| 94 | perf_event_attr attr = CreateDefaultPerfEventAttr(*type); |
| 95 | has_hw_counter = IsEventAttrSupported(attr, "cpu-cycles") ? 1 : 0; |
| 96 | } else if (arch == ARCH_ARM) { |
| 97 | // For arm32 devices, external non-invasive debug signal controls PMU counters. Once it is |
| 98 | // disabled for security reason, we always get zero values for PMU counters. And we want to |
| 99 | // skip hardware counter tests once we detect it. |
| 100 | has_hw_counter &= HasNonZeroInstructionEventCount() ? 1 : 0; |
| 101 | } |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 102 | } |
| 103 | return has_hw_counter == 1; |
| 104 | } |
| 105 | |
Yabin Cui | 8c426d9 | 2021-10-27 14:45:08 -0700 | [diff] [blame] | 106 | #else // !defined(__linux__) |
| 107 | bool HasHardwareCounter() { |
| 108 | return false; |
| 109 | } |
| 110 | #endif // !defined(__linux__) |
| 111 | |
Thiébaud Weksteen | 90a6825 | 2020-10-27 15:05:33 +0100 | [diff] [blame] | 112 | bool HasPmuCounter() { |
| 113 | static int has_pmu_counter = -1; |
| 114 | if (has_pmu_counter == -1) { |
| 115 | has_pmu_counter = 0; |
| 116 | auto callback = [&](const simpleperf::EventType& event_type) { |
| 117 | if (event_type.IsPmuEvent()) { |
| 118 | has_pmu_counter = 1; |
| 119 | return false; |
| 120 | } |
| 121 | return true; |
| 122 | }; |
| 123 | simpleperf::EventTypeManager::Instance().ForEachType(callback); |
| 124 | } |
| 125 | return has_pmu_counter == 1; |
| 126 | } |
| 127 | |
| 128 | bool HasTracepointEvents() { |
| 129 | static int has_tracepoint_events = -1; |
| 130 | if (has_tracepoint_events == -1) { |
| 131 | has_tracepoint_events = (GetTraceFsDir() != nullptr) ? 1 : 0; |
| 132 | } |
| 133 | return has_tracepoint_events == 1; |
| 134 | } |