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