1 /* 2 * Copyright (C) 2015 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 #include <map> 18 #include <memory> 19 #include <optional> 20 #include <string> 21 #include <vector> 22 23 #include <android-base/file.h> 24 #include <android-base/strings.h> 25 26 #include "environment.h" 27 #include "read_elf.h" 28 #include "utils.h" 29 #include "workload.h" 30 31 using namespace simpleperf; 32 33 static const std::string SLEEP_SEC = "0.001"; 34 35 void RunWorkloadFunction(); 36 void CreateProcesses(size_t count, std::vector<std::unique_ptr<Workload>>* workloads); 37 38 void ParseSymbol(const ElfFileSymbol& symbol, std::map<std::string, ElfFileSymbol>* symbols); 39 void CheckElfFileSymbols(const std::map<std::string, ElfFileSymbol>& symbols); 40 41 #define TEST_IN_ROOT(TestStatement) \ 42 do { \ 43 if (IsRoot()) { \ 44 TestStatement; \ 45 } else { \ 46 GTEST_LOG_(INFO) << "Didn't test \"" << #TestStatement << "\" requires root privileges"; \ 47 } \ 48 } while (0) 49 50 #define TEST_REQUIRE_ROOT() \ 51 do { \ 52 if (!IsRoot()) { \ 53 GTEST_LOG_(INFO) << "Skip this test as it needs root privileges."; \ 54 return; \ 55 } \ 56 } while (0) 57 58 #define TEST_REQUIRE_NON_ROOT() \ 59 do { \ 60 if (IsRoot()) { \ 61 GTEST_LOG_(INFO) << "Skip this test as it tests non-root behavior."; \ 62 return; \ 63 } \ 64 } while (0) 65 66 #if defined(__ANDROID__) 67 #define TEST_REQUIRE_HOST_ROOT() 68 #else 69 #define TEST_REQUIRE_HOST_ROOT() TEST_REQUIRE_ROOT() 70 #endif 71 72 std::optional<bool> IsInNativeAbi(); 73 // Used to skip tests not supposed to run on non-native ABIs. 74 #define OMIT_TEST_ON_NON_NATIVE_ABIS() \ 75 do { \ 76 std::optional<bool> in_native_abi = IsInNativeAbi(); \ 77 ASSERT_TRUE(in_native_abi.has_value()); \ 78 if (!in_native_abi.value()) { \ 79 GTEST_LOG_(INFO) << "Skip this test as it only runs on native ABIs."; \ 80 return; \ 81 } \ 82 } while (0) 83 84 bool HasHardwareCounter(); 85 #define TEST_REQUIRE_HW_COUNTER() \ 86 do { \ 87 if (!HasHardwareCounter()) { \ 88 GTEST_LOG_(INFO) << "Skip this test as the machine doesn't have hardware PMU counters."; \ 89 return; \ 90 } \ 91 } while (0) 92 93 bool HasPmuCounter(); 94 #define TEST_REQUIRE_PMU_COUNTER() \ 95 do { \ 96 if (!HasPmuCounter()) { \ 97 GTEST_LOG_(INFO) << "Skip this test as the machine doesn't have low-level PMU counters."; \ 98 return; \ 99 } \ 100 } while (0) 101 102 bool HasTracepointEvents(); 103 #define TEST_REQUIRE_TRACEPOINT_EVENTS() \ 104 do { \ 105 if (!HasTracepointEvents()) { \ 106 GTEST_LOG_(INFO) << "Skip this test as the machine doesn't support tracepoint events."; \ 107 return; \ 108 } \ 109 } while (0) 110 111 #if defined(IN_CTS_TEST) 112 #define TEST_REQUIRE_APPS() 113 #else 114 #define TEST_REQUIRE_APPS() \ 115 do { \ 116 GTEST_LOG_(INFO) << "Skip this test as test apps aren't available."; \ 117 return; \ 118 } while (0) 119 #endif 120 121 class CaptureStdout { 122 public: CaptureStdout()123 CaptureStdout() : started_(false) {} 124 ~CaptureStdout()125 ~CaptureStdout() { 126 if (started_) { 127 Finish(); 128 } 129 } 130 Start()131 bool Start() { 132 fflush(stdout); 133 old_stdout_ = dup(STDOUT_FILENO); 134 if (old_stdout_ == -1) { 135 return false; 136 } 137 started_ = true; 138 tmpfile_.reset(new TemporaryFile); 139 if (dup2(tmpfile_->fd, STDOUT_FILENO) == -1) { 140 return false; 141 } 142 return true; 143 } 144 Finish()145 std::string Finish() { 146 fflush(stdout); 147 started_ = false; 148 dup2(old_stdout_, STDOUT_FILENO); 149 close(old_stdout_); 150 std::string s; 151 if (!android::base::ReadFileToString(tmpfile_->path, &s)) { 152 return ""; 153 } 154 return s; 155 } 156 157 private: 158 bool started_; 159 int old_stdout_; 160 std::unique_ptr<TemporaryFile> tmpfile_; 161 }; 162 163 class AppHelper { 164 public: ~AppHelper()165 ~AppHelper() { 166 for (auto& package : installed_packages_) { 167 Workload::RunCmd({"pm", "uninstall", package}); 168 } 169 } 170 InstallApk(const std::string & apk_path,const std::string & package_name)171 bool InstallApk(const std::string& apk_path, const std::string& package_name) { 172 if (Workload::RunCmd({"pm", "install", "-t", "--abi", GetABI(), apk_path})) { 173 installed_packages_.emplace_back(package_name); 174 return true; 175 } 176 return false; 177 } 178 StartApp(const std::string & start_cmd)179 bool StartApp(const std::string& start_cmd) { 180 app_start_proc_ = Workload::CreateWorkload(android::base::Split(start_cmd, " ")); 181 return app_start_proc_ && app_start_proc_->Start(); 182 } 183 184 private: GetABI()185 const char* GetABI() { 186 #if defined(__i386__) 187 return "x86"; 188 #elif defined(__x86_64__) 189 return "x86_64"; 190 #elif defined(__aarch64__) 191 return "arm64-v8a"; 192 #elif defined(__arm__) 193 return "armeabi-v7a"; 194 #elif defined(__riscv) 195 return "riscv64"; 196 #else 197 #error "unrecognized ABI" 198 #endif 199 } 200 201 std::vector<std::string> installed_packages_; 202 std::unique_ptr<Workload> app_start_proc_; 203 }; 204