hists_output.c 19 KB

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