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 (GetTargetArch() == ARCH_ARM64 || GetTargetArch() == 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 (GetTargetArch() == ARCH_X86_32 || GetTargetArch() == 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 " << GetTargetArch();
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 (GetTargetArch() == ARCH_X86_64) {
83 event_string = "cpu/instructions/";
84 } else if (GetTargetArch() == ARCH_ARM64) {
85 event_string = "armv8_pmuv3/inst_retired/";
86 } else {
87 GTEST_LOG_(INFO) << "Omit arch " << GetTargetArch();
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
TEST(stat_cmd,print_hw_counter_option)376 TEST(stat_cmd, print_hw_counter_option) {
377 ASSERT_TRUE(StatCmd()->Run({"--print-hw-counter"}));
378 }
379
380 class StatCmdSummaryBuilderTest : public ::testing::Test {
381 protected:
382 struct CounterArg {
383 int event_id = 0;
384 int tid = 0;
385 int cpu = 0;
386 int value = 1;
387 int time_enabled = 1;
388 int time_running = 1;
389 };
390
SetUp()391 void SetUp() override { sort_keys_ = {"count_per_thread", "tid", "cpu", "count"}; }
392
AddCounter(const CounterArg & arg)393 void AddCounter(const CounterArg& arg) {
394 if (thread_map_.count(arg.tid) == 0) {
395 ThreadInfo& thread = thread_map_[arg.tid];
396 thread.pid = thread.tid = arg.tid;
397 thread.name = "thread" + std::to_string(arg.tid);
398 }
399 if (arg.event_id >= counters_.size()) {
400 counters_.resize(arg.event_id + 1);
401 counters_[arg.event_id].group_id = 0;
402 counters_[arg.event_id].event_name = "event" + std::to_string(arg.event_id);
403 }
404 CountersInfo& info = counters_[arg.event_id];
405 info.counters.resize(info.counters.size() + 1);
406 CounterInfo& counter = info.counters.back();
407 counter.tid = arg.tid;
408 counter.cpu = arg.cpu;
409 counter.counter.id = 0;
410 counter.counter.value = arg.value;
411 counter.counter.time_enabled = arg.time_enabled;
412 counter.counter.time_running = arg.time_running;
413 }
414
BuildSummary(bool report_per_thread,bool report_per_core)415 std::vector<CounterSummary> BuildSummary(bool report_per_thread, bool report_per_core) {
416 std::optional<SummaryComparator> comparator =
417 BuildSummaryComparator(sort_keys_, report_per_thread, report_per_core);
418 CounterSummaryBuilder builder(report_per_thread, report_per_core, false, thread_map_,
419 comparator);
420 for (auto& info : counters_) {
421 builder.AddCountersForOneEventType(info);
422 }
423 return builder.Build();
424 }
425
426 std::unordered_map<pid_t, ThreadInfo> thread_map_;
427 std::vector<CountersInfo> counters_;
428 std::vector<std::string> sort_keys_;
429 };
430
TEST_F(StatCmdSummaryBuilderTest,multiple_events)431 TEST_F(StatCmdSummaryBuilderTest, multiple_events) {
432 AddCounter({.event_id = 0, .value = 1, .time_enabled = 1, .time_running = 1});
433 AddCounter({.event_id = 1, .value = 2, .time_enabled = 2, .time_running = 2});
434 std::vector<CounterSummary> summaries = BuildSummary(false, false);
435 ASSERT_EQ(summaries.size(), 2);
436 ASSERT_EQ(summaries[0].type_name, "event0");
437 ASSERT_EQ(summaries[0].count, 1);
438 ASSERT_NEAR(summaries[0].scale, 1.0, 1e-5);
439 ASSERT_EQ(summaries[1].type_name, "event1");
440 ASSERT_EQ(summaries[1].count, 2);
441 ASSERT_NEAR(summaries[1].scale, 1.0, 1e-5);
442 }
443
TEST_F(StatCmdSummaryBuilderTest,default_aggregate)444 TEST_F(StatCmdSummaryBuilderTest, default_aggregate) {
445 AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
446 AddCounter({.tid = 0, .cpu = 1, .value = 1, .time_enabled = 1, .time_running = 1});
447 AddCounter({.tid = 1, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
448 AddCounter({.tid = 1, .cpu = 1, .value = 2, .time_enabled = 2, .time_running = 1});
449 std::vector<CounterSummary> summaries = BuildSummary(false, false);
450 ASSERT_EQ(summaries.size(), 1);
451 ASSERT_EQ(summaries[0].count, 5);
452 ASSERT_NEAR(summaries[0].scale, 1.25, 1e-5);
453 }
454
TEST_F(StatCmdSummaryBuilderTest,per_thread_aggregate)455 TEST_F(StatCmdSummaryBuilderTest, per_thread_aggregate) {
456 AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
457 AddCounter({.tid = 0, .cpu = 1, .value = 1, .time_enabled = 1, .time_running = 1});
458 AddCounter({.tid = 1, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
459 AddCounter({.tid = 1, .cpu = 1, .value = 2, .time_enabled = 2, .time_running = 1});
460 std::vector<CounterSummary> summaries = BuildSummary(true, false);
461 ASSERT_EQ(summaries.size(), 2);
462 ASSERT_EQ(summaries[0].thread->tid, 1);
463 ASSERT_EQ(summaries[0].cpu, -1);
464 ASSERT_EQ(summaries[0].count, 3);
465 ASSERT_NEAR(summaries[0].scale, 1.5, 1e-5);
466 ASSERT_EQ(summaries[1].thread->tid, 0);
467 ASSERT_EQ(summaries[0].cpu, -1);
468 ASSERT_EQ(summaries[1].count, 2);
469 ASSERT_NEAR(summaries[1].scale, 1.0, 1e-5);
470 }
471
TEST_F(StatCmdSummaryBuilderTest,per_core_aggregate)472 TEST_F(StatCmdSummaryBuilderTest, per_core_aggregate) {
473 AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
474 AddCounter({.tid = 0, .cpu = 1, .value = 1, .time_enabled = 1, .time_running = 1});
475 AddCounter({.tid = 1, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
476 AddCounter({.tid = 1, .cpu = 1, .value = 2, .time_enabled = 2, .time_running = 1});
477 std::vector<CounterSummary> summaries = BuildSummary(false, true);
478 ASSERT_TRUE(summaries[0].thread == nullptr);
479 ASSERT_EQ(summaries[0].cpu, 0);
480 ASSERT_EQ(summaries[0].count, 2);
481 ASSERT_NEAR(summaries[0].scale, 1.0, 1e-5);
482 ASSERT_EQ(summaries.size(), 2);
483 ASSERT_TRUE(summaries[1].thread == nullptr);
484 ASSERT_EQ(summaries[1].cpu, 1);
485 ASSERT_EQ(summaries[1].count, 3);
486 ASSERT_NEAR(summaries[1].scale, 1.5, 1e-5);
487 }
488
TEST_F(StatCmdSummaryBuilderTest,per_thread_core_aggregate)489 TEST_F(StatCmdSummaryBuilderTest, per_thread_core_aggregate) {
490 AddCounter({.tid = 0, .cpu = 0, .value = 1, .time_enabled = 1, .time_running = 1});
491 AddCounter({.tid = 0, .cpu = 1, .value = 2, .time_enabled = 1, .time_running = 1});
492 AddCounter({.tid = 1, .cpu = 0, .value = 3, .time_enabled = 1, .time_running = 1});
493 AddCounter({.tid = 1, .cpu = 1, .value = 4, .time_enabled = 2, .time_running = 1});
494 std::vector<CounterSummary> summaries = BuildSummary(true, true);
495 ASSERT_EQ(summaries.size(), 4);
496 ASSERT_EQ(summaries[0].thread->tid, 1);
497 ASSERT_EQ(summaries[0].cpu, 0);
498 ASSERT_EQ(summaries[0].count, 3);
499 ASSERT_NEAR(summaries[0].scale, 1.0, 1e-5);
500 ASSERT_EQ(summaries[1].thread->tid, 1);
501 ASSERT_EQ(summaries[1].cpu, 1);
502 ASSERT_EQ(summaries[1].count, 4);
503 ASSERT_NEAR(summaries[1].scale, 2.0, 1e-5);
504 ASSERT_EQ(summaries[2].thread->tid, 0);
505 ASSERT_EQ(summaries[2].cpu, 0);
506 ASSERT_EQ(summaries[2].count, 1);
507 ASSERT_NEAR(summaries[2].scale, 1.0, 1e-5);
508 ASSERT_EQ(summaries[3].thread->tid, 0);
509 ASSERT_EQ(summaries[3].cpu, 1);
510 ASSERT_EQ(summaries[3].count, 2);
511 ASSERT_NEAR(summaries[3].scale, 1.0, 1e-5);
512 }
513
TEST_F(StatCmdSummaryBuilderTest,sort_key_count)514 TEST_F(StatCmdSummaryBuilderTest, sort_key_count) {
515 sort_keys_ = {"count"};
516 AddCounter({.tid = 0, .cpu = 0, .value = 1});
517 AddCounter({.tid = 1, .cpu = 1, .value = 2});
518 std::vector<CounterSummary> summaries = BuildSummary(true, true);
519 ASSERT_EQ(summaries[0].count, 2);
520 ASSERT_EQ(summaries[1].count, 1);
521 }
522
TEST_F(StatCmdSummaryBuilderTest,sort_key_count_per_thread)523 TEST_F(StatCmdSummaryBuilderTest, sort_key_count_per_thread) {
524 sort_keys_ = {"count_per_thread", "count"};
525 AddCounter({.tid = 0, .cpu = 0, .value = 1});
526 AddCounter({.tid = 0, .cpu = 1, .value = 5});
527 AddCounter({.tid = 1, .cpu = 0, .value = 3});
528 std::vector<CounterSummary> summaries = BuildSummary(true, true);
529 ASSERT_EQ(summaries[0].count, 5);
530 ASSERT_EQ(summaries[1].count, 1);
531 ASSERT_EQ(summaries[2].count, 3);
532 }
533
TEST_F(StatCmdSummaryBuilderTest,sort_key_cpu)534 TEST_F(StatCmdSummaryBuilderTest, sort_key_cpu) {
535 sort_keys_ = {"cpu"};
536 AddCounter({.tid = 0, .cpu = 1, .value = 2});
537 AddCounter({.tid = 1, .cpu = 0, .value = 1});
538 std::vector<CounterSummary> summaries = BuildSummary(false, true);
539 ASSERT_EQ(summaries[0].cpu, 0);
540 ASSERT_EQ(summaries[1].cpu, 1);
541 }
542
TEST_F(StatCmdSummaryBuilderTest,sort_key_pid_tid_name)543 TEST_F(StatCmdSummaryBuilderTest, sort_key_pid_tid_name) {
544 AddCounter({.tid = 0, .cpu = 0, .value = 1});
545 AddCounter({.tid = 1, .cpu = 0, .value = 2});
546
547 for (auto& key : std::vector<std::string>({"tid", "pid", "comm"})) {
548 sort_keys_ = {key};
549 std::vector<CounterSummary> summaries = BuildSummary(true, false);
550 ASSERT_EQ(summaries[0].count, 1) << "key = " << key;
551 ASSERT_EQ(summaries[1].count, 2) << "key = " << key;
552 }
553 }
554
555 class StatCmdSummariesTest : public ::testing::Test {
556 protected:
AddSummary(const std::string event_name,pid_t tid,int cpu,uint64_t count,uint64_t runtime_in_ns)557 void AddSummary(const std::string event_name, pid_t tid, int cpu, uint64_t count,
558 uint64_t runtime_in_ns) {
559 ThreadInfo* thread = nullptr;
560 if (tid != -1) {
561 thread = &thread_map_[tid];
562 }
563 summary_v_.emplace_back(event_name, "", 0, thread, cpu, count, runtime_in_ns, 1.0, false,
564 false);
565 }
566
GetComment(size_t index)567 const std::string* GetComment(size_t index) {
568 if (!summaries_) {
569 summaries_.reset(new CounterSummaries(std::move(summary_v_), false));
570 summaries_->GenerateComments(1.0);
571 }
572 if (index < summaries_->Summaries().size()) {
573 return &(summaries_->Summaries()[index].comment);
574 }
575 return nullptr;
576 }
577
578 std::unordered_map<pid_t, ThreadInfo> thread_map_;
579 std::vector<CounterSummary> summary_v_;
580 std::unique_ptr<CounterSummaries> summaries_;
581 };
582
TEST_F(StatCmdSummariesTest,task_clock_comment)583 TEST_F(StatCmdSummariesTest, task_clock_comment) {
584 AddSummary("task-clock", -1, -1, 1e9, 0);
585 AddSummary("task-clock", 0, -1, 2e9, 0);
586 AddSummary("task-clock", -1, 0, 0.5e9, 0);
587 AddSummary("task-clock", 1, 1, 3e9, 0);
588 ASSERT_EQ(*GetComment(0), "1.000000 cpus used");
589 ASSERT_EQ(*GetComment(1), "2.000000 cpus used");
590 ASSERT_EQ(*GetComment(2), "0.500000 cpus used");
591 ASSERT_EQ(*GetComment(3), "3.000000 cpus used");
592 }
593
TEST_F(StatCmdSummariesTest,cpu_cycles_comment)594 TEST_F(StatCmdSummariesTest, cpu_cycles_comment) {
595 AddSummary("cpu-cycles", -1, -1, 100, 100);
596 AddSummary("cpu-cycles", 0, -1, 200, 100);
597 AddSummary("cpu-cycles", -1, 0, 50, 100);
598 AddSummary("cpu-cycles", 1, 1, 300, 100);
599 ASSERT_EQ(*GetComment(0), "1.000000 GHz");
600 ASSERT_EQ(*GetComment(1), "2.000000 GHz");
601 ASSERT_EQ(*GetComment(2), "0.500000 GHz");
602 ASSERT_EQ(*GetComment(3), "3.000000 GHz");
603 }
604
TEST_F(StatCmdSummariesTest,rate_comment)605 TEST_F(StatCmdSummariesTest, rate_comment) {
606 AddSummary("branch-misses", -1, -1, 1e9, 1e9);
607 AddSummary("branch-misses", 0, -1, 1e6, 1e9);
608 AddSummary("branch-misses", -1, 0, 1e3, 1e9);
609 AddSummary("branch-misses", 1, 1, 1, 1e9);
610 ASSERT_EQ(*GetComment(0), "1.000 G/sec");
611 ASSERT_EQ(*GetComment(1), "1.000 M/sec");
612 ASSERT_EQ(*GetComment(2), "1.000 K/sec");
613 ASSERT_EQ(*GetComment(3), "1.000 /sec");
614 }