blob: 196054bfc346fa8089cd5f013e2df2312219a0ee [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -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
17#include <gtest/gtest.h>
18
Colin Cross1e8ad072016-12-17 12:38:46 -080019#include <libgen.h>
20
Yabin Cuibe7ec662016-03-02 13:56:28 -080021#include <memory>
22
23#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080024#include <android-base/logging.h>
Yabin Cui1b9b1c12018-10-29 14:23:48 -070025#include <android-base/strings.h>
Yabin Cui323e9452015-04-20 18:07:17 -070026
Yabin Cui58961322016-06-10 12:15:11 -070027#if defined(__ANDROID__)
Yabin Cui80a1e122017-08-11 14:52:51 -070028#include <android-base/properties.h>
Yabin Cui58961322016-06-10 12:15:11 -070029#endif
30
Yabin Cui616b3a02017-07-14 15:59:56 -070031#include "command.h"
32#include "environment.h"
Yabin Cui569f64a2016-02-05 17:32:08 -080033#include "get_test_data.h"
Yabin Cuibe7ec662016-03-02 13:56:28 -080034#include "read_elf.h"
Yabin Cui616b3a02017-07-14 15:59:56 -070035#include "test_util.h"
Yabin Cui569f64a2016-02-05 17:32:08 -080036#include "utils.h"
Yabin Cui616b3a02017-07-14 15:59:56 -070037#include "workload.h"
Yabin Cui569f64a2016-02-05 17:32:08 -080038
Yabin Cuiacbdb242020-07-07 15:56:34 -070039using namespace simpleperf;
40
Yabin Cui569f64a2016-02-05 17:32:08 -080041static std::string testdata_dir;
42
Yabin Cui6acf8c62016-03-18 13:48:42 -070043#if defined(__ANDROID__)
Yabin Cui58961322016-06-10 12:15:11 -070044
Yabin Cuif560a6f2016-12-14 17:43:26 -080045class ScopedEnablingPerf {
Yabin Cui58961322016-06-10 12:15:11 -070046 public:
Yabin Cuif560a6f2016-12-14 17:43:26 -080047 ScopedEnablingPerf() {
Yabin Cui80a1e122017-08-11 14:52:51 -070048 prop_value_ = android::base::GetProperty("security.perf_harden", "");
Yabin Cuif560a6f2016-12-14 17:43:26 -080049 SetProp("0");
Yabin Cui58961322016-06-10 12:15:11 -070050 }
51
Yabin Cuif560a6f2016-12-14 17:43:26 -080052 ~ScopedEnablingPerf() {
Yabin Cui80a1e122017-08-11 14:52:51 -070053 if (!prop_value_.empty()) {
Yabin Cuif560a6f2016-12-14 17:43:26 -080054 SetProp(prop_value_);
Yabin Cui58961322016-06-10 12:15:11 -070055 }
56 }
57
58 private:
Yabin Cui80a1e122017-08-11 14:52:51 -070059 void SetProp(const std::string& value) {
60 android::base::SetProperty("security.perf_harden", value);
61
Yabin Cuif560a6f2016-12-14 17:43:26 -080062 // Sleep one second to wait for security.perf_harden changing
Yabin Cui17cffbb2020-07-28 17:11:39 -070063 // perf_event_allow_path.
Yabin Cuif560a6f2016-12-14 17:43:26 -080064 sleep(1);
65 }
66
Yabin Cui80a1e122017-08-11 14:52:51 -070067 std::string prop_value_;
Yabin Cui58961322016-06-10 12:15:11 -070068};
69
Yabin Cui6acf8c62016-03-18 13:48:42 -070070#endif // defined(__ANDROID__)
Yabin Cuibe7ec662016-03-02 13:56:28 -080071
Yabin Cui7a013452024-06-25 14:07:34 -070072// Detect test environment before running tests to utilize cached results for delay-sensitive
73// tests.
74void DetectTestEnvironment() {
75#if defined(__linux__)
76 IsInNativeAbi();
77 HasHardwareCounter();
78#endif // defined(__linux__)
79 HasPmuCounter();
80 HasTracepointEvents();
81}
82
Yabin Cui323e9452015-04-20 18:07:17 -070083int main(int argc, char** argv) {
Yabin Cui34227b22023-07-20 19:40:28 +000084 RegisterAllCommands();
Yabin Cui56233cc2019-09-30 16:46:26 -070085 // To test profiling apps, simpleperf_unit_test needs to copy itself to the app's directory,
86 // and run the binary as simpleperf executable.
87 if (android::base::Basename(argv[0]) == "simpleperf") {
88 return RunSimpleperfCmd(argc, argv) ? 0 : 1;
89 }
90
Krzysztof KosiƄski5f895342023-03-03 04:26:06 +000091 testing::InitGoogleTest(&argc, argv);
Yabin Cuif560a6f2016-12-14 17:43:26 -080092 android::base::InitLogging(argv, android::base::StderrLogger);
Yabin Cui8f680f62016-03-18 18:47:43 -070093 android::base::LogSeverity log_severity = android::base::WARNING;
Yabin Cui803c27e2019-01-25 10:47:25 -080094 testdata_dir = std::string(dirname(argv[0])) + "/testdata";
Yabin Cui569f64a2016-02-05 17:32:08 -080095 for (int i = 1; i < argc; ++i) {
96 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
97 testdata_dir = argv[i + 1];
Yabin Cuibe7ec662016-03-02 13:56:28 -080098 i++;
Yabin Cui8f680f62016-03-18 18:47:43 -070099 } else if (strcmp(argv[i], "--log") == 0) {
100 if (i + 1 < argc) {
101 ++i;
102 if (!GetLogSeverity(argv[i], &log_severity)) {
103 LOG(ERROR) << "Unknown log severity: " << argv[i];
104 return 1;
105 }
106 } else {
107 LOG(ERROR) << "Missing argument for --log option.\n";
108 return 1;
109 }
Yabin Cui569f64a2016-02-05 17:32:08 -0800110 }
111 }
Yabin Cui8f680f62016-03-18 18:47:43 -0700112 android::base::ScopedLogSeverity severity(log_severity);
Yabin Cuibe7ec662016-03-02 13:56:28 -0800113
Yabin Cui6acf8c62016-03-18 13:48:42 -0700114#if defined(__ANDROID__)
Yabin Cui17cffbb2020-07-28 17:11:39 -0700115 // A cts test is testing if perf_event_allow_path is 3, so restore perf_harden
Yabin Cui616b3a02017-07-14 15:59:56 -0700116 // value after current test to not break that test.
117 ScopedEnablingPerf scoped_enabling_perf;
Yabin Cuibe7ec662016-03-02 13:56:28 -0800118#endif
Yabin Cui6acf8c62016-03-18 13:48:42 -0700119
Yabin Cui803c27e2019-01-25 10:47:25 -0800120 if (!::testing::GTEST_FLAG(list_tests)) {
121 if (!IsDir(testdata_dir)) {
122 LOG(ERROR) << "testdata wasn't found. Use \"" << argv[0] << " -t <testdata_dir>\"";
123 return 1;
124 }
Yabin Cui569f64a2016-02-05 17:32:08 -0800125 }
Yabin Cui1b9b1c12018-10-29 14:23:48 -0700126 if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) {
127 testdata_dir += OS_PATH_SEPARATOR;
Yabin Cui569f64a2016-02-05 17:32:08 -0800128 }
Yabin Cuibe7ec662016-03-02 13:56:28 -0800129 LOG(INFO) << "testdata is in " << testdata_dir;
Yabin Cui7a013452024-06-25 14:07:34 -0700130 DetectTestEnvironment();
Yabin Cui323e9452015-04-20 18:07:17 -0700131 return RUN_ALL_TESTS();
132}
Yabin Cui569f64a2016-02-05 17:32:08 -0800133
134std::string GetTestData(const std::string& filename) {
135 return testdata_dir + filename;
136}
Yabin Cuib1a885b2016-02-14 19:18:02 -0800137
138const std::string& GetTestDataDir() {
139 return testdata_dir;
140}