hists_cumulate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. #include "perf.h"
  2. #include "util/debug.h"
  3. #include "util/symbol.h"
  4. #include "util/sort.h"
  5. #include "util/evsel.h"
  6. #include "util/evlist.h"
  7. #include "util/machine.h"
  8. #include "util/thread.h"
  9. #include "util/parse-events.h"
  10. #include "tests/tests.h"
  11. #include "tests/hists_common.h"
  12. struct sample {
  13. u32 pid;
  14. u64 ip;
  15. struct thread *thread;
  16. struct map *map;
  17. struct symbol *sym;
  18. };
  19. /* For the numbers, see hists_common.c */
  20. static struct sample fake_samples[] = {
  21. /* perf [kernel] schedule() */
  22. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
  23. /* perf [perf] main() */
  24. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
  25. /* perf [perf] cmd_record() */
  26. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
  27. /* perf [libc] malloc() */
  28. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
  29. /* perf [libc] free() */
  30. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
  31. /* perf [perf] main() */
  32. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
  33. /* perf [kernel] page_fault() */
  34. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
  35. /* bash [bash] main() */
  36. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, },
  37. /* bash [bash] xmalloc() */
  38. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
  39. /* bash [kernel] page_fault() */
  40. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
  41. };
  42. /*
  43. * Will be casted to struct ip_callchain which has all 64 bit entries
  44. * of nr and ips[].
  45. */
  46. static u64 fake_callchains[][10] = {
  47. /* schedule => run_command => main */
  48. { 3, FAKE_IP_KERNEL_SCHEDULE, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
  49. /* main */
  50. { 1, FAKE_IP_PERF_MAIN, },
  51. /* cmd_record => run_command => main */
  52. { 3, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
  53. /* malloc => cmd_record => run_command => main */
  54. { 4, FAKE_IP_LIBC_MALLOC, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
  55. FAKE_IP_PERF_MAIN, },
  56. /* free => cmd_record => run_command => main */
  57. { 4, FAKE_IP_LIBC_FREE, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
  58. FAKE_IP_PERF_MAIN, },
  59. /* main */
  60. { 1, FAKE_IP_PERF_MAIN, },
  61. /* page_fault => sys_perf_event_open => run_command => main */
  62. { 4, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN,
  63. FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
  64. /* main */
  65. { 1, FAKE_IP_BASH_MAIN, },
  66. /* xmalloc => malloc => xmalloc => malloc => xmalloc => main */
  67. { 6, FAKE_IP_BASH_XMALLOC, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC,
  68. FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC, FAKE_IP_BASH_MAIN, },
  69. /* page_fault => malloc => main */
  70. { 3, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_MAIN, },
  71. };
  72. static int add_hist_entries(struct hists *hists, struct machine *machine)
  73. {
  74. struct addr_location al;
  75. struct perf_evsel *evsel = hists_to_evsel(hists);
  76. struct perf_sample sample = { .period = 1000, };
  77. size_t i;
  78. for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
  79. const union perf_event event = {
  80. .header = {
  81. .misc = PERF_RECORD_MISC_USER,
  82. },
  83. };
  84. struct hist_entry_iter iter = {
  85. .evsel = evsel,
  86. .sample = &sample,
  87. .hide_unresolved = false,
  88. };
  89. if (symbol_conf.cumulate_callchain)
  90. iter.ops = &hist_iter_cumulative;
  91. else
  92. iter.ops = &hist_iter_normal;
  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 (perf_event__preprocess_sample(&event, machine, &al,
  98. &sample) < 0)
  99. goto out;
  100. if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH,
  101. NULL) < 0) {
  102. addr_location__put(&al);
  103. goto out;
  104. }
  105. fake_samples[i].thread = al.thread;
  106. fake_samples[i].map = al.map;
  107. fake_samples[i].sym = al.sym;
  108. }
  109. return TEST_OK;
  110. out:
  111. pr_debug("Not enough memory for adding a hist entry\n");
  112. return TEST_FAIL;
  113. }
  114. static void del_hist_entries(struct hists *hists)
  115. {
  116. struct hist_entry *he;
  117. struct rb_root *root_in;
  118. struct rb_root *root_out;
  119. struct rb_node *node;
  120. if (sort__need_collapse)
  121. root_in = &hists->entries_collapsed;
  122. else
  123. root_in = hists->entries_in;
  124. root_out = &hists->entries;
  125. while (!RB_EMPTY_ROOT(root_out)) {
  126. node = rb_first(root_out);
  127. he = rb_entry(node, struct hist_entry, rb_node);
  128. rb_erase(node, root_out);
  129. rb_erase(&he->rb_node_in, root_in);
  130. hist_entry__delete(he);
  131. }
  132. }
  133. typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);
  134. #define COMM(he) (thread__comm_str(he->thread))
  135. #define DSO(he) (he->ms.map->dso->short_name)
  136. #define SYM(he) (he->ms.sym->name)
  137. #define CPU(he) (he->cpu)
  138. #define PID(he) (he->thread->tid)
  139. #define DEPTH(he) (he->callchain->max_depth)
  140. #define CDSO(cl) (cl->ms.map->dso->short_name)
  141. #define CSYM(cl) (cl->ms.sym->name)
  142. struct result {
  143. u64 children;
  144. u64 self;
  145. const char *comm;
  146. const char *dso;
  147. const char *sym;
  148. };
  149. struct callchain_result {
  150. u64 nr;
  151. struct {
  152. const char *dso;
  153. const char *sym;
  154. } node[10];
  155. };
  156. static int do_test(struct hists *hists, struct result *expected, size_t nr_expected,
  157. struct callchain_result *expected_callchain, size_t nr_callchain)
  158. {
  159. char buf[32];
  160. size_t i, c;
  161. struct hist_entry *he;
  162. struct rb_root *root;
  163. struct rb_node *node;
  164. struct callchain_node *cnode;
  165. struct callchain_list *clist;
  166. /*
  167. * adding and deleting hist entries must be done outside of this
  168. * function since TEST_ASSERT_VAL() returns in case of failure.
  169. */
  170. hists__collapse_resort(hists, NULL);
  171. hists__output_resort(hists, NULL);
  172. if (verbose > 2) {
  173. pr_info("use callchain: %d, cumulate callchain: %d\n",
  174. symbol_conf.use_callchain,
  175. symbol_conf.cumulate_callchain);
  176. print_hists_out(hists);
  177. }
  178. root = &hists->entries;
  179. for (node = rb_first(root), i = 0;
  180. node && (he = rb_entry(node, struct hist_entry, rb_node));
  181. node = rb_next(node), i++) {
  182. scnprintf(buf, sizeof(buf), "Invalid hist entry #%zd", i);
  183. TEST_ASSERT_VAL("Incorrect number of hist entry",
  184. i < nr_expected);
  185. TEST_ASSERT_VAL(buf, he->stat.period == expected[i].self &&
  186. !strcmp(COMM(he), expected[i].comm) &&
  187. !strcmp(DSO(he), expected[i].dso) &&
  188. !strcmp(SYM(he), expected[i].sym));
  189. if (symbol_conf.cumulate_callchain)
  190. TEST_ASSERT_VAL(buf, he->stat_acc->period == expected[i].children);
  191. if (!symbol_conf.use_callchain)
  192. continue;
  193. /* check callchain entries */
  194. root = &he->callchain->node.rb_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 perf_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. setup_sorting();
  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. setup_sorting();
  390. callchain_register_param(&callchain_param);
  391. err = add_hist_entries(hists, machine);
  392. if (err < 0)
  393. goto out;
  394. err = do_test(hists, expected, ARRAY_SIZE(expected),
  395. expected_callchain, ARRAY_SIZE(expected_callchain));
  396. out:
  397. del_hist_entries(hists);
  398. reset_output_field();
  399. return err;
  400. }
  401. /* NO callchain + children */
  402. static int test3(struct perf_evsel *evsel, struct machine *machine)
  403. {
  404. int err;
  405. struct hists *hists = evsel__hists(evsel);
  406. /*
  407. * expected output:
  408. *
  409. * Children Self Command Shared Object Symbol
  410. * ======== ======== ======= ============= =======================
  411. * 70.00% 20.00% perf perf [.] main
  412. * 50.00% 0.00% perf perf [.] run_command
  413. * 30.00% 10.00% bash bash [.] main
  414. * 30.00% 10.00% perf perf [.] cmd_record
  415. * 20.00% 0.00% bash libc [.] malloc
  416. * 10.00% 10.00% bash [kernel] [k] page_fault
  417. * 10.00% 10.00% bash bash [.] xmalloc
  418. * 10.00% 10.00% perf [kernel] [k] page_fault
  419. * 10.00% 10.00% perf libc [.] malloc
  420. * 10.00% 10.00% perf [kernel] [k] schedule
  421. * 10.00% 10.00% perf libc [.] free
  422. * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
  423. */
  424. struct result expected[] = {
  425. { 7000, 2000, "perf", "perf", "main" },
  426. { 5000, 0, "perf", "perf", "run_command" },
  427. { 3000, 1000, "bash", "bash", "main" },
  428. { 3000, 1000, "perf", "perf", "cmd_record" },
  429. { 2000, 0, "bash", "libc", "malloc" },
  430. { 1000, 1000, "bash", "[kernel]", "page_fault" },
  431. { 1000, 1000, "bash", "bash", "xmalloc" },
  432. { 1000, 1000, "perf", "[kernel]", "page_fault" },
  433. { 1000, 1000, "perf", "[kernel]", "schedule" },
  434. { 1000, 1000, "perf", "libc", "free" },
  435. { 1000, 1000, "perf", "libc", "malloc" },
  436. { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
  437. };
  438. symbol_conf.use_callchain = false;
  439. symbol_conf.cumulate_callchain = true;
  440. setup_sorting();
  441. callchain_register_param(&callchain_param);
  442. err = add_hist_entries(hists, machine);
  443. if (err < 0)
  444. goto out;
  445. err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
  446. out:
  447. del_hist_entries(hists);
  448. reset_output_field();
  449. return err;
  450. }
  451. /* callchain + children */
  452. static int test4(struct perf_evsel *evsel, struct machine *machine)
  453. {
  454. int err;
  455. struct hists *hists = evsel__hists(evsel);
  456. /*
  457. * expected output:
  458. *
  459. * Children Self Command Shared Object Symbol
  460. * ======== ======== ======= ============= =======================
  461. * 70.00% 20.00% perf perf [.] main
  462. * |
  463. * --- main
  464. *
  465. * 50.00% 0.00% perf perf [.] run_command
  466. * |
  467. * --- run_command
  468. * main
  469. *
  470. * 30.00% 10.00% bash bash [.] main
  471. * |
  472. * --- main
  473. *
  474. * 30.00% 10.00% perf perf [.] cmd_record
  475. * |
  476. * --- cmd_record
  477. * run_command
  478. * main
  479. *
  480. * 20.00% 0.00% bash libc [.] malloc
  481. * |
  482. * --- malloc
  483. * |
  484. * |--50.00%-- xmalloc
  485. * | main
  486. * --50.00%-- main
  487. *
  488. * 10.00% 10.00% bash [kernel] [k] page_fault
  489. * |
  490. * --- page_fault
  491. * malloc
  492. * main
  493. *
  494. * 10.00% 10.00% bash bash [.] xmalloc
  495. * |
  496. * --- xmalloc
  497. * malloc
  498. * xmalloc <--- NOTE: there's a cycle
  499. * malloc
  500. * xmalloc
  501. * main
  502. *
  503. * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
  504. * |
  505. * --- sys_perf_event_open
  506. * run_command
  507. * main
  508. *
  509. * 10.00% 10.00% perf [kernel] [k] page_fault
  510. * |
  511. * --- page_fault
  512. * sys_perf_event_open
  513. * run_command
  514. * main
  515. *
  516. * 10.00% 10.00% perf [kernel] [k] schedule
  517. * |
  518. * --- schedule
  519. * run_command
  520. * main
  521. *
  522. * 10.00% 10.00% perf libc [.] free
  523. * |
  524. * --- free
  525. * cmd_record
  526. * run_command
  527. * main
  528. *
  529. * 10.00% 10.00% perf libc [.] malloc
  530. * |
  531. * --- malloc
  532. * cmd_record
  533. * run_command
  534. * main
  535. *
  536. */
  537. struct result expected[] = {
  538. { 7000, 2000, "perf", "perf", "main" },
  539. { 5000, 0, "perf", "perf", "run_command" },
  540. { 3000, 1000, "bash", "bash", "main" },
  541. { 3000, 1000, "perf", "perf", "cmd_record" },
  542. { 2000, 0, "bash", "libc", "malloc" },
  543. { 1000, 1000, "bash", "[kernel]", "page_fault" },
  544. { 1000, 1000, "bash", "bash", "xmalloc" },
  545. { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
  546. { 1000, 1000, "perf", "[kernel]", "page_fault" },
  547. { 1000, 1000, "perf", "[kernel]", "schedule" },
  548. { 1000, 1000, "perf", "libc", "free" },
  549. { 1000, 1000, "perf", "libc", "malloc" },
  550. };
  551. struct callchain_result expected_callchain[] = {
  552. {
  553. 1, { { "perf", "main" }, },
  554. },
  555. {
  556. 2, { { "perf", "run_command" },
  557. { "perf", "main" }, },
  558. },
  559. {
  560. 1, { { "bash", "main" }, },
  561. },
  562. {
  563. 3, { { "perf", "cmd_record" },
  564. { "perf", "run_command" },
  565. { "perf", "main" }, },
  566. },
  567. {
  568. 4, { { "libc", "malloc" },
  569. { "bash", "xmalloc" },
  570. { "bash", "main" },
  571. { "bash", "main" }, },
  572. },
  573. {
  574. 3, { { "[kernel]", "page_fault" },
  575. { "libc", "malloc" },
  576. { "bash", "main" }, },
  577. },
  578. {
  579. 6, { { "bash", "xmalloc" },
  580. { "libc", "malloc" },
  581. { "bash", "xmalloc" },
  582. { "libc", "malloc" },
  583. { "bash", "xmalloc" },
  584. { "bash", "main" }, },
  585. },
  586. {
  587. 3, { { "[kernel]", "sys_perf_event_open" },
  588. { "perf", "run_command" },
  589. { "perf", "main" }, },
  590. },
  591. {
  592. 4, { { "[kernel]", "page_fault" },
  593. { "[kernel]", "sys_perf_event_open" },
  594. { "perf", "run_command" },
  595. { "perf", "main" }, },
  596. },
  597. {
  598. 3, { { "[kernel]", "schedule" },
  599. { "perf", "run_command" },
  600. { "perf", "main" }, },
  601. },
  602. {
  603. 4, { { "libc", "free" },
  604. { "perf", "cmd_record" },
  605. { "perf", "run_command" },
  606. { "perf", "main" }, },
  607. },
  608. {
  609. 4, { { "libc", "malloc" },
  610. { "perf", "cmd_record" },
  611. { "perf", "run_command" },
  612. { "perf", "main" }, },
  613. },
  614. };
  615. symbol_conf.use_callchain = true;
  616. symbol_conf.cumulate_callchain = true;
  617. setup_sorting();
  618. callchain_register_param(&callchain_param);
  619. err = add_hist_entries(hists, machine);
  620. if (err < 0)
  621. goto out;
  622. err = do_test(hists, expected, ARRAY_SIZE(expected),
  623. expected_callchain, ARRAY_SIZE(expected_callchain));
  624. out:
  625. del_hist_entries(hists);
  626. reset_output_field();
  627. return err;
  628. }
  629. int test__hists_cumulate(void)
  630. {
  631. int err = TEST_FAIL;
  632. struct machines machines;
  633. struct machine *machine;
  634. struct perf_evsel *evsel;
  635. struct perf_evlist *evlist = perf_evlist__new();
  636. size_t i;
  637. test_fn_t testcases[] = {
  638. test1,
  639. test2,
  640. test3,
  641. test4,
  642. };
  643. TEST_ASSERT_VAL("No memory", evlist);
  644. err = parse_events(evlist, "cpu-clock", NULL);
  645. if (err)
  646. goto out;
  647. machines__init(&machines);
  648. /* setup threads/dso/map/symbols also */
  649. machine = setup_fake_machine(&machines);
  650. if (!machine)
  651. goto out;
  652. if (verbose > 1)
  653. machine__fprintf(machine, stderr);
  654. evsel = perf_evlist__first(evlist);
  655. for (i = 0; i < ARRAY_SIZE(testcases); i++) {
  656. err = testcases[i](evsel, machine);
  657. if (err < 0)
  658. break;
  659. }
  660. out:
  661. /* tear down everything */
  662. perf_evlist__delete(evlist);
  663. machines__exit(&machines);
  664. return err;
  665. }