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 static std::string testdata_dir; 40 41 #if defined(__ANDROID__) 42 43 class ScopedEnablingPerf { 44 public: 45 ScopedEnablingPerf() { 46 prop_value_ = android::base::GetProperty("security.perf_harden", ""); 47 SetProp("0"); 48 } 49 50 ~ScopedEnablingPerf() { 51 if (!prop_value_.empty()) { 52 SetProp(prop_value_); 53 } 54 } 55 56 private: 57 void SetProp(const std::string& value) { 58 android::base::SetProperty("security.perf_harden", value); 59 60 // Sleep one second to wait for security.perf_harden changing 61 // /proc/sys/kernel/perf_event_paranoid. 62 sleep(1); 63 } 64 65 std::string prop_value_; 66 }; 67 68 #endif // defined(__ANDROID__) 69 70 int main(int argc, char** argv) { 71 // To test profiling apps, simpleperf_unit_test needs to copy itself to the app's directory, 72 // and run the binary as simpleperf executable. 73 if (android::base::Basename(argv[0]) == "simpleperf") { 74 return RunSimpleperfCmd(argc, argv) ? 0 : 1; 75 } 76 77 android::base::InitLogging(argv, android::base::StderrLogger); 78 android::base::LogSeverity log_severity = android::base::WARNING; 79 testdata_dir = std::string(dirname(argv[0])) + "/testdata"; 80 for (int i = 1; i < argc; ++i) { 81 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) { 82 testdata_dir = argv[i + 1]; 83 i++; 84 } else if (strcmp(argv[i], "--log") == 0) { 85 if (i + 1 < argc) { 86 ++i; 87 if (!GetLogSeverity(argv[i], &log_severity)) { 88 LOG(ERROR) << "Unknown log severity: " << argv[i]; 89 return 1; 90 } 91 } else { 92 LOG(ERROR) << "Missing argument for --log option.\n"; 93 return 1; 94 } 95 } 96 } 97 android::base::ScopedLogSeverity severity(log_severity); 98 99 #if defined(__ANDROID__) 100 // A cts test PerfEventParanoidTest.java is testing if 101 // /proc/sys/kernel/perf_event_paranoid is 3, so restore perf_harden 102 // value after current test to not break that test. 103 ScopedEnablingPerf scoped_enabling_perf; 104 #endif 105 106 testing::InitGoogleTest(&argc, argv); 107 if (!::testing::GTEST_FLAG(list_tests)) { 108 if (!IsDir(testdata_dir)) { 109 LOG(ERROR) << "testdata wasn't found. Use \"" << argv[0] << " -t <testdata_dir>\""; 110 return 1; 111 } 112 } 113 if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) { 114 testdata_dir += OS_PATH_SEPARATOR; 115 } 116 LOG(INFO) << "testdata is in " << testdata_dir; 117 return RUN_ALL_TESTS(); 118 } 119 120 std::string GetTestData(const std::string& filename) { 121 return testdata_dir + filename; 122 } 123 124 const std::string& GetTestDataDir() { 125 return testdata_dir; 126 } 127