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 | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 23 | |
Yabin Cui | 87418d8 | 2018-05-21 18:31:56 -0700 | [diff] [blame] | 24 | #include "environment.h" |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 25 | #include "read_elf.h" |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 26 | #include "workload.h" |
| 27 | |
Yabin Cui | 6e51bef | 2016-02-23 21:41:03 -0800 | [diff] [blame] | 28 | static const std::string SLEEP_SEC = "0.001"; |
| 29 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 30 | void RunWorkloadFunction(); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 31 | void CreateProcesses(size_t count, std::vector<std::unique_ptr<Workload>>* workloads); |
| 32 | |
| 33 | void ParseSymbol(const ElfFileSymbol& symbol, std::map<std::string, ElfFileSymbol>* symbols); |
| 34 | void CheckElfFileSymbols(const std::map<std::string, ElfFileSymbol>& symbols); |
Yabin Cui | 4b6720d | 2016-03-31 14:39:19 -0700 | [diff] [blame] | 35 | |
| 36 | bool 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 Cui | 68b8383 | 2017-07-19 17:54:57 -0700 | [diff] [blame] | 46 | |
| 47 | #if defined(__ANDROID__) |
| 48 | #define TEST_REQUIRE_HOST_ROOT() |
| 49 | #else |
| 50 | #define TEST_REQUIRE_HOST_ROOT() if (!IsRoot()) return |
| 51 | #endif |
Yabin Cui | 64a9ecd | 2017-09-13 13:21:32 -0700 | [diff] [blame] | 52 | |
| 53 | bool 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 Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 62 | |
Yabin Cui | 8faa615 | 2018-06-07 15:52:57 -0700 | [diff] [blame] | 63 | bool 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 Cui | a24cf96 | 2019-01-29 17:06:42 -0800 | [diff] [blame] | 72 | #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 Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 82 | class 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 | }; |