2 #include "util/debug.h"
3 #include "util/symbol.h"
5 #include "util/evsel.h"
6 #include "util/evlist.h"
7 #include "util/machine.h"
8 #include "util/thread.h"
9 #include "util/parse-events.h"
10 #include "tests/tests.h"
11 #include "tests/hists_common.h"
16 struct thread *thread;
21 /* For the numbers, see hists_common.c */
22 static struct sample fake_samples[] = {
23 /* perf [kernel] schedule() */
24 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
25 /* perf [perf] main() */
26 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
27 /* perf [perf] cmd_record() */
28 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
29 /* perf [libc] malloc() */
30 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
31 /* perf [libc] free() */
32 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
33 /* perf [perf] main() */
34 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
35 /* perf [kernel] page_fault() */
36 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
37 /* bash [bash] main() */
38 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, },
39 /* bash [bash] xmalloc() */
40 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
41 /* bash [kernel] page_fault() */
42 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
46 * Will be casted to struct ip_callchain which has all 64 bit entries
49 static u64 fake_callchains[][10] = {
50 /* schedule => run_command => main */
51 { 3, FAKE_IP_KERNEL_SCHEDULE, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
53 { 1, FAKE_IP_PERF_MAIN, },
54 /* cmd_record => run_command => main */
55 { 3, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
56 /* malloc => cmd_record => run_command => main */
57 { 4, FAKE_IP_LIBC_MALLOC, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
59 /* free => cmd_record => run_command => main */
60 { 4, FAKE_IP_LIBC_FREE, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
63 { 1, FAKE_IP_PERF_MAIN, },
64 /* page_fault => sys_perf_event_open => run_command => main */
65 { 4, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN,
66 FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
68 { 1, FAKE_IP_BASH_MAIN, },
69 /* xmalloc => malloc => xmalloc => malloc => xmalloc => main */
70 { 6, FAKE_IP_BASH_XMALLOC, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC,
71 FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC, FAKE_IP_BASH_MAIN, },
72 /* page_fault => malloc => main */
73 { 3, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_MAIN, },
76 static int add_hist_entries(struct hists *hists, struct machine *machine)
78 struct addr_location al;
79 struct perf_evsel *evsel = hists_to_evsel(hists);
80 struct perf_sample sample = { .period = 1000, };
83 for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
84 const union perf_event event = {
86 .misc = PERF_RECORD_MISC_USER,
89 struct hist_entry_iter iter = {
92 .hide_unresolved = false,
95 if (symbol_conf.cumulate_callchain)
96 iter.ops = &hist_iter_cumulative;
98 iter.ops = &hist_iter_normal;
100 sample.pid = fake_samples[i].pid;
101 sample.tid = fake_samples[i].pid;
102 sample.ip = fake_samples[i].ip;
103 sample.callchain = (struct ip_callchain *)fake_callchains[i];
105 if (perf_event__preprocess_sample(&event, machine, &al,
109 if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH,
111 addr_location__put(&al);
115 fake_samples[i].thread = al.thread;
116 fake_samples[i].map = al.map;
117 fake_samples[i].sym = al.sym;
123 pr_debug("Not enough memory for adding a hist entry\n");
127 static void del_hist_entries(struct hists *hists)
129 struct hist_entry *he;
130 struct rb_root *root_in;
131 struct rb_root *root_out;
132 struct rb_node *node;
134 if (sort__need_collapse)
135 root_in = &hists->entries_collapsed;
137 root_in = hists->entries_in;
139 root_out = &hists->entries;
141 while (!RB_EMPTY_ROOT(root_out)) {
142 node = rb_first(root_out);
144 he = rb_entry(node, struct hist_entry, rb_node);
145 rb_erase(node, root_out);
146 rb_erase(&he->rb_node_in, root_in);
147 hist_entry__delete(he);
151 typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);
153 #define COMM(he) (thread__comm_str(he->thread))
154 #define DSO(he) (he->ms.map->dso->short_name)
155 #define SYM(he) (he->ms.sym->name)
156 #define CPU(he) (he->cpu)
157 #define PID(he) (he->thread->tid)
158 #define DEPTH(he) (he->callchain->max_depth)
159 #define CDSO(cl) (cl->ms.map->dso->short_name)
160 #define CSYM(cl) (cl->ms.sym->name)
170 struct callchain_result {
178 static int do_test(struct hists *hists, struct result *expected, size_t nr_expected,
179 struct callchain_result *expected_callchain, size_t nr_callchain)
183 struct hist_entry *he;
184 struct rb_root *root;
185 struct rb_node *node;
186 struct callchain_node *cnode;
187 struct callchain_list *clist;
190 * adding and deleting hist entries must be done outside of this
191 * function since TEST_ASSERT_VAL() returns in case of failure.
193 hists__collapse_resort(hists, NULL);
194 hists__output_resort(hists, NULL);
197 pr_info("use callchain: %d, cumulate callchain: %d\n",
198 symbol_conf.use_callchain,
199 symbol_conf.cumulate_callchain);
200 print_hists_out(hists);
203 root = &hists->entries;
204 for (node = rb_first(root), i = 0;
205 node && (he = rb_entry(node, struct hist_entry, rb_node));
206 node = rb_next(node), i++) {
207 scnprintf(buf, sizeof(buf), "Invalid hist entry #%zd", i);
209 TEST_ASSERT_VAL("Incorrect number of hist entry",
211 TEST_ASSERT_VAL(buf, he->stat.period == expected[i].self &&
212 !strcmp(COMM(he), expected[i].comm) &&
213 !strcmp(DSO(he), expected[i].dso) &&
214 !strcmp(SYM(he), expected[i].sym));
216 if (symbol_conf.cumulate_callchain)
217 TEST_ASSERT_VAL(buf, he->stat_acc->period == expected[i].children);
219 if (!symbol_conf.use_callchain)
222 /* check callchain entries */
223 root = &he->callchain->node.rb_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;
284 callchain_register_param(&callchain_param);
286 err = add_hist_entries(hists, machine);
290 err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
293 del_hist_entries(hists);
294 reset_output_field();
298 /* callcain + NO children */
299 static int test2(struct perf_evsel *evsel, struct machine *machine)
302 struct hists *hists = evsel__hists(evsel);
306 * Overhead Command Shared Object Symbol
307 * ======== ======= ============= ==============
308 * 20.00% perf perf [.] main
312 * 10.00% bash [kernel] [k] page_fault
318 * 10.00% bash bash [.] main
322 * 10.00% bash bash [.] xmalloc
326 * xmalloc <--- NOTE: there's a cycle
331 * 10.00% perf [kernel] [k] page_fault
334 * sys_perf_event_open
338 * 10.00% perf [kernel] [k] schedule
344 * 10.00% perf libc [.] free
351 * 10.00% perf libc [.] malloc
358 * 10.00% perf perf [.] cmd_record
365 struct result expected[] = {
366 { 0, 2000, "perf", "perf", "main" },
367 { 0, 1000, "bash", "[kernel]", "page_fault" },
368 { 0, 1000, "bash", "bash", "main" },
369 { 0, 1000, "bash", "bash", "xmalloc" },
370 { 0, 1000, "perf", "[kernel]", "page_fault" },
371 { 0, 1000, "perf", "[kernel]", "schedule" },
372 { 0, 1000, "perf", "libc", "free" },
373 { 0, 1000, "perf", "libc", "malloc" },
374 { 0, 1000, "perf", "perf", "cmd_record" },
376 struct callchain_result expected_callchain[] = {
378 1, { { "perf", "main" }, },
381 3, { { "[kernel]", "page_fault" },
382 { "libc", "malloc" },
383 { "bash", "main" }, },
386 1, { { "bash", "main" }, },
389 6, { { "bash", "xmalloc" },
390 { "libc", "malloc" },
391 { "bash", "xmalloc" },
392 { "libc", "malloc" },
393 { "bash", "xmalloc" },
394 { "bash", "main" }, },
397 4, { { "[kernel]", "page_fault" },
398 { "[kernel]", "sys_perf_event_open" },
399 { "perf", "run_command" },
400 { "perf", "main" }, },
403 3, { { "[kernel]", "schedule" },
404 { "perf", "run_command" },
405 { "perf", "main" }, },
408 4, { { "libc", "free" },
409 { "perf", "cmd_record" },
410 { "perf", "run_command" },
411 { "perf", "main" }, },
414 4, { { "libc", "malloc" },
415 { "perf", "cmd_record" },
416 { "perf", "run_command" },
417 { "perf", "main" }, },
420 3, { { "perf", "cmd_record" },
421 { "perf", "run_command" },
422 { "perf", "main" }, },
426 symbol_conf.use_callchain = true;
427 symbol_conf.cumulate_callchain = false;
430 callchain_register_param(&callchain_param);
432 err = add_hist_entries(hists, machine);
436 err = do_test(hists, expected, ARRAY_SIZE(expected),
437 expected_callchain, ARRAY_SIZE(expected_callchain));
440 del_hist_entries(hists);
441 reset_output_field();
445 /* NO callchain + children */
446 static int test3(struct perf_evsel *evsel, struct machine *machine)
449 struct hists *hists = evsel__hists(evsel);
453 * Children Self Command Shared Object Symbol
454 * ======== ======== ======= ============= =======================
455 * 70.00% 20.00% perf perf [.] main
456 * 50.00% 0.00% perf perf [.] run_command
457 * 30.00% 10.00% bash bash [.] main
458 * 30.00% 10.00% perf perf [.] cmd_record
459 * 20.00% 0.00% bash libc [.] malloc
460 * 10.00% 10.00% bash [kernel] [k] page_fault
461 * 10.00% 10.00% bash bash [.] xmalloc
462 * 10.00% 10.00% perf [kernel] [k] page_fault
463 * 10.00% 10.00% perf libc [.] malloc
464 * 10.00% 10.00% perf [kernel] [k] schedule
465 * 10.00% 10.00% perf libc [.] free
466 * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
468 struct result expected[] = {
469 { 7000, 2000, "perf", "perf", "main" },
470 { 5000, 0, "perf", "perf", "run_command" },
471 { 3000, 1000, "bash", "bash", "main" },
472 { 3000, 1000, "perf", "perf", "cmd_record" },
473 { 2000, 0, "bash", "libc", "malloc" },
474 { 1000, 1000, "bash", "[kernel]", "page_fault" },
475 { 1000, 1000, "bash", "bash", "xmalloc" },
476 { 1000, 1000, "perf", "[kernel]", "page_fault" },
477 { 1000, 1000, "perf", "[kernel]", "schedule" },
478 { 1000, 1000, "perf", "libc", "free" },
479 { 1000, 1000, "perf", "libc", "malloc" },
480 { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
483 symbol_conf.use_callchain = false;
484 symbol_conf.cumulate_callchain = true;
487 callchain_register_param(&callchain_param);
489 err = add_hist_entries(hists, machine);
493 err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
496 del_hist_entries(hists);
497 reset_output_field();
501 /* callchain + children */
502 static int test4(struct perf_evsel *evsel, struct machine *machine)
505 struct hists *hists = evsel__hists(evsel);
509 * Children Self Command Shared Object Symbol
510 * ======== ======== ======= ============= =======================
511 * 70.00% 20.00% perf perf [.] main
515 * 50.00% 0.00% perf perf [.] run_command
520 * 30.00% 10.00% bash bash [.] main
524 * 30.00% 10.00% perf perf [.] cmd_record
530 * 20.00% 0.00% bash libc [.] malloc
534 * |--50.00%-- xmalloc
538 * 10.00% 10.00% bash [kernel] [k] page_fault
544 * 10.00% 10.00% bash bash [.] xmalloc
548 * xmalloc <--- NOTE: there's a cycle
553 * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
555 * --- sys_perf_event_open
559 * 10.00% 10.00% perf [kernel] [k] page_fault
562 * sys_perf_event_open
566 * 10.00% 10.00% perf [kernel] [k] schedule
572 * 10.00% 10.00% perf libc [.] free
579 * 10.00% 10.00% perf libc [.] malloc
587 struct result expected[] = {
588 { 7000, 2000, "perf", "perf", "main" },
589 { 5000, 0, "perf", "perf", "run_command" },
590 { 3000, 1000, "bash", "bash", "main" },
591 { 3000, 1000, "perf", "perf", "cmd_record" },
592 { 2000, 0, "bash", "libc", "malloc" },
593 { 1000, 1000, "bash", "[kernel]", "page_fault" },
594 { 1000, 1000, "bash", "bash", "xmalloc" },
595 { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
596 { 1000, 1000, "perf", "[kernel]", "page_fault" },
597 { 1000, 1000, "perf", "[kernel]", "schedule" },
598 { 1000, 1000, "perf", "libc", "free" },
599 { 1000, 1000, "perf", "libc", "malloc" },
601 struct callchain_result expected_callchain[] = {
603 1, { { "perf", "main" }, },
606 2, { { "perf", "run_command" },
607 { "perf", "main" }, },
610 1, { { "bash", "main" }, },
613 3, { { "perf", "cmd_record" },
614 { "perf", "run_command" },
615 { "perf", "main" }, },
618 4, { { "libc", "malloc" },
619 { "bash", "xmalloc" },
621 { "bash", "main" }, },
624 3, { { "[kernel]", "page_fault" },
625 { "libc", "malloc" },
626 { "bash", "main" }, },
629 6, { { "bash", "xmalloc" },
630 { "libc", "malloc" },
631 { "bash", "xmalloc" },
632 { "libc", "malloc" },
633 { "bash", "xmalloc" },
634 { "bash", "main" }, },
637 3, { { "[kernel]", "sys_perf_event_open" },
638 { "perf", "run_command" },
639 { "perf", "main" }, },
642 4, { { "[kernel]", "page_fault" },
643 { "[kernel]", "sys_perf_event_open" },
644 { "perf", "run_command" },
645 { "perf", "main" }, },
648 3, { { "[kernel]", "schedule" },
649 { "perf", "run_command" },
650 { "perf", "main" }, },
653 4, { { "libc", "free" },
654 { "perf", "cmd_record" },
655 { "perf", "run_command" },
656 { "perf", "main" }, },
659 4, { { "libc", "malloc" },
660 { "perf", "cmd_record" },
661 { "perf", "run_command" },
662 { "perf", "main" }, },
666 symbol_conf.use_callchain = true;
667 symbol_conf.cumulate_callchain = true;
670 callchain_register_param(&callchain_param);
672 err = add_hist_entries(hists, machine);
676 err = do_test(hists, expected, ARRAY_SIZE(expected),
677 expected_callchain, ARRAY_SIZE(expected_callchain));
680 del_hist_entries(hists);
681 reset_output_field();
685 int test__hists_cumulate(void)
688 struct machines machines;
689 struct machine *machine;
690 struct perf_evsel *evsel;
691 struct perf_evlist *evlist = perf_evlist__new();
693 test_fn_t testcases[] = {
700 TEST_ASSERT_VAL("No memory", evlist);
702 err = parse_events(evlist, "cpu-clock", NULL);
706 machines__init(&machines);
708 /* setup threads/dso/map/symbols also */
709 machine = setup_fake_machine(&machines);
714 machine__fprintf(machine, stderr);
716 evsel = perf_evlist__first(evlist);
718 for (i = 0; i < ARRAY_SIZE(testcases); i++) {
719 err = testcases[i](evsel, machine);
725 /* tear down everything */
726 perf_evlist__delete(evlist);
727 machines__exit(&machines);