hists_cumulate.c 20 KB

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