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. struct hist_entry_iter iter = {
  80. .evsel = evsel,
  81. .sample = &sample,
  82. .hide_unresolved = false,
  83. };
  84. if (symbol_conf.cumulate_callchain)
  85. iter.ops = &hist_iter_cumulative;
  86. else
  87. iter.ops = &hist_iter_normal;
  88. sample.cpumode = PERF_RECORD_MISC_USER;
  89. sample.pid = fake_samples[i].pid;
  90. sample.tid = fake_samples[i].pid;
  91. sample.ip = fake_samples[i].ip;
  92. sample.callchain = (struct ip_callchain *)fake_callchains[i];
  93. if (machine__resolve(machine, &al, &sample) < 0)
  94. goto out;
  95. if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH,
  96. NULL) < 0) {
  97. addr_location__put(&al);
  98. goto out;
  99. }
  100. fake_samples[i].thread = al.thread;
  101. fake_samples[i].map = al.map;
  102. fake_samples[i].sym = al.sym;
  103. }
  104. return TEST_OK;
  105. out:
  106. pr_debug("Not enough memory for adding a hist entry\n");
  107. return TEST_FAIL;
  108. }
  109. static void del_hist_entries(struct hists *hists)
  110. {
  111. struct hist_entry *he;
  112. struct rb_root *root_in;
  113. struct rb_root *root_out;
  114. struct rb_node *node;
  115. if (sort__need_collapse)
  116. root_in = &hists->entries_collapsed;
  117. else
  118. root_in = hists->entries_in;
  119. root_out = &hists->entries;
  120. while (!RB_EMPTY_ROOT(root_out)) {
  121. node = rb_first(root_out);
  122. he = rb_entry(node, struct hist_entry, rb_node);
  123. rb_erase(node, root_out);
  124. rb_erase(&he->rb_node_in, root_in);
  125. hist_entry__delete(he);
  126. }
  127. }
  128. typedef int (*test_fn_t)(struct perf_evsel *, struct machine *);
  129. #define COMM(he) (thread__comm_str(he->thread))
  130. #define DSO(he) (he->ms.map->dso->short_name)
  131. #define SYM(he) (he->ms.sym->name)
  132. #define CPU(he) (he->cpu)
  133. #define PID(he) (he->thread->tid)
  134. #define DEPTH(he) (he->callchain->max_depth)
  135. #define CDSO(cl) (cl->ms.map->dso->short_name)
  136. #define CSYM(cl) (cl->ms.sym->name)
  137. struct result {
  138. u64 children;
  139. u64 self;
  140. const char *comm;
  141. const char *dso;
  142. const char *sym;
  143. };
  144. struct callchain_result {
  145. u64 nr;
  146. struct {
  147. const char *dso;
  148. const char *sym;
  149. } node[10];
  150. };
  151. static int do_test(struct hists *hists, struct result *expected, size_t nr_expected,
  152. struct callchain_result *expected_callchain, size_t nr_callchain)
  153. {
  154. char buf[32];
  155. size_t i, c;
  156. struct hist_entry *he;
  157. struct rb_root *root;
  158. struct rb_node *node;
  159. struct callchain_node *cnode;
  160. struct callchain_list *clist;
  161. /*
  162. * adding and deleting hist entries must be done outside of this
  163. * function since TEST_ASSERT_VAL() returns in case of failure.
  164. */
  165. hists__collapse_resort(hists, NULL);
  166. perf_evsel__output_resort(hists_to_evsel(hists), NULL);
  167. if (verbose > 2) {
  168. pr_info("use callchain: %d, cumulate callchain: %d\n",
  169. symbol_conf.use_callchain,
  170. symbol_conf.cumulate_callchain);
  171. print_hists_out(hists);
  172. }
  173. root = &hists->entries;
  174. for (node = rb_first(root), i = 0;
  175. node && (he = rb_entry(node, struct hist_entry, rb_node));
  176. node = rb_next(node), i++) {
  177. scnprintf(buf, sizeof(buf), "Invalid hist entry #%zd", i);
  178. TEST_ASSERT_VAL("Incorrect number of hist entry",
  179. i < nr_expected);
  180. TEST_ASSERT_VAL(buf, he->stat.period == expected[i].self &&
  181. !strcmp(COMM(he), expected[i].comm) &&
  182. !strcmp(DSO(he), expected[i].dso) &&
  183. !strcmp(SYM(he), expected[i].sym));
  184. if (symbol_conf.cumulate_callchain)
  185. TEST_ASSERT_VAL(buf, he->stat_acc->period == expected[i].children);
  186. if (!symbol_conf.use_callchain)
  187. continue;
  188. /* check callchain entries */
  189. root = &he->callchain->node.rb_root;
  190. cnode = rb_entry(rb_first(root), struct callchain_node, rb_node);
  191. c = 0;
  192. list_for_each_entry(clist, &cnode->val, list) {
  193. scnprintf(buf, sizeof(buf), "Invalid callchain entry #%zd/%zd", i, c);
  194. TEST_ASSERT_VAL("Incorrect number of callchain entry",
  195. c < expected_callchain[i].nr);
  196. TEST_ASSERT_VAL(buf,
  197. !strcmp(CDSO(clist), expected_callchain[i].node[c].dso) &&
  198. !strcmp(CSYM(clist), expected_callchain[i].node[c].sym));
  199. c++;
  200. }
  201. /* TODO: handle multiple child nodes properly */
  202. TEST_ASSERT_VAL("Incorrect number of callchain entry",
  203. c <= expected_callchain[i].nr);
  204. }
  205. TEST_ASSERT_VAL("Incorrect number of hist entry",
  206. i == nr_expected);
  207. TEST_ASSERT_VAL("Incorrect number of callchain entry",
  208. !symbol_conf.use_callchain || nr_expected == nr_callchain);
  209. return 0;
  210. }
  211. /* NO callchain + NO children */
  212. static int test1(struct perf_evsel *evsel, struct machine *machine)
  213. {
  214. int err;
  215. struct hists *hists = evsel__hists(evsel);
  216. /*
  217. * expected output:
  218. *
  219. * Overhead Command Shared Object Symbol
  220. * ======== ======= ============= ==============
  221. * 20.00% perf perf [.] main
  222. * 10.00% bash [kernel] [k] page_fault
  223. * 10.00% bash bash [.] main
  224. * 10.00% bash bash [.] xmalloc
  225. * 10.00% perf [kernel] [k] page_fault
  226. * 10.00% perf [kernel] [k] schedule
  227. * 10.00% perf libc [.] free
  228. * 10.00% perf libc [.] malloc
  229. * 10.00% perf perf [.] cmd_record
  230. */
  231. struct result expected[] = {
  232. { 0, 2000, "perf", "perf", "main" },
  233. { 0, 1000, "bash", "[kernel]", "page_fault" },
  234. { 0, 1000, "bash", "bash", "main" },
  235. { 0, 1000, "bash", "bash", "xmalloc" },
  236. { 0, 1000, "perf", "[kernel]", "page_fault" },
  237. { 0, 1000, "perf", "[kernel]", "schedule" },
  238. { 0, 1000, "perf", "libc", "free" },
  239. { 0, 1000, "perf", "libc", "malloc" },
  240. { 0, 1000, "perf", "perf", "cmd_record" },
  241. };
  242. symbol_conf.use_callchain = false;
  243. symbol_conf.cumulate_callchain = false;
  244. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  245. setup_sorting(NULL);
  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. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  386. setup_sorting(NULL);
  387. callchain_register_param(&callchain_param);
  388. err = add_hist_entries(hists, machine);
  389. if (err < 0)
  390. goto out;
  391. err = do_test(hists, expected, ARRAY_SIZE(expected),
  392. expected_callchain, ARRAY_SIZE(expected_callchain));
  393. out:
  394. del_hist_entries(hists);
  395. reset_output_field();
  396. return err;
  397. }
  398. /* NO callchain + children */
  399. static int test3(struct perf_evsel *evsel, struct machine *machine)
  400. {
  401. int err;
  402. struct hists *hists = evsel__hists(evsel);
  403. /*
  404. * expected output:
  405. *
  406. * Children Self Command Shared Object Symbol
  407. * ======== ======== ======= ============= =======================
  408. * 70.00% 20.00% perf perf [.] main
  409. * 50.00% 0.00% perf perf [.] run_command
  410. * 30.00% 10.00% bash bash [.] main
  411. * 30.00% 10.00% perf perf [.] cmd_record
  412. * 20.00% 0.00% bash libc [.] malloc
  413. * 10.00% 10.00% bash [kernel] [k] page_fault
  414. * 10.00% 10.00% bash bash [.] xmalloc
  415. * 10.00% 10.00% perf [kernel] [k] page_fault
  416. * 10.00% 10.00% perf libc [.] malloc
  417. * 10.00% 10.00% perf [kernel] [k] schedule
  418. * 10.00% 10.00% perf libc [.] free
  419. * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
  420. */
  421. struct result expected[] = {
  422. { 7000, 2000, "perf", "perf", "main" },
  423. { 5000, 0, "perf", "perf", "run_command" },
  424. { 3000, 1000, "bash", "bash", "main" },
  425. { 3000, 1000, "perf", "perf", "cmd_record" },
  426. { 2000, 0, "bash", "libc", "malloc" },
  427. { 1000, 1000, "bash", "[kernel]", "page_fault" },
  428. { 1000, 1000, "bash", "bash", "xmalloc" },
  429. { 1000, 1000, "perf", "[kernel]", "page_fault" },
  430. { 1000, 1000, "perf", "[kernel]", "schedule" },
  431. { 1000, 1000, "perf", "libc", "free" },
  432. { 1000, 1000, "perf", "libc", "malloc" },
  433. { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
  434. };
  435. symbol_conf.use_callchain = false;
  436. symbol_conf.cumulate_callchain = true;
  437. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  438. setup_sorting(NULL);
  439. callchain_register_param(&callchain_param);
  440. err = add_hist_entries(hists, machine);
  441. if (err < 0)
  442. goto out;
  443. err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
  444. out:
  445. del_hist_entries(hists);
  446. reset_output_field();
  447. return err;
  448. }
  449. /* callchain + children */
  450. static int test4(struct perf_evsel *evsel, struct machine *machine)
  451. {
  452. int err;
  453. struct hists *hists = evsel__hists(evsel);
  454. /*
  455. * expected output:
  456. *
  457. * Children Self Command Shared Object Symbol
  458. * ======== ======== ======= ============= =======================
  459. * 70.00% 20.00% perf perf [.] main
  460. * |
  461. * --- main
  462. *
  463. * 50.00% 0.00% perf perf [.] run_command
  464. * |
  465. * --- run_command
  466. * main
  467. *
  468. * 30.00% 10.00% bash bash [.] main
  469. * |
  470. * --- main
  471. *
  472. * 30.00% 10.00% perf perf [.] cmd_record
  473. * |
  474. * --- cmd_record
  475. * run_command
  476. * main
  477. *
  478. * 20.00% 0.00% bash libc [.] malloc
  479. * |
  480. * --- malloc
  481. * |
  482. * |--50.00%-- xmalloc
  483. * | main
  484. * --50.00%-- main
  485. *
  486. * 10.00% 10.00% bash [kernel] [k] page_fault
  487. * |
  488. * --- page_fault
  489. * malloc
  490. * main
  491. *
  492. * 10.00% 10.00% bash bash [.] xmalloc
  493. * |
  494. * --- xmalloc
  495. * malloc
  496. * xmalloc <--- NOTE: there's a cycle
  497. * malloc
  498. * xmalloc
  499. * main
  500. *
  501. * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
  502. * |
  503. * --- sys_perf_event_open
  504. * run_command
  505. * main
  506. *
  507. * 10.00% 10.00% perf [kernel] [k] page_fault
  508. * |
  509. * --- page_fault
  510. * sys_perf_event_open
  511. * run_command
  512. * main
  513. *
  514. * 10.00% 10.00% perf [kernel] [k] schedule
  515. * |
  516. * --- schedule
  517. * run_command
  518. * main
  519. *
  520. * 10.00% 10.00% perf libc [.] free
  521. * |
  522. * --- free
  523. * cmd_record
  524. * run_command
  525. * main
  526. *
  527. * 10.00% 10.00% perf libc [.] malloc
  528. * |
  529. * --- malloc
  530. * cmd_record
  531. * run_command
  532. * main
  533. *
  534. */
  535. struct result expected[] = {
  536. { 7000, 2000, "perf", "perf", "main" },
  537. { 5000, 0, "perf", "perf", "run_command" },
  538. { 3000, 1000, "bash", "bash", "main" },
  539. { 3000, 1000, "perf", "perf", "cmd_record" },
  540. { 2000, 0, "bash", "libc", "malloc" },
  541. { 1000, 1000, "bash", "[kernel]", "page_fault" },
  542. { 1000, 1000, "bash", "bash", "xmalloc" },
  543. { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
  544. { 1000, 1000, "perf", "[kernel]", "page_fault" },
  545. { 1000, 1000, "perf", "[kernel]", "schedule" },
  546. { 1000, 1000, "perf", "libc", "free" },
  547. { 1000, 1000, "perf", "libc", "malloc" },
  548. };
  549. struct callchain_result expected_callchain[] = {
  550. {
  551. 1, { { "perf", "main" }, },
  552. },
  553. {
  554. 2, { { "perf", "run_command" },
  555. { "perf", "main" }, },
  556. },
  557. {
  558. 1, { { "bash", "main" }, },
  559. },
  560. {
  561. 3, { { "perf", "cmd_record" },
  562. { "perf", "run_command" },
  563. { "perf", "main" }, },
  564. },
  565. {
  566. 4, { { "libc", "malloc" },
  567. { "bash", "xmalloc" },
  568. { "bash", "main" },
  569. { "bash", "main" }, },
  570. },
  571. {
  572. 3, { { "[kernel]", "page_fault" },
  573. { "libc", "malloc" },
  574. { "bash", "main" }, },
  575. },
  576. {
  577. 6, { { "bash", "xmalloc" },
  578. { "libc", "malloc" },
  579. { "bash", "xmalloc" },
  580. { "libc", "malloc" },
  581. { "bash", "xmalloc" },
  582. { "bash", "main" }, },
  583. },
  584. {
  585. 3, { { "[kernel]", "sys_perf_event_open" },
  586. { "perf", "run_command" },
  587. { "perf", "main" }, },
  588. },
  589. {
  590. 4, { { "[kernel]", "page_fault" },
  591. { "[kernel]", "sys_perf_event_open" },
  592. { "perf", "run_command" },
  593. { "perf", "main" }, },
  594. },
  595. {
  596. 3, { { "[kernel]", "schedule" },
  597. { "perf", "run_command" },
  598. { "perf", "main" }, },
  599. },
  600. {
  601. 4, { { "libc", "free" },
  602. { "perf", "cmd_record" },
  603. { "perf", "run_command" },
  604. { "perf", "main" }, },
  605. },
  606. {
  607. 4, { { "libc", "malloc" },
  608. { "perf", "cmd_record" },
  609. { "perf", "run_command" },
  610. { "perf", "main" }, },
  611. },
  612. };
  613. symbol_conf.use_callchain = true;
  614. symbol_conf.cumulate_callchain = true;
  615. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  616. setup_sorting(NULL);
  617. callchain_register_param(&callchain_param);
  618. err = add_hist_entries(hists, machine);
  619. if (err < 0)
  620. goto out;
  621. err = do_test(hists, expected, ARRAY_SIZE(expected),
  622. expected_callchain, ARRAY_SIZE(expected_callchain));
  623. out:
  624. del_hist_entries(hists);
  625. reset_output_field();
  626. return err;
  627. }
  628. int test__hists_cumulate(int subtest __maybe_unused)
  629. {
  630. int err = TEST_FAIL;
  631. struct machines machines;
  632. struct machine *machine;
  633. struct perf_evsel *evsel;
  634. struct perf_evlist *evlist = perf_evlist__new();
  635. size_t i;
  636. test_fn_t testcases[] = {
  637. test1,
  638. test2,
  639. test3,
  640. test4,
  641. };
  642. TEST_ASSERT_VAL("No memory", evlist);
  643. err = parse_events(evlist, "cpu-clock", NULL);
  644. if (err)
  645. goto out;
  646. err = TEST_FAIL;
  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. }