hists_cumulate.c 20 KB

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