perf machine: Protect the machine->threads with a rwlock
[linux-2.6-block.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
13 struct sample {
14         u32 pid;
15         u64 ip;
16         struct thread *thread;
17         struct map *map;
18         struct symbol *sym;
19 };
20
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, },
43 };
44
45 /*
46  * Will be casted to struct ip_callchain which has all 64 bit entries
47  * of nr and ips[].
48  */
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, },
52         /*   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,
58              FAKE_IP_PERF_MAIN, },
59         /*   free => cmd_record => run_command => main */
60         { 4, FAKE_IP_LIBC_FREE, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
61              FAKE_IP_PERF_MAIN, },
62         /*   main */
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, },
67         /*   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, },
74 };
75
76 static int add_hist_entries(struct hists *hists, struct machine *machine)
77 {
78         struct addr_location al;
79         struct perf_evsel *evsel = hists_to_evsel(hists);
80         struct perf_sample sample = { .period = 1000, };
81         size_t i;
82
83         for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
84                 const union perf_event event = {
85                         .header = {
86                                 .misc = PERF_RECORD_MISC_USER,
87                         },
88                 };
89                 struct hist_entry_iter iter = {
90                         .hide_unresolved = false,
91                 };
92
93                 if (symbol_conf.cumulate_callchain)
94                         iter.ops = &hist_iter_cumulative;
95                 else
96                         iter.ops = &hist_iter_normal;
97
98                 sample.pid = fake_samples[i].pid;
99                 sample.tid = fake_samples[i].pid;
100                 sample.ip = fake_samples[i].ip;
101                 sample.callchain = (struct ip_callchain *)fake_callchains[i];
102
103                 if (perf_event__preprocess_sample(&event, machine, &al,
104                                                   &sample) < 0)
105                         goto out;
106
107                 if (hist_entry_iter__add(&iter, &al, evsel, &sample,
108                                          PERF_MAX_STACK_DEPTH, NULL) < 0) {
109                         addr_location__put(&al);
110                         goto out;
111                 }
112
113                 fake_samples[i].thread = al.thread;
114                 fake_samples[i].map = al.map;
115                 fake_samples[i].sym = al.sym;
116         }
117
118         return TEST_OK;
119
120 out:
121         pr_debug("Not enough memory for adding a hist entry\n");
122         return TEST_FAIL;
123 }
124
125 static void del_hist_entries(struct hists *hists)
126 {
127         struct hist_entry *he;
128         struct rb_root *root_in;
129         struct rb_root *root_out;
130         struct rb_node *node;
131
132         if (sort__need_collapse)
133                 root_in = &hists->entries_collapsed;
134         else
135                 root_in = hists->entries_in;
136
137         root_out = &hists->entries;
138
139         while (!RB_EMPTY_ROOT(root_out)) {
140                 node = rb_first(root_out);
141
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);
146         }
147 }
148
149 typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);
150
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)
159
160 struct result {
161         u64 children;
162         u64 self;
163         const char *comm;
164         const char *dso;
165         const char *sym;
166 };
167
168 struct callchain_result {
169         u64 nr;
170         struct {
171                 const char *dso;
172                 const char *sym;
173         } node[10];
174 };
175
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)
178 {
179         char buf[32];
180         size_t i, c;
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;
186
187         /*
188          * adding and deleting hist entries must be done outside of this
189          * function since TEST_ASSERT_VAL() returns in case of failure.
190          */
191         hists__collapse_resort(hists, NULL);
192         hists__output_resort(hists, NULL);
193
194         if (verbose > 2) {
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);
199         }
200
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);
206
207                 TEST_ASSERT_VAL("Incorrect number of hist entry",
208                                 i < nr_expected);
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));
213
214                 if (symbol_conf.cumulate_callchain)
215                         TEST_ASSERT_VAL(buf, he->stat_acc->period == expected[i].children);
216
217                 if (!symbol_conf.use_callchain)
218                         continue;
219
220                 /* check callchain entries */
221                 root = &he->callchain->node.rb_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
281         setup_sorting();
282         callchain_register_param(&callchain_param);
283
284         err = add_hist_entries(hists, machine);
285         if (err < 0)
286                 goto out;
287
288         err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
289
290 out:
291         del_hist_entries(hists);
292         reset_output_field();
293         return err;
294 }
295
296 /* callcain + NO children */
297 static int test2(struct perf_evsel *evsel, struct machine *machine)
298 {
299         int err;
300         struct hists *hists = evsel__hists(evsel);
301         /*
302          * expected output:
303          *
304          * Overhead  Command  Shared Object          Symbol
305          * ========  =======  =============  ==============
306          *   20.00%     perf  perf           [.] main
307          *              |
308          *              --- main
309          *
310          *   10.00%     bash  [kernel]       [k] page_fault
311          *              |
312          *              --- page_fault
313          *                  malloc
314          *                  main
315          *
316          *   10.00%     bash  bash           [.] main
317          *              |
318          *              --- main
319          *
320          *   10.00%     bash  bash           [.] xmalloc
321          *              |
322          *              --- xmalloc
323          *                  malloc
324          *                  xmalloc     <--- NOTE: there's a cycle
325          *                  malloc
326          *                  xmalloc
327          *                  main
328          *
329          *   10.00%     perf  [kernel]       [k] page_fault
330          *              |
331          *              --- page_fault
332          *                  sys_perf_event_open
333          *                  run_command
334          *                  main
335          *
336          *   10.00%     perf  [kernel]       [k] schedule
337          *              |
338          *              --- schedule
339          *                  run_command
340          *                  main
341          *
342          *   10.00%     perf  libc           [.] free
343          *              |
344          *              --- free
345          *                  cmd_record
346          *                  run_command
347          *                  main
348          *
349          *   10.00%     perf  libc           [.] malloc
350          *              |
351          *              --- malloc
352          *                  cmd_record
353          *                  run_command
354          *                  main
355          *
356          *   10.00%     perf  perf           [.] cmd_record
357          *              |
358          *              --- cmd_record
359          *                  run_command
360          *                  main
361          *
362          */
363         struct result expected[] = {
364                 { 0, 2000, "perf", "perf",     "main" },
365                 { 0, 1000, "bash", "[kernel]", "page_fault" },
366                 { 0, 1000, "bash", "bash",     "main" },
367                 { 0, 1000, "bash", "bash",     "xmalloc" },
368                 { 0, 1000, "perf", "[kernel]", "page_fault" },
369                 { 0, 1000, "perf", "[kernel]", "schedule" },
370                 { 0, 1000, "perf", "libc",     "free" },
371                 { 0, 1000, "perf", "libc",     "malloc" },
372                 { 0, 1000, "perf", "perf",     "cmd_record" },
373         };
374         struct callchain_result expected_callchain[] = {
375                 {
376                         1, {    { "perf",     "main" }, },
377                 },
378                 {
379                         3, {    { "[kernel]", "page_fault" },
380                                 { "libc",     "malloc" },
381                                 { "bash",     "main" }, },
382                 },
383                 {
384                         1, {    { "bash",     "main" }, },
385                 },
386                 {
387                         6, {    { "bash",     "xmalloc" },
388                                 { "libc",     "malloc" },
389                                 { "bash",     "xmalloc" },
390                                 { "libc",     "malloc" },
391                                 { "bash",     "xmalloc" },
392                                 { "bash",     "main" }, },
393                 },
394                 {
395                         4, {    { "[kernel]", "page_fault" },
396                                 { "[kernel]", "sys_perf_event_open" },
397                                 { "perf",     "run_command" },
398                                 { "perf",     "main" }, },
399                 },
400                 {
401                         3, {    { "[kernel]", "schedule" },
402                                 { "perf",     "run_command" },
403                                 { "perf",     "main" }, },
404                 },
405                 {
406                         4, {    { "libc",     "free" },
407                                 { "perf",     "cmd_record" },
408                                 { "perf",     "run_command" },
409                                 { "perf",     "main" }, },
410                 },
411                 {
412                         4, {    { "libc",     "malloc" },
413                                 { "perf",     "cmd_record" },
414                                 { "perf",     "run_command" },
415                                 { "perf",     "main" }, },
416                 },
417                 {
418                         3, {    { "perf",     "cmd_record" },
419                                 { "perf",     "run_command" },
420                                 { "perf",     "main" }, },
421                 },
422         };
423
424         symbol_conf.use_callchain = true;
425         symbol_conf.cumulate_callchain = false;
426
427         setup_sorting();
428         callchain_register_param(&callchain_param);
429
430         err = add_hist_entries(hists, machine);
431         if (err < 0)
432                 goto out;
433
434         err = do_test(hists, expected, ARRAY_SIZE(expected),
435                       expected_callchain, ARRAY_SIZE(expected_callchain));
436
437 out:
438         del_hist_entries(hists);
439         reset_output_field();
440         return err;
441 }
442
443 /* NO callchain + children */
444 static int test3(struct perf_evsel *evsel, struct machine *machine)
445 {
446         int err;
447         struct hists *hists = evsel__hists(evsel);
448         /*
449          * expected output:
450          *
451          * Children      Self  Command  Shared Object                   Symbol
452          * ========  ========  =======  =============  =======================
453          *   70.00%    20.00%     perf  perf           [.] main
454          *   50.00%     0.00%     perf  perf           [.] run_command
455          *   30.00%    10.00%     bash  bash           [.] main
456          *   30.00%    10.00%     perf  perf           [.] cmd_record
457          *   20.00%     0.00%     bash  libc           [.] malloc
458          *   10.00%    10.00%     bash  [kernel]       [k] page_fault
459          *   10.00%    10.00%     bash  bash           [.] xmalloc
460          *   10.00%    10.00%     perf  [kernel]       [k] page_fault
461          *   10.00%    10.00%     perf  libc           [.] malloc
462          *   10.00%    10.00%     perf  [kernel]       [k] schedule
463          *   10.00%    10.00%     perf  libc           [.] free
464          *   10.00%     0.00%     perf  [kernel]       [k] sys_perf_event_open
465          */
466         struct result expected[] = {
467                 { 7000, 2000, "perf", "perf",     "main" },
468                 { 5000,    0, "perf", "perf",     "run_command" },
469                 { 3000, 1000, "bash", "bash",     "main" },
470                 { 3000, 1000, "perf", "perf",     "cmd_record" },
471                 { 2000,    0, "bash", "libc",     "malloc" },
472                 { 1000, 1000, "bash", "[kernel]", "page_fault" },
473                 { 1000, 1000, "bash", "bash",     "xmalloc" },
474                 { 1000, 1000, "perf", "[kernel]", "page_fault" },
475                 { 1000, 1000, "perf", "[kernel]", "schedule" },
476                 { 1000, 1000, "perf", "libc",     "free" },
477                 { 1000, 1000, "perf", "libc",     "malloc" },
478                 { 1000,    0, "perf", "[kernel]", "sys_perf_event_open" },
479         };
480
481         symbol_conf.use_callchain = false;
482         symbol_conf.cumulate_callchain = true;
483
484         setup_sorting();
485         callchain_register_param(&callchain_param);
486
487         err = add_hist_entries(hists, machine);
488         if (err < 0)
489                 goto out;
490
491         err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
492
493 out:
494         del_hist_entries(hists);
495         reset_output_field();
496         return err;
497 }
498
499 /* callchain + children */
500 static int test4(struct perf_evsel *evsel, struct machine *machine)
501 {
502         int err;
503         struct hists *hists = evsel__hists(evsel);
504         /*
505          * expected output:
506          *
507          * Children      Self  Command  Shared Object                   Symbol
508          * ========  ========  =======  =============  =======================
509          *   70.00%    20.00%     perf  perf           [.] main
510          *              |
511          *              --- main
512          *
513          *   50.00%     0.00%     perf  perf           [.] run_command
514          *              |
515          *              --- run_command
516          *                  main
517          *
518          *   30.00%    10.00%     bash  bash           [.] main
519          *              |
520          *              --- main
521          *
522          *   30.00%    10.00%     perf  perf           [.] cmd_record
523          *              |
524          *              --- cmd_record
525          *                  run_command
526          *                  main
527          *
528          *   20.00%     0.00%     bash  libc           [.] malloc
529          *              |
530          *              --- malloc
531          *                 |
532          *                 |--50.00%-- xmalloc
533          *                 |           main
534          *                  --50.00%-- main
535          *
536          *   10.00%    10.00%     bash  [kernel]       [k] page_fault
537          *              |
538          *              --- page_fault
539          *                  malloc
540          *                  main
541          *
542          *   10.00%    10.00%     bash  bash           [.] xmalloc
543          *              |
544          *              --- xmalloc
545          *                  malloc
546          *                  xmalloc     <--- NOTE: there's a cycle
547          *                  malloc
548          *                  xmalloc
549          *                  main
550          *
551          *   10.00%     0.00%     perf  [kernel]       [k] sys_perf_event_open
552          *              |
553          *              --- sys_perf_event_open
554          *                  run_command
555          *                  main
556          *
557          *   10.00%    10.00%     perf  [kernel]       [k] page_fault
558          *              |
559          *              --- page_fault
560          *                  sys_perf_event_open
561          *                  run_command
562          *                  main
563          *
564          *   10.00%    10.00%     perf  [kernel]       [k] schedule
565          *              |
566          *              --- schedule
567          *                  run_command
568          *                  main
569          *
570          *   10.00%    10.00%     perf  libc           [.] free
571          *              |
572          *              --- free
573          *                  cmd_record
574          *                  run_command
575          *                  main
576          *
577          *   10.00%    10.00%     perf  libc           [.] malloc
578          *              |
579          *              --- malloc
580          *                  cmd_record
581          *                  run_command
582          *                  main
583          *
584          */
585         struct result expected[] = {
586                 { 7000, 2000, "perf", "perf",     "main" },
587                 { 5000,    0, "perf", "perf",     "run_command" },
588                 { 3000, 1000, "bash", "bash",     "main" },
589                 { 3000, 1000, "perf", "perf",     "cmd_record" },
590                 { 2000,    0, "bash", "libc",     "malloc" },
591                 { 1000, 1000, "bash", "[kernel]", "page_fault" },
592                 { 1000, 1000, "bash", "bash",     "xmalloc" },
593                 { 1000,    0, "perf", "[kernel]", "sys_perf_event_open" },
594                 { 1000, 1000, "perf", "[kernel]", "page_fault" },
595                 { 1000, 1000, "perf", "[kernel]", "schedule" },
596                 { 1000, 1000, "perf", "libc",     "free" },
597                 { 1000, 1000, "perf", "libc",     "malloc" },
598         };
599         struct callchain_result expected_callchain[] = {
600                 {
601                         1, {    { "perf",     "main" }, },
602                 },
603                 {
604                         2, {    { "perf",     "run_command" },
605                                 { "perf",     "main" }, },
606                 },
607                 {
608                         1, {    { "bash",     "main" }, },
609                 },
610                 {
611                         3, {    { "perf",     "cmd_record" },
612                                 { "perf",     "run_command" },
613                                 { "perf",     "main" }, },
614                 },
615                 {
616                         4, {    { "libc",     "malloc" },
617                                 { "bash",     "xmalloc" },
618                                 { "bash",     "main" },
619                                 { "bash",     "main" }, },
620                 },
621                 {
622                         3, {    { "[kernel]", "page_fault" },
623                                 { "libc",     "malloc" },
624                                 { "bash",     "main" }, },
625                 },
626                 {
627                         6, {    { "bash",     "xmalloc" },
628                                 { "libc",     "malloc" },
629                                 { "bash",     "xmalloc" },
630                                 { "libc",     "malloc" },
631                                 { "bash",     "xmalloc" },
632                                 { "bash",     "main" }, },
633                 },
634                 {
635                         3, {    { "[kernel]", "sys_perf_event_open" },
636                                 { "perf",     "run_command" },
637                                 { "perf",     "main" }, },
638                 },
639                 {
640                         4, {    { "[kernel]", "page_fault" },
641                                 { "[kernel]", "sys_perf_event_open" },
642                                 { "perf",     "run_command" },
643                                 { "perf",     "main" }, },
644                 },
645                 {
646                         3, {    { "[kernel]", "schedule" },
647                                 { "perf",     "run_command" },
648                                 { "perf",     "main" }, },
649                 },
650                 {
651                         4, {    { "libc",     "free" },
652                                 { "perf",     "cmd_record" },
653                                 { "perf",     "run_command" },
654                                 { "perf",     "main" }, },
655                 },
656                 {
657                         4, {    { "libc",     "malloc" },
658                                 { "perf",     "cmd_record" },
659                                 { "perf",     "run_command" },
660                                 { "perf",     "main" }, },
661                 },
662         };
663
664         symbol_conf.use_callchain = true;
665         symbol_conf.cumulate_callchain = true;
666
667         setup_sorting();
668         callchain_register_param(&callchain_param);
669
670         err = add_hist_entries(hists, machine);
671         if (err < 0)
672                 goto out;
673
674         err = do_test(hists, expected, ARRAY_SIZE(expected),
675                       expected_callchain, ARRAY_SIZE(expected_callchain));
676
677 out:
678         del_hist_entries(hists);
679         reset_output_field();
680         return err;
681 }
682
683 int test__hists_cumulate(void)
684 {
685         int err = TEST_FAIL;
686         struct machines machines;
687         struct machine *machine;
688         struct perf_evsel *evsel;
689         struct perf_evlist *evlist = perf_evlist__new();
690         size_t i;
691         test_fn_t testcases[] = {
692                 test1,
693                 test2,
694                 test3,
695                 test4,
696         };
697
698         TEST_ASSERT_VAL("No memory", evlist);
699
700         err = parse_events(evlist, "cpu-clock", NULL);
701         if (err)
702                 goto out;
703
704         machines__init(&machines);
705
706         /* setup threads/dso/map/symbols also */
707         machine = setup_fake_machine(&machines);
708         if (!machine)
709                 goto out;
710
711         if (verbose > 1)
712                 machine__fprintf(machine, stderr);
713
714         evsel = perf_evlist__first(evlist);
715
716         for (i = 0; i < ARRAY_SIZE(testcases); i++) {
717                 err = testcases[i](evsel, machine);
718                 if (err < 0)
719                         break;
720         }
721
722 out:
723         /* tear down everything */
724         perf_evlist__delete(evlist);
725         machines__exit(&machines);
726
727         return err;
728 }