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
17 #include <gtest/gtest.h>
18
19 #include <libgen.h>
20
21 #include <memory>
22
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/strings.h>
26
27 #if defined(__ANDROID__)
28 #include <android-base/properties.h>
29 #endif
30
31 #include "command.h"
32 #include "environment.h"
33 #include "get_test_data.h"
34 #include "read_elf.h"
35 #include "test_util.h"
36 #include "utils.h"
37 #include "workload.h"
38
39 using namespace simpleperf;
40
41 static std::string testdata_dir;
42
43 #if defined(__ANDROID__)
44
45 class ScopedEnablingPerf {
46 public:
ScopedEnablingPerf()47 ScopedEnablingPerf() {
48 prop_value_ = android::base::GetProperty("security.perf_harden", "");
49 SetProp("0");
50 }
51
~ScopedEnablingPerf()52 ~ScopedEnablingPerf() {
53 if (!prop_value_.empty()) {
54 SetProp(prop_value_);
55 }
56 }
57
58 private:
SetProp(const std::string & value)59 void SetProp(const std::string& value) {
60 android::base::SetProperty("security.perf_harden", value);
61
62 // Sleep one second to wait for security.perf_harden changing
63 // perf_event_allow_path.
64 sleep(1);
65 }
66
67 std::string prop_value_;
68 };
69
70 #endif // defined(__ANDROID__)
71
72 // Detect test environment before running tests to utilize cached results for delay-sensitive
73 // tests.
DetectTestEnvironment()74 void DetectTestEnvironment() {
75 #if defined(__linux__)
76 IsInNativeAbi();
77 HasHardwareCounter();
78 #endif // defined(__linux__)
79 HasPmuCounter();
80 HasTracepointEvents();
81 }
82
main(int argc,char ** argv)83 int main(int argc, char** argv) {
84 RegisterAllCommands();
85 // 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
91 testing::InitGoogleTest(&argc, argv);
92 android::base::InitLogging(argv, android::base::StderrLogger);
93 android::base::LogSeverity log_severity = android::base::WARNING;
94 testdata_dir = std::string(dirname(argv[0])) + "/testdata";
95 for (int i = 1; i < argc; ++i) {
96 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
97 testdata_dir = argv[i + 1];
98 i++;
99 } 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 }
110 }
111 }
112 android::base::ScopedLogSeverity severity(log_severity);
113
114 #if defined(__ANDROID__)
115 // A cts test is testing if perf_event_allow_path is 3, so restore perf_harden
116 // value after current test to not break that test.
117 ScopedEnablingPerf scoped_enabling_perf;
118 #endif
119
120 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 }
125 }
126 if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) {
127 testdata_dir += OS_PATH_SEPARATOR;
128 }
129 LOG(INFO) << "testdata is in " << testdata_dir;
130 DetectTestEnvironment();
131 return RUN_ALL_TESTS();
132 }
133
GetTestData(const std::string & filename)134 std::string GetTestData(const std::string& filename) {
135 return testdata_dir + filename;
136 }
137
GetTestDataDir()138 const std::string& GetTestDataDir() {
139 return testdata_dir;
140 }
141