Commit | Line | Data |
---|---|---|
0506aecc NK |
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 | ||
13 | struct sample { | |
14 | u32 pid; | |
15 | u64 ip; | |
16 | struct thread *thread; | |
17 | struct map *map; | |
18 | struct symbol *sym; | |
19 | }; | |
20 | ||
21 | /* For the numbers, see hists_common.c */ | |
22 | static struct sample fake_samples[] = { | |
23 | /* perf [kernel] schedule() */ | |
24 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, }, | |
25 | /* perf [perf] main() */ | |
26 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, }, | |
27 | /* perf [perf] cmd_record() */ | |
28 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_CMD_RECORD, }, | |
29 | /* perf [libc] malloc() */ | |
30 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, }, | |
31 | /* perf [libc] free() */ | |
32 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_FREE, }, | |
33 | /* perf [perf] main() */ | |
34 | { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, }, | |
35 | /* perf [kernel] page_fault() */ | |
36 | { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, | |
37 | /* bash [bash] main() */ | |
38 | { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, }, | |
39 | /* bash [bash] xmalloc() */ | |
40 | { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, }, | |
41 | /* bash [kernel] page_fault() */ | |
42 | { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, | |
43 | }; | |
44 | ||
45 | /* | |
46 | * Will be casted to struct ip_callchain which has all 64 bit entries | |
47 | * of nr and ips[]. | |
48 | */ | |
49 | static u64 fake_callchains[][10] = { | |
50 | /* schedule => run_command => main */ | |
51 | { 3, FAKE_IP_KERNEL_SCHEDULE, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, }, | |
52 | /* main */ | |
53 | { 1, FAKE_IP_PERF_MAIN, }, | |
54 | /* cmd_record => run_command => main */ | |
55 | { 3, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, }, | |
56 | /* malloc => cmd_record => run_command => main */ | |
57 | { 4, FAKE_IP_LIBC_MALLOC, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, | |
58 | FAKE_IP_PERF_MAIN, }, | |
59 | /* free => cmd_record => run_command => main */ | |
60 | { 4, FAKE_IP_LIBC_FREE, FAKE_IP_PERF_CMD_RECORD, FAKE_IP_PERF_RUN_COMMAND, | |
61 | FAKE_IP_PERF_MAIN, }, | |
62 | /* main */ | |
63 | { 1, FAKE_IP_PERF_MAIN, }, | |
64 | /* page_fault => sys_perf_event_open => run_command => main */ | |
65 | { 4, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, | |
66 | FAKE_IP_PERF_RUN_COMMAND, FAKE_IP_PERF_MAIN, }, | |
67 | /* main */ | |
68 | { 1, FAKE_IP_BASH_MAIN, }, | |
69 | /* xmalloc => malloc => xmalloc => malloc => xmalloc => main */ | |
70 | { 6, FAKE_IP_BASH_XMALLOC, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC, | |
71 | FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_XMALLOC, FAKE_IP_BASH_MAIN, }, | |
72 | /* page_fault => malloc => main */ | |
73 | { 3, FAKE_IP_KERNEL_PAGE_FAULT, FAKE_IP_LIBC_MALLOC, FAKE_IP_BASH_MAIN, }, | |
74 | }; | |
75 | ||
76 | static int add_hist_entries(struct hists *hists, struct machine *machine) | |
77 | { | |
78 | struct addr_location al; | |
79 | struct perf_evsel *evsel = hists_to_evsel(hists); | |
80 | struct perf_sample sample = { .period = 1000, }; | |
81 | size_t i; | |
82 | ||
83 | for (i = 0; i < ARRAY_SIZE(fake_samples); i++) { | |
84 | const union perf_event event = { | |
85 | .header = { | |
86 | .misc = PERF_RECORD_MISC_USER, | |
87 | }, | |
88 | }; | |
89 | struct hist_entry_iter iter = { | |
063bd936 NK |
90 | .evsel = evsel, |
91 | .sample = &sample, | |
0506aecc NK |
92 | .hide_unresolved = false, |
93 | }; | |
94 | ||
95 | if (symbol_conf.cumulate_callchain) | |
96 | iter.ops = &hist_iter_cumulative; | |
97 | else | |
98 | iter.ops = &hist_iter_normal; | |
99 | ||
473398a2 | 100 | sample.cpumode = PERF_RECORD_MISC_USER; |
0506aecc NK |
101 | sample.pid = fake_samples[i].pid; |
102 | sample.tid = fake_samples[i].pid; | |
103 | sample.ip = fake_samples[i].ip; | |
104 | sample.callchain = (struct ip_callchain *)fake_callchains[i]; | |
105 | ||
106 | if (perf_event__preprocess_sample(&event, machine, &al, | |
107 | &sample) < 0) | |
108 | goto out; | |
109 | ||
063bd936 NK |
110 | if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH, |
111 | NULL) < 0) { | |
b91fc39f | 112 | addr_location__put(&al); |
0506aecc | 113 | goto out; |
b91fc39f | 114 | } |
0506aecc NK |
115 | |
116 | fake_samples[i].thread = al.thread; | |
117 | fake_samples[i].map = al.map; | |
118 | fake_samples[i].sym = al.sym; | |
119 | } | |
120 | ||
121 | return TEST_OK; | |
122 | ||
123 | out: | |
124 | pr_debug("Not enough memory for adding a hist entry\n"); | |
125 | return TEST_FAIL; | |
126 | } | |
127 | ||
128 | static void del_hist_entries(struct hists *hists) | |
129 | { | |
130 | struct hist_entry *he; | |
131 | struct rb_root *root_in; | |
132 | struct rb_root *root_out; | |
133 | struct rb_node *node; | |
134 | ||
135 | if (sort__need_collapse) | |
136 | root_in = &hists->entries_collapsed; | |
137 | else | |
138 | root_in = hists->entries_in; | |
139 | ||
140 | root_out = &hists->entries; | |
141 | ||
142 | while (!RB_EMPTY_ROOT(root_out)) { | |
143 | node = rb_first(root_out); | |
144 | ||
145 | he = rb_entry(node, struct hist_entry, rb_node); | |
146 | rb_erase(node, root_out); | |
147 | rb_erase(&he->rb_node_in, root_in); | |
6733d1bf | 148 | hist_entry__delete(he); |
0506aecc NK |
149 | } |
150 | } | |
151 | ||
152 | typedef int (*test_fn_t)(struct perf_evsel *, struct machine *); | |
153 | ||
154 | #define COMM(he) (thread__comm_str(he->thread)) | |
155 | #define DSO(he) (he->ms.map->dso->short_name) | |
156 | #define SYM(he) (he->ms.sym->name) | |
157 | #define CPU(he) (he->cpu) | |
158 | #define PID(he) (he->thread->tid) | |
159 | #define DEPTH(he) (he->callchain->max_depth) | |
160 | #define CDSO(cl) (cl->ms.map->dso->short_name) | |
161 | #define CSYM(cl) (cl->ms.sym->name) | |
162 | ||
163 | struct result { | |
164 | u64 children; | |
165 | u64 self; | |
166 | const char *comm; | |
167 | const char *dso; | |
168 | const char *sym; | |
169 | }; | |
170 | ||
171 | struct callchain_result { | |
172 | u64 nr; | |
173 | struct { | |
174 | const char *dso; | |
175 | const char *sym; | |
176 | } node[10]; | |
177 | }; | |
178 | ||
179 | static int do_test(struct hists *hists, struct result *expected, size_t nr_expected, | |
180 | struct callchain_result *expected_callchain, size_t nr_callchain) | |
181 | { | |
182 | char buf[32]; | |
183 | size_t i, c; | |
184 | struct hist_entry *he; | |
185 | struct rb_root *root; | |
186 | struct rb_node *node; | |
187 | struct callchain_node *cnode; | |
188 | struct callchain_list *clist; | |
189 | ||
190 | /* | |
191 | * adding and deleting hist entries must be done outside of this | |
192 | * function since TEST_ASSERT_VAL() returns in case of failure. | |
193 | */ | |
194 | hists__collapse_resort(hists, NULL); | |
452ce03b | 195 | perf_evsel__output_resort(hists_to_evsel(hists), NULL); |
0506aecc NK |
196 | |
197 | if (verbose > 2) { | |
198 | pr_info("use callchain: %d, cumulate callchain: %d\n", | |
199 | symbol_conf.use_callchain, | |
200 | symbol_conf.cumulate_callchain); | |
201 | print_hists_out(hists); | |
202 | } | |
203 | ||
204 | root = &hists->entries; | |
205 | for (node = rb_first(root), i = 0; | |
206 | node && (he = rb_entry(node, struct hist_entry, rb_node)); | |
207 | node = rb_next(node), i++) { | |
208 | scnprintf(buf, sizeof(buf), "Invalid hist entry #%zd", i); | |
209 | ||
210 | TEST_ASSERT_VAL("Incorrect number of hist entry", | |
211 | i < nr_expected); | |
212 | TEST_ASSERT_VAL(buf, he->stat.period == expected[i].self && | |
213 | !strcmp(COMM(he), expected[i].comm) && | |
214 | !strcmp(DSO(he), expected[i].dso) && | |
215 | !strcmp(SYM(he), expected[i].sym)); | |
216 | ||
217 | if (symbol_conf.cumulate_callchain) | |
218 | TEST_ASSERT_VAL(buf, he->stat_acc->period == expected[i].children); | |
219 | ||
220 | if (!symbol_conf.use_callchain) | |
221 | continue; | |
222 | ||
223 | /* check callchain entries */ | |
224 | root = &he->callchain->node.rb_root; | |
225 | cnode = rb_entry(rb_first(root), struct callchain_node, rb_node); | |
226 | ||
227 | c = 0; | |
228 | list_for_each_entry(clist, &cnode->val, list) { | |
229 | scnprintf(buf, sizeof(buf), "Invalid callchain entry #%zd/%zd", i, c); | |
230 | ||
231 | TEST_ASSERT_VAL("Incorrect number of callchain entry", | |
232 | c < expected_callchain[i].nr); | |
233 | TEST_ASSERT_VAL(buf, | |
234 | !strcmp(CDSO(clist), expected_callchain[i].node[c].dso) && | |
235 | !strcmp(CSYM(clist), expected_callchain[i].node[c].sym)); | |
236 | c++; | |
237 | } | |
238 | /* TODO: handle multiple child nodes properly */ | |
239 | TEST_ASSERT_VAL("Incorrect number of callchain entry", | |
240 | c <= expected_callchain[i].nr); | |
241 | } | |
242 | TEST_ASSERT_VAL("Incorrect number of hist entry", | |
243 | i == nr_expected); | |
244 | TEST_ASSERT_VAL("Incorrect number of callchain entry", | |
245 | !symbol_conf.use_callchain || nr_expected == nr_callchain); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | /* NO callchain + NO children */ | |
250 | static int test1(struct perf_evsel *evsel, struct machine *machine) | |
251 | { | |
252 | int err; | |
4ea062ed | 253 | struct hists *hists = evsel__hists(evsel); |
0506aecc NK |
254 | /* |
255 | * expected output: | |
256 | * | |
257 | * Overhead Command Shared Object Symbol | |
258 | * ======== ======= ============= ============== | |
259 | * 20.00% perf perf [.] main | |
260 | * 10.00% bash [kernel] [k] page_fault | |
261 | * 10.00% bash bash [.] main | |
262 | * 10.00% bash bash [.] xmalloc | |
263 | * 10.00% perf [kernel] [k] page_fault | |
264 | * 10.00% perf [kernel] [k] schedule | |
265 | * 10.00% perf libc [.] free | |
266 | * 10.00% perf libc [.] malloc | |
267 | * 10.00% perf perf [.] cmd_record | |
268 | */ | |
269 | struct result expected[] = { | |
270 | { 0, 2000, "perf", "perf", "main" }, | |
271 | { 0, 1000, "bash", "[kernel]", "page_fault" }, | |
272 | { 0, 1000, "bash", "bash", "main" }, | |
273 | { 0, 1000, "bash", "bash", "xmalloc" }, | |
274 | { 0, 1000, "perf", "[kernel]", "page_fault" }, | |
275 | { 0, 1000, "perf", "[kernel]", "schedule" }, | |
276 | { 0, 1000, "perf", "libc", "free" }, | |
277 | { 0, 1000, "perf", "libc", "malloc" }, | |
278 | { 0, 1000, "perf", "perf", "cmd_record" }, | |
279 | }; | |
280 | ||
281 | symbol_conf.use_callchain = false; | |
282 | symbol_conf.cumulate_callchain = false; | |
f9db0d0f | 283 | perf_evsel__reset_sample_bit(evsel, CALLCHAIN); |
0506aecc | 284 | |
40184c46 | 285 | setup_sorting(NULL); |
0506aecc NK |
286 | callchain_register_param(&callchain_param); |
287 | ||
288 | err = add_hist_entries(hists, machine); | |
289 | if (err < 0) | |
290 | goto out; | |
291 | ||
292 | err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0); | |
293 | ||
294 | out: | |
295 | del_hist_entries(hists); | |
296 | reset_output_field(); | |
297 | return err; | |
298 | } | |
299 | ||
300 | /* callcain + NO children */ | |
301 | static int test2(struct perf_evsel *evsel, struct machine *machine) | |
302 | { | |
303 | int err; | |
4ea062ed | 304 | struct hists *hists = evsel__hists(evsel); |
0506aecc NK |
305 | /* |
306 | * expected output: | |
307 | * | |
308 | * Overhead Command Shared Object Symbol | |
309 | * ======== ======= ============= ============== | |
310 | * 20.00% perf perf [.] main | |
311 | * | | |
312 | * --- main | |
313 | * | |
314 | * 10.00% bash [kernel] [k] page_fault | |
315 | * | | |
316 | * --- page_fault | |
317 | * malloc | |
318 | * main | |
319 | * | |
320 | * 10.00% bash bash [.] main | |
321 | * | | |
322 | * --- main | |
323 | * | |
324 | * 10.00% bash bash [.] xmalloc | |
325 | * | | |
326 | * --- xmalloc | |
327 | * malloc | |
328 | * xmalloc <--- NOTE: there's a cycle | |
329 | * malloc | |
330 | * xmalloc | |
331 | * main | |
332 | * | |
333 | * 10.00% perf [kernel] [k] page_fault | |
334 | * | | |
335 | * --- page_fault | |
336 | * sys_perf_event_open | |
337 | * run_command | |
338 | * main | |
339 | * | |
340 | * 10.00% perf [kernel] [k] schedule | |
341 | * | | |
342 | * --- schedule | |
343 | * run_command | |
344 | * main | |
345 | * | |
346 | * 10.00% perf libc [.] free | |
347 | * | | |
348 | * --- free | |
349 | * cmd_record | |
350 | * run_command | |
351 | * main | |
352 | * | |
353 | * 10.00% perf libc [.] malloc | |
354 | * | | |
355 | * --- malloc | |
356 | * cmd_record | |
357 | * run_command | |
358 | * main | |
359 | * | |
360 | * 10.00% perf perf [.] cmd_record | |
361 | * | | |
362 | * --- cmd_record | |
363 | * run_command | |
364 | * main | |
365 | * | |
366 | */ | |
367 | struct result expected[] = { | |
368 | { 0, 2000, "perf", "perf", "main" }, | |
369 | { 0, 1000, "bash", "[kernel]", "page_fault" }, | |
370 | { 0, 1000, "bash", "bash", "main" }, | |
371 | { 0, 1000, "bash", "bash", "xmalloc" }, | |
372 | { 0, 1000, "perf", "[kernel]", "page_fault" }, | |
373 | { 0, 1000, "perf", "[kernel]", "schedule" }, | |
374 | { 0, 1000, "perf", "libc", "free" }, | |
375 | { 0, 1000, "perf", "libc", "malloc" }, | |
376 | { 0, 1000, "perf", "perf", "cmd_record" }, | |
377 | }; | |
378 | struct callchain_result expected_callchain[] = { | |
379 | { | |
380 | 1, { { "perf", "main" }, }, | |
381 | }, | |
382 | { | |
383 | 3, { { "[kernel]", "page_fault" }, | |
384 | { "libc", "malloc" }, | |
385 | { "bash", "main" }, }, | |
386 | }, | |
387 | { | |
388 | 1, { { "bash", "main" }, }, | |
389 | }, | |
390 | { | |
391 | 6, { { "bash", "xmalloc" }, | |
392 | { "libc", "malloc" }, | |
393 | { "bash", "xmalloc" }, | |
394 | { "libc", "malloc" }, | |
395 | { "bash", "xmalloc" }, | |
396 | { "bash", "main" }, }, | |
397 | }, | |
398 | { | |
399 | 4, { { "[kernel]", "page_fault" }, | |
400 | { "[kernel]", "sys_perf_event_open" }, | |
401 | { "perf", "run_command" }, | |
402 | { "perf", "main" }, }, | |
403 | }, | |
404 | { | |
405 | 3, { { "[kernel]", "schedule" }, | |
406 | { "perf", "run_command" }, | |
407 | { "perf", "main" }, }, | |
408 | }, | |
409 | { | |
410 | 4, { { "libc", "free" }, | |
411 | { "perf", "cmd_record" }, | |
412 | { "perf", "run_command" }, | |
413 | { "perf", "main" }, }, | |
414 | }, | |
415 | { | |
416 | 4, { { "libc", "malloc" }, | |
417 | { "perf", "cmd_record" }, | |
418 | { "perf", "run_command" }, | |
419 | { "perf", "main" }, }, | |
420 | }, | |
421 | { | |
422 | 3, { { "perf", "cmd_record" }, | |
423 | { "perf", "run_command" }, | |
424 | { "perf", "main" }, }, | |
425 | }, | |
426 | }; | |
427 | ||
428 | symbol_conf.use_callchain = true; | |
429 | symbol_conf.cumulate_callchain = false; | |
f9db0d0f | 430 | perf_evsel__set_sample_bit(evsel, CALLCHAIN); |
0506aecc | 431 | |
40184c46 | 432 | setup_sorting(NULL); |
0506aecc NK |
433 | callchain_register_param(&callchain_param); |
434 | ||
435 | err = add_hist_entries(hists, machine); | |
436 | if (err < 0) | |
437 | goto out; | |
438 | ||
439 | err = do_test(hists, expected, ARRAY_SIZE(expected), | |
440 | expected_callchain, ARRAY_SIZE(expected_callchain)); | |
441 | ||
442 | out: | |
443 | del_hist_entries(hists); | |
444 | reset_output_field(); | |
445 | return err; | |
446 | } | |
447 | ||
448 | /* NO callchain + children */ | |
449 | static int test3(struct perf_evsel *evsel, struct machine *machine) | |
450 | { | |
451 | int err; | |
4ea062ed | 452 | struct hists *hists = evsel__hists(evsel); |
0506aecc NK |
453 | /* |
454 | * expected output: | |
455 | * | |
456 | * Children Self Command Shared Object Symbol | |
457 | * ======== ======== ======= ============= ======================= | |
458 | * 70.00% 20.00% perf perf [.] main | |
459 | * 50.00% 0.00% perf perf [.] run_command | |
460 | * 30.00% 10.00% bash bash [.] main | |
461 | * 30.00% 10.00% perf perf [.] cmd_record | |
462 | * 20.00% 0.00% bash libc [.] malloc | |
463 | * 10.00% 10.00% bash [kernel] [k] page_fault | |
5ca82710 | 464 | * 10.00% 10.00% bash bash [.] xmalloc |
0506aecc | 465 | * 10.00% 10.00% perf [kernel] [k] page_fault |
0506aecc | 466 | * 10.00% 10.00% perf libc [.] malloc |
5ca82710 NK |
467 | * 10.00% 10.00% perf [kernel] [k] schedule |
468 | * 10.00% 10.00% perf libc [.] free | |
469 | * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open | |
0506aecc NK |
470 | */ |
471 | struct result expected[] = { | |
472 | { 7000, 2000, "perf", "perf", "main" }, | |
473 | { 5000, 0, "perf", "perf", "run_command" }, | |
474 | { 3000, 1000, "bash", "bash", "main" }, | |
475 | { 3000, 1000, "perf", "perf", "cmd_record" }, | |
476 | { 2000, 0, "bash", "libc", "malloc" }, | |
477 | { 1000, 1000, "bash", "[kernel]", "page_fault" }, | |
5ca82710 | 478 | { 1000, 1000, "bash", "bash", "xmalloc" }, |
0506aecc | 479 | { 1000, 1000, "perf", "[kernel]", "page_fault" }, |
5ca82710 | 480 | { 1000, 1000, "perf", "[kernel]", "schedule" }, |
0506aecc NK |
481 | { 1000, 1000, "perf", "libc", "free" }, |
482 | { 1000, 1000, "perf", "libc", "malloc" }, | |
5ca82710 | 483 | { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" }, |
0506aecc NK |
484 | }; |
485 | ||
486 | symbol_conf.use_callchain = false; | |
487 | symbol_conf.cumulate_callchain = true; | |
f9db0d0f | 488 | perf_evsel__reset_sample_bit(evsel, CALLCHAIN); |
0506aecc | 489 | |
40184c46 | 490 | setup_sorting(NULL); |
0506aecc NK |
491 | callchain_register_param(&callchain_param); |
492 | ||
493 | err = add_hist_entries(hists, machine); | |
494 | if (err < 0) | |
495 | goto out; | |
496 | ||
497 | err = do_test(hists, expected, ARRAY_SIZE(expected), NULL, 0); | |
498 | ||
499 | out: | |
500 | del_hist_entries(hists); | |
501 | reset_output_field(); | |
502 | return err; | |
503 | } | |
504 | ||
505 | /* callchain + children */ | |
506 | static int test4(struct perf_evsel *evsel, struct machine *machine) | |
507 | { | |
508 | int err; | |
4ea062ed | 509 | struct hists *hists = evsel__hists(evsel); |
0506aecc NK |
510 | /* |
511 | * expected output: | |
512 | * | |
513 | * Children Self Command Shared Object Symbol | |
514 | * ======== ======== ======= ============= ======================= | |
515 | * 70.00% 20.00% perf perf [.] main | |
516 | * | | |
517 | * --- main | |
518 | * | |
519 | * 50.00% 0.00% perf perf [.] run_command | |
520 | * | | |
521 | * --- run_command | |
522 | * main | |
523 | * | |
524 | * 30.00% 10.00% bash bash [.] main | |
525 | * | | |
526 | * --- main | |
527 | * | |
528 | * 30.00% 10.00% perf perf [.] cmd_record | |
529 | * | | |
530 | * --- cmd_record | |
531 | * run_command | |
532 | * main | |
533 | * | |
534 | * 20.00% 0.00% bash libc [.] malloc | |
535 | * | | |
536 | * --- malloc | |
537 | * | | |
538 | * |--50.00%-- xmalloc | |
539 | * | main | |
540 | * --50.00%-- main | |
541 | * | |
542 | * 10.00% 10.00% bash [kernel] [k] page_fault | |
543 | * | | |
544 | * --- page_fault | |
545 | * malloc | |
546 | * main | |
547 | * | |
5ca82710 | 548 | * 10.00% 10.00% bash bash [.] xmalloc |
0506aecc | 549 | * | |
5ca82710 NK |
550 | * --- xmalloc |
551 | * malloc | |
552 | * xmalloc <--- NOTE: there's a cycle | |
553 | * malloc | |
554 | * xmalloc | |
0506aecc NK |
555 | * main |
556 | * | |
557 | * 10.00% 0.00% perf [kernel] [k] sys_perf_event_open | |
558 | * | | |
559 | * --- sys_perf_event_open | |
560 | * run_command | |
561 | * main | |
562 | * | |
563 | * 10.00% 10.00% perf [kernel] [k] page_fault | |
564 | * | | |
565 | * --- page_fault | |
566 | * sys_perf_event_open | |
567 | * run_command | |
568 | * main | |
569 | * | |
5ca82710 NK |
570 | * 10.00% 10.00% perf [kernel] [k] schedule |
571 | * | | |
572 | * --- schedule | |
573 | * run_command | |
574 | * main | |
575 | * | |
0506aecc NK |
576 | * 10.00% 10.00% perf libc [.] free |
577 | * | | |
578 | * --- free | |
579 | * cmd_record | |
580 | * run_command | |
581 | * main | |
582 | * | |
583 | * 10.00% 10.00% perf libc [.] malloc | |
584 | * | | |
585 | * --- malloc | |
586 | * cmd_record | |
587 | * run_command | |
588 | * main | |
589 | * | |
0506aecc NK |
590 | */ |
591 | struct result expected[] = { | |
592 | { 7000, 2000, "perf", "perf", "main" }, | |
593 | { 5000, 0, "perf", "perf", "run_command" }, | |
594 | { 3000, 1000, "bash", "bash", "main" }, | |
595 | { 3000, 1000, "perf", "perf", "cmd_record" }, | |
596 | { 2000, 0, "bash", "libc", "malloc" }, | |
597 | { 1000, 1000, "bash", "[kernel]", "page_fault" }, | |
5ca82710 | 598 | { 1000, 1000, "bash", "bash", "xmalloc" }, |
0506aecc NK |
599 | { 1000, 0, "perf", "[kernel]", "sys_perf_event_open" }, |
600 | { 1000, 1000, "perf", "[kernel]", "page_fault" }, | |
5ca82710 | 601 | { 1000, 1000, "perf", "[kernel]", "schedule" }, |
0506aecc NK |
602 | { 1000, 1000, "perf", "libc", "free" }, |
603 | { 1000, 1000, "perf", "libc", "malloc" }, | |
0506aecc NK |
604 | }; |
605 | struct callchain_result expected_callchain[] = { | |
606 | { | |
607 | 1, { { "perf", "main" }, }, | |
608 | }, | |
609 | { | |
610 | 2, { { "perf", "run_command" }, | |
611 | { "perf", "main" }, }, | |
612 | }, | |
613 | { | |
614 | 1, { { "bash", "main" }, }, | |
615 | }, | |
616 | { | |
617 | 3, { { "perf", "cmd_record" }, | |
618 | { "perf", "run_command" }, | |
619 | { "perf", "main" }, }, | |
620 | }, | |
621 | { | |
622 | 4, { { "libc", "malloc" }, | |
623 | { "bash", "xmalloc" }, | |
624 | { "bash", "main" }, | |
625 | { "bash", "main" }, }, | |
626 | }, | |
627 | { | |
628 | 3, { { "[kernel]", "page_fault" }, | |
629 | { "libc", "malloc" }, | |
630 | { "bash", "main" }, }, | |
631 | }, | |
632 | { | |
5ca82710 NK |
633 | 6, { { "bash", "xmalloc" }, |
634 | { "libc", "malloc" }, | |
635 | { "bash", "xmalloc" }, | |
636 | { "libc", "malloc" }, | |
637 | { "bash", "xmalloc" }, | |
638 | { "bash", "main" }, }, | |
0506aecc NK |
639 | }, |
640 | { | |
641 | 3, { { "[kernel]", "sys_perf_event_open" }, | |
642 | { "perf", "run_command" }, | |
643 | { "perf", "main" }, }, | |
644 | }, | |
645 | { | |
646 | 4, { { "[kernel]", "page_fault" }, | |
647 | { "[kernel]", "sys_perf_event_open" }, | |
648 | { "perf", "run_command" }, | |
649 | { "perf", "main" }, }, | |
650 | }, | |
5ca82710 NK |
651 | { |
652 | 3, { { "[kernel]", "schedule" }, | |
653 | { "perf", "run_command" }, | |
654 | { "perf", "main" }, }, | |
655 | }, | |
0506aecc NK |
656 | { |
657 | 4, { { "libc", "free" }, | |
658 | { "perf", "cmd_record" }, | |
659 | { "perf", "run_command" }, | |
660 | { "perf", "main" }, }, | |
661 | }, | |
662 | { | |
663 | 4, { { "libc", "malloc" }, | |
664 | { "perf", "cmd_record" }, | |
665 | { "perf", "run_command" }, | |
666 | { "perf", "main" }, }, | |
667 | }, | |
0506aecc NK |
668 | }; |
669 | ||
670 | symbol_conf.use_callchain = true; | |
671 | symbol_conf.cumulate_callchain = true; | |
f9db0d0f | 672 | perf_evsel__set_sample_bit(evsel, CALLCHAIN); |
0506aecc | 673 | |
40184c46 | 674 | setup_sorting(NULL); |
0506aecc NK |
675 | callchain_register_param(&callchain_param); |
676 | ||
677 | err = add_hist_entries(hists, machine); | |
678 | if (err < 0) | |
679 | goto out; | |
680 | ||
681 | err = do_test(hists, expected, ARRAY_SIZE(expected), | |
682 | expected_callchain, ARRAY_SIZE(expected_callchain)); | |
683 | ||
684 | out: | |
685 | del_hist_entries(hists); | |
686 | reset_output_field(); | |
687 | return err; | |
688 | } | |
689 | ||
721a1f53 | 690 | int test__hists_cumulate(int subtest __maybe_unused) |
0506aecc NK |
691 | { |
692 | int err = TEST_FAIL; | |
693 | struct machines machines; | |
694 | struct machine *machine; | |
695 | struct perf_evsel *evsel; | |
696 | struct perf_evlist *evlist = perf_evlist__new(); | |
697 | size_t i; | |
698 | test_fn_t testcases[] = { | |
699 | test1, | |
700 | test2, | |
701 | test3, | |
702 | test4, | |
703 | }; | |
704 | ||
705 | TEST_ASSERT_VAL("No memory", evlist); | |
706 | ||
b39b8393 | 707 | err = parse_events(evlist, "cpu-clock", NULL); |
0506aecc NK |
708 | if (err) |
709 | goto out; | |
b0500c16 | 710 | err = TEST_FAIL; |
0506aecc NK |
711 | |
712 | machines__init(&machines); | |
713 | ||
714 | /* setup threads/dso/map/symbols also */ | |
715 | machine = setup_fake_machine(&machines); | |
716 | if (!machine) | |
717 | goto out; | |
718 | ||
719 | if (verbose > 1) | |
720 | machine__fprintf(machine, stderr); | |
721 | ||
722 | evsel = perf_evlist__first(evlist); | |
723 | ||
724 | for (i = 0; i < ARRAY_SIZE(testcases); i++) { | |
725 | err = testcases[i](evsel, machine); | |
726 | if (err < 0) | |
727 | break; | |
728 | } | |
729 | ||
730 | out: | |
731 | /* tear down everything */ | |
732 | perf_evlist__delete(evlist); | |
733 | machines__exit(&machines); | |
734 | ||
735 | return err; | |
736 | } |