• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <android-base/logging.h>
21 #include <android-base/properties.h>
22 
23 #include "event_attr.h"
24 #include "event_fd.h"
25 #include "event_type.h"
26 #include "test_util.h"
27 
IsInNativeAbi()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;
38     if (GetBuildArch() == ARCH_X86_32 || GetBuildArch() == ARCH_X86_64) {
39       if (s.find("86") == std::string::npos) {
40         in_native_abi = 0;
41       }
42     } else if (GetBuildArch() == ARCH_ARM || GetBuildArch() == ARCH_ARM64) {
43       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 
51 #if defined(__arm__)
52 // Check if we can get a non-zero instruction event count by monitoring current thread.
HasNonZeroInstructionEventCount()53 static 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 }
73 #endif  // defined(__arm__)
74 
HasHardwareCounter()75 bool HasHardwareCounter() {
76 #if defined(__linux__)
77   static int has_hw_counter = -1;
78   if (has_hw_counter == -1) {
79     has_hw_counter = 1;
80 #if defined(__x86__) || defined(__x86_64__)
81     // On x86 and x86_64, it's likely to run on an emulator or vm without hardware perf counters.
82     // It's hard to enumerate them all. So check the support at runtime.
83     const simpleperf::EventType* type = simpleperf::FindEventTypeByName("cpu-cycles", false);
84     CHECK(type != nullptr);
85     perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
86     has_hw_counter = IsEventAttrSupported(attr, "cpu-cycles") ? 1 : 0;
87 #elif defined(__arm__)
88     // For arm32 devices, external non-invasive debug signal controls PMU counters. Once it is
89     // disabled for security reason, we always get zero values for PMU counters. And we want to
90     // skip hardware counter tests once we detect it.
91     has_hw_counter &= HasNonZeroInstructionEventCount() ? 1 : 0;
92 #endif  // defined(__arm__)
93   }
94   return has_hw_counter == 1;
95 #else   // defined(__linux__)
96   return false;
97 #endif  // defined(__linux__)
98 }
99 
HasPmuCounter()100 bool HasPmuCounter() {
101   static int has_pmu_counter = -1;
102   if (has_pmu_counter == -1) {
103     has_pmu_counter = 0;
104     auto callback = [&](const simpleperf::EventType& event_type) {
105       if (event_type.IsPmuEvent()) {
106         has_pmu_counter = 1;
107         return false;
108       }
109       return true;
110     };
111     simpleperf::EventTypeManager::Instance().ForEachType(callback);
112   }
113   return has_pmu_counter == 1;
114 }
115 
HasTracepointEvents()116 bool HasTracepointEvents() {
117   static int has_tracepoint_events = -1;
118   if (has_tracepoint_events == -1) {
119     has_tracepoint_events = (GetTraceFsDir() != nullptr) ? 1 : 0;
120   }
121   return has_tracepoint_events == 1;
122 }
123