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