xref: /linux/tools/perf/tests/hists_output.c (revision eacaf5ae747f7dead6cc268de17a7382d79031fc)
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/thread.h"
12 #include "util/parse-events.h"
13 #include "tests/tests.h"
14 #include "tests/hists_common.h"
15 #include <linux/kernel.h>
16 
17 struct sample {
18 	u32 cpu;
19 	u32 pid;
20 	u64 ip;
21 	struct thread *thread;
22 	struct map *map;
23 	struct symbol *sym;
24 };
25 
26 /* For the numbers, see hists_common.c */
27 static struct sample fake_samples[] = {
28 	/* perf [kernel] schedule() */
29 	{ .cpu = 0, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
30 	/* perf [perf]   main() */
31 	{ .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
32 	/* perf [perf]   cmd_record() */
33 	{ .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
34 	/* perf [libc]   malloc() */
35 	{ .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
36 	/* perf [libc]   free() */
37 	{ .cpu = 2, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
38 	/* perf [perf]   main() */
39 	{ .cpu = 2, .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
40 	/* perf [kernel] page_fault() */
41 	{ .cpu = 2, .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
42 	/* bash [bash]   main() */
43 	{ .cpu = 3, .pid = FAKE_PID_BASH,  .ip = FAKE_IP_BASH_MAIN, },
44 	/* bash [bash]   xmalloc() */
45 	{ .cpu = 0, .pid = FAKE_PID_BASH,  .ip = FAKE_IP_BASH_XMALLOC, },
46 	/* bash [kernel] page_fault() */
47 	{ .cpu = 1, .pid = FAKE_PID_BASH,  .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
48 };
49 
50 static int add_hist_entries(struct hists *hists, struct machine *machine)
51 {
52 	struct addr_location al;
53 	struct evsel *evsel = hists_to_evsel(hists);
54 	struct perf_sample sample = { .evsel = evsel, .period = 100, };
55 	size_t i;
56 
57 	addr_location__init(&al);
58 	for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
59 		struct hist_entry_iter iter = {
60 			.sample = &sample,
61 			.ops = &hist_iter_normal,
62 			.hide_unresolved = false,
63 		};
64 
65 		sample.cpumode = PERF_RECORD_MISC_USER;
66 		sample.cpu = fake_samples[i].cpu;
67 		sample.pid = fake_samples[i].pid;
68 		sample.tid = fake_samples[i].pid;
69 		sample.ip = fake_samples[i].ip;
70 
71 		if (machine__resolve(machine, &al, &sample) < 0)
72 			goto out;
73 
74 		if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack,
75 					 NULL) < 0) {
76 			goto out;
77 		}
78 
79 		fake_samples[i].thread = al.thread;
80 		map__put(fake_samples[i].map);
81 		fake_samples[i].map = map__get(al.map);
82 		fake_samples[i].sym = al.sym;
83 	}
84 
85 	addr_location__exit(&al);
86 	return TEST_OK;
87 
88 out:
89 	pr_debug("Not enough memory for adding a hist entry\n");
90 	addr_location__exit(&al);
91 	return TEST_FAIL;
92 }
93 
94 static void del_hist_entries(struct hists *hists)
95 {
96 	struct hist_entry *he;
97 	struct rb_root_cached *root_in;
98 	struct rb_root_cached *root_out;
99 	struct rb_node *node;
100 
101 	if (hists__has(hists, need_collapse))
102 		root_in = &hists->entries_collapsed;
103 	else
104 		root_in = hists->entries_in;
105 
106 	root_out = &hists->entries;
107 
108 	while (!RB_EMPTY_ROOT(&root_out->rb_root)) {
109 		node = rb_first_cached(root_out);
110 
111 		he = rb_entry(node, struct hist_entry, rb_node);
112 		rb_erase_cached(node, root_out);
113 		rb_erase_cached(&he->rb_node_in, root_in);
114 		hist_entry__delete(he);
115 	}
116 }
117 
118 static void put_fake_samples(void)
119 {
120 	size_t i;
121 
122 	for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
123 		map__put(fake_samples[i].map);
124 		fake_samples[i].map = NULL;
125 	}
126 }
127 
128 typedef int (*test_fn_t)(struct evsel *, struct machine *);
129 
130 #define COMM(he)  (thread__comm_str(he->thread))
131 #define DSO(he)   (dso__short_name(map__dso(he->ms.map)))
132 #define SYM(he)   (he->ms.sym->name)
133 #define CPU(he)   (he->cpu)
134 #define PID(he)   (thread__tid(he->thread))
135 
136 /* default sort keys (no field) */
137 static int test1(struct evsel *evsel, struct machine *machine)
138 {
139 	int err;
140 	struct hists *hists = evsel__hists(evsel);
141 	struct hist_entry *he;
142 	struct rb_root_cached *root;
143 	struct rb_node *node;
144 
145 	field_order = NULL;
146 	sort_order = NULL; /* equivalent to sort_order = "comm,dso,sym" */
147 
148 	setup_sorting(/*evlist=*/NULL, machine->env);
149 
150 	/*
151 	 * expected output:
152 	 *
153 	 * Overhead  Command  Shared Object          Symbol
154 	 * ========  =======  =============  ==============
155 	 *   20.00%     perf  perf           [.] main
156 	 *   10.00%     bash  [kernel]       [k] page_fault
157 	 *   10.00%     bash  bash           [.] main
158 	 *   10.00%     bash  bash           [.] xmalloc
159 	 *   10.00%     perf  [kernel]       [k] page_fault
160 	 *   10.00%     perf  [kernel]       [k] schedule
161 	 *   10.00%     perf  libc           [.] free
162 	 *   10.00%     perf  libc           [.] malloc
163 	 *   10.00%     perf  perf           [.] cmd_record
164 	 */
165 	err = add_hist_entries(hists, machine);
166 	if (err < 0)
167 		goto out;
168 
169 	hists__collapse_resort(hists, NULL);
170 	evsel__output_resort(evsel, NULL);
171 
172 	if (verbose > 2) {
173 		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
174 		print_hists_out(hists);
175 	}
176 
177 	root = &hists->entries;
178 	node = rb_first_cached(root);
179 	he = rb_entry(node, struct hist_entry, rb_node);
180 	TEST_ASSERT_VAL("Invalid hist entry",
181 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
182 			!strcmp(SYM(he), "main") && he->stat.period == 200);
183 
184 	node = rb_next(node);
185 	he = rb_entry(node, struct hist_entry, rb_node);
186 	TEST_ASSERT_VAL("Invalid hist entry",
187 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
188 			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);
189 
190 	node = rb_next(node);
191 	he = rb_entry(node, struct hist_entry, rb_node);
192 	TEST_ASSERT_VAL("Invalid hist entry",
193 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
194 			!strcmp(SYM(he), "main") && he->stat.period == 100);
195 
196 	node = rb_next(node);
197 	he = rb_entry(node, struct hist_entry, rb_node);
198 	TEST_ASSERT_VAL("Invalid hist entry",
199 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
200 			!strcmp(SYM(he), "xmalloc") && he->stat.period == 100);
201 
202 	node = rb_next(node);
203 	he = rb_entry(node, struct hist_entry, rb_node);
204 	TEST_ASSERT_VAL("Invalid hist entry",
205 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
206 			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);
207 
208 	node = rb_next(node);
209 	he = rb_entry(node, struct hist_entry, rb_node);
210 	TEST_ASSERT_VAL("Invalid hist entry",
211 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
212 			!strcmp(SYM(he), "schedule") && he->stat.period == 100);
213 
214 	node = rb_next(node);
215 	he = rb_entry(node, struct hist_entry, rb_node);
216 	TEST_ASSERT_VAL("Invalid hist entry",
217 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
218 			!strcmp(SYM(he), "free") && he->stat.period == 100);
219 
220 	node = rb_next(node);
221 	he = rb_entry(node, struct hist_entry, rb_node);
222 	TEST_ASSERT_VAL("Invalid hist entry",
223 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
224 			!strcmp(SYM(he), "malloc") && he->stat.period == 100);
225 
226 	node = rb_next(node);
227 	he = rb_entry(node, struct hist_entry, rb_node);
228 	TEST_ASSERT_VAL("Invalid hist entry",
229 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
230 			!strcmp(SYM(he), "cmd_record") && he->stat.period == 100);
231 
232 out:
233 	del_hist_entries(hists);
234 	reset_output_field();
235 	return err;
236 }
237 
238 /* mixed fields and sort keys */
239 static int test2(struct evsel *evsel, struct machine *machine)
240 {
241 	int err;
242 	struct hists *hists = evsel__hists(evsel);
243 	struct hist_entry *he;
244 	struct rb_root_cached *root;
245 	struct rb_node *node;
246 
247 	field_order = "overhead,cpu";
248 	sort_order = "pid";
249 
250 	setup_sorting(/*evlist=*/NULL, machine->env);
251 
252 	/*
253 	 * expected output:
254 	 *
255 	 * Overhead  CPU  Command:  Pid
256 	 * ========  ===  =============
257 	 *   30.00%    1  perf   :  100
258 	 *   10.00%    0  perf   :  100
259 	 *   10.00%    2  perf   :  100
260 	 *   20.00%    2  perf   :  200
261 	 *   10.00%    0  bash   :  300
262 	 *   10.00%    1  bash   :  300
263 	 *   10.00%    3  bash   :  300
264 	 */
265 	err = add_hist_entries(hists, machine);
266 	if (err < 0)
267 		goto out;
268 
269 	hists__collapse_resort(hists, NULL);
270 	evsel__output_resort(evsel, NULL);
271 
272 	if (verbose > 2) {
273 		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
274 		print_hists_out(hists);
275 	}
276 
277 	root = &hists->entries;
278 	node = rb_first_cached(root);
279 	he = rb_entry(node, struct hist_entry, rb_node);
280 	TEST_ASSERT_VAL("Invalid hist entry",
281 			CPU(he) == 1 && PID(he) == 100 && he->stat.period == 300);
282 
283 	node = rb_next(node);
284 	he = rb_entry(node, struct hist_entry, rb_node);
285 	TEST_ASSERT_VAL("Invalid hist entry",
286 			CPU(he) == 0 && PID(he) == 100 && he->stat.period == 100);
287 
288 out:
289 	del_hist_entries(hists);
290 	reset_output_field();
291 	return err;
292 }
293 
294 /* fields only (no sort key) */
295 static int test3(struct evsel *evsel, struct machine *machine)
296 {
297 	int err;
298 	struct hists *hists = evsel__hists(evsel);
299 	struct hist_entry *he;
300 	struct rb_root_cached *root;
301 	struct rb_node *node;
302 
303 	field_order = "comm,overhead,dso";
304 	sort_order = NULL;
305 
306 	setup_sorting(/*evlist=*/NULL, machine->env);
307 
308 	/*
309 	 * expected output:
310 	 *
311 	 * Command  Overhead  Shared Object
312 	 * =======  ========  =============
313 	 *    bash    20.00%  bash
314 	 *    bash    10.00%  [kernel]
315 	 *    perf    30.00%  perf
316 	 *    perf    20.00%  [kernel]
317 	 *    perf    20.00%  libc
318 	 */
319 	err = add_hist_entries(hists, machine);
320 	if (err < 0)
321 		goto out;
322 
323 	hists__collapse_resort(hists, NULL);
324 	evsel__output_resort(evsel, NULL);
325 
326 	if (verbose > 2) {
327 		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
328 		print_hists_out(hists);
329 	}
330 
331 	root = &hists->entries;
332 	node = rb_first_cached(root);
333 	he = rb_entry(node, struct hist_entry, rb_node);
334 	TEST_ASSERT_VAL("Invalid hist entry",
335 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
336 			he->stat.period == 200);
337 
338 	node = rb_next(node);
339 	he = rb_entry(node, struct hist_entry, rb_node);
340 	TEST_ASSERT_VAL("Invalid hist entry",
341 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
342 			he->stat.period == 100);
343 
344 	node = rb_next(node);
345 	he = rb_entry(node, struct hist_entry, rb_node);
346 	TEST_ASSERT_VAL("Invalid hist entry",
347 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
348 			he->stat.period == 300);
349 
350 	node = rb_next(node);
351 	he = rb_entry(node, struct hist_entry, rb_node);
352 	TEST_ASSERT_VAL("Invalid hist entry",
353 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
354 			he->stat.period == 200);
355 
356 	node = rb_next(node);
357 	he = rb_entry(node, struct hist_entry, rb_node);
358 	TEST_ASSERT_VAL("Invalid hist entry",
359 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
360 			he->stat.period == 200);
361 
362 out:
363 	del_hist_entries(hists);
364 	reset_output_field();
365 	return err;
366 }
367 
368 /* handle duplicate 'dso' field */
369 static int test4(struct evsel *evsel, struct machine *machine)
370 {
371 	int err;
372 	struct hists *hists = evsel__hists(evsel);
373 	struct hist_entry *he;
374 	struct rb_root_cached *root;
375 	struct rb_node *node;
376 
377 	field_order = "dso,sym,comm,overhead,dso";
378 	sort_order = "sym";
379 
380 	setup_sorting(/*evlist=*/NULL, machine->env);
381 
382 	/*
383 	 * expected output:
384 	 *
385 	 * Shared Object          Symbol  Command  Overhead
386 	 * =============  ==============  =======  ========
387 	 *          perf  [.] cmd_record     perf    10.00%
388 	 *          libc  [.] free           perf    10.00%
389 	 *          bash  [.] main           bash    10.00%
390 	 *          perf  [.] main           perf    20.00%
391 	 *          libc  [.] malloc         perf    10.00%
392 	 *      [kernel]  [k] page_fault     bash    10.00%
393 	 *      [kernel]  [k] page_fault     perf    10.00%
394 	 *      [kernel]  [k] schedule       perf    10.00%
395 	 *          bash  [.] xmalloc        bash    10.00%
396 	 */
397 	err = add_hist_entries(hists, machine);
398 	if (err < 0)
399 		goto out;
400 
401 	hists__collapse_resort(hists, NULL);
402 	evsel__output_resort(evsel, NULL);
403 
404 	if (verbose > 2) {
405 		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
406 		print_hists_out(hists);
407 	}
408 
409 	root = &hists->entries;
410 	node = rb_first_cached(root);
411 	he = rb_entry(node, struct hist_entry, rb_node);
412 	TEST_ASSERT_VAL("Invalid hist entry",
413 			!strcmp(DSO(he), "perf") && !strcmp(SYM(he), "cmd_record") &&
414 			!strcmp(COMM(he), "perf") && he->stat.period == 100);
415 
416 	node = rb_next(node);
417 	he = rb_entry(node, struct hist_entry, rb_node);
418 	TEST_ASSERT_VAL("Invalid hist entry",
419 			!strcmp(DSO(he), "libc") && !strcmp(SYM(he), "free") &&
420 			!strcmp(COMM(he), "perf") && he->stat.period == 100);
421 
422 	node = rb_next(node);
423 	he = rb_entry(node, struct hist_entry, rb_node);
424 	TEST_ASSERT_VAL("Invalid hist entry",
425 			!strcmp(DSO(he), "bash") && !strcmp(SYM(he), "main") &&
426 			!strcmp(COMM(he), "bash") && he->stat.period == 100);
427 
428 	node = rb_next(node);
429 	he = rb_entry(node, struct hist_entry, rb_node);
430 	TEST_ASSERT_VAL("Invalid hist entry",
431 			!strcmp(DSO(he), "perf") && !strcmp(SYM(he), "main") &&
432 			!strcmp(COMM(he), "perf") && he->stat.period == 200);
433 
434 	node = rb_next(node);
435 	he = rb_entry(node, struct hist_entry, rb_node);
436 	TEST_ASSERT_VAL("Invalid hist entry",
437 			!strcmp(DSO(he), "libc") && !strcmp(SYM(he), "malloc") &&
438 			!strcmp(COMM(he), "perf") && he->stat.period == 100);
439 
440 	node = rb_next(node);
441 	he = rb_entry(node, struct hist_entry, rb_node);
442 	TEST_ASSERT_VAL("Invalid hist entry",
443 			!strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "page_fault") &&
444 			!strcmp(COMM(he), "bash") && he->stat.period == 100);
445 
446 	node = rb_next(node);
447 	he = rb_entry(node, struct hist_entry, rb_node);
448 	TEST_ASSERT_VAL("Invalid hist entry",
449 			!strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "page_fault") &&
450 			!strcmp(COMM(he), "perf") && he->stat.period == 100);
451 
452 	node = rb_next(node);
453 	he = rb_entry(node, struct hist_entry, rb_node);
454 	TEST_ASSERT_VAL("Invalid hist entry",
455 			!strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "schedule") &&
456 			!strcmp(COMM(he), "perf") && he->stat.period == 100);
457 
458 	node = rb_next(node);
459 	he = rb_entry(node, struct hist_entry, rb_node);
460 	TEST_ASSERT_VAL("Invalid hist entry",
461 			!strcmp(DSO(he), "bash") && !strcmp(SYM(he), "xmalloc") &&
462 			!strcmp(COMM(he), "bash") && he->stat.period == 100);
463 
464 out:
465 	del_hist_entries(hists);
466 	reset_output_field();
467 	return err;
468 }
469 
470 /* full sort keys w/o overhead field */
471 static int test5(struct evsel *evsel, struct machine *machine)
472 {
473 	int err;
474 	struct hists *hists = evsel__hists(evsel);
475 	struct hist_entry *he;
476 	struct rb_root_cached *root;
477 	struct rb_node *node;
478 
479 	field_order = "cpu,pid,comm,dso,sym";
480 	sort_order = "dso,pid";
481 
482 	setup_sorting(/*evlist=*/NULL, machine->env);
483 
484 	/*
485 	 * expected output:
486 	 *
487 	 * CPU  Command:  Pid  Command  Shared Object          Symbol
488 	 * ===  =============  =======  =============  ==============
489 	 *   0     perf:  100     perf       [kernel]  [k] schedule
490 	 *   2     perf:  200     perf       [kernel]  [k] page_fault
491 	 *   1     bash:  300     bash       [kernel]  [k] page_fault
492 	 *   0     bash:  300     bash           bash  [.] xmalloc
493 	 *   3     bash:  300     bash           bash  [.] main
494 	 *   1     perf:  100     perf           libc  [.] malloc
495 	 *   2     perf:  100     perf           libc  [.] free
496 	 *   1     perf:  100     perf           perf  [.] cmd_record
497 	 *   1     perf:  100     perf           perf  [.] main
498 	 *   2     perf:  200     perf           perf  [.] main
499 	 */
500 	err = add_hist_entries(hists, machine);
501 	if (err < 0)
502 		goto out;
503 
504 	hists__collapse_resort(hists, NULL);
505 	evsel__output_resort(evsel, NULL);
506 
507 	if (verbose > 2) {
508 		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
509 		print_hists_out(hists);
510 	}
511 
512 	root = &hists->entries;
513 	node = rb_first_cached(root);
514 	he = rb_entry(node, struct hist_entry, rb_node);
515 
516 	TEST_ASSERT_VAL("Invalid hist entry",
517 			CPU(he) == 0 && PID(he) == 100 &&
518 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
519 			!strcmp(SYM(he), "schedule") && he->stat.period == 100);
520 
521 	node = rb_next(node);
522 	he = rb_entry(node, struct hist_entry, rb_node);
523 	TEST_ASSERT_VAL("Invalid hist entry",
524 			CPU(he) == 2 && PID(he) == 200 &&
525 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
526 			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);
527 
528 	node = rb_next(node);
529 	he = rb_entry(node, struct hist_entry, rb_node);
530 	TEST_ASSERT_VAL("Invalid hist entry",
531 			CPU(he) == 1 && PID(he) == 300 &&
532 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
533 			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);
534 
535 	node = rb_next(node);
536 	he = rb_entry(node, struct hist_entry, rb_node);
537 	TEST_ASSERT_VAL("Invalid hist entry",
538 			CPU(he) == 0 && PID(he) == 300 &&
539 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
540 			!strcmp(SYM(he), "xmalloc") && he->stat.period == 100);
541 
542 	node = rb_next(node);
543 	he = rb_entry(node, struct hist_entry, rb_node);
544 	TEST_ASSERT_VAL("Invalid hist entry",
545 			CPU(he) == 3 && PID(he) == 300 &&
546 			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
547 			!strcmp(SYM(he), "main") && he->stat.period == 100);
548 
549 	node = rb_next(node);
550 	he = rb_entry(node, struct hist_entry, rb_node);
551 	TEST_ASSERT_VAL("Invalid hist entry",
552 			CPU(he) == 1 && PID(he) == 100 &&
553 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
554 			!strcmp(SYM(he), "malloc") && he->stat.period == 100);
555 
556 	node = rb_next(node);
557 	he = rb_entry(node, struct hist_entry, rb_node);
558 	TEST_ASSERT_VAL("Invalid hist entry",
559 			CPU(he) == 2 && PID(he) == 100 &&
560 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
561 			!strcmp(SYM(he), "free") && he->stat.period == 100);
562 
563 	node = rb_next(node);
564 	he = rb_entry(node, struct hist_entry, rb_node);
565 	TEST_ASSERT_VAL("Invalid hist entry",
566 			CPU(he) == 1 && PID(he) == 100 &&
567 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
568 			!strcmp(SYM(he), "cmd_record") && he->stat.period == 100);
569 
570 	node = rb_next(node);
571 	he = rb_entry(node, struct hist_entry, rb_node);
572 	TEST_ASSERT_VAL("Invalid hist entry",
573 			CPU(he) == 1 && PID(he) == 100 &&
574 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
575 			!strcmp(SYM(he), "main") && he->stat.period == 100);
576 
577 	node = rb_next(node);
578 	he = rb_entry(node, struct hist_entry, rb_node);
579 	TEST_ASSERT_VAL("Invalid hist entry",
580 			CPU(he) == 2 && PID(he) == 200 &&
581 			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
582 			!strcmp(SYM(he), "main") && he->stat.period == 100);
583 
584 out:
585 	del_hist_entries(hists);
586 	reset_output_field();
587 	return err;
588 }
589 
590 static int test__hists_output(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
591 {
592 	int err = TEST_FAIL;
593 	struct machines machines = { 0 };
594 	struct machine *machine;
595 	struct evsel *evsel;
596 	struct evlist *evlist = evlist__new();
597 	size_t i;
598 	test_fn_t testcases[] = {
599 		test1,
600 		test2,
601 		test3,
602 		test4,
603 		test5,
604 	};
605 
606 	TEST_ASSERT_VAL("No memory", evlist);
607 
608 	err = parse_event(evlist, "cpu-clock");
609 	if (err)
610 		goto out;
611 	err = TEST_FAIL;
612 
613 	if (machines__init(&machines))
614 		goto out;
615 
616 	/* setup threads/dso/map/symbols also */
617 	machine = setup_fake_machine(&machines);
618 	if (!machine)
619 		goto out;
620 
621 	if (verbose > 1)
622 		machine__fprintf(machine, stderr);
623 
624 	evsel = evlist__first(evlist);
625 
626 	for (i = 0; i < ARRAY_SIZE(testcases); i++) {
627 		err = testcases[i](evsel, machine);
628 		if (err < 0)
629 			break;
630 	}
631 
632 out:
633 	/* tear down everything */
634 	evlist__delete(evlist);
635 	machines__exit(&machines);
636 	put_fake_samples();
637 
638 	return err;
639 }
640 
641 DEFINE_SUITE("Sort output of hist entries", hists_output);
642