hists_cumulate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. #include <linux/kernel.h>
  13. struct sample {
  14. u32 pid;
  15. u64 ip;
  16. struct thread *thread;
  17. struct map *map;
  18. struct symbol *sym;
  19. };
  20. /* For the numbers, see hists_common.c */
  21. static struct sample fake_samples[] = {
  22. /* perf [kernel] schedule() */
  23. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
  24. /* perf [perf] main() */
  25. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, },
  26. /* perf [perf] cmd_record() */
  27. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, },
  28. /* perf [libc] malloc() */
  29. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
  30. /* perf [libc] free() */
  31. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, },
  32. /* perf [perf] main() */
  33. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
  34. /* perf [kernel] page_fault() */
  35. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
  36. /* bash [bash] main() */
  37. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, },
  38. /* bash [bash] xmalloc() */
  39. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
  40. /* bash [kernel] page_fault() */
  41. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
  42. };
  43. /*
  44. * Will be casted to struct ip_callchain which has all 64 bit entries
  45. * of nr and ips[].
  46. */
  47. static u64 fake_callchains[][10] = {
  48. /* schedule => run_command => main */
  49. { 3, FAKE_IP_KERNEL_SCHEDULE, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
  50. /* main */
  51. { 1, FAKE_IP_PERF_MAIN, },
  52. /* cmd_record => run_command => main */
  53. { 3, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
  54. /* malloc => cmd_record => run_command => main */
  55. { 4, FAKE_IP_LIBC_MALLOC, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
  56. FAKE_IP_PERF_MAIN, },
  57. /* free => cmd_record => run_command => main */
  58. { 4, FAKE_IP_LIBC_FREE, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND,
  59. FAKE_IP_PERF_MAIN, },
  60. /* main */
  61. { 1, FAKE_IP_PERF_MAIN, },
  62. /* page_fault => sys_perf_event_open => run_command => main */
  63. { 4, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN,
  64. FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, },
  65. /* main */
  66. { 1, FAKE_IP_BASH_MAIN, },
  67. /* xmalloc => malloc => xmalloc => malloc => xmalloc => main */
  68. { 6, FAKE_IP_BASH_XMALLOC, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC,
  69. FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC, FAKE_IP_BASH_MAIN, },
  70. /* page_fault => malloc => main */
  71. { 3, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_MAIN, },
  72. };
  73. static int add_hist_entries(struct hists *hists, struct machine *machine)
  74. {
  75. struct addr_location al;
  76. struct perf_evsel *evsel = hists_to_evsel(hists);
  77. struct perf_sample sample = { .period = 1000, };
  78. size_t i;
  79. for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
  80. struct hist_entry_iter iter = {
  81. .evsel = evsel,
  82. .sample = &sample,
  83. .hide_unresolved = false,
  84. };
  85. if (symbol_conf.cumulate_callchain)
  86. iter.ops = &hist_iter_cumulative;
  87. else
  88. iter.ops = &hist_iter_normal;
  89. sample.cpumode = PERF_RECORD_MISC_USER;
  90. sample.pid = fake_samples[i].pid;
  91. sample.tid = fake_samples[i].pid;
  92. sample.ip = fake_samples[i].ip;
  93. sample.callchain = (struct ip_callchain *)fake_callchains[i];
  94. if (machine__resolve(machine, &al, &sample) < 0)
  95. goto out;
  96. if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack,
  97. NULL) < 0) {
  98. addr_location__put(&al);
  99. goto out;
  100. }
  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 (hists__has(hists, 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. perf_evsel__output_resort(hists_to_evsel(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. TEST_ASSERT_VAL("callchains expected", !RB_EMPTY_ROOT(root));
  192. cnode = rb_entry(rb_first(root), struct callchain_node, rb_node);
  193. c = 0;
  194. list_for_each_entry(clist, &cnode->val, list) {
  195. scnprintf(buf, sizeof(buf), "Invalid callchain entry #%zd/%zd", i, c);
  196. TEST_ASSERT_VAL("Incorrect number of callchain entry",
  197. c < expected_callchain[i].nr);
  198. TEST_ASSERT_VAL(buf,
  199. !strcmp(CDSO(clist), expected_callchain[i].node[c].dso) &&
  200. !strcmp(CSYM(clist), expected_callchain[i].node[c].sym));
  201. c++;
  202. }
  203. /* TODO: handle multiple child nodes properly */
  204. TEST_ASSERT_VAL("Incorrect number of callchain entry",
  205. c <= expected_callchain[i].nr);
  206. }
  207. TEST_ASSERT_VAL("Incorrect number of hist entry",
  208. i == nr_expected);
  209. TEST_ASSERT_VAL("Incorrect number of callchain entry",
  210. !symbol_conf.use_callchain || nr_expected == nr_callchain);
  211. return 0;
  212. }
  213. /* NO callchain + NO children */
  214. static int test1(struct perf_evsel *evsel, struct machine *machine)
  215. {
  216. int err;
  217. struct hists *hists = evsel__hists(evsel);
  218. /*
  219. * expected output:
  220. *
  221. * Overhead Command Shared Object Symbol
  222. * ======== ======= ============= ==============
  223. * 20.00% perf perf [.] main
  224. * 10.00% bash [kernel] [k] page_fault
  225. * 10.00% bash bash [.] main
  226. * 10.00% bash bash [.] xmalloc
  227. * 10.00% perf [kernel] [k] page_fault
  228. * 10.00% perf [kernel] [k] schedule
  229. * 10.00% perf libc [.] free
  230. * 10.00% perf libc [.] malloc
  231. * 10.00% perf perf [.] cmd_record
  232. */
  233. struct result expected[] = {
  234. { 0, 2000, "perf", "perf", "main" },
  235. { 0, 1000, "bash", "[kernel]", "page_fault" },
  236. { 0, 1000, "bash", "bash", "main" },
  237. { 0, 1000, "bash", "bash", "xmalloc" },
  238. { 0, 1000, "perf", "[kernel]", "page_fault" },
  239. { 0, 1000, "perf", "[kernel]", "schedule" },
  240. { 0, 1000, "perf", "libc", "free" },
  241. { 0, 1000, "perf", "libc", "malloc" },
  242. { 0, 1000, "perf", "perf", "cmd_record" },
  243. };
  244. symbol_conf.use_callchain = false;
  245. symbol_conf.cumulate_callchain = false;
  246. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  247. setup_sorting(NULL);
  248. callchain_register_param(&callchain_param);
  249. err = add_hist_entries(hists, machine);
  250. if (err < 0)
  251. goto out;
  252. err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
  253. out:
  254. del_hist_entries(hists);
  255. reset_output_field();
  256. return err;
  257. }
  258. /* callcain + NO children */
  259. static int test2(struct perf_evsel *evsel, struct machine *machine)
  260. {
  261. int err;
  262. struct hists *hists = evsel__hists(evsel);
  263. /*
  264. * expected output:
  265. *
  266. * Overhead Command Shared Object Symbol
  267. * ======== ======= ============= ==============
  268. * 20.00% perf perf [.] main
  269. * |
  270. * --- main
  271. *
  272. * 10.00% bash [kernel] [k] page_fault
  273. * |
  274. * --- page_fault
  275. * malloc
  276. * main
  277. *
  278. * 10.00% bash bash [.] main
  279. * |
  280. * --- main
  281. *
  282. * 10.00% bash bash [.] xmalloc
  283. * |
  284. * --- xmalloc
  285. * malloc
  286. * xmalloc <--- NOTE: there's a cycle
  287. * malloc
  288. * xmalloc
  289. * main
  290. *
  291. * 10.00% perf [kernel] [k] page_fault
  292. * |
  293. * --- page_fault
  294. * sys_perf_event_open
  295. * run_command
  296. * main
  297. *
  298. * 10.00% perf [kernel] [k] schedule
  299. * |
  300. * --- schedule
  301. * run_command
  302. * main
  303. *
  304. * 10.00% perf libc [.] free
  305. * |
  306. * --- free
  307. * cmd_record
  308. * run_command
  309. * main
  310. *
  311. * 10.00% perf libc [.] malloc
  312. * |
  313. * --- malloc
  314. * cmd_record
  315. * run_command
  316. * main
  317. *
  318. * 10.00% perf perf [.] cmd_record
  319. * |
  320. * --- cmd_record
  321. * run_command
  322. * main
  323. *
  324. */
  325. struct result expected[] = {
  326. { 0, 2000, "perf", "perf", "main" },
  327. { 0, 1000, "bash", "[kernel]", "page_fault" },
  328. { 0, 1000, "bash", "bash", "main" },
  329. { 0, 1000, "bash", "bash", "xmalloc" },
  330. { 0, 1000, "perf", "[kernel]", "page_fault" },
  331. { 0, 1000, "perf", "[kernel]", "schedule" },
  332. { 0, 1000, "perf", "libc", "free" },
  333. { 0, 1000, "perf", "libc", "malloc" },
  334. { 0, 1000, "perf", "perf", "cmd_record" },
  335. };
  336. struct callchain_result expected_callchain[] = {
  337. {
  338. 1, { { "perf", "main" }, },
  339. },
  340. {
  341. 3, { { "[kernel]", "page_fault" },
  342. { "libc", "malloc" },
  343. { "bash", "main" }, },
  344. },
  345. {
  346. 1, { { "bash", "main" }, },
  347. },
  348. {
  349. 6, { { "bash", "xmalloc" },
  350. { "libc", "malloc" },
  351. { "bash", "xmalloc" },
  352. { "libc", "malloc" },
  353. { "bash", "xmalloc" },
  354. { "bash", "main" }, },
  355. },
  356. {
  357. 4, { { "[kernel]", "page_fault" },
  358. { "[kernel]", "sys_perf_event_open" },
  359. { "perf", "run_command" },
  360. { "perf", "main" }, },
  361. },
  362. {
  363. 3, { { "[kernel]", "schedule" },
  364. { "perf", "run_command" },
  365. { "perf", "main" }, },
  366. },
  367. {
  368. 4, { { "libc", "free" },
  369. { "perf", "cmd_record" },
  370. { "perf", "run_command" },
  371. { "perf", "main" }, },
  372. },
  373. {
  374. 4, { { "libc", "malloc" },
  375. { "perf", "cmd_record" },
  376. { "perf", "run_command" },
  377. { "perf", "main" }, },
  378. },
  379. {
  380. 3, { { "perf", "cmd_record" },
  381. { "perf", "run_command" },
  382. { "perf", "main" }, },
  383. },
  384. };
  385. symbol_conf.use_callchain = true;
  386. symbol_conf.cumulate_callchain = false;
  387. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  388. setup_sorting(NULL);
  389. callchain_register_param(&callchain_param);
  390. err = add_hist_entries(hists, machine);
  391. if (err < 0)
  392. goto out;
  393. err = do_test(hists, expected, ARRAY_SIZE(expected),
  394. expected_callchain, ARRAY_SIZE(expected_callchain));
  395. out:
  396. del_hist_entries(hists);
  397. reset_output_field();
  398. return err;
  399. }
  400. /* NO callchain + children */
  401. static int test3(struct perf_evsel *evsel, struct machine *machine)
  402. {
  403. int err;
  404. struct hists *hists = evsel__hists(evsel);
  405. /*
  406. * expected output:
  407. *
  408. * Children Self Command Shared Object Symbol
  409. * ======== ======== ======= ============= =======================
  410. * 70.00% 20.00% perf perf [.] main
  411. * 50.00% 0.00% perf perf [.] run_command
  412. * 30.00% 10.00% bash bash [.] main
  413. * 30.00% 10.00% perf perf [.] cmd_record
  414. * 20.00% 0.00% bash libc [.] malloc
  415. * 10.00% 10.00% bash [kernel] [k] page_fault
  416. * 10.00% 10.00% bash bash [.] xmalloc
  417. * 10.00% 10.00% perf [kernel] [k] page_fault
  418. * 10.00% 10.00% perf libc [.] malloc
  419. * 10.00% 10.00% perf [kernel] [k] schedule
  420. * 10.00% 10.00% perf libc [.] free
  421. * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
  422. */
  423. struct result expected[] = {
  424. { 7000, 2000, "perf", "perf", "main" },
  425. { 5000, 0, "perf", "perf", "run_command" },
  426. { 3000, 1000, "bash", "bash", "main" },
  427. { 3000, 1000, "perf", "perf", "cmd_record" },
  428. { 2000, 0, "bash", "libc", "malloc" },
  429. { 1000, 1000, "bash", "[kernel]", "page_fault" },
  430. { 1000, 1000, "bash", "bash", "xmalloc" },
  431. { 1000, 1000, "perf", "[kernel]", "page_fault" },
  432. { 1000, 1000, "perf", "[kernel]", "schedule" },
  433. { 1000, 1000, "perf", "libc", "free" },
  434. { 1000, 1000, "perf", "libc", "malloc" },
  435. { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
  436. };
  437. symbol_conf.use_callchain = false;
  438. symbol_conf.cumulate_callchain = true;
  439. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  440. setup_sorting(NULL);
  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. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  618. setup_sorting(NULL);
  619. callchain_param = callchain_param_default;
  620. callchain_register_param(&callchain_param);
  621. err = add_hist_entries(hists, machine);
  622. if (err < 0)
  623. goto out;
  624. err = do_test(hists, expected, ARRAY_SIZE(expected),
  625. expected_callchain, ARRAY_SIZE(expected_callchain));
  626. out:
  627. del_hist_entries(hists);
  628. reset_output_field();
  629. return err;
  630. }
  631. int test__hists_cumulate(int subtest __maybe_unused)
  632. {
  633. int err = TEST_FAIL;
  634. struct machines machines;
  635. struct machine *machine;
  636. struct perf_evsel *evsel;
  637. struct perf_evlist *evlist = perf_evlist__new();
  638. size_t i;
  639. test_fn_t testcases[] = {
  640. test1,
  641. test2,
  642. test3,
  643. test4,
  644. };
  645. TEST_ASSERT_VAL("No memory", evlist);
  646. err = parse_events(evlist, "cpu-clock", NULL);
  647. if (err)
  648. goto out;
  649. err = TEST_FAIL;
  650. machines__init(&machines);
  651. /* setup threads/dso/map/symbols also */
  652. machine = setup_fake_machine(&machines);
  653. if (!machine)
  654. goto out;
  655. if (verbose > 1)
  656. machine__fprintf(machine, stderr);
  657. evsel = perf_evlist__first(evlist);
  658. for (i = 0; i < ARRAY_SIZE(testcases); i++) {
  659. err = testcases[i](evsel, machine);
  660. if (err < 0)
  661. break;
  662. }
  663. out:
  664. /* tear down everything */
  665. perf_evlist__delete(evlist);
  666. machines__exit(&machines);
  667. return err;
  668. }