hists_cumulate.c 20 KB

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