blob: 5780b6c0fe0238f78fb555fc58b7c110dd843dd7 [file] [log] [blame]
Yabin Cuib032de72015-06-17 21:15:09 -07001/*
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 Cuib1a885b2016-02-14 19:18:02 -080017#include <map>
Yabin Cui05400532016-03-17 21:18:53 -070018#include <memory>
Yabin Cui6e51bef2016-02-23 21:41:03 -080019#include <string>
Yabin Cui05400532016-03-17 21:18:53 -070020#include <vector>
Yabin Cuib1a885b2016-02-14 19:18:02 -080021
Yabin Cui900ea072018-03-06 11:35:22 -080022#include <android-base/file.h>
Yabin Cuidc1a4752020-02-10 17:53:34 -080023#include <android-base/strings.h>
Yabin Cui900ea072018-03-06 11:35:22 -080024
Yabin Cui87418d82018-05-21 18:31:56 -070025#include "environment.h"
Yabin Cuib1a885b2016-02-14 19:18:02 -080026#include "read_elf.h"
Yabin Cuib032de72015-06-17 21:15:09 -070027#include "workload.h"
28
Yabin Cui6e51bef2016-02-23 21:41:03 -080029static const std::string SLEEP_SEC = "0.001";
30
Yabin Cui616b3a02017-07-14 15:59:56 -070031void RunWorkloadFunction();
Yabin Cuib1a885b2016-02-14 19:18:02 -080032void CreateProcesses(size_t count, std::vector<std::unique_ptr<Workload>>* workloads);
33
34void ParseSymbol(const ElfFileSymbol& symbol, std::map<std::string, ElfFileSymbol>* symbols);
35void CheckElfFileSymbols(const std::map<std::string, ElfFileSymbol>& symbols);
Yabin Cui4b6720d2016-03-31 14:39:19 -070036
37bool IsRoot();
38
39#define TEST_IN_ROOT(TestStatement) \
40 do { \
41 if (IsRoot()) { \
42 TestStatement; \
43 } else { \
44 GTEST_LOG_(INFO) << "Didn't test \"" << #TestStatement << "\" requires root privileges"; \
45 } \
46 } while (0)
Yabin Cui68b83832017-07-19 17:54:57 -070047
48#if defined(__ANDROID__)
49#define TEST_REQUIRE_HOST_ROOT()
50#else
51#define TEST_REQUIRE_HOST_ROOT() if (!IsRoot()) return
52#endif
Yabin Cui64a9ecd2017-09-13 13:21:32 -070053
54bool IsInNativeAbi();
55// Used to skip tests not supposed to run on non-native ABIs.
56#define OMIT_TEST_ON_NON_NATIVE_ABIS() \
57 do { \
58 if (!IsInNativeAbi()) { \
59 GTEST_LOG_(INFO) << "Skip this test as it only runs on native ABIs."; \
60 return; \
61 } \
62 } while (0)
Yabin Cui900ea072018-03-06 11:35:22 -080063
Yabin Cui8faa6152018-06-07 15:52:57 -070064bool HasHardwareCounter();
65#define TEST_REQUIRE_HW_COUNTER() \
66 do { \
67 if (!HasHardwareCounter()) { \
68 GTEST_LOG_(INFO) << "Skip this test as the machine doesn't have hardware PMU counters."; \
69 return; \
70 } \
71 } while (0)
72
Namhyung Kim52ff4262019-10-23 12:37:34 +090073bool HasPmuCounter();
74#define TEST_REQUIRE_PMU_COUNTER() \
75 do { \
76 if (!HasPmuCounter()) { \
77 GTEST_LOG_(INFO) << "Skip this test as the machine doesn't have low-level PMU counters."; \
78 return; \
79 } \
80 } while (0)
81
Yabin Cui538fb532020-01-27 13:59:24 -080082bool HasTracepointEvents();
83#define TEST_REQUIRE_TRACEPOINT_EVENTS() \
84 do { \
85 if (!HasTracepointEvents()) { \
86 GTEST_LOG_(INFO) << "Skip this test as the machine doesn't support tracepoint events."; \
87 return; \
88 } \
89 } while (0)
90
Yabin Cuia24cf962019-01-29 17:06:42 -080091#if defined(IN_CTS_TEST)
92#define TEST_REQUIRE_APPS()
93#else
94#define TEST_REQUIRE_APPS() \
95 do { \
96 GTEST_LOG_(INFO) << "Skip this test as test apps aren't available."; \
97 return; \
98 } while (0)
99#endif
100
Yabin Cui900ea072018-03-06 11:35:22 -0800101class CaptureStdout {
102 public:
103 CaptureStdout() : started_(false) {}
104
105 ~CaptureStdout() {
106 if (started_) {
107 Finish();
108 }
109 }
110
111 bool Start() {
112 fflush(stdout);
113 old_stdout_ = dup(STDOUT_FILENO);
114 if (old_stdout_ == -1) {
115 return false;
116 }
117 started_ = true;
118 tmpfile_.reset(new TemporaryFile);
119 if (dup2(tmpfile_->fd, STDOUT_FILENO) == -1) {
120 return false;
121 }
122 return true;
123 }
124
125 std::string Finish() {
126 fflush(stdout);
127 started_ = false;
128 dup2(old_stdout_, STDOUT_FILENO);
129 close(old_stdout_);
130 std::string s;
131 if (!android::base::ReadFileToString(tmpfile_->path, &s)) {
132 return "";
133 }
134 return s;
135 }
136
137 private:
138 bool started_;
139 int old_stdout_;
140 std::unique_ptr<TemporaryFile> tmpfile_;
141};
Yabin Cuidc1a4752020-02-10 17:53:34 -0800142
143class AppHelper {
144 public:
145 ~AppHelper() {
146 for (auto& package : installed_packages_) {
147 Workload::RunCmd({"pm", "uninstall", package});
148 }
149 }
150
151 bool InstallApk(const std::string& apk_path, const std::string& package_name) {
152 if (Workload::RunCmd({"pm", "install", "-t", "--abi", GetABI(), apk_path})) {
153 installed_packages_.emplace_back(package_name);
154 return true;
155 }
156 return false;
157 }
158
159 bool StartApp(const std::string& start_cmd) {
160 app_start_proc_ = Workload::CreateWorkload(android::base::Split(start_cmd, " "));
161 return app_start_proc_ && app_start_proc_->Start();
162 }
163
164 private:
165 const char* GetABI() {
166#if defined(__i386__)
167 return "x86";
168#elif defined(__x86_64__)
169 return "x86_64";
170#elif defined(__aarch64__)
171 return "arm64-v8a";
172#elif defined(__arm__)
173 return "armeabi-v7a";
174#else
175 #error "unrecognized ABI"
176#endif
177 }
178
179 std::vector<std::string> installed_packages_;
180 std::unique_ptr<Workload> app_start_proc_;
181};