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