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 | 0099467 | 2023-07-26 10:59:42 -0700 | [diff] [blame] | 19 | #include <optional> |
Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 20 | #include <string> |
Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 21 | #include <vector> |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 22 | |
Yabin Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 23 | #include <android-base/file.h> |
Yabin Cui | dc1a475 | 2020-02-10 17:53:34 -0800 | [diff] [blame] | 24 | #include <android-base/strings.h> |
Yabin Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 25 | |
Yabin Cui | 87418d8 | 2018-05-21 18:31:56 -0700 | [diff] [blame] | 26 | #include "environment.h" |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 27 | #include "read_elf.h" |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 28 | #include "utils.h" |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 29 | #include "workload.h" |
| 30 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 31 | using namespace simpleperf; |
| 32 | |
Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 33 | static const std::string SLEEP_SEC = "0.001"; |
| 34 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 35 | void RunWorkloadFunction(); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 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); |
Yabin Cui | 4b6720d | 2016-03-31 14:39:19 -0700 | [diff] [blame] | 40 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 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 | } \ |
Yabin Cui | 4b6720d | 2016-03-31 14:39:19 -0700 | [diff] [blame] | 48 | } while (0) |
Yabin Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 49 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 50 | #define TEST_REQUIRE_ROOT() \ |
| 51 | do { \ |
| 52 | if (!IsRoot()) { \ |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 53 | GTEST_LOG_(INFO) << "Skip this test as it needs root privileges."; \ |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 54 | return; \ |
| 55 | } \ |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 56 | } while (0) |
| 57 | |
Yabin Cui | 6e7f33a | 2021-05-05 15:43:35 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 66 | #if defined(__ANDROID__) |
| 67 | #define TEST_REQUIRE_HOST_ROOT() |
| 68 | #else |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 69 | #define TEST_REQUIRE_HOST_ROOT() TEST_REQUIRE_ROOT() |
Yabin Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 70 | #endif |
Yabin Cui | 64a9ecd | 2017-09-13 13:21:32 -0700 | [diff] [blame] | 71 | |
Yabin Cui | 0099467 | 2023-07-26 10:59:42 -0700 | [diff] [blame] | 72 | std::optional<bool> IsInNativeAbi(); |
Yabin Cui | 64a9ecd | 2017-09-13 13:21:32 -0700 | [diff] [blame] | 73 | // 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] | 74 | #define OMIT_TEST_ON_NON_NATIVE_ABIS() \ |
| 75 | do { \ |
Yabin Cui | 0099467 | 2023-07-26 10:59:42 -0700 | [diff] [blame] | 76 | std::optional<bool> in_native_abi = IsInNativeAbi(); \ |
| 77 | ASSERT_TRUE(in_native_abi.has_value()); \ |
| 78 | if (!in_native_abi.value()) { \ |
Yabin Cui | 64a9ecd | 2017-09-13 13:21:32 -0700 | [diff] [blame] | 79 | 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] | 80 | return; \ |
| 81 | } \ |
Yabin Cui | 64a9ecd | 2017-09-13 13:21:32 -0700 | [diff] [blame] | 82 | } while (0) |
Yabin Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 83 | |
Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 84 | bool HasHardwareCounter(); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 85 | #define TEST_REQUIRE_HW_COUNTER() \ |
| 86 | do { \ |
| 87 | if (!HasHardwareCounter()) { \ |
Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 88 | 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] | 89 | return; \ |
| 90 | } \ |
Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 91 | } while (0) |
| 92 | |
Namhyung Kim | 52ff426 | 2019-10-23 12:37:34 +0900 | [diff] [blame] | 93 | bool HasPmuCounter(); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 94 | #define TEST_REQUIRE_PMU_COUNTER() \ |
| 95 | do { \ |
| 96 | if (!HasPmuCounter()) { \ |
Namhyung Kim | 52ff426 | 2019-10-23 12:37:34 +0900 | [diff] [blame] | 97 | 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] | 98 | return; \ |
| 99 | } \ |
Namhyung Kim | 52ff426 | 2019-10-23 12:37:34 +0900 | [diff] [blame] | 100 | } while (0) |
| 101 | |
Yabin Cui | 538fb53 | 2020-01-27 13:59:24 -0800 | [diff] [blame] | 102 | bool HasTracepointEvents(); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 103 | #define TEST_REQUIRE_TRACEPOINT_EVENTS() \ |
| 104 | do { \ |
| 105 | if (!HasTracepointEvents()) { \ |
Yabin Cui | 538fb53 | 2020-01-27 13:59:24 -0800 | [diff] [blame] | 106 | 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] | 107 | return; \ |
| 108 | } \ |
Yabin Cui | 538fb53 | 2020-01-27 13:59:24 -0800 | [diff] [blame] | 109 | } while (0) |
| 110 | |
Yabin Cui | a24cf96 | 2019-01-29 17:06:42 -0800 | [diff] [blame] | 111 | #if defined(IN_CTS_TEST) |
| 112 | #define TEST_REQUIRE_APPS() |
| 113 | #else |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 114 | #define TEST_REQUIRE_APPS() \ |
| 115 | do { \ |
Yabin Cui | a24cf96 | 2019-01-29 17:06:42 -0800 | [diff] [blame] | 116 | 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] | 117 | return; \ |
Yabin Cui | a24cf96 | 2019-01-29 17:06:42 -0800 | [diff] [blame] | 118 | } while (0) |
| 119 | #endif |
| 120 | |
Yabin Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 121 | class CaptureStdout { |
| 122 | public: |
| 123 | CaptureStdout() : started_(false) {} |
| 124 | |
| 125 | ~CaptureStdout() { |
| 126 | if (started_) { |
| 127 | Finish(); |
| 128 | } |
| 129 | } |
| 130 | |
| 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 | |
| 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 | }; |
Yabin Cui | dc1a475 | 2020-02-10 17:53:34 -0800 | [diff] [blame] | 162 | |
| 163 | class AppHelper { |
| 164 | public: |
| 165 | ~AppHelper() { |
| 166 | for (auto& package : installed_packages_) { |
| 167 | Workload::RunCmd({"pm", "uninstall", package}); |
| 168 | } |
| 169 | } |
| 170 | |
| 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 | |
| 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: |
| 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"; |
haocheng.zy | 1808a5b | 2022-10-12 22:59:45 +0800 | [diff] [blame] | 194 | #elif defined(__riscv) |
| 195 | return "riscv64"; |
Yabin Cui | dc1a475 | 2020-02-10 17:53:34 -0800 | [diff] [blame] | 196 | #else |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 197 | #error "unrecognized ABI" |
Yabin Cui | dc1a475 | 2020-02-10 17:53:34 -0800 | [diff] [blame] | 198 | #endif |
| 199 | } |
| 200 | |
| 201 | std::vector<std::string> installed_packages_; |
| 202 | std::unique_ptr<Workload> app_start_proc_; |
haocheng.zy | 1808a5b | 2022-10-12 22:59:45 +0800 | [diff] [blame] | 203 | }; |
Yabin Cui | b68f0f9 | 2024-04-15 16:34:00 -0700 | [diff] [blame] | 204 | |
| 205 | bool IsInEmulator(); |