blob: 660994af16b8fa479201d24e381350566dab3c3d [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 Cui900ea072018-03-06 11:35:22 -080023
Yabin Cui87418d82018-05-21 18:31:56 -070024#include "environment.h"
Yabin Cuib1a885b2016-02-14 19:18:02 -080025#include "read_elf.h"
Yabin Cuib032de72015-06-17 21:15:09 -070026#include "workload.h"
27
Yabin Cui6e51bef2016-02-23 21:41:03 -080028static const std::string SLEEP_SEC = "0.001";
29
Yabin Cui616b3a02017-07-14 15:59:56 -070030void RunWorkloadFunction();
Yabin Cuib1a885b2016-02-14 19:18:02 -080031void CreateProcesses(size_t count, std::vector<std::unique_ptr<Workload>>* workloads);
32
33void ParseSymbol(const ElfFileSymbol& symbol, std::map<std::string, ElfFileSymbol>* symbols);
34void CheckElfFileSymbols(const std::map<std::string, ElfFileSymbol>& symbols);
Yabin Cui4b6720d2016-03-31 14:39:19 -070035
36bool IsRoot();
37
38#define TEST_IN_ROOT(TestStatement) \
39 do { \
40 if (IsRoot()) { \
41 TestStatement; \
42 } else { \
43 GTEST_LOG_(INFO) << "Didn't test \"" << #TestStatement << "\" requires root privileges"; \
44 } \
45 } while (0)
Yabin Cui68b83832017-07-19 17:54:57 -070046
47#if defined(__ANDROID__)
48#define TEST_REQUIRE_HOST_ROOT()
49#else
50#define TEST_REQUIRE_HOST_ROOT() if (!IsRoot()) return
51#endif
Yabin Cui64a9ecd2017-09-13 13:21:32 -070052
53bool IsInNativeAbi();
54// Used to skip tests not supposed to run on non-native ABIs.
55#define OMIT_TEST_ON_NON_NATIVE_ABIS() \
56 do { \
57 if (!IsInNativeAbi()) { \
58 GTEST_LOG_(INFO) << "Skip this test as it only runs on native ABIs."; \
59 return; \
60 } \
61 } while (0)
Yabin Cui900ea072018-03-06 11:35:22 -080062
Yabin Cui8faa6152018-06-07 15:52:57 -070063bool HasHardwareCounter();
64#define TEST_REQUIRE_HW_COUNTER() \
65 do { \
66 if (!HasHardwareCounter()) { \
67 GTEST_LOG_(INFO) << "Skip this test as the machine doesn't have hardware PMU counters."; \
68 return; \
69 } \
70 } while (0)
71
Yabin Cuia24cf962019-01-29 17:06:42 -080072#if defined(IN_CTS_TEST)
73#define TEST_REQUIRE_APPS()
74#else
75#define TEST_REQUIRE_APPS() \
76 do { \
77 GTEST_LOG_(INFO) << "Skip this test as test apps aren't available."; \
78 return; \
79 } while (0)
80#endif
81
Yabin Cui900ea072018-03-06 11:35:22 -080082class CaptureStdout {
83 public:
84 CaptureStdout() : started_(false) {}
85
86 ~CaptureStdout() {
87 if (started_) {
88 Finish();
89 }
90 }
91
92 bool Start() {
93 fflush(stdout);
94 old_stdout_ = dup(STDOUT_FILENO);
95 if (old_stdout_ == -1) {
96 return false;
97 }
98 started_ = true;
99 tmpfile_.reset(new TemporaryFile);
100 if (dup2(tmpfile_->fd, STDOUT_FILENO) == -1) {
101 return false;
102 }
103 return true;
104 }
105
106 std::string Finish() {
107 fflush(stdout);
108 started_ = false;
109 dup2(old_stdout_, STDOUT_FILENO);
110 close(old_stdout_);
111 std::string s;
112 if (!android::base::ReadFileToString(tmpfile_->path, &s)) {
113 return "";
114 }
115 return s;
116 }
117
118 private:
119 bool started_;
120 int old_stdout_;
121 std::unique_ptr<TemporaryFile> tmpfile_;
122};