]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/tests/hists_link.c
87f9f7280c4012ede9825c66abbce22628f4e394
[mirror_ubuntu-artful-kernel.git] / tools / perf / tests / hists_link.c
1 #include "perf.h"
2 #include "tests.h"
3 #include "debug.h"
4 #include "symbol.h"
5 #include "sort.h"
6 #include "evsel.h"
7 #include "evlist.h"
8 #include "machine.h"
9 #include "thread.h"
10 #include "parse-events.h"
11
12 static struct {
13 u32 pid;
14 const char *comm;
15 } fake_threads[] = {
16 { 100, "perf" },
17 { 200, "perf" },
18 { 300, "bash" },
19 };
20
21 static struct {
22 u32 pid;
23 u64 start;
24 const char *filename;
25 } fake_mmap_info[] = {
26 { 100, 0x40000, "perf" },
27 { 100, 0x50000, "libc" },
28 { 100, 0xf0000, "[kernel]" },
29 { 200, 0x40000, "perf" },
30 { 200, 0x50000, "libc" },
31 { 200, 0xf0000, "[kernel]" },
32 { 300, 0x40000, "bash" },
33 { 300, 0x50000, "libc" },
34 { 300, 0xf0000, "[kernel]" },
35 };
36
37 struct fake_sym {
38 u64 start;
39 u64 length;
40 const char *name;
41 };
42
43 static struct fake_sym perf_syms[] = {
44 { 700, 100, "main" },
45 { 800, 100, "run_command" },
46 { 900, 100, "cmd_record" },
47 };
48
49 static struct fake_sym bash_syms[] = {
50 { 700, 100, "main" },
51 { 800, 100, "xmalloc" },
52 { 900, 100, "xfree" },
53 };
54
55 static struct fake_sym libc_syms[] = {
56 { 700, 100, "malloc" },
57 { 800, 100, "free" },
58 { 900, 100, "realloc" },
59 };
60
61 static struct fake_sym kernel_syms[] = {
62 { 700, 100, "schedule" },
63 { 800, 100, "page_fault" },
64 { 900, 100, "sys_perf_event_open" },
65 };
66
67 static struct {
68 const char *dso_name;
69 struct fake_sym *syms;
70 size_t nr_syms;
71 } fake_symbols[] = {
72 { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
73 { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
74 { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
75 { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
76 };
77
78 static struct machine *setup_fake_machine(struct machines *machines)
79 {
80 struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
81 size_t i;
82
83 if (machine == NULL) {
84 pr_debug("Not enough memory for machine setup\n");
85 return NULL;
86 }
87
88 for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
89 struct thread *thread;
90
91 thread = machine__findnew_thread(machine, fake_threads[i].pid,
92 fake_threads[i].pid);
93 if (thread == NULL)
94 goto out;
95
96 thread__set_comm(thread, fake_threads[i].comm);
97 }
98
99 for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
100 union perf_event fake_mmap_event = {
101 .mmap = {
102 .header = { .misc = PERF_RECORD_MISC_USER, },
103 .pid = fake_mmap_info[i].pid,
104 .start = fake_mmap_info[i].start,
105 .len = 0x1000ULL,
106 .pgoff = 0ULL,
107 },
108 };
109
110 strcpy(fake_mmap_event.mmap.filename,
111 fake_mmap_info[i].filename);
112
113 machine__process_mmap_event(machine, &fake_mmap_event);
114 }
115
116 for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
117 size_t k;
118 struct dso *dso;
119
120 dso = __dsos__findnew(&machine->user_dsos,
121 fake_symbols[i].dso_name);
122 if (dso == NULL)
123 goto out;
124
125 /* emulate dso__load() */
126 dso__set_loaded(dso, MAP__FUNCTION);
127
128 for (k = 0; k < fake_symbols[i].nr_syms; k++) {
129 struct symbol *sym;
130 struct fake_sym *fsym = &fake_symbols[i].syms[k];
131
132 sym = symbol__new(fsym->start, fsym->length,
133 STB_GLOBAL, fsym->name);
134 if (sym == NULL)
135 goto out;
136
137 symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
138 }
139 }
140
141 return machine;
142
143 out:
144 pr_debug("Not enough memory for machine setup\n");
145 machine__delete_threads(machine);
146 machine__delete(machine);
147 return NULL;
148 }
149
150 struct sample {
151 u32 pid;
152 u64 ip;
153 struct thread *thread;
154 struct map *map;
155 struct symbol *sym;
156 };
157
158 static struct sample fake_common_samples[] = {
159 /* perf [kernel] schedule() */
160 { .pid = 100, .ip = 0xf0000 + 700, },
161 /* perf [perf] main() */
162 { .pid = 200, .ip = 0x40000 + 700, },
163 /* perf [perf] cmd_record() */
164 { .pid = 200, .ip = 0x40000 + 900, },
165 /* bash [bash] xmalloc() */
166 { .pid = 300, .ip = 0x40000 + 800, },
167 /* bash [libc] malloc() */
168 { .pid = 300, .ip = 0x50000 + 700, },
169 };
170
171 static struct sample fake_samples[][5] = {
172 {
173 /* perf [perf] run_command() */
174 { .pid = 100, .ip = 0x40000 + 800, },
175 /* perf [libc] malloc() */
176 { .pid = 100, .ip = 0x50000 + 700, },
177 /* perf [kernel] page_fault() */
178 { .pid = 100, .ip = 0xf0000 + 800, },
179 /* perf [kernel] sys_perf_event_open() */
180 { .pid = 200, .ip = 0xf0000 + 900, },
181 /* bash [libc] free() */
182 { .pid = 300, .ip = 0x50000 + 800, },
183 },
184 {
185 /* perf [libc] free() */
186 { .pid = 200, .ip = 0x50000 + 800, },
187 /* bash [libc] malloc() */
188 { .pid = 300, .ip = 0x50000 + 700, }, /* will be merged */
189 /* bash [bash] xfee() */
190 { .pid = 300, .ip = 0x40000 + 900, },
191 /* bash [libc] realloc() */
192 { .pid = 300, .ip = 0x50000 + 900, },
193 /* bash [kernel] page_fault() */
194 { .pid = 300, .ip = 0xf0000 + 800, },
195 },
196 };
197
198 static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
199 {
200 struct perf_evsel *evsel;
201 struct addr_location al;
202 struct hist_entry *he;
203 struct perf_sample sample = { .cpu = 0, };
204 size_t i = 0, k;
205
206 /*
207 * each evsel will have 10 samples - 5 common and 5 distinct.
208 * However the second evsel also has a collapsed entry for
209 * "bash [libc] malloc" so total 9 entries will be in the tree.
210 */
211 list_for_each_entry(evsel, &evlist->entries, node) {
212 for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) {
213 const union perf_event event = {
214 .ip = {
215 .header = {
216 .misc = PERF_RECORD_MISC_USER,
217 },
218 .pid = fake_common_samples[k].pid,
219 .ip = fake_common_samples[k].ip,
220 },
221 };
222
223 if (perf_event__preprocess_sample(&event, machine, &al,
224 &sample) < 0)
225 goto out;
226
227 he = __hists__add_entry(&evsel->hists, &al, NULL, 1, 1);
228 if (he == NULL)
229 goto out;
230
231 fake_common_samples[k].thread = al.thread;
232 fake_common_samples[k].map = al.map;
233 fake_common_samples[k].sym = al.sym;
234 }
235
236 for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) {
237 const union perf_event event = {
238 .ip = {
239 .header = {
240 .misc = PERF_RECORD_MISC_USER,
241 },
242 .pid = fake_samples[i][k].pid,
243 .ip = fake_samples[i][k].ip,
244 },
245 };
246
247 if (perf_event__preprocess_sample(&event, machine, &al,
248 &sample) < 0)
249 goto out;
250
251 he = __hists__add_entry(&evsel->hists, &al, NULL, 1, 1);
252 if (he == NULL)
253 goto out;
254
255 fake_samples[i][k].thread = al.thread;
256 fake_samples[i][k].map = al.map;
257 fake_samples[i][k].sym = al.sym;
258 }
259 i++;
260 }
261
262 return 0;
263
264 out:
265 pr_debug("Not enough memory for adding a hist entry\n");
266 return -1;
267 }
268
269 static int find_sample(struct sample *samples, size_t nr_samples,
270 struct thread *t, struct map *m, struct symbol *s)
271 {
272 while (nr_samples--) {
273 if (samples->thread == t && samples->map == m &&
274 samples->sym == s)
275 return 1;
276 samples++;
277 }
278 return 0;
279 }
280
281 static int __validate_match(struct hists *hists)
282 {
283 size_t count = 0;
284 struct rb_root *root;
285 struct rb_node *node;
286
287 /*
288 * Only entries from fake_common_samples should have a pair.
289 */
290 if (sort__need_collapse)
291 root = &hists->entries_collapsed;
292 else
293 root = hists->entries_in;
294
295 node = rb_first(root);
296 while (node) {
297 struct hist_entry *he;
298
299 he = rb_entry(node, struct hist_entry, rb_node_in);
300
301 if (hist_entry__has_pairs(he)) {
302 if (find_sample(fake_common_samples,
303 ARRAY_SIZE(fake_common_samples),
304 he->thread, he->ms.map, he->ms.sym)) {
305 count++;
306 } else {
307 pr_debug("Can't find the matched entry\n");
308 return -1;
309 }
310 }
311
312 node = rb_next(node);
313 }
314
315 if (count != ARRAY_SIZE(fake_common_samples)) {
316 pr_debug("Invalid count for matched entries: %zd of %zd\n",
317 count, ARRAY_SIZE(fake_common_samples));
318 return -1;
319 }
320
321 return 0;
322 }
323
324 static int validate_match(struct hists *leader, struct hists *other)
325 {
326 return __validate_match(leader) || __validate_match(other);
327 }
328
329 static int __validate_link(struct hists *hists, int idx)
330 {
331 size_t count = 0;
332 size_t count_pair = 0;
333 size_t count_dummy = 0;
334 struct rb_root *root;
335 struct rb_node *node;
336
337 /*
338 * Leader hists (idx = 0) will have dummy entries from other,
339 * and some entries will have no pair. However every entry
340 * in other hists should have (dummy) pair.
341 */
342 if (sort__need_collapse)
343 root = &hists->entries_collapsed;
344 else
345 root = hists->entries_in;
346
347 node = rb_first(root);
348 while (node) {
349 struct hist_entry *he;
350
351 he = rb_entry(node, struct hist_entry, rb_node_in);
352
353 if (hist_entry__has_pairs(he)) {
354 if (!find_sample(fake_common_samples,
355 ARRAY_SIZE(fake_common_samples),
356 he->thread, he->ms.map, he->ms.sym) &&
357 !find_sample(fake_samples[idx],
358 ARRAY_SIZE(fake_samples[idx]),
359 he->thread, he->ms.map, he->ms.sym)) {
360 count_dummy++;
361 }
362 count_pair++;
363 } else if (idx) {
364 pr_debug("A entry from the other hists should have pair\n");
365 return -1;
366 }
367
368 count++;
369 node = rb_next(node);
370 }
371
372 /*
373 * Note that we have a entry collapsed in the other (idx = 1) hists.
374 */
375 if (idx == 0) {
376 if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) {
377 pr_debug("Invalid count of dummy entries: %zd of %zd\n",
378 count_dummy, ARRAY_SIZE(fake_samples[1]) - 1);
379 return -1;
380 }
381 if (count != count_pair + ARRAY_SIZE(fake_samples[0])) {
382 pr_debug("Invalid count of total leader entries: %zd of %zd\n",
383 count, count_pair + ARRAY_SIZE(fake_samples[0]));
384 return -1;
385 }
386 } else {
387 if (count != count_pair) {
388 pr_debug("Invalid count of total other entries: %zd of %zd\n",
389 count, count_pair);
390 return -1;
391 }
392 if (count_dummy > 0) {
393 pr_debug("Other hists should not have dummy entries: %zd\n",
394 count_dummy);
395 return -1;
396 }
397 }
398
399 return 0;
400 }
401
402 static int validate_link(struct hists *leader, struct hists *other)
403 {
404 return __validate_link(leader, 0) || __validate_link(other, 1);
405 }
406
407 static void print_hists(struct hists *hists)
408 {
409 int i = 0;
410 struct rb_root *root;
411 struct rb_node *node;
412
413 if (sort__need_collapse)
414 root = &hists->entries_collapsed;
415 else
416 root = hists->entries_in;
417
418 pr_info("----- %s --------\n", __func__);
419 node = rb_first(root);
420 while (node) {
421 struct hist_entry *he;
422
423 he = rb_entry(node, struct hist_entry, rb_node_in);
424
425 pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
426 i, he->thread->comm, he->ms.map->dso->short_name,
427 he->ms.sym->name, he->stat.period);
428
429 i++;
430 node = rb_next(node);
431 }
432 }
433
434 int test__hists_link(void)
435 {
436 int err = -1;
437 struct machines machines;
438 struct machine *machine = NULL;
439 struct perf_evsel *evsel, *first;
440 struct perf_evlist *evlist = perf_evlist__new();
441
442 if (evlist == NULL)
443 return -ENOMEM;
444
445 err = parse_events(evlist, "cpu-clock");
446 if (err)
447 goto out;
448 err = parse_events(evlist, "task-clock");
449 if (err)
450 goto out;
451
452 /* default sort order (comm,dso,sym) will be used */
453 if (setup_sorting() < 0)
454 goto out;
455
456 machines__init(&machines);
457
458 /* setup threads/dso/map/symbols also */
459 machine = setup_fake_machine(&machines);
460 if (!machine)
461 goto out;
462
463 if (verbose > 1)
464 machine__fprintf(machine, stderr);
465
466 /* process sample events */
467 err = add_hist_entries(evlist, machine);
468 if (err < 0)
469 goto out;
470
471 list_for_each_entry(evsel, &evlist->entries, node) {
472 hists__collapse_resort(&evsel->hists);
473
474 if (verbose > 2)
475 print_hists(&evsel->hists);
476 }
477
478 first = perf_evlist__first(evlist);
479 evsel = perf_evlist__last(evlist);
480
481 /* match common entries */
482 hists__match(&first->hists, &evsel->hists);
483 err = validate_match(&first->hists, &evsel->hists);
484 if (err)
485 goto out;
486
487 /* link common and/or dummy entries */
488 hists__link(&first->hists, &evsel->hists);
489 err = validate_link(&first->hists, &evsel->hists);
490 if (err)
491 goto out;
492
493 err = 0;
494
495 out:
496 /* tear down everything */
497 perf_evlist__delete(evlist);
498 machines__exit(&machines);
499
500 return err;
501 }