2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include <android-base/file.h>
20 #include <android-base/stringprintf.h>
21 #include <android-base/test_utils.h>
26 #include "environment.h"
27 #include "get_test_data.h"
28 #include "test_util.h"
30 static std::unique_ptr<Command> StatCmd() {
31 return CreateCommandInstance("stat");
34 TEST(stat_cmd, no_options) { ASSERT_TRUE(StatCmd()->Run({"sleep", "1"})); }
36 TEST(stat_cmd, event_option) {
37 ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-clock,task-clock", "sleep", "1"}));
40 TEST(stat_cmd, system_wide_option) {
41 TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"})));
44 TEST(stat_cmd, verbose_option) {
45 ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
48 TEST(stat_cmd, tracepoint_event) {
49 TEST_IN_ROOT(ASSERT_TRUE(
50 StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"})));
53 TEST(stat_cmd, event_modifier) {
55 StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
58 void RunWorkloadFunction() {
60 for (volatile int i = 0; i < 10000; ++i);
65 void CreateProcesses(size_t count,
66 std::vector<std::unique_ptr<Workload>>* workloads) {
68 // Create workloads run longer than profiling time.
69 for (size_t i = 0; i < count; ++i) {
70 std::unique_ptr<Workload> workload;
71 if (GetDefaultAppPackageName().empty()) {
72 workload = Workload::CreateWorkload(RunWorkloadFunction);
74 workload = Workload::CreateWorkload({"run-as", GetDefaultAppPackageName(), "./workload"});
75 workload->SetKillFunction([](pid_t pid) {
76 Workload::RunCmd({"run-as", GetDefaultAppPackageName(), "kill", std::to_string(pid)});
79 ASSERT_TRUE(workload != nullptr);
80 ASSERT_TRUE(workload->Start());
81 workloads->push_back(std::move(workload));
85 TEST(stat_cmd, existing_processes) {
86 std::vector<std::unique_ptr<Workload>> workloads;
87 CreateProcesses(2, &workloads);
88 std::string pid_list = android::base::StringPrintf(
89 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
90 ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
93 TEST(stat_cmd, existing_threads) {
94 std::vector<std::unique_ptr<Workload>> workloads;
95 CreateProcesses(2, &workloads);
96 // Process id can be used as thread id in linux.
97 std::string tid_list = android::base::StringPrintf(
98 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
99 ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
102 TEST(stat_cmd, no_monitored_threads) { ASSERT_FALSE(StatCmd()->Run({""})); }
104 TEST(stat_cmd, group_option) {
106 StatCmd()->Run({"--group", "cpu-clock,page-faults", "sleep", "1"}));
107 ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,instructions", "--group",
108 "cpu-cycles:u,instructions:u", "--group",
109 "cpu-cycles:k,instructions:k", "sleep", "1"}));
112 TEST(stat_cmd, auto_generated_summary) {
113 TemporaryFile tmp_file;
114 ASSERT_TRUE(StatCmd()->Run({"--group", "instructions:u,instructions:k", "-o",
115 tmp_file.path, "sleep", "1"}));
117 ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
118 size_t pos = s.find("instructions:u");
119 ASSERT_NE(s.npos, pos);
120 pos = s.find("instructions:k", pos);
121 ASSERT_NE(s.npos, pos);
122 pos += strlen("instructions:k");
123 // Check if the summary of instructions is generated.
124 ASSERT_NE(s.npos, s.find("instructions", pos));
127 TEST(stat_cmd, duration_option) {
129 StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid()), "--in-app"}));
130 ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
133 TEST(stat_cmd, interval_option) {
134 TemporaryFile tmp_file;
136 StatCmd()->Run({"--interval", "500.0", "--duration", "1.2", "-o",
137 tmp_file.path, "sleep", "2"}));
139 ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
142 std::string subs = "statistics:";
143 while((pos = s.find(subs, pos)) != s.npos) {
147 ASSERT_EQ(count, 3UL);
150 TEST(stat_cmd, no_modifier_for_clock_events) {
151 for (const std::string& e : {"cpu-clock", "task-clock"}) {
152 for (const std::string& m : {"u", "k"}) {
153 ASSERT_FALSE(StatCmd()->Run({"-e", e + ":" + m, "sleep", "0.1"}))
154 << "event " << e << ":" << m;
159 TEST(stat_cmd, handle_SIGHUP) {
160 std::thread thread([]() {
162 kill(getpid(), SIGHUP);
165 ASSERT_TRUE(StatCmd()->Run({"sleep", "1000000"}));
168 TEST(stat_cmd, stop_when_no_more_targets) {
169 std::atomic<int> tid(0);
170 std::thread thread([&]() {
176 ASSERT_TRUE(StatCmd()->Run({"-t", std::to_string(tid), "--in-app"}));