]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/tests/hists_cumulate.c
scsi: cxgb4i: libcxgbi: in error case RST tcp conn
[karo-tx-linux.git] / tools / perf / tests / hists_cumulate.c
1 #include "perf.h"
2 #include "util/debug.h"
3 #include "util/symbol.h"
4 #include "util/sort.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"
12 #include <linux/kernel.h>
13
14 struct sample {
15         u32 pid;
16         u64 ip;
17         struct thread *thread;
18         struct map *map;
19         struct symbol *sym;
20 };
21
22 /* For the numbers, see hists_common.c */
23 static struct sample fake_samples[] = {
24         /* perf [kernel] schedule() */
25         { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
26         /* perf [perf]   main() */
27         { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
28         /* perf [perf]   cmd_record() */
29         { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
30         /* perf [libc]   malloc() */
31         { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
32         /* perf [libc]   free() */
33         { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
34         /* perf [perf]   main() */
35         { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
36         /* perf [kernel] page_fault() */
37         { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
38         /* bash [bash]   main() */
39         { .pid = FAKE_PID_BASH,  .ip = FAKE_IP_BASH_MAIN, },
40         /* bash [bash]   xmalloc() */
41         { .pid = FAKE_PID_BASH,  .ip = FAKE_IP_BASH_XMALLOC, },
42         /* bash [kernel] page_fault() */
43         { .pid = FAKE_PID_BASH,  .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
44 };
45
46 /*
47  * Will be casted to struct ip_callchain which has all 64 bit entries
48  * of nr and ips[].
49  */
50 static u64 fake_callchains[][10] = {
51         /*   schedule => run_command => main */
52         { 3, FAKE_IP_KERNEL_SCHEDULE, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
53         /*   main  */
54         { 1, FAKE_IP_PERF_MAIN, },
55         /*   cmd_record => run_command => main */
56         { 3, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
57         /*   malloc => cmd_record => run_command => main */
58         { 4, FAKE_IP_LIBC_MALLOC, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
59              FAKE_IP_PERF_MAIN, },
60         /*   free => cmd_record => run_command => main */
61         { 4, FAKE_IP_LIBC_FREE, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
62              FAKE_IP_PERF_MAIN, },
63         /*   main */
64         { 1, FAKE_IP_PERF_MAIN, },
65         /*   page_fault => sys_perf_event_open => run_command => main */
66         { 4, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN,
67              FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
68         /*   main */
69         { 1, FAKE_IP_BASH_MAIN, },
70         /*   xmalloc => malloc => xmalloc => malloc => xmalloc => main */
71         { 6, FAKE_IP_BASH_XMALLOC, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC,
72              FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC, FAKE_IP_BASH_MAIN, },
73         /*   page_fault => malloc => main */
74         { 3, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_MAIN, },
75 };
76
77 static int add_hist_entries(struct hists *hists, struct machine *machine)
78 {
79         struct addr_location al;
80         struct perf_evsel *evsel = hists_to_evsel(hists);
81         struct perf_sample sample = { .period = 1000, };
82         size_t i;
83
84         for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
85                 struct hist_entry_iter iter = {
86                         .evsel = evsel,
87                         .sample = &sample,
88                         .hide_unresolved = false,
89                 };
90
91                 if (symbol_conf.cumulate_callchain)
92                         iter.ops = &hist_iter_cumulative;
93                 else
94                         iter.ops = &hist_iter_normal;
95
96                 sample.cpumode = PERF_RECORD_MISC_USER;
97                 sample.pid = fake_samples[i].pid;
98                 sample.tid = fake_samples[i].pid;
99                 sample.ip = fake_samples[i].ip;
100                 sample.callchain = (struct ip_callchain *)fake_callchains[i];
101
102                 if (machine__resolve(machine, &al, &sample) < 0)
103                         goto out;
104
105                 if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack,
106                                          NULL) < 0) {
107                         addr_location__put(&al);
108                         goto out;
109                 }
110
111                 fake_samples[i].thread = al.thread;
112                 fake_samples[i].map = al.map;
113                 fake_samples[i].sym = al.sym;
114         }
115
116         return TEST_OK;
117
118 out:
119         pr_debug("Not enough memory for adding a hist entry\n");
120         return TEST_FAIL;
121 }
122
123 static void del_hist_entries(struct hists *hists)
124 {
125         struct hist_entry *he;
126         struct rb_root *root_in;
127         struct rb_root *root_out;
128         struct rb_node *node;
129
130         if (hists__has(hists, need_collapse))
131                 root_in = &hists->entries_collapsed;
132         else
133                 root_in = hists->entries_in;
134
135         root_out = &hists->entries;
136
137         while (!RB_EMPTY_ROOT(root_out)) {
138                 node = rb_first(root_out);
139
140                 he = rb_entry(node, struct hist_entry, rb_node);
141                 rb_erase(node, root_out);
142                 rb_erase(&he->rb_node_in, root_in);
143                 hist_entry__delete(he);
144         }
145 }
146
147 typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);
148
149 #define COMM(he)  (thread__comm_str(he->thread))
150 #define DSO(he)   (he->ms.map->dso->short_name)
151 #define SYM(he)   (he->ms.sym->name)
152 #define CPU(he)   (he->cpu)
153 #define PID(he)   (he->thread->tid)
154 #define DEPTH(he) (he->callchain->max_depth)
155 #define CDSO(cl)  (cl->ms.map->dso->short_name)
156 #define CSYM(cl)  (cl->ms.sym->name)
157
158 struct result {
159         u64 children;
160         u64 self;
161         const char *comm;
162         const char *dso;
163         const char *sym;
164 };
165
166 struct callchain_result {
167         u64 nr;
168         struct {
169                 const char *dso;
170                 const char *sym;
171         } node[10];
172 };
173
174 static int do_test(struct hists *hists, struct result *expected, size_t nr_expected,
175                    struct callchain_result *expected_callchain, size_t nr_callchain)
176 {
177         char buf[32];
178         size_t i, c;
179         struct hist_entry *he;
180         struct rb_root *root;
181         struct rb_node *node;
182         struct callchain_node *cnode;
183         struct callchain_list *clist;
184
185         /*
186          * adding and deleting hist entries must be done outside of this
187          * function since TEST_ASSERT_VAL() returns in case of failure.
188          */
189         hists__collapse_resort(hists, NULL);
190         perf_evsel__output_resort(hists_to_evsel(hists), NULL);
191
192         if (verbose > 2) {
193                 pr_info("use callchain: %d, cumulate callchain: %d\n",
194                         symbol_conf.use_callchain,
195                         symbol_conf.cumulate_callchain);
196                 print_hists_out(hists);
197         }
198
199         root = &hists->entries;
200         for (node = rb_first(root), i = 0;
201              node && (he = rb_entry(node, struct hist_entry, rb_node));
202              node = rb_next(node), i++) {
203                 scnprintf(buf, sizeof(buf), "Invalid hist entry #%zd", i);
204
205                 TEST_ASSERT_VAL("Incorrect number of hist entry",
206                                 i < nr_expected);
207                 TEST_ASSERT_VAL(buf, he->stat.period == expected[i].self &&
208                                 !strcmp(COMM(he), expected[i].comm) &&
209                                 !strcmp(DSO(he), expected[i].dso) &&
210                                 !strcmp(SYM(he), expected[i].sym));
211
212                 if (symbol_conf.cumulate_callchain)
213                         TEST_ASSERT_VAL(buf, he->stat_acc->period == expected[i].children);
214
215                 if (!symbol_conf.use_callchain)
216                         continue;
217
218                 /* check callchain entries */
219                 root = &he->callchain->node.rb_root;
220
221                 TEST_ASSERT_VAL("callchains expected", !RB_EMPTY_ROOT(root));
222                 cnode = rb_entry(rb_first(root), struct callchain_node, rb_node);
223
224                 c = 0;
225                 list_for_each_entry(clist, &cnode->val, list) {
226                         scnprintf(buf, sizeof(buf), "Invalid callchain entry #%zd/%zd", i, c);
227
228                         TEST_ASSERT_VAL("Incorrect number of callchain entry",
229                                         c < expected_callchain[i].nr);
230                         TEST_ASSERT_VAL(buf,
231                                 !strcmp(CDSO(clist), expected_callchain[i].node[c].dso) &&
232                                 !strcmp(CSYM(clist), expected_callchain[i].node[c].sym));
233                         c++;
234                 }
235                 /* TODO: handle multiple child nodes properly */
236                 TEST_ASSERT_VAL("Incorrect number of callchain entry",
237                                 c <= expected_callchain[i].nr);
238         }
239         TEST_ASSERT_VAL("Incorrect number of hist entry",
240                         i == nr_expected);
241         TEST_ASSERT_VAL("Incorrect number of callchain entry",
242                         !symbol_conf.use_callchain || nr_expected == nr_callchain);
243         return 0;
244 }
245
246 /* NO callchain + NO children */
247 static int test1(struct perf_evsel *evsel, struct machine *machine)
248 {
249         int err;
250         struct hists *hists = evsel__hists(evsel);
251         /*
252          * expected output:
253          *
254          * Overhead  Command  Shared Object          Symbol
255          * ========  =======  =============  ==============
256          *   20.00%     perf  perf           [.] main
257          *   10.00%     bash  [kernel]       [k] page_fault
258          *   10.00%     bash  bash           [.] main
259          *   10.00%     bash  bash           [.] xmalloc
260          *   10.00%     perf  [kernel]       [k] page_fault
261          *   10.00%     perf  [kernel]       [k] schedule
262          *   10.00%     perf  libc           [.] free
263          *   10.00%     perf  libc           [.] malloc
264          *   10.00%     perf  perf           [.] cmd_record
265          */
266         struct result expected[] = {
267                 { 0, 2000, "perf", "perf",     "main" },
268                 { 0, 1000, "bash", "[kernel]", "page_fault" },
269                 { 0, 1000, "bash", "bash",     "main" },
270                 { 0, 1000, "bash", "bash",     "xmalloc" },
271                 { 0, 1000, "perf", "[kernel]", "page_fault" },
272                 { 0, 1000, "perf", "[kernel]", "schedule" },
273                 { 0, 1000, "perf", "libc",     "free" },
274                 { 0, 1000, "perf", "libc",     "malloc" },
275                 { 0, 1000, "perf", "perf",     "cmd_record" },
276         };
277
278         symbol_conf.use_callchain = false;
279         symbol_conf.cumulate_callchain = false;
280         perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
281
282         setup_sorting(NULL);
283         callchain_register_param(&callchain_param);
284
285         err = add_hist_entries(hists, machine);
286         if (err < 0)
287                 goto out;
288
289         err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
290
291 out:
292         del_hist_entries(hists);
293         reset_output_field();
294         return err;
295 }
296
297 /* callcain + NO children */
298 static int test2(struct perf_evsel *evsel, struct machine *machine)
299 {
300         int err;
301         struct hists *hists = evsel__hists(evsel);
302         /*
303          * expected output:
304          *
305          * Overhead  Command  Shared Object          Symbol
306          * ========  =======  =============  ==============
307          *   20.00%     perf  perf           [.] main
308          *              |
309          *              --- main
310          *
311          *   10.00%     bash  [kernel]       [k] page_fault
312          *              |
313          *              --- page_fault
314          *                  malloc
315          *                  main
316          *
317          *   10.00%     bash  bash           [.] main
318          *              |
319          *              --- main
320          *
321          *   10.00%     bash  bash           [.] xmalloc
322          *              |
323          *              --- xmalloc
324          *                  malloc
325          *                  xmalloc     <--- NOTE: there's a cycle
326          *                  malloc
327          *                  xmalloc
328          *                  main
329          *
330          *   10.00%     perf  [kernel]       [k] page_fault
331          *              |
332          *              --- page_fault
333          *                  sys_perf_event_open
334          *                  run_command
335          *                  main
336          *
337          *   10.00%     perf  [kernel]       [k] schedule
338          *              |
339          *              --- schedule
340          *                  run_command
341          *                  main
342          *
343          *   10.00%     perf  libc           [.] free
344          *              |
345          *              --- free
346          *                  cmd_record
347          *                  run_command
348          *                  main
349          *
350          *   10.00%     perf  libc           [.] malloc
351          *              |
352          *              --- malloc
353          *                  cmd_record
354          *                  run_command
355          *                  main
356          *
357          *   10.00%     perf  perf           [.] cmd_record
358          *              |
359          *              --- cmd_record
360          *                  run_command
361          *                  main
362          *
363          */
364         struct result expected[] = {
365                 { 0, 2000, "perf", "perf",     "main" },
366                 { 0, 1000, "bash", "[kernel]", "page_fault" },
367                 { 0, 1000, "bash", "bash",     "main" },
368                 { 0, 1000, "bash", "bash",     "xmalloc" },
369                 { 0, 1000, "perf", "[kernel]", "page_fault" },
370                 { 0, 1000, "perf", "[kernel]", "schedule" },
371                 { 0, 1000, "perf", "libc",     "free" },
372                 { 0, 1000, "perf", "libc",     "malloc" },
373                 { 0, 1000, "perf", "perf",     "cmd_record" },
374         };
375         struct callchain_result expected_callchain[] = {
376                 {
377                         1, {    { "perf",     "main" }, },
378                 },
379                 {
380                         3, {    { "[kernel]", "page_fault" },
381                                 { "libc",     "malloc" },
382                                 { "bash",     "main" }, },
383                 },
384                 {
385                         1, {    { "bash",     "main" }, },
386                 },
387                 {
388                         6, {    { "bash",     "xmalloc" },
389                                 { "libc",     "malloc" },
390                                 { "bash",     "xmalloc" },
391                                 { "libc",     "malloc" },
392                                 { "bash",     "xmalloc" },
393                                 { "bash",     "main" }, },
394                 },
395                 {
396                         4, {    { "[kernel]", "page_fault" },
397                                 { "[kernel]", "sys_perf_event_open" },
398                                 { "perf",     "run_command" },
399                                 { "perf",     "main" }, },
400                 },
401                 {
402                         3, {    { "[kernel]", "schedule" },
403                                 { "perf",     "run_command" },
404                                 { "perf",     "main" }, },
405                 },
406                 {
407                         4, {    { "libc",     "free" },
408                                 { "perf",     "cmd_record" },
409                                 { "perf",     "run_command" },
410                                 { "perf",     "main" }, },
411                 },
412                 {
413                         4, {    { "libc",     "malloc" },
414                                 { "perf",     "cmd_record" },
415                                 { "perf",     "run_command" },
416                                 { "perf",     "main" }, },
417                 },
418                 {
419                         3, {    { "perf",     "cmd_record" },
420                                 { "perf",     "run_command" },
421                                 { "perf",     "main" }, },
422                 },
423         };
424
425         symbol_conf.use_callchain = true;
426         symbol_conf.cumulate_callchain = false;
427         perf_evsel__set_sample_bit(evsel, CALLCHAIN);
428
429         setup_sorting(NULL);
430         callchain_register_param(&callchain_param);
431
432         err = add_hist_entries(hists, machine);
433         if (err < 0)
434                 goto out;
435
436         err = do_test(hists, expected, ARRAY_SIZE(expected),
437                       expected_callchain, ARRAY_SIZE(expected_callchain));
438
439 out:
440         del_hist_entries(hists);
441         reset_output_field();
442         return err;
443 }
444
445 /* NO callchain + children */
446 static int test3(struct perf_evsel *evsel, struct machine *machine)
447 {
448         int err;
449         struct hists *hists = evsel__hists(evsel);
450         /*
451          * expected output:
452          *
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
467          */
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" },
481         };
482
483         symbol_conf.use_callchain = false;
484         symbol_conf.cumulate_callchain = true;
485         perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
486
487         setup_sorting(NULL);
488         callchain_register_param(&callchain_param);
489
490         err = add_hist_entries(hists, machine);
491         if (err < 0)
492                 goto out;
493
494         err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
495
496 out:
497         del_hist_entries(hists);
498         reset_output_field();
499         return err;
500 }
501
502 /* callchain + children */
503 static int test4(struct perf_evsel *evsel, struct machine *machine)
504 {
505         int err;
506         struct hists *hists = evsel__hists(evsel);
507         /*
508          * expected output:
509          *
510          * Children      Self  Command  Shared Object                   Symbol
511          * ========  ========  =======  =============  =======================
512          *   70.00%    20.00%     perf  perf           [.] main
513          *              |
514          *              --- main
515          *
516          *   50.00%     0.00%     perf  perf           [.] run_command
517          *              |
518          *              --- run_command
519          *                  main
520          *
521          *   30.00%    10.00%     bash  bash           [.] main
522          *              |
523          *              --- main
524          *
525          *   30.00%    10.00%     perf  perf           [.] cmd_record
526          *              |
527          *              --- cmd_record
528          *                  run_command
529          *                  main
530          *
531          *   20.00%     0.00%     bash  libc           [.] malloc
532          *              |
533          *              --- malloc
534          *                 |
535          *                 |--50.00%-- xmalloc
536          *                 |           main
537          *                  --50.00%-- main
538          *
539          *   10.00%    10.00%     bash  [kernel]       [k] page_fault
540          *              |
541          *              --- page_fault
542          *                  malloc
543          *                  main
544          *
545          *   10.00%    10.00%     bash  bash           [.] xmalloc
546          *              |
547          *              --- xmalloc
548          *                  malloc
549          *                  xmalloc     <--- NOTE: there's a cycle
550          *                  malloc
551          *                  xmalloc
552          *                  main
553          *
554          *   10.00%     0.00%     perf  [kernel]       [k] sys_perf_event_open
555          *              |
556          *              --- sys_perf_event_open
557          *                  run_command
558          *                  main
559          *
560          *   10.00%    10.00%     perf  [kernel]       [k] page_fault
561          *              |
562          *              --- page_fault
563          *                  sys_perf_event_open
564          *                  run_command
565          *                  main
566          *
567          *   10.00%    10.00%     perf  [kernel]       [k] schedule
568          *              |
569          *              --- schedule
570          *                  run_command
571          *                  main
572          *
573          *   10.00%    10.00%     perf  libc           [.] free
574          *              |
575          *              --- free
576          *                  cmd_record
577          *                  run_command
578          *                  main
579          *
580          *   10.00%    10.00%     perf  libc           [.] malloc
581          *              |
582          *              --- malloc
583          *                  cmd_record
584          *                  run_command
585          *                  main
586          *
587          */
588         struct result expected[] = {
589                 { 7000, 2000, "perf", "perf",     "main" },
590                 { 5000,    0, "perf", "perf",     "run_command" },
591                 { 3000, 1000, "bash", "bash",     "main" },
592                 { 3000, 1000, "perf", "perf",     "cmd_record" },
593                 { 2000,    0, "bash", "libc",     "malloc" },
594                 { 1000, 1000, "bash", "[kernel]", "page_fault" },
595                 { 1000, 1000, "bash", "bash",     "xmalloc" },
596                 { 1000,    0, "perf", "[kernel]", "sys_perf_event_open" },
597                 { 1000, 1000, "perf", "[kernel]", "page_fault" },
598                 { 1000, 1000, "perf", "[kernel]", "schedule" },
599                 { 1000, 1000, "perf", "libc",     "free" },
600                 { 1000, 1000, "perf", "libc",     "malloc" },
601         };
602         struct callchain_result expected_callchain[] = {
603                 {
604                         1, {    { "perf",     "main" }, },
605                 },
606                 {
607                         2, {    { "perf",     "run_command" },
608                                 { "perf",     "main" }, },
609                 },
610                 {
611                         1, {    { "bash",     "main" }, },
612                 },
613                 {
614                         3, {    { "perf",     "cmd_record" },
615                                 { "perf",     "run_command" },
616                                 { "perf",     "main" }, },
617                 },
618                 {
619                         4, {    { "libc",     "malloc" },
620                                 { "bash",     "xmalloc" },
621                                 { "bash",     "main" },
622                                 { "bash",     "main" }, },
623                 },
624                 {
625                         3, {    { "[kernel]", "page_fault" },
626                                 { "libc",     "malloc" },
627                                 { "bash",     "main" }, },
628                 },
629                 {
630                         6, {    { "bash",     "xmalloc" },
631                                 { "libc",     "malloc" },
632                                 { "bash",     "xmalloc" },
633                                 { "libc",     "malloc" },
634                                 { "bash",     "xmalloc" },
635                                 { "bash",     "main" }, },
636                 },
637                 {
638                         3, {    { "[kernel]", "sys_perf_event_open" },
639                                 { "perf",     "run_command" },
640                                 { "perf",     "main" }, },
641                 },
642                 {
643                         4, {    { "[kernel]", "page_fault" },
644                                 { "[kernel]", "sys_perf_event_open" },
645                                 { "perf",     "run_command" },
646                                 { "perf",     "main" }, },
647                 },
648                 {
649                         3, {    { "[kernel]", "schedule" },
650                                 { "perf",     "run_command" },
651                                 { "perf",     "main" }, },
652                 },
653                 {
654                         4, {    { "libc",     "free" },
655                                 { "perf",     "cmd_record" },
656                                 { "perf",     "run_command" },
657                                 { "perf",     "main" }, },
658                 },
659                 {
660                         4, {    { "libc",     "malloc" },
661                                 { "perf",     "cmd_record" },
662                                 { "perf",     "run_command" },
663                                 { "perf",     "main" }, },
664                 },
665         };
666
667         symbol_conf.use_callchain = true;
668         symbol_conf.cumulate_callchain = true;
669         perf_evsel__set_sample_bit(evsel, CALLCHAIN);
670
671         setup_sorting(NULL);
672
673         callchain_param = callchain_param_default;
674         callchain_register_param(&callchain_param);
675
676         err = add_hist_entries(hists, machine);
677         if (err < 0)
678                 goto out;
679
680         err = do_test(hists, expected, ARRAY_SIZE(expected),
681                       expected_callchain, ARRAY_SIZE(expected_callchain));
682
683 out:
684         del_hist_entries(hists);
685         reset_output_field();
686         return err;
687 }
688
689 int test__hists_cumulate(int subtest __maybe_unused)
690 {
691         int err = TEST_FAIL;
692         struct machines machines;
693         struct machine *machine;
694         struct perf_evsel *evsel;
695         struct perf_evlist *evlist = perf_evlist__new();
696         size_t i;
697         test_fn_t testcases[] = {
698                 test1,
699                 test2,
700                 test3,
701                 test4,
702         };
703
704         TEST_ASSERT_VAL("No memory", evlist);
705
706         err = parse_events(evlist, "cpu-clock", NULL);
707         if (err)
708                 goto out;
709         err = TEST_FAIL;
710
711         machines__init(&machines);
712
713         /* setup threads/dso/map/symbols also */
714         machine = setup_fake_machine(&machines);
715         if (!machine)
716                 goto out;
717
718         if (verbose > 1)
719                 machine__fprintf(machine, stderr);
720
721         evsel = perf_evlist__first(evlist);
722
723         for (i = 0; i < ARRAY_SIZE(testcases); i++) {
724                 err = testcases[i](evsel, machine);
725                 if (err < 0)
726                         break;
727         }
728
729 out:
730         /* tear down everything */
731         perf_evlist__delete(evlist);
732         machines__exit(&machines);
733
734         return err;
735 }