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 <android-base/file.h>
20 #include <android-base/stringprintf.h>
21 #include <android-base/strings.h>
22 
23 #include <thread>
24 
25 #include "command.h"
26 #include "environment.h"
27 #include "event_selection_set.h"
28 #include "get_test_data.h"
29 #include "test_util.h"
30 
StatCmd()31 static std::unique_ptr<Command> StatCmd() {
32   return CreateCommandInstance("stat");
33 }
34 
TEST(stat_cmd,no_options)35 TEST(stat_cmd, no_options) { ASSERT_TRUE(StatCmd()->Run({"sleep", "1"})); }
36 
TEST(stat_cmd,event_option)37 TEST(stat_cmd, event_option) {
38   ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-clock,task-clock", "sleep", "1"}));
39 }
40 
TEST(stat_cmd,system_wide_option)41 TEST(stat_cmd, system_wide_option) {
42   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"})));
43 }
44 
TEST(stat_cmd,verbose_option)45 TEST(stat_cmd, verbose_option) {
46   ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
47 }
48 
TEST(stat_cmd,tracepoint_event)49 TEST(stat_cmd, tracepoint_event) {
50   TEST_IN_ROOT(ASSERT_TRUE(
51       StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"})));
52 }
53 
TEST(stat_cmd,rN_event)54 TEST(stat_cmd, rN_event) {
55   TEST_REQUIRE_HW_COUNTER();
56   OMIT_TEST_ON_NON_NATIVE_ABIS();
57   size_t event_number;
58   if (GetBuildArch() == ARCH_ARM64 || GetBuildArch() == ARCH_ARM) {
59     // As in D5.10.2 of the ARMv8 manual, ARM defines the event number space for PMU. part of the
60     // space is for common event numbers (which will stay the same for all ARM chips), part of the
61     // space is for implementation defined events. Here 0x08 is a common event for instructions.
62     event_number = 0x08;
63   } else if (GetBuildArch() == ARCH_X86_32 || GetBuildArch() == ARCH_X86_64) {
64     // As in volume 3 chapter 19 of the Intel manual, 0x00c0 is the event number for instruction.
65     event_number = 0x00c0;
66   } else {
67     GTEST_LOG_(INFO) << "Omit arch " << GetBuildArch();
68     return;
69   }
70   std::string event_name = android::base::StringPrintf("r%zx", event_number);
71   ASSERT_TRUE(StatCmd()->Run({"-e", event_name, "sleep", "1"}));
72 }
73 
TEST(stat_cmd,event_modifier)74 TEST(stat_cmd, event_modifier) {
75   TEST_REQUIRE_HW_COUNTER();
76   ASSERT_TRUE(
77       StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
78 }
79 
RunWorkloadFunction()80 void RunWorkloadFunction() {
81   while (true) {
82     for (volatile int i = 0; i < 10000; ++i);
83     usleep(1);
84   }
85 }
86 
CreateProcesses(size_t count,std::vector<std::unique_ptr<Workload>> * workloads)87 void CreateProcesses(size_t count,
88                      std::vector<std::unique_ptr<Workload>>* workloads) {
89   workloads->clear();
90   // Create workloads run longer than profiling time.
91   for (size_t i = 0; i < count; ++i) {
92     std::unique_ptr<Workload> workload;
93     workload = Workload::CreateWorkload(RunWorkloadFunction);
94     ASSERT_TRUE(workload != nullptr);
95     ASSERT_TRUE(workload->Start());
96     workloads->push_back(std::move(workload));
97   }
98 }
99 
TEST(stat_cmd,existing_processes)100 TEST(stat_cmd, existing_processes) {
101   std::vector<std::unique_ptr<Workload>> workloads;
102   CreateProcesses(2, &workloads);
103   std::string pid_list = android::base::StringPrintf(
104       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
105   ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
106 }
107 
TEST(stat_cmd,existing_threads)108 TEST(stat_cmd, existing_threads) {
109   std::vector<std::unique_ptr<Workload>> workloads;
110   CreateProcesses(2, &workloads);
111   // Process id can be used as thread id in linux.
112   std::string tid_list = android::base::StringPrintf(
113       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
114   ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
115 }
116 
TEST(stat_cmd,no_monitored_threads)117 TEST(stat_cmd, no_monitored_threads) {
118   ASSERT_FALSE(StatCmd()->Run({}));
119   ASSERT_FALSE(StatCmd()->Run({""}));
120 }
121 
TEST(stat_cmd,group_option)122 TEST(stat_cmd, group_option) {
123   TEST_REQUIRE_HW_COUNTER();
124   ASSERT_TRUE(
125       StatCmd()->Run({"--group", "cpu-clock,page-faults", "sleep", "1"}));
126   ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,instructions", "--group",
127                               "cpu-cycles:u,instructions:u", "--group",
128                               "cpu-cycles:k,instructions:k", "sleep", "1"}));
129 }
130 
TEST(stat_cmd,auto_generated_summary)131 TEST(stat_cmd, auto_generated_summary) {
132   TEST_REQUIRE_HW_COUNTER();
133   TemporaryFile tmp_file;
134   ASSERT_TRUE(StatCmd()->Run({"--group", "instructions:u,instructions:k", "-o",
135                               tmp_file.path, "sleep", "1"}));
136   std::string s;
137   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
138   size_t pos = s.find("instructions:u");
139   ASSERT_NE(s.npos, pos);
140   pos = s.find("instructions:k", pos);
141   ASSERT_NE(s.npos, pos);
142   pos += strlen("instructions:k");
143   // Check if the summary of instructions is generated.
144   ASSERT_NE(s.npos, s.find("instructions", pos));
145 }
146 
TEST(stat_cmd,duration_option)147 TEST(stat_cmd, duration_option) {
148   ASSERT_TRUE(
149       StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid()), "--in-app"}));
150   ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
151 }
152 
TEST(stat_cmd,interval_option)153 TEST(stat_cmd, interval_option) {
154   TemporaryFile tmp_file;
155   ASSERT_TRUE(
156     StatCmd()->Run({"--interval", "500.0", "--duration", "1.2", "-o",
157           tmp_file.path, "sleep", "2"}));
158   std::string s;
159   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
160   size_t count = 0;
161   size_t pos = 0;
162   std::string subs = "statistics:";
163   while((pos = s.find(subs, pos)) != s.npos) {
164     pos += subs.size();
165     ++count ;
166   }
167   ASSERT_EQ(count, 2UL);
168 }
169 
TEST(stat_cmd,interval_option_in_system_wide)170 TEST(stat_cmd, interval_option_in_system_wide) {
171   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "--interval", "100", "--duration", "0.3"})));
172 }
173 
TEST(stat_cmd,interval_only_values_option)174 TEST(stat_cmd, interval_only_values_option) {
175   ASSERT_TRUE(StatCmd()->Run({"--interval", "500", "--interval-only-values", "sleep", "2"}));
176   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "--interval", "100", "--interval-only-values",
177                                            "--duration", "0.3"})));
178 }
179 
TEST(stat_cmd,no_modifier_for_clock_events)180 TEST(stat_cmd, no_modifier_for_clock_events) {
181   for (const std::string& e : {"cpu-clock", "task-clock"}) {
182     for (const std::string& m : {"u", "k"}) {
183       ASSERT_FALSE(StatCmd()->Run({"-e", e + ":" + m, "sleep", "0.1"}))
184           << "event " << e << ":" << m;
185     }
186   }
187 }
188 
TEST(stat_cmd,handle_SIGHUP)189 TEST(stat_cmd, handle_SIGHUP) {
190   std::thread thread([]() {
191     sleep(1);
192     kill(getpid(), SIGHUP);
193   });
194   thread.detach();
195   ASSERT_TRUE(StatCmd()->Run({"sleep", "1000000"}));
196 }
197 
TEST(stat_cmd,stop_when_no_more_targets)198 TEST(stat_cmd, stop_when_no_more_targets) {
199   std::atomic<int> tid(0);
200   std::thread thread([&]() {
201     tid = gettid();
202     sleep(1);
203   });
204   thread.detach();
205   while (tid == 0);
206   ASSERT_TRUE(StatCmd()->Run({"-t", std::to_string(tid), "--in-app"}));
207 }
208 
TEST(stat_cmd,sample_speed_should_be_zero)209 TEST(stat_cmd, sample_speed_should_be_zero) {
210   TEST_REQUIRE_HW_COUNTER();
211   EventSelectionSet set(true);
212   ASSERT_TRUE(set.AddEventType("cpu-cycles"));
213   set.AddMonitoredProcesses({getpid()});
214   ASSERT_TRUE(set.OpenEventFiles({-1}));
215   std::vector<EventAttrWithId> attrs = set.GetEventAttrWithId();
216   ASSERT_GT(attrs.size(), 0u);
217   for (auto& attr : attrs) {
218     ASSERT_EQ(attr.attr->sample_period, 0u);
219     ASSERT_EQ(attr.attr->sample_freq, 0u);
220     ASSERT_EQ(attr.attr->freq, 0u);
221   }
222 }
223 
TEST(stat_cmd,calculating_cpu_frequency)224 TEST(stat_cmd, calculating_cpu_frequency) {
225   TEST_REQUIRE_HW_COUNTER();
226   CaptureStdout capture;
227   ASSERT_TRUE(capture.Start());
228   ASSERT_TRUE(StatCmd()->Run({"--csv", "--group", "task-clock,cpu-cycles", "sleep", "1"}));
229   std::string output = capture.Finish();
230   double task_clock_in_ms = 0;
231   uint64_t cpu_cycle_count = 0;
232   double cpu_frequency = 0;
233   for (auto& line : android::base::Split(output, "\n")) {
234     if (line.find("task-clock") != std::string::npos) {
235       ASSERT_EQ(sscanf(line.c_str(), "%lf(ms)", &task_clock_in_ms), 1);
236     } else if (line.find("cpu-cycles") != std::string::npos) {
237       ASSERT_EQ(sscanf(line.c_str(), "%" SCNu64 ",cpu-cycles,%lf", &cpu_cycle_count,
238                        &cpu_frequency), 2);
239     }
240   }
241   ASSERT_NE(task_clock_in_ms, 0.0f);
242   ASSERT_NE(cpu_cycle_count, 0u);
243   ASSERT_NE(cpu_frequency, 0.0f);
244   double calculated_frequency = cpu_cycle_count / task_clock_in_ms / 1e6;
245   // Accept error up to 1e-3. Because the stat cmd print values with precision 1e-6.
246   ASSERT_NEAR(cpu_frequency, calculated_frequency, 1e-3);
247 }
248 
TEST(stat_cmd,set_comm_in_another_thread)249 TEST(stat_cmd, set_comm_in_another_thread) {
250   // Test a kernel bug which was fixed in 3.15. If kernel panic happens, please cherry pick kernel
251   // patch: e041e328c4b41e perf: Fix perf_event_comm() vs. exec() assumption
252   TEST_REQUIRE_HW_COUNTER();
253 
254   for (size_t loop = 0; loop < 3; ++loop) {
255     std::atomic<int> child_tid(0);
256     std::atomic<bool> stop_child(false);
257     std::thread child([&]() {
258       child_tid = gettid();
259       // stay on a cpu to make the monitored events of the child thread on that cpu.
260       while (!stop_child) {}
261     });
262 
263     while (child_tid == 0) {}
264 
265     {
266       EventSelectionSet set(true);
267       ASSERT_TRUE(set.AddEventType("cpu-cycles"));
268       set.AddMonitoredThreads({child_tid});
269       ASSERT_TRUE(set.OpenEventFiles({-1}));
270 
271       EventSelectionSet set2(true);
272       ASSERT_TRUE(set2.AddEventType("instructions"));
273       set2.AddMonitoredThreads({gettid()});
274       ASSERT_TRUE(set2.OpenEventFiles({-1}));
275 
276       // For kernels with the bug, setting comm will make the monitored events of the child thread
277       // on the cpu of the current thread.
278       ASSERT_TRUE(android::base::WriteStringToFile("child",
279                                                    "/proc/" + std::to_string(child_tid) + "/comm"));
280       // Release monitored events. For kernels with the bug, the events still exist on the cpu of
281       // the child thread.
282     }
283 
284     stop_child = true;
285     child.join();
286     // Sleep 1s to enter and exit cpu idle, which may abort the kernel.
287     sleep(1);
288   }
289 }
290 
TestStatingApps(const std::string & app_name)291 static void TestStatingApps(const std::string& app_name) {
292   // Bring the app to foreground.
293   ASSERT_TRUE(Workload::RunCmd({"am", "start", app_name + "/.MainActivity"}));
294   ASSERT_TRUE(StatCmd()->Run({"--app", app_name, "--duration", "3"}));
295 }
296 
TEST(stat_cmd,app_option_for_debuggable_app)297 TEST(stat_cmd, app_option_for_debuggable_app) {
298   TEST_REQUIRE_APPS();
299   TestStatingApps("com.android.simpleperf.debuggable");
300 }
301 
TEST(stat_cmd,app_option_for_profileable_app)302 TEST(stat_cmd, app_option_for_profileable_app) {
303   TEST_REQUIRE_APPS();
304   TestStatingApps("com.android.simpleperf.profileable");
305 }
306 
TEST(stat_cmd,use_devfreq_counters_option)307 TEST(stat_cmd, use_devfreq_counters_option) {
308 #if defined(__ANDROID__)
309   TEST_IN_ROOT(StatCmd()->Run({"--use-devfreq-counters", "sleep", "0.1"}));
310 #else
311   GTEST_LOG_(INFO) << "This test tests an option only available on Android.";
312 #endif
313 }
314