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 "cmd_stat_impl.h"
26 #include "command.h"
27 #include "environment.h"
28 #include "event_selection_set.h"
29 #include "get_test_data.h"
30 #include "test_util.h"
31 
32 using namespace simpleperf;
33 
StatCmd()34 static std::unique_ptr<Command> StatCmd() {
35   return CreateCommandInstance("stat");
36 }
37 
TEST(stat_cmd,no_options)38 TEST(stat_cmd, no_options) { ASSERT_TRUE(StatCmd()->Run({"sleep", "1"})); }
39 
TEST(stat_cmd,event_option)40 TEST(stat_cmd, event_option) {
41   ASSERT_TRUE(StatCmd()->Run({"-e", "cpu-clock,task-clock", "sleep", "1"}));
42 }
43 
TEST(stat_cmd,system_wide_option)44 TEST(stat_cmd, system_wide_option) {
45   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "sleep", "1"})));
46 }
47 
TEST(stat_cmd,verbose_option)48 TEST(stat_cmd, verbose_option) {
49   ASSERT_TRUE(StatCmd()->Run({"--verbose", "sleep", "1"}));
50 }
51 
TEST(stat_cmd,tracepoint_event)52 TEST(stat_cmd, tracepoint_event) {
53   TEST_IN_ROOT(ASSERT_TRUE(
54       StatCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"})));
55 }
56 
TEST(stat_cmd,rN_event)57 TEST(stat_cmd, rN_event) {
58   TEST_REQUIRE_HW_COUNTER();
59   OMIT_TEST_ON_NON_NATIVE_ABIS();
60   size_t event_number;
61   if (GetBuildArch() == ARCH_ARM64 || GetBuildArch() == ARCH_ARM) {
62     // As in D5.10.2 of the ARMv8 manual, ARM defines the event number space for PMU. part of the
63     // space is for common event numbers (which will stay the same for all ARM chips), part of the
64     // space is for implementation defined events. Here 0x08 is a common event for instructions.
65     event_number = 0x08;
66   } else if (GetBuildArch() == ARCH_X86_32 || GetBuildArch() == ARCH_X86_64) {
67     // As in volume 3 chapter 19 of the Intel manual, 0x00c0 is the event number for instruction.
68     event_number = 0x00c0;
69   } else {
70     GTEST_LOG_(INFO) << "Omit arch " << GetBuildArch();
71     return;
72   }
73   std::string event_name = android::base::StringPrintf("r%zx", event_number);
74   ASSERT_TRUE(StatCmd()->Run({"-e", event_name, "sleep", "1"}));
75 }
76 
TEST(stat_cmd,pmu_event)77 TEST(stat_cmd, pmu_event) {
78   TEST_REQUIRE_PMU_COUNTER();
79   TEST_REQUIRE_HW_COUNTER();
80   std::string event_string;
81   if (GetBuildArch() == ARCH_X86_64) {
82     event_string = "cpu/instructions/";
83   } else if (GetBuildArch() == ARCH_ARM64) {
84     event_string = "armv8_pmuv3/inst_retired/";
85   } else {
86     GTEST_LOG_(INFO) << "Omit arch " << GetBuildArch();
87     return;
88   }
89   TEST_IN_ROOT(ASSERT_TRUE(
90       StatCmd()->Run({"-a", "-e", event_string, "sleep", "1"})));
91 }
92 
TEST(stat_cmd,event_modifier)93 TEST(stat_cmd, event_modifier) {
94   TEST_REQUIRE_HW_COUNTER();
95   ASSERT_TRUE(
96       StatCmd()->Run({"-e", "cpu-cycles:u,cpu-cycles:k", "sleep", "1"}));
97 }
98 
RunWorkloadFunction()99 void RunWorkloadFunction() {
100   while (true) {
101     for (volatile int i = 0; i < 10000; ++i);
102     usleep(1);
103   }
104 }
105 
CreateProcesses(size_t count,std::vector<std::unique_ptr<Workload>> * workloads)106 void CreateProcesses(size_t count,
107                      std::vector<std::unique_ptr<Workload>>* workloads) {
108   workloads->clear();
109   // Create workloads run longer than profiling time.
110   for (size_t i = 0; i < count; ++i) {
111     std::unique_ptr<Workload> workload;
112     workload = Workload::CreateWorkload(RunWorkloadFunction);
113     ASSERT_TRUE(workload != nullptr);
114     ASSERT_TRUE(workload->Start());
115     workloads->push_back(std::move(workload));
116   }
117 }
118 
TEST(stat_cmd,existing_processes)119 TEST(stat_cmd, existing_processes) {
120   std::vector<std::unique_ptr<Workload>> workloads;
121   CreateProcesses(2, &workloads);
122   std::string pid_list = android::base::StringPrintf(
123       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
124   ASSERT_TRUE(StatCmd()->Run({"-p", pid_list, "sleep", "1"}));
125 }
126 
TEST(stat_cmd,existing_threads)127 TEST(stat_cmd, existing_threads) {
128   std::vector<std::unique_ptr<Workload>> workloads;
129   CreateProcesses(2, &workloads);
130   // Process id can be used as thread id in linux.
131   std::string tid_list = android::base::StringPrintf(
132       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
133   ASSERT_TRUE(StatCmd()->Run({"-t", tid_list, "sleep", "1"}));
134 }
135 
TEST(stat_cmd,no_monitored_threads)136 TEST(stat_cmd, no_monitored_threads) {
137   ASSERT_FALSE(StatCmd()->Run({}));
138   ASSERT_FALSE(StatCmd()->Run({""}));
139 }
140 
TEST(stat_cmd,group_option)141 TEST(stat_cmd, group_option) {
142   TEST_REQUIRE_HW_COUNTER();
143   ASSERT_TRUE(
144       StatCmd()->Run({"--group", "cpu-clock,page-faults", "sleep", "1"}));
145   ASSERT_TRUE(StatCmd()->Run({"--group", "cpu-cycles,instructions", "--group",
146                               "cpu-cycles:u,instructions:u", "--group",
147                               "cpu-cycles:k,instructions:k", "sleep", "1"}));
148 }
149 
TEST(stat_cmd,auto_generated_summary)150 TEST(stat_cmd, auto_generated_summary) {
151   TEST_REQUIRE_HW_COUNTER();
152   TemporaryFile tmp_file;
153   ASSERT_TRUE(StatCmd()->Run({"--group", "instructions:u,instructions:k", "-o",
154                               tmp_file.path, "sleep", "1"}));
155   std::string s;
156   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
157   size_t pos = s.find("instructions:u");
158   ASSERT_NE(s.npos, pos);
159   pos = s.find("instructions:k", pos);
160   ASSERT_NE(s.npos, pos);
161   pos += strlen("instructions:k");
162   // Check if the summary of instructions is generated.
163   ASSERT_NE(s.npos, s.find("instructions", pos));
164 }
165 
TEST(stat_cmd,duration_option)166 TEST(stat_cmd, duration_option) {
167   ASSERT_TRUE(
168       StatCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid()), "--in-app"}));
169   ASSERT_TRUE(StatCmd()->Run({"--duration", "1", "sleep", "2"}));
170 }
171 
TEST(stat_cmd,interval_option)172 TEST(stat_cmd, interval_option) {
173   TemporaryFile tmp_file;
174   ASSERT_TRUE(
175     StatCmd()->Run({"--interval", "500.0", "--duration", "1.2", "-o",
176           tmp_file.path, "sleep", "2"}));
177   std::string s;
178   ASSERT_TRUE(android::base::ReadFileToString(tmp_file.path, &s));
179   size_t count = 0;
180   size_t pos = 0;
181   std::string subs = "statistics:";
182   while((pos = s.find(subs, pos)) != s.npos) {
183     pos += subs.size();
184     ++count ;
185   }
186   ASSERT_EQ(count, 2UL);
187 }
188 
TEST(stat_cmd,interval_option_in_system_wide)189 TEST(stat_cmd, interval_option_in_system_wide) {
190   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "--interval", "100", "--duration", "0.3"})));
191 }
192 
TEST(stat_cmd,interval_only_values_option)193 TEST(stat_cmd, interval_only_values_option) {
194   ASSERT_TRUE(StatCmd()->Run({"--interval", "500", "--interval-only-values", "sleep", "2"}));
195   TEST_IN_ROOT(ASSERT_TRUE(StatCmd()->Run({"-a", "--interval", "100", "--interval-only-values",
196                                            "--duration", "0.3"})));
197 }
198 
TEST(stat_cmd,no_modifier_for_clock_events)199 TEST(stat_cmd, no_modifier_for_clock_events) {
200   for (const std::string& e : {"cpu-clock", "task-clock"}) {
201     for (const std::string& m : {"u", "k"}) {
202       ASSERT_FALSE(StatCmd()->Run({"-e", e + ":" + m, "sleep", "0.1"}))
203           << "event " << e << ":" << m;
204     }
205   }
206 }
207 
TEST(stat_cmd,handle_SIGHUP)208 TEST(stat_cmd, handle_SIGHUP) {
209   std::thread thread([]() {
210     sleep(1);
211     kill(getpid(), SIGHUP);
212   });
213   thread.detach();
214   ASSERT_TRUE(StatCmd()->Run({"sleep", "1000000"}));
215 }
216 
TEST(stat_cmd,stop_when_no_more_targets)217 TEST(stat_cmd, stop_when_no_more_targets) {
218   std::atomic<int> tid(0);
219   std::thread thread([&]() {
220     tid = gettid();
221     sleep(1);
222   });
223   thread.detach();
224   while (tid == 0);
225   ASSERT_TRUE(StatCmd()->Run({"-t", std::to_string(tid), "--in-app"}));
226 }
227 
TEST(stat_cmd,sample_speed_should_be_zero)228 TEST(stat_cmd, sample_speed_should_be_zero) {
229   TEST_REQUIRE_HW_COUNTER();
230   EventSelectionSet set(true);
231   ASSERT_TRUE(set.AddEventType("cpu-cycles"));
232   set.AddMonitoredProcesses({getpid()});
233   ASSERT_TRUE(set.OpenEventFiles({-1}));
234   std::vector<EventAttrWithId> attrs = set.GetEventAttrWithId();
235   ASSERT_GT(attrs.size(), 0u);
236   for (auto& attr : attrs) {
237     ASSERT_EQ(attr.attr->sample_period, 0u);
238     ASSERT_EQ(attr.attr->sample_freq, 0u);
239     ASSERT_EQ(attr.attr->freq, 0u);
240   }
241 }
242 
TEST(stat_cmd,calculating_cpu_frequency)243 TEST(stat_cmd, calculating_cpu_frequency) {
244   TEST_REQUIRE_HW_COUNTER();
245   CaptureStdout capture;
246   ASSERT_TRUE(capture.Start());
247   ASSERT_TRUE(StatCmd()->Run({"--csv", "--group", "task-clock,cpu-cycles", "sleep", "1"}));
248   std::string output = capture.Finish();
249   double task_clock_in_ms = 0;
250   uint64_t cpu_cycle_count = 0;
251   double cpu_frequency = 0;
252   for (auto& line : android::base::Split(output, "\n")) {
253     if (line.find("task-clock") != std::string::npos) {
254       ASSERT_EQ(sscanf(line.c_str(), "%lf(ms)", &task_clock_in_ms), 1);
255     } else if (line.find("cpu-cycles") != std::string::npos) {
256       ASSERT_EQ(sscanf(line.c_str(), "%" SCNu64 ",cpu-cycles,%lf", &cpu_cycle_count,
257                        &cpu_frequency), 2);
258     }
259   }
260   ASSERT_NE(task_clock_in_ms, 0.0f);
261   ASSERT_NE(cpu_cycle_count, 0u);
262   ASSERT_NE(cpu_frequency, 0.0f);
263   double calculated_frequency = cpu_cycle_count / task_clock_in_ms / 1e6;
264   // Accept error up to 1e-3. Because the stat cmd print values with precision 1e-6.
265   ASSERT_NEAR(cpu_frequency, calculated_frequency, 1e-3);
266 }
267 
TEST(stat_cmd,set_comm_in_another_thread)268 TEST(stat_cmd, set_comm_in_another_thread) {
269   // Test a kernel bug which was fixed in 3.15. If kernel panic happens, please cherry pick kernel
270   // patch: e041e328c4b41e perf: Fix perf_event_comm() vs. exec() assumption
271   TEST_REQUIRE_HW_COUNTER();
272 
273   for (size_t loop = 0; loop < 3; ++loop) {
274     std::atomic<int> child_tid(0);
275     std::atomic<bool> stop_child(false);
276     std::thread child([&]() {
277       child_tid = gettid();
278       // stay on a cpu to make the monitored events of the child thread on that cpu.
279       while (!stop_child) {}
280     });
281 
282     while (child_tid == 0) {}
283 
284     {
285       EventSelectionSet set(true);
286       ASSERT_TRUE(set.AddEventType("cpu-cycles"));
287       set.AddMonitoredThreads({child_tid});
288       ASSERT_TRUE(set.OpenEventFiles({-1}));
289 
290       EventSelectionSet set2(true);
291       ASSERT_TRUE(set2.AddEventType("instructions"));
292       set2.AddMonitoredThreads({gettid()});
293       ASSERT_TRUE(set2.OpenEventFiles({-1}));
294 
295       // For kernels with the bug, setting comm will make the monitored events of the child thread
296       // on the cpu of the current thread.
297       ASSERT_TRUE(android::base::WriteStringToFile("child",
298                                                    "/proc/" + std::to_string(child_tid) + "/comm"));
299       // Release monitored events. For kernels with the bug, the events still exist on the cpu of
300       // the child thread.
301     }
302 
303     stop_child = true;
304     child.join();
305     // Sleep 1s to enter and exit cpu idle, which may abort the kernel.
306     sleep(1);
307   }
308 }
309 
TestStatingApps(const std::string & app_name)310 static void TestStatingApps(const std::string& app_name) {
311   // Bring the app to foreground.
312   ASSERT_TRUE(Workload::RunCmd({"am", "start", app_name + "/.MainActivity"}));
313   ASSERT_TRUE(StatCmd()->Run({"--app", app_name, "--duration", "3"}));
314 }
315 
TEST(stat_cmd,app_option_for_debuggable_app)316 TEST(stat_cmd, app_option_for_debuggable_app) {
317   TEST_REQUIRE_APPS();
318   SetRunInAppToolForTesting(true, false);
319   TestStatingApps("com.android.simpleperf.debuggable");
320   SetRunInAppToolForTesting(false, true);
321   TestStatingApps("com.android.simpleperf.debuggable");
322 }
323 
TEST(stat_cmd,app_option_for_profileable_app)324 TEST(stat_cmd, app_option_for_profileable_app) {
325   TEST_REQUIRE_APPS();
326   SetRunInAppToolForTesting(false, true);
327   TestStatingApps("com.android.simpleperf.profileable");
328 }
329 
TEST(stat_cmd,use_devfreq_counters_option)330 TEST(stat_cmd, use_devfreq_counters_option) {
331 #if defined(__ANDROID__)
332   TEST_IN_ROOT(StatCmd()->Run({"--use-devfreq-counters", "sleep", "0.1"}));
333 #else
334   GTEST_LOG_(INFO) << "This test tests an option only available on Android.";
335 #endif
336 }
337 
TEST(stat_cmd,per_thread_option)338 TEST(stat_cmd, per_thread_option) {
339   ASSERT_TRUE(StatCmd()->Run({"--per-thread", "sleep", "0.1"}));
340   TEST_IN_ROOT(StatCmd()->Run({"--per-thread", "-a", "--duration", "0.1"}));
341 }
342 
TEST(stat_cmd,per_core_option)343 TEST(stat_cmd, per_core_option) {
344   ASSERT_TRUE(StatCmd()->Run({"--per-core", "sleep", "0.1"}));
345   TEST_IN_ROOT(StatCmd()->Run({"--per-core", "-a", "--duration", "0.1"}));
346 }
347 
TEST(stat_cmd,sort_option)348 TEST(stat_cmd, sort_option) {
349   ASSERT_TRUE(StatCmd()->Run({"--per-thread", "--per-core", "--sort", "cpu,count", "sleep", "0.1"}));
350 }
351 
TEST(stat_cmd,counter_sum)352 TEST(stat_cmd, counter_sum) {
353   PerfCounter counter;
354   counter.value = 1;
355   counter.time_enabled = 2;
356   counter.time_running = 3;
357   CounterSum a;
358   a.FromCounter(counter);
359   ASSERT_EQ(a.value, 1);
360   ASSERT_EQ(a.time_enabled, 2);
361   ASSERT_EQ(a.time_running, 3);
362   CounterSum b = a + a;
363   ASSERT_EQ(b.value, 2);
364   ASSERT_EQ(b.time_enabled, 4);
365   ASSERT_EQ(b.time_running, 6);
366   CounterSum c = a - a;
367   ASSERT_EQ(c.value, 0);
368   ASSERT_EQ(c.time_enabled, 0);
369   ASSERT_EQ(c.time_running, 0);
370   b.ToCounter(counter);
371   ASSERT_EQ(counter.value, 2);
372   ASSERT_EQ(counter.time_enabled, 4);
373   ASSERT_EQ(counter.time_running, 6);
374 }
375 
376 class StatCmdSummaryBuilderTest : public ::testing::Test {
377  protected:
378   struct CounterArg {
379     int event_id = 0;
380     int tid = 0;
381     int cpu = 0;
382     int value = 1;
383     int time_enabled = 1;
384     int time_running = 1;
385   };
386 
SetUp()387   void SetUp() override {
388     sort_keys_ = {"count_per_thread", "tid", "cpu", "count"};
389   }
390 
AddCounter(const CounterArg & arg)391   void AddCounter(const CounterArg& arg) {
392     if (thread_map_.count(arg.tid) == 0) {
393       ThreadInfo& thread = thread_map_[arg.tid];
394       thread.pid = thread.tid = arg.tid;
395       thread.name = "thread" + std::to_string(arg.tid);
396     }
397     if (arg.event_id >= counters_.size()) {
398       counters_.resize(arg.event_id + 1);
399       counters_[arg.event_id].group_id = 0;
400       counters_[arg.event_id].event_name = "event" + std::to_string(arg.event_id);
401     }
402     CountersInfo& info = counters_[arg.event_id];
403     info.counters.resize(info.counters.size() + 1);
404     CounterInfo& counter = info.counters.back();
405     counter.tid = arg.tid;
406     counter.cpu = arg.cpu;
407     counter.counter.id = 0;
408     counter.counter.value = arg.value;
409     counter.counter.time_enabled = arg.time_enabled;
410     counter.counter.time_running = arg.time_running;
411   }
412 
BuildSummary(bool report_per_thread,bool report_per_core)413   std::vector<CounterSummary> BuildSummary(bool report_per_thread, bool report_per_core) {
414     std::optional<SummaryComparator> comparator =
415         BuildSummaryComparator(sort_keys_, report_per_thread, report_per_core);
416     CounterSummaryBuilder builder(report_per_thread, report_per_core, false, thread_map_,
417                                   comparator);
418     for (auto& info : counters_) {
419       builder.AddCountersForOneEventType(info);
420     }
421     return builder.Build();
422   }
423 
424   std::unordered_map<pid_t, ThreadInfo> thread_map_;
425   std::vector<CountersInfo> counters_;
426   std::vector<std::string> sort_keys_;
427 };
428 
TEST_F(StatCmdSummaryBuilderTest,multiple_events)429 TEST_F(StatCmdSummaryBuilderTest, multiple_events) {
430   AddCounter({.event_id = 0, .value = 1, .time_enabled = 1, .time_running = 1});
431   AddCounter({.event_id = 1, .value = 2, .time_enabled = 2, .time_running = 2});
432   std::vector<CounterSummary> summaries = BuildSummary(false, false);
433   ASSERT_EQ(summaries.size(), 2);
434   ASSERT_EQ(summaries[0].type_name, "event0");
435   ASSERT_EQ(summaries[0].count, 1);
436   ASSERT_NEAR(summaries[0].scale, 1.0, 1e-5);
437   ASSERT_EQ(summaries[1].type_name, "event1");
438   ASSERT_EQ(summaries[1].count, 2);
439   ASSERT_NEAR(summaries[1].scale, 1.0, 1e-5);
440 }
441 
TEST_F(StatCmdSummaryBuilderTest,default_aggregate)442 TEST_F(StatCmdSummaryBuilderTest, default_aggregate) {
443   AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
444   AddCounter({.tid = 0, .cpu = 1, .value = 1, .time_enabled = 1, .time_running = 1});
445   AddCounter({.tid = 1, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
446   AddCounter({.tid = 1, .cpu = 1, .value = 2, .time_enabled = 2, .time_running = 1});
447   std::vector<CounterSummary> summaries = BuildSummary(false, false);
448   ASSERT_EQ(summaries.size(), 1);
449   ASSERT_EQ(summaries[0].count, 5);
450   ASSERT_NEAR(summaries[0].scale, 1.25, 1e-5);
451 }
452 
TEST_F(StatCmdSummaryBuilderTest,per_thread_aggregate)453 TEST_F(StatCmdSummaryBuilderTest, per_thread_aggregate) {
454   AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
455   AddCounter({.tid = 0, .cpu = 1, .value = 1, .time_enabled = 1, .time_running = 1});
456   AddCounter({.tid = 1, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
457   AddCounter({.tid = 1, .cpu = 1, .value = 2, .time_enabled = 2, .time_running = 1});
458   std::vector<CounterSummary> summaries = BuildSummary(true, false);
459   ASSERT_EQ(summaries.size(), 2);
460   ASSERT_EQ(summaries[0].thread->tid, 1);
461   ASSERT_EQ(summaries[0].cpu, -1);
462   ASSERT_EQ(summaries[0].count, 3);
463   ASSERT_NEAR(summaries[0].scale, 1.5, 1e-5);
464   ASSERT_EQ(summaries[1].thread->tid, 0);
465   ASSERT_EQ(summaries[0].cpu, -1);
466   ASSERT_EQ(summaries[1].count, 2);
467   ASSERT_NEAR(summaries[1].scale, 1.0, 1e-5);
468 }
469 
TEST_F(StatCmdSummaryBuilderTest,per_core_aggregate)470 TEST_F(StatCmdSummaryBuilderTest, per_core_aggregate) {
471   AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
472   AddCounter({.tid = 0, .cpu = 1, .value = 1, .time_enabled = 1, .time_running = 1});
473   AddCounter({.tid = 1, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
474   AddCounter({.tid = 1, .cpu = 1, .value = 2, .time_enabled = 2, .time_running = 1});
475   std::vector<CounterSummary> summaries = BuildSummary(false, true);
476   ASSERT_TRUE(summaries[0].thread == nullptr);
477   ASSERT_EQ(summaries[0].cpu, 0);
478   ASSERT_EQ(summaries[0].count, 2);
479   ASSERT_NEAR(summaries[0].scale, 1.0, 1e-5);
480   ASSERT_EQ(summaries.size(), 2);
481   ASSERT_TRUE(summaries[1].thread == nullptr);
482   ASSERT_EQ(summaries[1].cpu, 1);
483   ASSERT_EQ(summaries[1].count, 3);
484   ASSERT_NEAR(summaries[1].scale, 1.5, 1e-5);
485 }
486 
TEST_F(StatCmdSummaryBuilderTest,per_thread_core_aggregate)487 TEST_F(StatCmdSummaryBuilderTest, per_thread_core_aggregate) {
488   AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
489   AddCounter({.tid = 0, .cpu = 1, .value = 2, .time_enabled = 1, .time_running = 1});
490   AddCounter({.tid = 1, .cpu = 0, .value = 3, .time_enabled = 1, .time_running = 1});
491   AddCounter({.tid = 1, .cpu = 1, .value = 4, .time_enabled = 2, .time_running = 1});
492   std::vector<CounterSummary> summaries = BuildSummary(true, true);
493   ASSERT_EQ(summaries.size(), 4);
494   ASSERT_EQ(summaries[0].thread->tid, 1);
495   ASSERT_EQ(summaries[0].cpu, 0);
496   ASSERT_EQ(summaries[0].count, 3);
497   ASSERT_NEAR(summaries[0].scale, 1.0, 1e-5);
498   ASSERT_EQ(summaries[1].thread->tid, 1);
499   ASSERT_EQ(summaries[1].cpu, 1);
500   ASSERT_EQ(summaries[1].count, 4);
501   ASSERT_NEAR(summaries[1].scale, 2.0, 1e-5);
502   ASSERT_EQ(summaries[2].thread->tid, 0);
503   ASSERT_EQ(summaries[2].cpu, 0);
504   ASSERT_EQ(summaries[2].count, 1);
505   ASSERT_NEAR(summaries[2].scale, 1.0, 1e-5);
506   ASSERT_EQ(summaries[3].thread->tid, 0);
507   ASSERT_EQ(summaries[3].cpu, 1);
508   ASSERT_EQ(summaries[3].count, 2);
509   ASSERT_NEAR(summaries[3].scale, 1.0, 1e-5);
510 }
511 
TEST_F(StatCmdSummaryBuilderTest,sort_key_count)512 TEST_F(StatCmdSummaryBuilderTest, sort_key_count) {
513   sort_keys_ = {"count"};
514   AddCounter({.tid = 0, .cpu = 0, .value = 1});
515   AddCounter({.tid = 1, .cpu = 1, .value = 2});
516   std::vector<CounterSummary> summaries = BuildSummary(true, true);
517   ASSERT_EQ(summaries[0].count, 2);
518   ASSERT_EQ(summaries[1].count, 1);
519 }
520 
TEST_F(StatCmdSummaryBuilderTest,sort_key_count_per_thread)521 TEST_F(StatCmdSummaryBuilderTest, sort_key_count_per_thread) {
522   sort_keys_ = {"count_per_thread", "count"};
523   AddCounter({.tid = 0, .cpu = 0, .value = 1});
524   AddCounter({.tid = 0, .cpu = 1, .value = 5});
525   AddCounter({.tid = 1, .cpu = 0, .value = 3});
526   std::vector<CounterSummary> summaries = BuildSummary(true, true);
527   ASSERT_EQ(summaries[0].count, 5);
528   ASSERT_EQ(summaries[1].count, 1);
529   ASSERT_EQ(summaries[2].count, 3);
530 }
531 
TEST_F(StatCmdSummaryBuilderTest,sort_key_cpu)532 TEST_F(StatCmdSummaryBuilderTest, sort_key_cpu) {
533   sort_keys_ = {"cpu"};
534   AddCounter({.tid = 0, .cpu = 1, .value = 2});
535   AddCounter({.tid = 1, .cpu = 0, .value = 1});
536   std::vector<CounterSummary> summaries = BuildSummary(false, true);
537   ASSERT_EQ(summaries[0].cpu, 0);
538   ASSERT_EQ(summaries[1].cpu, 1);
539 }
540 
TEST_F(StatCmdSummaryBuilderTest,sort_key_pid_tid_name)541 TEST_F(StatCmdSummaryBuilderTest, sort_key_pid_tid_name) {
542   AddCounter({.tid = 0, .cpu = 0, .value = 1});
543   AddCounter({.tid = 1, .cpu = 0, .value = 2});
544 
545   for (auto& key : std::vector<std::string>({"tid", "pid", "comm"})) {
546     sort_keys_ = {key};
547     std::vector<CounterSummary> summaries = BuildSummary(true, false);
548     ASSERT_EQ(summaries[0].count, 1) << "key = " << key;
549     ASSERT_EQ(summaries[1].count, 2) << "key = " << key;
550   }
551 }
552 
553 class StatCmdSummariesTest : public ::testing::Test {
554  protected:
AddSummary(const std::string event_name,pid_t tid,int cpu,uint64_t count,uint64_t runtime_in_ns)555   void AddSummary(const std::string event_name, pid_t tid, int cpu, uint64_t count,
556                   uint64_t runtime_in_ns) {
557     ThreadInfo* thread = nullptr;
558     if (tid != -1) {
559       thread = &thread_map_[tid];
560     }
561     summary_v_.emplace_back(event_name, "", 0, thread, cpu, count, runtime_in_ns, 1.0, false,
562                             false);
563   }
564 
GetComment(size_t index)565   const std::string* GetComment(size_t index) {
566     if (!summaries_) {
567       summaries_.reset(new CounterSummaries(std::move(summary_v_), false));
568       summaries_->GenerateComments(1.0);
569     }
570     if (index < summaries_->Summaries().size()) {
571       return &(summaries_->Summaries()[index].comment);
572     }
573     return nullptr;
574   }
575 
576   std::unordered_map<pid_t, ThreadInfo> thread_map_;
577   std::vector<CounterSummary> summary_v_;
578   std::unique_ptr<CounterSummaries> summaries_;
579 };
580 
TEST_F(StatCmdSummariesTest,task_clock_comment)581 TEST_F(StatCmdSummariesTest, task_clock_comment) {
582   AddSummary("task-clock", -1, -1, 1e9, 0);
583   AddSummary("task-clock", 0, -1, 2e9, 0);
584   AddSummary("task-clock", -1, 0, 0.5e9, 0);
585   AddSummary("task-clock", 1, 1, 3e9, 0);
586   ASSERT_EQ(*GetComment(0), "1.000000 cpus used");
587   ASSERT_EQ(*GetComment(1), "2.000000 cpus used");
588   ASSERT_EQ(*GetComment(2), "0.500000 cpus used");
589   ASSERT_EQ(*GetComment(3), "3.000000 cpus used");
590 }
591 
TEST_F(StatCmdSummariesTest,cpu_cycles_comment)592 TEST_F(StatCmdSummariesTest, cpu_cycles_comment) {
593   AddSummary("cpu-cycles", -1, -1, 100, 100);
594   AddSummary("cpu-cycles", 0, -1, 200, 100);
595   AddSummary("cpu-cycles", -1, 0, 50, 100);
596   AddSummary("cpu-cycles", 1, 1, 300, 100);
597   ASSERT_EQ(*GetComment(0), "1.000000 GHz");
598   ASSERT_EQ(*GetComment(1), "2.000000 GHz");
599   ASSERT_EQ(*GetComment(2), "0.500000 GHz");
600   ASSERT_EQ(*GetComment(3), "3.000000 GHz");
601 }
602 
TEST_F(StatCmdSummariesTest,rate_comment)603 TEST_F(StatCmdSummariesTest, rate_comment) {
604   AddSummary("branch-misses", -1, -1, 1e9, 1e9);
605   AddSummary("branch-misses", 0, -1, 1e6, 1e9);
606   AddSummary("branch-misses", -1, 0, 1e3, 1e9);
607   AddSummary("branch-misses", 1, 1, 1, 1e9);
608   ASSERT_EQ(*GetComment(0), "1.000 G/sec");
609   ASSERT_EQ(*GetComment(1), "1.000 M/sec");
610   ASSERT_EQ(*GetComment(2), "1.000 K/sec");
611   ASSERT_EQ(*GetComment(3), "1.000 /sec");
612 }