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. 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, sysctl_perf_event_max_stack,
  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 (hists__has(hists, 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. TEST_ASSERT_VAL("callchains expected", !RB_EMPTY_ROOT(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. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  246. setup_sorting(NULL);
  247. callchain_register_param(&callchain_param);
  248. err = add_hist_entries(hists, machine);
  249. if (err < 0)
  250. goto out;
  251. err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
  252. out:
  253. del_hist_entries(hists);
  254. reset_output_field();
  255. return err;
  256. }
  257. /* callcain + NO children */
  258. static int test2(struct perf_evsel *evsel, struct machine *machine)
  259. {
  260. int err;
  261. struct hists *hists = evsel__hists(evsel);
  262. /*
  263. * expected output:
  264. *
  265. * Overhead Command Shared Object Symbol
  266. * ======== ======= ============= ==============
  267. * 20.00% perf perf [.] main
  268. * |
  269. * --- main
  270. *
  271. * 10.00% bash [kernel] [k] page_fault
  272. * |
  273. * --- page_fault
  274. * malloc
  275. * main
  276. *
  277. * 10.00% bash bash [.] main
  278. * |
  279. * --- main
  280. *
  281. * 10.00% bash bash [.] xmalloc
  282. * |
  283. * --- xmalloc
  284. * malloc
  285. * xmalloc <--- NOTE: there's a cycle
  286. * malloc
  287. * xmalloc
  288. * main
  289. *
  290. * 10.00% perf [kernel] [k] page_fault
  291. * |
  292. * --- page_fault
  293. * sys_perf_event_open
  294. * run_command
  295. * main
  296. *
  297. * 10.00% perf [kernel] [k] schedule
  298. * |
  299. * --- schedule
  300. * run_command
  301. * main
  302. *
  303. * 10.00% perf libc [.] free
  304. * |
  305. * --- free
  306. * cmd_record
  307. * run_command
  308. * main
  309. *
  310. * 10.00% perf libc [.] malloc
  311. * |
  312. * --- malloc
  313. * cmd_record
  314. * run_command
  315. * main
  316. *
  317. * 10.00% perf perf [.] cmd_record
  318. * |
  319. * --- cmd_record
  320. * run_command
  321. * main
  322. *
  323. */
  324. struct result expected[] = {
  325. { 0, 2000, "perf", "perf", "main" },
  326. { 0, 1000, "bash", "[kernel]", "page_fault" },
  327. { 0, 1000, "bash", "bash", "main" },
  328. { 0, 1000, "bash", "bash", "xmalloc" },
  329. { 0, 1000, "perf", "[kernel]", "page_fault" },
  330. { 0, 1000, "perf", "[kernel]", "schedule" },
  331. { 0, 1000, "perf", "libc", "free" },
  332. { 0, 1000, "perf", "libc", "malloc" },
  333. { 0, 1000, "perf", "perf", "cmd_record" },
  334. };
  335. struct callchain_result expected_callchain[] = {
  336. {
  337. 1, { { "perf", "main" }, },
  338. },
  339. {
  340. 3, { { "[kernel]", "page_fault" },
  341. { "libc", "malloc" },
  342. { "bash", "main" }, },
  343. },
  344. {
  345. 1, { { "bash", "main" }, },
  346. },
  347. {
  348. 6, { { "bash", "xmalloc" },
  349. { "libc", "malloc" },
  350. { "bash", "xmalloc" },
  351. { "libc", "malloc" },
  352. { "bash", "xmalloc" },
  353. { "bash", "main" }, },
  354. },
  355. {
  356. 4, { { "[kernel]", "page_fault" },
  357. { "[kernel]", "sys_perf_event_open" },
  358. { "perf", "run_command" },
  359. { "perf", "main" }, },
  360. },
  361. {
  362. 3, { { "[kernel]", "schedule" },
  363. { "perf", "run_command" },
  364. { "perf", "main" }, },
  365. },
  366. {
  367. 4, { { "libc", "free" },
  368. { "perf", "cmd_record" },
  369. { "perf", "run_command" },
  370. { "perf", "main" }, },
  371. },
  372. {
  373. 4, { { "libc", "malloc" },
  374. { "perf", "cmd_record" },
  375. { "perf", "run_command" },
  376. { "perf", "main" }, },
  377. },
  378. {
  379. 3, { { "perf", "cmd_record" },
  380. { "perf", "run_command" },
  381. { "perf", "main" }, },
  382. },
  383. };
  384. symbol_conf.use_callchain = true;
  385. symbol_conf.cumulate_callchain = false;
  386. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  387. setup_sorting(NULL);
  388. callchain_register_param(&callchain_param);
  389. err = add_hist_entries(hists, machine);
  390. if (err < 0)
  391. goto out;
  392. err = do_test(hists, expected, ARRAY_SIZE(expected),
  393. expected_callchain, ARRAY_SIZE(expected_callchain));
  394. out:
  395. del_hist_entries(hists);
  396. reset_output_field();
  397. return err;
  398. }
  399. /* NO callchain + children */
  400. static int test3(struct perf_evsel *evsel, struct machine *machine)
  401. {
  402. int err;
  403. struct hists *hists = evsel__hists(evsel);
  404. /*
  405. * expected output:
  406. *
  407. * Children Self Command Shared Object Symbol
  408. * ======== ======== ======= ============= =======================
  409. * 70.00% 20.00% perf perf [.] main
  410. * 50.00% 0.00% perf perf [.] run_command
  411. * 30.00% 10.00% bash bash [.] main
  412. * 30.00% 10.00% perf perf [.] cmd_record
  413. * 20.00% 0.00% bash libc [.] malloc
  414. * 10.00% 10.00% bash [kernel] [k] page_fault
  415. * 10.00% 10.00% bash bash [.] xmalloc
  416. * 10.00% 10.00% perf [kernel] [k] page_fault
  417. * 10.00% 10.00% perf libc [.] malloc
  418. * 10.00% 10.00% perf [kernel] [k] schedule
  419. * 10.00% 10.00% perf libc [.] free
  420. * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
  421. */
  422. struct result expected[] = {
  423. { 7000, 2000, "perf", "perf", "main" },
  424. { 5000, 0, "perf", "perf", "run_command" },
  425. { 3000, 1000, "bash", "bash", "main" },
  426. { 3000, 1000, "perf", "perf", "cmd_record" },
  427. { 2000, 0, "bash", "libc", "malloc" },
  428. { 1000, 1000, "bash", "[kernel]", "page_fault" },
  429. { 1000, 1000, "bash", "bash", "xmalloc" },
  430. { 1000, 1000, "perf", "[kernel]", "page_fault" },
  431. { 1000, 1000, "perf", "[kernel]", "schedule" },
  432. { 1000, 1000, "perf", "libc", "free" },
  433. { 1000, 1000, "perf", "libc", "malloc" },
  434. { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
  435. };
  436. symbol_conf.use_callchain = false;
  437. symbol_conf.cumulate_callchain = true;
  438. perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
  439. setup_sorting(NULL);
  440. callchain_register_param(&callchain_param);
  441. err = add_hist_entries(hists, machine);
  442. if (err < 0)
  443. goto out;
  444. err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0);
  445. out:
  446. del_hist_entries(hists);
  447. reset_output_field();
  448. return err;
  449. }
  450. /* callchain + children */
  451. static int test4(struct perf_evsel *evsel, struct machine *machine)
  452. {
  453. int err;
  454. struct hists *hists = evsel__hists(evsel);
  455. /*
  456. * expected output:
  457. *
  458. * Children Self Command Shared Object Symbol
  459. * ======== ======== ======= ============= =======================
  460. * 70.00% 20.00% perf perf [.] main
  461. * |
  462. * --- main
  463. *
  464. * 50.00% 0.00% perf perf [.] run_command
  465. * |
  466. * --- run_command
  467. * main
  468. *
  469. * 30.00% 10.00% bash bash [.] main
  470. * |
  471. * --- main
  472. *
  473. * 30.00% 10.00% perf perf [.] cmd_record
  474. * |
  475. * --- cmd_record
  476. * run_command
  477. * main
  478. *
  479. * 20.00% 0.00% bash libc [.] malloc
  480. * |
  481. * --- malloc
  482. * |
  483. * |--50.00%-- xmalloc
  484. * | main
  485. * --50.00%-- main
  486. *
  487. * 10.00% 10.00% bash [kernel] [k] page_fault
  488. * |
  489. * --- page_fault
  490. * malloc
  491. * main
  492. *
  493. * 10.00% 10.00% bash bash [.] xmalloc
  494. * |
  495. * --- xmalloc
  496. * malloc
  497. * xmalloc <--- NOTE: there's a cycle
  498. * malloc
  499. * xmalloc
  500. * main
  501. *
  502. * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open
  503. * |
  504. * --- sys_perf_event_open
  505. * run_command
  506. * main
  507. *
  508. * 10.00% 10.00% perf [kernel] [k] page_fault
  509. * |
  510. * --- page_fault
  511. * sys_perf_event_open
  512. * run_command
  513. * main
  514. *
  515. * 10.00% 10.00% perf [kernel] [k] schedule
  516. * |
  517. * --- schedule
  518. * run_command
  519. * main
  520. *
  521. * 10.00% 10.00% perf libc [.] free
  522. * |
  523. * --- free
  524. * cmd_record
  525. * run_command
  526. * main
  527. *
  528. * 10.00% 10.00% perf libc [.] malloc
  529. * |
  530. * --- malloc
  531. * cmd_record
  532. * run_command
  533. * main
  534. *
  535. */
  536. struct result expected[] = {
  537. { 7000, 2000, "perf", "perf", "main" },
  538. { 5000, 0, "perf", "perf", "run_command" },
  539. { 3000, 1000, "bash", "bash", "main" },
  540. { 3000, 1000, "perf", "perf", "cmd_record" },
  541. { 2000, 0, "bash", "libc", "malloc" },
  542. { 1000, 1000, "bash", "[kernel]", "page_fault" },
  543. { 1000, 1000, "bash", "bash", "xmalloc" },
  544. { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" },
  545. { 1000, 1000, "perf", "[kernel]", "page_fault" },
  546. { 1000, 1000, "perf", "[kernel]", "schedule" },
  547. { 1000, 1000, "perf", "libc", "free" },
  548. { 1000, 1000, "perf", "libc", "malloc" },
  549. };
  550. struct callchain_result expected_callchain[] = {
  551. {
  552. 1, { { "perf", "main" }, },
  553. },
  554. {
  555. 2, { { "perf", "run_command" },
  556. { "perf", "main" }, },
  557. },
  558. {
  559. 1, { { "bash", "main" }, },
  560. },
  561. {
  562. 3, { { "perf", "cmd_record" },
  563. { "perf", "run_command" },
  564. { "perf", "main" }, },
  565. },
  566. {
  567. 4, { { "libc", "malloc" },
  568. { "bash", "xmalloc" },
  569. { "bash", "main" },
  570. { "bash", "main" }, },
  571. },
  572. {
  573. 3, { { "[kernel]", "page_fault" },
  574. { "libc", "malloc" },
  575. { "bash", "main" }, },
  576. },
  577. {
  578. 6, { { "bash", "xmalloc" },
  579. { "libc", "malloc" },
  580. { "bash", "xmalloc" },
  581. { "libc", "malloc" },
  582. { "bash", "xmalloc" },
  583. { "bash", "main" }, },
  584. },
  585. {
  586. 3, { { "[kernel]", "sys_perf_event_open" },
  587. { "perf", "run_command" },
  588. { "perf", "main" }, },
  589. },
  590. {
  591. 4, { { "[kernel]", "page_fault" },
  592. { "[kernel]", "sys_perf_event_open" },
  593. { "perf", "run_command" },
  594. { "perf", "main" }, },
  595. },
  596. {
  597. 3, { { "[kernel]", "schedule" },
  598. { "perf", "run_command" },
  599. { "perf", "main" }, },
  600. },
  601. {
  602. 4, { { "libc", "free" },
  603. { "perf", "cmd_record" },
  604. { "perf", "run_command" },
  605. { "perf", "main" }, },
  606. },
  607. {
  608. 4, { { "libc", "malloc" },
  609. { "perf", "cmd_record" },
  610. { "perf", "run_command" },
  611. { "perf", "main" }, },
  612. },
  613. };
  614. symbol_conf.use_callchain = true;
  615. symbol_conf.cumulate_callchain = true;
  616. perf_evsel__set_sample_bit(evsel, CALLCHAIN);
  617. setup_sorting(NULL);
  618. callchain_param = callchain_param_default;
  619. callchain_register_param(&callchain_param);
  620. err = add_hist_entries(hists, machine);
  621. if (err < 0)
  622. goto out;
  623. err = do_test(hists, expected, ARRAY_SIZE(expected),
  624. expected_callchain, ARRAY_SIZE(expected_callchain));
  625. out:
  626. del_hist_entries(hists);
  627. reset_output_field();
  628. return err;
  629. }
  630. int test__hists_cumulate(int subtest __maybe_unused)
  631. {
  632. int err = TEST_FAIL;
  633. struct machines machines;
  634. struct machine *machine;
  635. struct perf_evsel *evsel;
  636. struct perf_evlist *evlist = perf_evlist__new();
  637. size_t i;
  638. test_fn_t testcases[] = {
  639. test1,
  640. test2,
  641. test3,
  642. test4,
  643. };
  644. TEST_ASSERT_VAL("No memory", evlist);
  645. err = parse_events(evlist, "cpu-clock", NULL);
  646. if (err)
  647. goto out;
  648. err = TEST_FAIL;
  649. machines__init(&machines);
  650. /* setup threads/dso/map/symbols also */
  651. machine = setup_fake_machine(&machines);
  652. if (!machine)
  653. goto out;
  654. if (verbose > 1)
  655. machine__fprintf(machine, stderr);
  656. evsel = perf_evlist__first(evlist);
  657. for (i = 0; i < ARRAY_SIZE(testcases); i++) {
  658. err = testcases[i](evsel, machine);
  659. if (err < 0)
  660. break;
  661. }
  662. out:
  663. /* tear down everything */
  664. perf_evlist__delete(evlist);
  665. machines__exit(&machines);
  666. return err;
  667. }