hists_output.c 18.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3
#include "perf.h"
#include "util/debug.h"
4
#include "util/event.h"
5 6 7 8 9 10 11 12 13
#include "util/symbol.h"
#include "util/sort.h"
#include "util/evsel.h"
#include "util/evlist.h"
#include "util/machine.h"
#include "util/thread.h"
#include "util/parse-events.h"
#include "tests/tests.h"
#include "tests/hists_common.h"
14
#include <linux/kernel.h>
15 16 17 18 19 20 21 22 23 24 25 26 27

struct sample {
	u32 cpu;
	u32 pid;
	u64 ip;
	struct thread *thread;
	struct map *map;
	struct symbol *sym;
};

/* For the numbers, see hists_common.c */
static struct sample fake_samples[] = {
	/* perf [kernel] schedule() */
28
	{ .cpu = 0, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
29
	/* perf [perf]   main() */
30
	{ .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
31
	/* perf [perf]   cmd_record() */
32
	{ .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
33
	/* perf [libc]   malloc() */
34
	{ .cpu = 1, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
35
	/* perf [libc]   free() */
36
	{ .cpu = 2, .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
37
	/* perf [perf]   main() */
38
	{ .cpu = 2, .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
39
	/* perf [kernel] page_fault() */
40
	{ .cpu = 2, .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
41
	/* bash [bash]   main() */
42
	{ .cpu = 3, .pid = FAKE_PID_BASH,  .ip = FAKE_IP_BASH_MAIN, },
43
	/* bash [bash]   xmalloc() */
44
	{ .cpu = 0, .pid = FAKE_PID_BASH,  .ip = FAKE_IP_BASH_XMALLOC, },
45
	/* bash [kernel] page_fault() */
46
	{ .cpu = 1, .pid = FAKE_PID_BASH,  .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
47 48 49 50 51
};

static int add_hist_entries(struct hists *hists, struct machine *machine)
{
	struct addr_location al;
52
	struct perf_evsel *evsel = hists_to_evsel(hists);
53 54 55 56
	struct perf_sample sample = { .period = 100, };
	size_t i;

	for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
57
		struct hist_entry_iter iter = {
58 59
			.evsel = evsel,
			.sample = &sample,
60 61 62
			.ops = &hist_iter_normal,
			.hide_unresolved = false,
		};
63

64
		sample.cpumode = PERF_RECORD_MISC_USER;
65 66 67 68 69
		sample.cpu = fake_samples[i].cpu;
		sample.pid = fake_samples[i].pid;
		sample.tid = fake_samples[i].pid;
		sample.ip = fake_samples[i].ip;

70
		if (machine__resolve(machine, &al, &sample) < 0)
71 72
			goto out;

73
		if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack,
74
					 NULL) < 0) {
75
			addr_location__put(&al);
76
			goto out;
77
		}
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

		fake_samples[i].thread = al.thread;
		fake_samples[i].map = al.map;
		fake_samples[i].sym = al.sym;
	}

	return TEST_OK;

out:
	pr_debug("Not enough memory for adding a hist entry\n");
	return TEST_FAIL;
}

static void del_hist_entries(struct hists *hists)
{
	struct hist_entry *he;
D
Davidlohr Bueso 已提交
94 95
	struct rb_root_cached *root_in;
	struct rb_root_cached *root_out;
96 97
	struct rb_node *node;

98
	if (hists__has(hists, need_collapse))
99 100 101 102 103 104
		root_in = &hists->entries_collapsed;
	else
		root_in = hists->entries_in;

	root_out = &hists->entries;

D
Davidlohr Bueso 已提交
105 106
	while (!RB_EMPTY_ROOT(&root_out->rb_root)) {
		node = rb_first_cached(root_out);
107 108

		he = rb_entry(node, struct hist_entry, rb_node);
D
Davidlohr Bueso 已提交
109 110
		rb_erase_cached(node, root_out);
		rb_erase_cached(&he->rb_node_in, root_in);
111
		hist_entry__delete(he);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	}
}

typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);

#define COMM(he)  (thread__comm_str(he->thread))
#define DSO(he)   (he->ms.map->dso->short_name)
#define SYM(he)   (he->ms.sym->name)
#define CPU(he)   (he->cpu)
#define PID(he)   (he->thread->tid)

/* default sort keys (no field) */
static int test1(struct perf_evsel *evsel, struct machine *machine)
{
	int err;
127
	struct hists *hists = evsel__hists(evsel);
128
	struct hist_entry *he;
D
Davidlohr Bueso 已提交
129
	struct rb_root_cached *root;
130 131 132 133 134
	struct rb_node *node;

	field_order = NULL;
	sort_order = NULL; /* equivalent to sort_order = "comm,dso,sym" */

135
	setup_sorting(NULL);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

	/*
	 * expected output:
	 *
	 * Overhead  Command  Shared Object          Symbol
	 * ========  =======  =============  ==============
	 *   20.00%     perf  perf           [.] main
	 *   10.00%     bash  [kernel]       [k] page_fault
	 *   10.00%     bash  bash           [.] main
	 *   10.00%     bash  bash           [.] xmalloc
	 *   10.00%     perf  [kernel]       [k] page_fault
	 *   10.00%     perf  [kernel]       [k] schedule
	 *   10.00%     perf  libc           [.] free
	 *   10.00%     perf  libc           [.] malloc
	 *   10.00%     perf  perf           [.] cmd_record
	 */
	err = add_hist_entries(hists, machine);
	if (err < 0)
		goto out;

	hists__collapse_resort(hists, NULL);
157
	perf_evsel__output_resort(evsel, NULL);
158 159 160 161 162 163

	if (verbose > 2) {
		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
		print_hists_out(hists);
	}

164
	root = &hists->entries;
D
Davidlohr Bueso 已提交
165
	node = rb_first_cached(root);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
			!strcmp(SYM(he), "main") && he->stat.period == 200);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
			!strcmp(SYM(he), "main") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
			!strcmp(SYM(he), "xmalloc") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
			!strcmp(SYM(he), "schedule") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
			!strcmp(SYM(he), "free") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
			!strcmp(SYM(he), "malloc") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
			!strcmp(SYM(he), "cmd_record") && he->stat.period == 100);

out:
	del_hist_entries(hists);
	reset_output_field();
	return err;
}

/* mixed fields and sort keys */
static int test2(struct perf_evsel *evsel, struct machine *machine)
{
	int err;
229
	struct hists *hists = evsel__hists(evsel);
230
	struct hist_entry *he;
D
Davidlohr Bueso 已提交
231
	struct rb_root_cached *root;
232 233 234 235 236
	struct rb_node *node;

	field_order = "overhead,cpu";
	sort_order = "pid";

237
	setup_sorting(NULL);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

	/*
	 * expected output:
	 *
	 * Overhead  CPU  Command:  Pid
	 * ========  ===  =============
	 *   30.00%    1  perf   :  100
	 *   10.00%    0  perf   :  100
	 *   10.00%    2  perf   :  100
	 *   20.00%    2  perf   :  200
	 *   10.00%    0  bash   :  300
	 *   10.00%    1  bash   :  300
	 *   10.00%    3  bash   :  300
	 */
	err = add_hist_entries(hists, machine);
	if (err < 0)
		goto out;

	hists__collapse_resort(hists, NULL);
257
	perf_evsel__output_resort(evsel, NULL);
258 259 260 261 262 263

	if (verbose > 2) {
		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
		print_hists_out(hists);
	}

264
	root = &hists->entries;
D
Davidlohr Bueso 已提交
265
	node = rb_first_cached(root);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 1 && PID(he) == 100 && he->stat.period == 300);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 0 && PID(he) == 100 && he->stat.period == 100);

out:
	del_hist_entries(hists);
	reset_output_field();
	return err;
}

/* fields only (no sort key) */
static int test3(struct perf_evsel *evsel, struct machine *machine)
{
	int err;
285
	struct hists *hists = evsel__hists(evsel);
286
	struct hist_entry *he;
D
Davidlohr Bueso 已提交
287
	struct rb_root_cached *root;
288 289 290 291 292
	struct rb_node *node;

	field_order = "comm,overhead,dso";
	sort_order = NULL;

293
	setup_sorting(NULL);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

	/*
	 * expected output:
	 *
	 * Command  Overhead  Shared Object
	 * =======  ========  =============
	 *    bash    20.00%  bash
	 *    bash    10.00%  [kernel]
	 *    perf    30.00%  perf
	 *    perf    20.00%  [kernel]
	 *    perf    20.00%  libc
	 */
	err = add_hist_entries(hists, machine);
	if (err < 0)
		goto out;

	hists__collapse_resort(hists, NULL);
311
	perf_evsel__output_resort(evsel, NULL);
312 313 314 315 316 317

	if (verbose > 2) {
		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
		print_hists_out(hists);
	}

318
	root = &hists->entries;
D
Davidlohr Bueso 已提交
319
	node = rb_first_cached(root);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
			he->stat.period == 200);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
			he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
			he->stat.period == 300);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
			he->stat.period == 200);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
			he->stat.period == 200);

out:
	del_hist_entries(hists);
	reset_output_field();
	return err;
}

/* handle duplicate 'dso' field */
static int test4(struct perf_evsel *evsel, struct machine *machine)
{
	int err;
359
	struct hists *hists = evsel__hists(evsel);
360
	struct hist_entry *he;
D
Davidlohr Bueso 已提交
361
	struct rb_root_cached *root;
362 363 364 365 366
	struct rb_node *node;

	field_order = "dso,sym,comm,overhead,dso";
	sort_order = "sym";

367
	setup_sorting(NULL);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

	/*
	 * expected output:
	 *
	 * Shared Object          Symbol  Command  Overhead
	 * =============  ==============  =======  ========
	 *          perf  [.] cmd_record     perf    10.00%
	 *          libc  [.] free           perf    10.00%
	 *          bash  [.] main           bash    10.00%
	 *          perf  [.] main           perf    20.00%
	 *          libc  [.] malloc         perf    10.00%
	 *      [kernel]  [k] page_fault     bash    10.00%
	 *      [kernel]  [k] page_fault     perf    10.00%
	 *      [kernel]  [k] schedule       perf    10.00%
	 *          bash  [.] xmalloc        bash    10.00%
	 */
	err = add_hist_entries(hists, machine);
	if (err < 0)
		goto out;

	hists__collapse_resort(hists, NULL);
389
	perf_evsel__output_resort(evsel, NULL);
390 391 392 393 394 395

	if (verbose > 2) {
		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
		print_hists_out(hists);
	}

396
	root = &hists->entries;
D
Davidlohr Bueso 已提交
397
	node = rb_first_cached(root);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "perf") && !strcmp(SYM(he), "cmd_record") &&
			!strcmp(COMM(he), "perf") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "libc") && !strcmp(SYM(he), "free") &&
			!strcmp(COMM(he), "perf") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "bash") && !strcmp(SYM(he), "main") &&
			!strcmp(COMM(he), "bash") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "perf") && !strcmp(SYM(he), "main") &&
			!strcmp(COMM(he), "perf") && he->stat.period == 200);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "libc") && !strcmp(SYM(he), "malloc") &&
			!strcmp(COMM(he), "perf") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "page_fault") &&
			!strcmp(COMM(he), "bash") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "page_fault") &&
			!strcmp(COMM(he), "perf") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "[kernel]") && !strcmp(SYM(he), "schedule") &&
			!strcmp(COMM(he), "perf") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			!strcmp(DSO(he), "bash") && !strcmp(SYM(he), "xmalloc") &&
			!strcmp(COMM(he), "bash") && he->stat.period == 100);

out:
	del_hist_entries(hists);
	reset_output_field();
	return err;
}

/* full sort keys w/o overhead field */
static int test5(struct perf_evsel *evsel, struct machine *machine)
{
	int err;
461
	struct hists *hists = evsel__hists(evsel);
462
	struct hist_entry *he;
D
Davidlohr Bueso 已提交
463
	struct rb_root_cached *root;
464 465 466 467 468
	struct rb_node *node;

	field_order = "cpu,pid,comm,dso,sym";
	sort_order = "dso,pid";

469
	setup_sorting(NULL);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

	/*
	 * expected output:
	 *
	 * CPU  Command:  Pid  Command  Shared Object          Symbol
	 * ===  =============  =======  =============  ==============
	 *   0     perf:  100     perf       [kernel]  [k] schedule
	 *   2     perf:  200     perf       [kernel]  [k] page_fault
	 *   1     bash:  300     bash       [kernel]  [k] page_fault
	 *   0     bash:  300     bash           bash  [.] xmalloc
	 *   3     bash:  300     bash           bash  [.] main
	 *   1     perf:  100     perf           libc  [.] malloc
	 *   2     perf:  100     perf           libc  [.] free
	 *   1     perf:  100     perf           perf  [.] cmd_record
	 *   1     perf:  100     perf           perf  [.] main
	 *   2     perf:  200     perf           perf  [.] main
	 */
	err = add_hist_entries(hists, machine);
	if (err < 0)
		goto out;

	hists__collapse_resort(hists, NULL);
492
	perf_evsel__output_resort(evsel, NULL);
493 494 495 496 497 498

	if (verbose > 2) {
		pr_info("[fields = %s, sort = %s]\n", field_order, sort_order);
		print_hists_out(hists);
	}

499
	root = &hists->entries;
D
Davidlohr Bueso 已提交
500
	node = rb_first_cached(root);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	he = rb_entry(node, struct hist_entry, rb_node);

	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 0 && PID(he) == 100 &&
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
			!strcmp(SYM(he), "schedule") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 2 && PID(he) == 200 &&
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "[kernel]") &&
			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 1 && PID(he) == 300 &&
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "[kernel]") &&
			!strcmp(SYM(he), "page_fault") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 0 && PID(he) == 300 &&
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
			!strcmp(SYM(he), "xmalloc") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 3 && PID(he) == 300 &&
			!strcmp(COMM(he), "bash") && !strcmp(DSO(he), "bash") &&
			!strcmp(SYM(he), "main") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 1 && PID(he) == 100 &&
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
			!strcmp(SYM(he), "malloc") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 2 && PID(he) == 100 &&
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "libc") &&
			!strcmp(SYM(he), "free") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 1 && PID(he) == 100 &&
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
			!strcmp(SYM(he), "cmd_record") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 1 && PID(he) == 100 &&
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
			!strcmp(SYM(he), "main") && he->stat.period == 100);

	node = rb_next(node);
	he = rb_entry(node, struct hist_entry, rb_node);
	TEST_ASSERT_VAL("Invalid hist entry",
			CPU(he) == 2 && PID(he) == 200 &&
			!strcmp(COMM(he), "perf") && !strcmp(DSO(he), "perf") &&
			!strcmp(SYM(he), "main") && he->stat.period == 100);

out:
	del_hist_entries(hists);
	reset_output_field();
	return err;
}

577
int test__hists_output(struct test *test __maybe_unused, int subtest __maybe_unused)
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
{
	int err = TEST_FAIL;
	struct machines machines;
	struct machine *machine;
	struct perf_evsel *evsel;
	struct perf_evlist *evlist = perf_evlist__new();
	size_t i;
	test_fn_t testcases[] = {
		test1,
		test2,
		test3,
		test4,
		test5,
	};

	TEST_ASSERT_VAL("No memory", evlist);

595
	err = parse_events(evlist, "cpu-clock", NULL);
596 597
	if (err)
		goto out;
598
	err = TEST_FAIL;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

	machines__init(&machines);

	/* setup threads/dso/map/symbols also */
	machine = setup_fake_machine(&machines);
	if (!machine)
		goto out;

	if (verbose > 1)
		machine__fprintf(machine, stderr);

	evsel = perf_evlist__first(evlist);

	for (i = 0; i < ARRAY_SIZE(testcases); i++) {
		err = testcases[i](evsel, machine);
		if (err < 0)
			break;
	}

out:
	/* tear down everything */
	perf_evlist__delete(evlist);
	machines__exit(&machines);

	return err;
}