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