1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker #include <libgen.h>
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker #include <memory>
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
24*288bf522SAndroid Build Coastguard Worker #include <android-base/logging.h>
25*288bf522SAndroid Build Coastguard Worker #include <android-base/strings.h>
26*288bf522SAndroid Build Coastguard Worker
27*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
28*288bf522SAndroid Build Coastguard Worker #include <android-base/properties.h>
29*288bf522SAndroid Build Coastguard Worker #endif
30*288bf522SAndroid Build Coastguard Worker
31*288bf522SAndroid Build Coastguard Worker #include "command.h"
32*288bf522SAndroid Build Coastguard Worker #include "environment.h"
33*288bf522SAndroid Build Coastguard Worker #include "get_test_data.h"
34*288bf522SAndroid Build Coastguard Worker #include "read_elf.h"
35*288bf522SAndroid Build Coastguard Worker #include "test_util.h"
36*288bf522SAndroid Build Coastguard Worker #include "utils.h"
37*288bf522SAndroid Build Coastguard Worker #include "workload.h"
38*288bf522SAndroid Build Coastguard Worker
39*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
40*288bf522SAndroid Build Coastguard Worker
41*288bf522SAndroid Build Coastguard Worker static std::string testdata_dir;
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
44*288bf522SAndroid Build Coastguard Worker
45*288bf522SAndroid Build Coastguard Worker class ScopedEnablingPerf {
46*288bf522SAndroid Build Coastguard Worker public:
ScopedEnablingPerf()47*288bf522SAndroid Build Coastguard Worker ScopedEnablingPerf() {
48*288bf522SAndroid Build Coastguard Worker prop_value_ = android::base::GetProperty("security.perf_harden", "");
49*288bf522SAndroid Build Coastguard Worker SetProp("0");
50*288bf522SAndroid Build Coastguard Worker }
51*288bf522SAndroid Build Coastguard Worker
~ScopedEnablingPerf()52*288bf522SAndroid Build Coastguard Worker ~ScopedEnablingPerf() {
53*288bf522SAndroid Build Coastguard Worker if (!prop_value_.empty()) {
54*288bf522SAndroid Build Coastguard Worker SetProp(prop_value_);
55*288bf522SAndroid Build Coastguard Worker }
56*288bf522SAndroid Build Coastguard Worker }
57*288bf522SAndroid Build Coastguard Worker
58*288bf522SAndroid Build Coastguard Worker private:
SetProp(const std::string & value)59*288bf522SAndroid Build Coastguard Worker void SetProp(const std::string& value) {
60*288bf522SAndroid Build Coastguard Worker android::base::SetProperty("security.perf_harden", value);
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker // Sleep one second to wait for security.perf_harden changing
63*288bf522SAndroid Build Coastguard Worker // perf_event_allow_path.
64*288bf522SAndroid Build Coastguard Worker sleep(1);
65*288bf522SAndroid Build Coastguard Worker }
66*288bf522SAndroid Build Coastguard Worker
67*288bf522SAndroid Build Coastguard Worker std::string prop_value_;
68*288bf522SAndroid Build Coastguard Worker };
69*288bf522SAndroid Build Coastguard Worker
70*288bf522SAndroid Build Coastguard Worker #endif // defined(__ANDROID__)
71*288bf522SAndroid Build Coastguard Worker
72*288bf522SAndroid Build Coastguard Worker // Detect test environment before running tests to utilize cached results for delay-sensitive
73*288bf522SAndroid Build Coastguard Worker // tests.
DetectTestEnvironment()74*288bf522SAndroid Build Coastguard Worker void DetectTestEnvironment() {
75*288bf522SAndroid Build Coastguard Worker #if defined(__linux__)
76*288bf522SAndroid Build Coastguard Worker IsInNativeAbi();
77*288bf522SAndroid Build Coastguard Worker HasHardwareCounter();
78*288bf522SAndroid Build Coastguard Worker #endif // defined(__linux__)
79*288bf522SAndroid Build Coastguard Worker HasPmuCounter();
80*288bf522SAndroid Build Coastguard Worker HasTracepointEvents();
81*288bf522SAndroid Build Coastguard Worker }
82*288bf522SAndroid Build Coastguard Worker
main(int argc,char ** argv)83*288bf522SAndroid Build Coastguard Worker int main(int argc, char** argv) {
84*288bf522SAndroid Build Coastguard Worker RegisterAllCommands();
85*288bf522SAndroid Build Coastguard Worker // To test profiling apps, simpleperf_unit_test needs to copy itself to the app's directory,
86*288bf522SAndroid Build Coastguard Worker // and run the binary as simpleperf executable.
87*288bf522SAndroid Build Coastguard Worker if (android::base::Basename(argv[0]) == "simpleperf") {
88*288bf522SAndroid Build Coastguard Worker return RunSimpleperfCmd(argc, argv) ? 0 : 1;
89*288bf522SAndroid Build Coastguard Worker }
90*288bf522SAndroid Build Coastguard Worker
91*288bf522SAndroid Build Coastguard Worker testing::InitGoogleTest(&argc, argv);
92*288bf522SAndroid Build Coastguard Worker android::base::InitLogging(argv, android::base::StderrLogger);
93*288bf522SAndroid Build Coastguard Worker android::base::LogSeverity log_severity = android::base::WARNING;
94*288bf522SAndroid Build Coastguard Worker testdata_dir = std::string(dirname(argv[0])) + "/testdata";
95*288bf522SAndroid Build Coastguard Worker for (int i = 1; i < argc; ++i) {
96*288bf522SAndroid Build Coastguard Worker if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
97*288bf522SAndroid Build Coastguard Worker testdata_dir = argv[i + 1];
98*288bf522SAndroid Build Coastguard Worker i++;
99*288bf522SAndroid Build Coastguard Worker } else if (strcmp(argv[i], "--log") == 0) {
100*288bf522SAndroid Build Coastguard Worker if (i + 1 < argc) {
101*288bf522SAndroid Build Coastguard Worker ++i;
102*288bf522SAndroid Build Coastguard Worker if (!GetLogSeverity(argv[i], &log_severity)) {
103*288bf522SAndroid Build Coastguard Worker LOG(ERROR) << "Unknown log severity: " << argv[i];
104*288bf522SAndroid Build Coastguard Worker return 1;
105*288bf522SAndroid Build Coastguard Worker }
106*288bf522SAndroid Build Coastguard Worker } else {
107*288bf522SAndroid Build Coastguard Worker LOG(ERROR) << "Missing argument for --log option.\n";
108*288bf522SAndroid Build Coastguard Worker return 1;
109*288bf522SAndroid Build Coastguard Worker }
110*288bf522SAndroid Build Coastguard Worker }
111*288bf522SAndroid Build Coastguard Worker }
112*288bf522SAndroid Build Coastguard Worker android::base::ScopedLogSeverity severity(log_severity);
113*288bf522SAndroid Build Coastguard Worker
114*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
115*288bf522SAndroid Build Coastguard Worker // A cts test is testing if perf_event_allow_path is 3, so restore perf_harden
116*288bf522SAndroid Build Coastguard Worker // value after current test to not break that test.
117*288bf522SAndroid Build Coastguard Worker ScopedEnablingPerf scoped_enabling_perf;
118*288bf522SAndroid Build Coastguard Worker #endif
119*288bf522SAndroid Build Coastguard Worker
120*288bf522SAndroid Build Coastguard Worker if (!::testing::GTEST_FLAG(list_tests)) {
121*288bf522SAndroid Build Coastguard Worker if (!IsDir(testdata_dir)) {
122*288bf522SAndroid Build Coastguard Worker LOG(ERROR) << "testdata wasn't found. Use \"" << argv[0] << " -t <testdata_dir>\"";
123*288bf522SAndroid Build Coastguard Worker return 1;
124*288bf522SAndroid Build Coastguard Worker }
125*288bf522SAndroid Build Coastguard Worker }
126*288bf522SAndroid Build Coastguard Worker if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) {
127*288bf522SAndroid Build Coastguard Worker testdata_dir += OS_PATH_SEPARATOR;
128*288bf522SAndroid Build Coastguard Worker }
129*288bf522SAndroid Build Coastguard Worker LOG(INFO) << "testdata is in " << testdata_dir;
130*288bf522SAndroid Build Coastguard Worker DetectTestEnvironment();
131*288bf522SAndroid Build Coastguard Worker return RUN_ALL_TESTS();
132*288bf522SAndroid Build Coastguard Worker }
133*288bf522SAndroid Build Coastguard Worker
GetTestData(const std::string & filename)134*288bf522SAndroid Build Coastguard Worker std::string GetTestData(const std::string& filename) {
135*288bf522SAndroid Build Coastguard Worker return testdata_dir + filename;
136*288bf522SAndroid Build Coastguard Worker }
137*288bf522SAndroid Build Coastguard Worker
GetTestDataDir()138*288bf522SAndroid Build Coastguard Worker const std::string& GetTestDataDir() {
139*288bf522SAndroid Build Coastguard Worker return testdata_dir;
140*288bf522SAndroid Build Coastguard Worker }
141