diff options
author | Yabin Cui <yabinc@google.com> | 2016-12-14 17:43:26 -0800 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2017-02-15 11:44:40 -0800 |
commit | f560a6fdf092f0c03c0e7a92d62948d34318146a (patch) | |
tree | a22ca918c3b47945afd8d33ada7ca58329bf6069 /simpleperf | |
parent | 264b69316cd385d887a2c7beb289eea5b0a9f05d (diff) | |
download | extras-f560a6fdf092f0c03c0e7a92d62948d34318146a.tar.gz |
simpleperf: build cts test running in app context.
When built for cts test, the test binary uses run-as to move itself
to the data directory of debuggable app 'com.android.simpleperf',
and run the tests in app context using run-as.
Bug: http://b/18790309
Test: run cts-tradefed, 'run everything -m CtsSimpleperfTestCase'.
Change-Id: I523470bb8062cb944b44dc6f264a8b1c14221b62
Diffstat (limited to 'simpleperf')
-rw-r--r-- | simpleperf/Android.mk | 2 | ||||
-rw-r--r-- | simpleperf/cmd_report_test.cpp | 2 | ||||
-rw-r--r-- | simpleperf/cpu_hotplug_test.cpp | 2 | ||||
-rw-r--r-- | simpleperf/dso.cpp | 2 | ||||
-rw-r--r-- | simpleperf/gtest_main.cpp | 92 | ||||
-rw-r--r-- | simpleperf/main.cpp | 2 |
6 files changed, 70 insertions, 32 deletions
diff --git a/simpleperf/Android.mk b/simpleperf/Android.mk index a677de4a..2a39b599 100644 --- a/simpleperf/Android.mk +++ b/simpleperf/Android.mk @@ -352,7 +352,7 @@ libsimpleperf_cts_test_src_files := \ include $(CLEAR_VARS) LOCAL_CLANG := true LOCAL_MODULE := libsimpleperf_cts_test -LOCAL_CPPFLAGS := $(simpleperf_cppflags_target) +LOCAL_CPPFLAGS := $(simpleperf_cppflags_target) -DRUN_IN_APP_CONTEXT LOCAL_SRC_FILES := $(libsimpleperf_cts_test_src_files) LOCAL_STATIC_LIBRARIES := $(simpleperf_static_libraries_target) LOCAL_MULTILIB := both diff --git a/simpleperf/cmd_report_test.cpp b/simpleperf/cmd_report_test.cpp index f445bd5a..9a5c23bd 100644 --- a/simpleperf/cmd_report_test.cpp +++ b/simpleperf/cmd_report_test.cpp @@ -45,6 +45,7 @@ class ReportCommandTest : public ::testing::Test { const std::string& perf_data, const std::vector<std::string>& add_args = std::vector<std::string>()) { success = false; + TemporaryFile tmp_file; std::vector<std::string> args = { "-i", perf_data, "--symfs", GetTestDataDir(), "-o", tmp_file.path}; args.insert(args.end(), add_args.begin(), add_args.end()); @@ -63,7 +64,6 @@ class ReportCommandTest : public ::testing::Test { success = true; } - TemporaryFile tmp_file; std::string content; std::vector<std::string> lines; bool success; diff --git a/simpleperf/cpu_hotplug_test.cpp b/simpleperf/cpu_hotplug_test.cpp index 51ec6779..c30ca67d 100644 --- a/simpleperf/cpu_hotplug_test.cpp +++ b/simpleperf/cpu_hotplug_test.cpp @@ -473,7 +473,7 @@ int main(int argc, char** argv) { verbose_mode = true; } } - InitLogging(argv, android::base::StderrLogger); + android::base::InitLogging(argv, android::base::StderrLogger); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } diff --git a/simpleperf/dso.cpp b/simpleperf/dso.cpp index 351d88ae..40603fe8 100644 --- a/simpleperf/dso.cpp +++ b/simpleperf/dso.cpp @@ -97,7 +97,7 @@ bool Dso::SetSymFsDir(const std::string& symfs_dir) { if (dirname.back() != '/') { dirname.push_back('/'); } - if (GetEntriesInDir(symfs_dir).empty()) { + if (!IsDir(symfs_dir)) { LOG(ERROR) << "Invalid symfs_dir '" << symfs_dir << "'"; return false; } diff --git a/simpleperf/gtest_main.cpp b/simpleperf/gtest_main.cpp index 922b490f..3cc929c8 100644 --- a/simpleperf/gtest_main.cpp +++ b/simpleperf/gtest_main.cpp @@ -99,45 +99,72 @@ static bool ExtractTestDataFromElfSection() { return true; } -class SavedPerfHardenProperty { +class ScopedEnablingPerf { public: - SavedPerfHardenProperty() { + ScopedEnablingPerf() { + memset(prop_value_, '\0', sizeof(prop_value_)); __system_property_get("security.perf_harden", prop_value_); - if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid", - ¶noid_value_)) { - PLOG(ERROR) << "failed to read /proc/sys/kernel/perf_event_paranoid"; - } + SetProp("0"); } - ~SavedPerfHardenProperty() { + ~ScopedEnablingPerf() { if (strlen(prop_value_) != 0) { - __system_property_set("security.perf_harden", prop_value_); - // Sleep one second to wait for security.perf_harden changing - // /proc/sys/kernel/perf_event_paranoid. - sleep(1); - std::string paranoid_value; - if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid", - ¶noid_value)) { - PLOG(ERROR) << "failed to read /proc/sys/kernel/perf_event_paranoid"; - return; - } - if (paranoid_value_ != paranoid_value) { - LOG(ERROR) << "failed to restore /proc/sys/kernel/perf_event_paranoid"; - } + SetProp(prop_value_); } } private: + void SetProp(const char* value) { + __system_property_set("security.perf_harden", value); + // Sleep one second to wait for security.perf_harden changing + // /proc/sys/kernel/perf_event_paranoid. + sleep(1); + } + char prop_value_[PROP_VALUE_MAX]; - std::string paranoid_value_; }; +static bool TestInAppContext(int argc, char** argv) { + // Use run-as to move the test executable to the data directory of debuggable app + // 'com.android.simpleperf', and run it. + std::string exe_path; + if (!android::base::Readlink("/proc/self/exe", &exe_path)) { + PLOG(ERROR) << "readlink failed"; + return false; + } + std::string copy_cmd = android::base::StringPrintf("run-as com.android.simpleperf cp %s .", + exe_path.c_str()); + if (system(copy_cmd.c_str()) == -1) { + PLOG(ERROR) << "system(" << copy_cmd << ") failed"; + return false; + } + std::string arg_str; + arg_str += basename(argv[0]); + for (int i = 1; i < argc; ++i) { + arg_str.push_back(' '); + arg_str += argv[i]; + } + std::string test_cmd = android::base::StringPrintf("run-as com.android.simpleperf ./%s", + arg_str.c_str()); + test_cmd += " --in-app-context"; + if (system(test_cmd.c_str()) == -1) { + PLOG(ERROR) << "system(" << test_cmd << ") failed"; + return false; + } + return true; +} + #endif // defined(__ANDROID__) int main(int argc, char** argv) { - InitLogging(argv, android::base::StderrLogger); - testing::InitGoogleTest(&argc, argv); + android::base::InitLogging(argv, android::base::StderrLogger); android::base::LogSeverity log_severity = android::base::WARNING; + bool need_app_context __attribute__((unused)) = false; + bool in_app_context __attribute__((unused)) = false; + +#if defined(RUN_IN_APP_CONTEXT) + need_app_context = true; +#endif for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) { @@ -154,11 +181,25 @@ int main(int argc, char** argv) { LOG(ERROR) << "Missing argument for --log option.\n"; return 1; } + } else if (strcmp(argv[i], "--in-app-context") == 0) { + in_app_context = true; } } android::base::ScopedLogSeverity severity(log_severity); #if defined(__ANDROID__) + std::unique_ptr<ScopedEnablingPerf> scoped_enabling_perf; + if (!in_app_context) { + // A cts test PerfEventParanoidTest.java is testing if + // /proc/sys/kernel/perf_event_paranoid is 3, so restore perf_harden + // value after current test to not break that test. + scoped_enabling_perf.reset(new ScopedEnablingPerf); + } + + if (need_app_context && !in_app_context) { + return TestInAppContext(argc, argv) ? 0 : 1; + } + std::unique_ptr<TemporaryDir> tmp_dir; if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { testdata_dir = std::string(dirname(argv[0])) + "/testdata"; @@ -172,12 +213,9 @@ int main(int argc, char** argv) { } } - // A cts test PerfEventParanoidTest.java is testing if - // /proc/sys/kernel/perf_event_paranoid is 3, so restore perf_harden - // value after current test to not break that test. - SavedPerfHardenProperty saved_perf_harden; #endif + testing::InitGoogleTest(&argc, argv); if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { printf("Usage: %s -t <testdata_dir>\n", argv[0]); return 1; diff --git a/simpleperf/main.cpp b/simpleperf/main.cpp index a8a8935e..07b0e4b3 100644 --- a/simpleperf/main.cpp +++ b/simpleperf/main.cpp @@ -27,7 +27,7 @@ constexpr int SIMPLEPERF_VERSION = 1; int main(int argc, char** argv) { - InitLogging(argv, android::base::StderrLogger); + android::base::InitLogging(argv, android::base::StderrLogger); std::vector<std::string> args; android::base::LogSeverity log_severity = android::base::INFO; |