hists_cumulate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  250. setup_sorting();
  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 perf_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. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  391. setup_sorting();
  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 perf_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. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  443. setup_sorting();
  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 perf_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. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  621. setup_sorting();
  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(void)
  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. machines__init(&machines);
  652. /* setup threads/dso/map/symbols also */
  653. machine = setup_fake_machine(&machines);
  654. if (!machine)
  655. goto out;
  656. if (verbose > 1)
  657. machine__fprintf(machine, stderr);
  658. evsel = perf_evlist__first(evlist);
  659. for (i = 0; i < ARRAY_SIZE(testcases); i++) {
  660. err = testcases[i](evsel, machine);
  661. if (err < 0)
  662. break;
  663. }
  664. out:
  665. /* tear down everything */
  666. perf_evlist__delete(evlist);
  667. machines__exit(&machines);
  668. return err;
  669. }