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