1 // SPDX-License-Identifier: GPL-2.0
3 #include "util/debug.h"
4 #include "util/event.h"
5 #include "util/symbol.h"
7 #include "util/evsel.h"
8 #include "util/evlist.h"
9 #include "util/machine.h"
10 #include "util/thread.h"
11 #include "util/parse-events.h"
12 #include "tests/tests.h"
13 #include "tests/hists_common.h"
14 #include <linux/kernel.h>
19 struct thread *thread;
24 /* For the numbers, see hists_common.c */
25 static struct sample fake_samples[] = {
26 /* perf [kernel] schedule() */
27 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
28 /* perf [perf] main() */
29 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
30 /* perf [perf] cmd_record() */
31 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
32 /* perf [libc] malloc() */
33 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
34 /* perf [libc] free() */
35 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
36 /* perf [perf] main() */
37 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
38 /* perf [kernel] page_fault() */
39 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
40 /* bash [bash] main() */
41 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, },
42 /* bash [bash] xmalloc() */
43 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
44 /* bash [kernel] page_fault() */
45 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
49 * Will be casted to struct ip_callchain which has all 64 bit entries
52 static u64 fake_callchains[][10] = {
53 /* schedule => run_command => main */
54 { 3, FAKE_IP_KERNEL_SCHEDULE, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
56 { 1, FAKE_IP_PERF_MAIN, },
57 /* cmd_record => run_command => main */
58 { 3, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
59 /* malloc => cmd_record => run_command => main */
60 { 4, FAKE_IP_LIBC_MALLOC, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
62 /* free => cmd_record => run_command => main */
63 { 4, FAKE_IP_LIBC_FREE, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
66 { 1, FAKE_IP_PERF_MAIN, },
67 /* page_fault => sys_perf_event_open => run_command => main */
68 { 4, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN,
69 FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
71 { 1, FAKE_IP_BASH_MAIN, },
72 /* xmalloc => malloc => xmalloc => malloc => xmalloc => main */
73 { 6, FAKE_IP_BASH_XMALLOC, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC,
74 FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC, FAKE_IP_BASH_MAIN, },
75 /* page_fault => malloc => main */
76 { 3, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_MAIN, },
79 static int add_hist_entries(struct hists *hists, struct machine *machine)
81 struct addr_location al;
82 struct perf_evsel *evsel = hists_to_evsel(hists);
83 struct perf_sample sample = { .period = 1000, };
86 for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
87 struct hist_entry_iter iter = {
90 .hide_unresolved = false,
93 if (symbol_conf.cumulate_callchain)
94 iter.ops = &hist_iter_cumulative;
96 iter.ops = &hist_iter_normal;
98 sample.cpumode = PERF_RECORD_MISC_USER;
99 sample.pid = fake_samples[i].pid;
100 sample.tid = fake_samples[i].pid;
101 sample.ip = fake_samples[i].ip;
102 sample.callchain = (struct ip_callchain *)fake_callchains[i];
104 if (machine__resolve(machine, &al, &sample) < 0)
107 if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack,
109 addr_location__put(&al);
113 fake_samples[i].thread = al.thread;
114 fake_samples[i].map = al.map;
115 fake_samples[i].sym = al.sym;
121 pr_debug("Not enough memory for adding a hist entry\n");
125 static void del_hist_entries(struct hists *hists)
127 struct hist_entry *he;
128 struct rb_root *root_in;
129 struct rb_root *root_out;
130 struct rb_node *node;
132 if (hists__has(hists, need_collapse))
133 root_in = &hists->entries_collapsed;
135 root_in = hists->entries_in;
137 root_out = &hists->entries;
139 while (!RB_EMPTY_ROOT(root_out)) {
140 node = rb_first(root_out);
142 he = rb_entry(node, struct hist_entry, rb_node);
143 rb_erase(node, root_out);
144 rb_erase(&he->rb_node_in, root_in);
145 hist_entry__delete(he);
149 typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);
151 #define COMM(he) (thread__comm_str(he->thread))
152 #define DSO(he) (he->ms.map->dso->short_name)
153 #define SYM(he) (he->ms.sym->name)
154 #define CPU(he) (he->cpu)
155 #define PID(he) (he->thread->tid)
156 #define DEPTH(he) (he->callchain->max_depth)
157 #define CDSO(cl) (cl->ms.map->dso->short_name)
158 #define CSYM(cl) (cl->ms.sym->name)
168 struct callchain_result {
176 static int do_test(struct hists *hists, struct result *expected, size_t nr_expected,
177 struct callchain_result *expected_callchain, size_t nr_callchain)
181 struct hist_entry *he;
182 struct rb_root *root;
183 struct rb_node *node;
184 struct callchain_node *cnode;
185 struct callchain_list *clist;
188 * adding and deleting hist entries must be done outside of this
189 * function since TEST_ASSERT_VAL() returns in case of failure.
191 hists__collapse_resort(hists, NULL);
192 perf_evsel__output_resort(hists_to_evsel(hists), NULL);
195 pr_info("use callchain: %d, cumulate callchain: %d\n",
196 symbol_conf.use_callchain,
197 symbol_conf.cumulate_callchain);
198 print_hists_out(hists);
201 root = &hists->entries;
202 for (node = rb_first(root), i = 0;
203 node && (he = rb_entry(node, struct hist_entry, rb_node));
204 node = rb_next(node), i++) {
205 scnprintf(buf, sizeof(buf), "Invalid hist entry #%zd", i);
207 TEST_ASSERT_VAL("Incorrect number of hist entry",
209 TEST_ASSERT_VAL(buf, he->stat.period == expected[i].self &&
210 !strcmp(COMM(he), expected[i].comm) &&
211 !strcmp(DSO(he), expected[i].dso) &&
212 !strcmp(SYM(he), expected[i].sym));
214 if (symbol_conf.cumulate_callchain)
215 TEST_ASSERT_VAL(buf, he->stat_acc->period == expected[i].children);
217 if (!symbol_conf.use_callchain)
220 /* check callchain entries */
221 root = &he->callchain->node.rb_root;
223 TEST_ASSERT_VAL("callchains expected", !RB_EMPTY_ROOT(root));
224 cnode = rb_entry(rb_first(root), struct callchain_node, rb_node);
227 list_for_each_entry(clist, &cnode->val, list) {
228 scnprintf(buf, sizeof(buf), "Invalid callchain entry #%zd/%zd", i, c);
230 TEST_ASSERT_VAL("Incorrect number of callchain entry",
231 c < expected_callchain[i].nr);
233 !strcmp(CDSO(clist), expected_callchain[i].node[c].dso) &&
234 !strcmp(CSYM(clist), expected_callchain[i].node[c].sym));
237 /* TODO: handle multiple child nodes properly */
238 TEST_ASSERT_VAL("Incorrect number of callchain entry",
239 c <= expected_callchain[i].nr);
241 TEST_ASSERT_VAL("Incorrect number of hist entry",
243 TEST_ASSERT_VAL("Incorrect number of callchain entry",
244 !symbol_conf.use_callchain || nr_expected == nr_callchain);
248 /* NO callchain + NO children */
249 static int test1(struct perf_evsel *evsel, struct machine *machine)
252 struct hists *hists = evsel__hists(evsel);
256 * Overhead Command Shared Object Symbol
257 * ======== ======= ============= ==============
258 * 20.00% perf perf [.] main
259 * 10.00% bash [kernel] [k] page_fault
260 * 10.00% bash bash [.] main
261 * 10.00% bash bash [.] xmalloc
262 * 10.00% perf [kernel] [k] page_fault
263 * 10.00% perf [kernel] [k] schedule
264 * 10.00% perf libc [.] free
265 * 10.00% perf libc [.] malloc
266 * 10.00% perf perf [.] cmd_record
268 struct result expected[] = {
269 { 0, 2000, "perf", "perf", "main" },
270 { 0, 1000, "bash", "[kernel]", "page_fault" },
271 { 0, 1000, "bash", "bash", "main" },
272 { 0, 1000, "bash", "bash", "xmalloc" },
273 { 0, 1000, "perf", "[kernel]", "page_fault" },
274 { 0, 1000, "perf", "[kernel]", "schedule" },
275 { 0, 1000, "perf", "libc", "free" },
276 { 0, 1000, "perf", "libc", "malloc" },
277 { 0, 1000, "perf", "perf", "cmd_record" },
280 symbol_conf.use_callchain = false;
281 symbol_conf.cumulate_callchain = false;
282 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
285 callchain_register_param(&callchain_param);
287 err = add_hist_entries(hists, machine);
291 err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
294 del_hist_entries(hists);
295 reset_output_field();
299 /* callcain + NO children */
300 static int test2(struct perf_evsel *evsel, struct machine *machine)
303 struct hists *hists = evsel__hists(evsel);
307 * Overhead Command Shared Object Symbol
308 * ======== ======= ============= ==============
309 * 20.00% perf perf [.] main
313 * 10.00% bash [kernel] [k] page_fault
319 * 10.00% bash bash [.] main
323 * 10.00% bash bash [.] xmalloc
327 * xmalloc <--- NOTE: there's a cycle
332 * 10.00% perf [kernel] [k] page_fault
335 * sys_perf_event_open
339 * 10.00% perf [kernel] [k] schedule
345 * 10.00% perf libc [.] free
352 * 10.00% perf libc [.] malloc
359 * 10.00% perf perf [.] cmd_record
366 struct result expected[] = {
367 { 0, 2000, "perf", "perf", "main" },
368 { 0, 1000, "bash", "[kernel]", "page_fault" },
369 { 0, 1000, "bash", "bash", "main" },
370 { 0, 1000, "bash", "bash", "xmalloc" },
371 { 0, 1000, "perf", "[kernel]", "page_fault" },
372 { 0, 1000, "perf", "[kernel]", "schedule" },
373 { 0, 1000, "perf", "libc", "free" },
374 { 0, 1000, "perf", "libc", "malloc" },
375 { 0, 1000, "perf", "perf", "cmd_record" },
377 struct callchain_result expected_callchain[] = {
379 1, { { "perf", "main" }, },
382 3, { { "[kernel]", "page_fault" },
383 { "libc", "malloc" },
384 { "bash", "main" }, },
387 1, { { "bash", "main" }, },
390 6, { { "bash", "xmalloc" },
391 { "libc", "malloc" },
392 { "bash", "xmalloc" },
393 { "libc", "malloc" },
394 { "bash", "xmalloc" },
395 { "bash", "main" }, },
398 4, { { "[kernel]", "page_fault" },
399 { "[kernel]", "sys_perf_event_open" },
400 { "perf", "run_command" },
401 { "perf", "main" }, },
404 3, { { "[kernel]", "schedule" },
405 { "perf", "run_command" },
406 { "perf", "main" }, },
409 4, { { "libc", "free" },
410 { "perf", "cmd_record" },
411 { "perf", "run_command" },
412 { "perf", "main" }, },
415 4, { { "libc", "malloc" },
416 { "perf", "cmd_record" },
417 { "perf", "run_command" },
418 { "perf", "main" }, },
421 3, { { "perf", "cmd_record" },
422 { "perf", "run_command" },
423 { "perf", "main" }, },
427 symbol_conf.use_callchain = true;
428 symbol_conf.cumulate_callchain = false;
429 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
432 callchain_register_param(&callchain_param);
434 err = add_hist_entries(hists, machine);
438 err = do_test(hists, expected, ARRAY_SIZE(expected),
439 expected_callchain, ARRAY_SIZE(expected_callchain));
442 del_hist_entries(hists);
443 reset_output_field();
447 /* NO callchain + children */
448 static int test3(struct perf_evsel *evsel, struct machine *machine)
451 struct hists *hists = evsel__hists(evsel);
455 * Children Self Command Shared Object Symbol
456 * ======== ======== ======= ============= =======================
457 * 70.00% 20.00% perf perf [.] main
458 * 50.00% 0.00% perf perf [.] run_command
459 * 30.00% 10.00% bash bash [.] main
460 * 30.00% 10.00% perf perf [.] cmd_record
461 * 20.00% 0.00% bash libc [.] malloc
462 * 10.00% 10.00% bash [kernel] [k] page_fault
463 * 10.00% 10.00% bash bash [.] xmalloc
464 * 10.00% 10.00% perf [kernel] [k] page_fault
465 * 10.00% 10.00% perf libc [.] malloc
466 * 10.00% 10.00% perf [kernel] [k] schedule
467 * 10.00% 10.00% perf libc [.] free
468 * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
470 struct result expected[] = {
471 { 7000, 2000, "perf", "perf", "main" },
472 { 5000, 0, "perf", "perf", "run_command" },
473 { 3000, 1000, "bash", "bash", "main" },
474 { 3000, 1000, "perf", "perf", "cmd_record" },
475 { 2000, 0, "bash", "libc", "malloc" },
476 { 1000, 1000, "bash", "[kernel]", "page_fault" },
477 { 1000, 1000, "bash", "bash", "xmalloc" },
478 { 1000, 1000, "perf", "[kernel]", "page_fault" },
479 { 1000, 1000, "perf", "[kernel]", "schedule" },
480 { 1000, 1000, "perf", "libc", "free" },
481 { 1000, 1000, "perf", "libc", "malloc" },
482 { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
485 symbol_conf.use_callchain = false;
486 symbol_conf.cumulate_callchain = true;
487 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
490 callchain_register_param(&callchain_param);
492 err = add_hist_entries(hists, machine);
496 err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
499 del_hist_entries(hists);
500 reset_output_field();
504 /* callchain + children */
505 static int test4(struct perf_evsel *evsel, struct machine *machine)
508 struct hists *hists = evsel__hists(evsel);
512 * Children Self Command Shared Object Symbol
513 * ======== ======== ======= ============= =======================
514 * 70.00% 20.00% perf perf [.] main
518 * 50.00% 0.00% perf perf [.] run_command
523 * 30.00% 10.00% bash bash [.] main
527 * 30.00% 10.00% perf perf [.] cmd_record
533 * 20.00% 0.00% bash libc [.] malloc
537 * |--50.00%-- xmalloc
541 * 10.00% 10.00% bash [kernel] [k] page_fault
547 * 10.00% 10.00% bash bash [.] xmalloc
551 * xmalloc <--- NOTE: there's a cycle
556 * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
558 * --- sys_perf_event_open
562 * 10.00% 10.00% perf [kernel] [k] page_fault
565 * sys_perf_event_open
569 * 10.00% 10.00% perf [kernel] [k] schedule
575 * 10.00% 10.00% perf libc [.] free
582 * 10.00% 10.00% perf libc [.] malloc
590 struct result expected[] = {
591 { 7000, 2000, "perf", "perf", "main" },
592 { 5000, 0, "perf", "perf", "run_command" },
593 { 3000, 1000, "bash", "bash", "main" },
594 { 3000, 1000, "perf", "perf", "cmd_record" },
595 { 2000, 0, "bash", "libc", "malloc" },
596 { 1000, 1000, "bash", "[kernel]", "page_fault" },
597 { 1000, 1000, "bash", "bash", "xmalloc" },
598 { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
599 { 1000, 1000, "perf", "[kernel]", "page_fault" },
600 { 1000, 1000, "perf", "[kernel]", "schedule" },
601 { 1000, 1000, "perf", "libc", "free" },
602 { 1000, 1000, "perf", "libc", "malloc" },
604 struct callchain_result expected_callchain[] = {
606 1, { { "perf", "main" }, },
609 2, { { "perf", "run_command" },
610 { "perf", "main" }, },
613 1, { { "bash", "main" }, },
616 3, { { "perf", "cmd_record" },
617 { "perf", "run_command" },
618 { "perf", "main" }, },
621 4, { { "libc", "malloc" },
622 { "bash", "xmalloc" },
624 { "bash", "main" }, },
627 3, { { "[kernel]", "page_fault" },
628 { "libc", "malloc" },
629 { "bash", "main" }, },
632 6, { { "bash", "xmalloc" },
633 { "libc", "malloc" },
634 { "bash", "xmalloc" },
635 { "libc", "malloc" },
636 { "bash", "xmalloc" },
637 { "bash", "main" }, },
640 3, { { "[kernel]", "sys_perf_event_open" },
641 { "perf", "run_command" },
642 { "perf", "main" }, },
645 4, { { "[kernel]", "page_fault" },
646 { "[kernel]", "sys_perf_event_open" },
647 { "perf", "run_command" },
648 { "perf", "main" }, },
651 3, { { "[kernel]", "schedule" },
652 { "perf", "run_command" },
653 { "perf", "main" }, },
656 4, { { "libc", "free" },
657 { "perf", "cmd_record" },
658 { "perf", "run_command" },
659 { "perf", "main" }, },
662 4, { { "libc", "malloc" },
663 { "perf", "cmd_record" },
664 { "perf", "run_command" },
665 { "perf", "main" }, },
669 symbol_conf.use_callchain = true;
670 symbol_conf.cumulate_callchain = true;
671 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
675 callchain_param = callchain_param_default;
676 callchain_register_param(&callchain_param);
678 err = add_hist_entries(hists, machine);
682 err = do_test(hists, expected, ARRAY_SIZE(expected),
683 expected_callchain, ARRAY_SIZE(expected_callchain));
686 del_hist_entries(hists);
687 reset_output_field();
691 int test__hists_cumulate(struct test *test __maybe_unused, int subtest __maybe_unused)
694 struct machines machines;
695 struct machine *machine;
696 struct perf_evsel *evsel;
697 struct perf_evlist *evlist = perf_evlist__new();
699 test_fn_t testcases[] = {
706 TEST_ASSERT_VAL("No memory", evlist);
708 err = parse_events(evlist, "cpu-clock", NULL);
713 machines__init(&machines);
715 /* setup threads/dso/map/symbols also */
716 machine = setup_fake_machine(&machines);
721 machine__fprintf(machine, stderr);
723 evsel = perf_evlist__first(evlist);
725 for (i = 0; i < ARRAY_SIZE(testcases); i++) {
726 err = testcases[i](evsel, machine);
732 /* tear down everything */
733 perf_evlist__delete(evlist);
734 machines__exit(&machines);