1 // SPDX-License-Identifier: GPL-2.0
9 #include "parse-events.h"
10 #include "hists_common.h"
11 #include "util/mmap.h"
13 #include <linux/kernel.h>
18 struct thread *thread;
23 /* For the numbers, see hists_common.c */
24 static struct sample fake_common_samples[] = {
25 /* perf [kernel] schedule() */
26 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
27 /* perf [perf] main() */
28 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
29 /* perf [perf] cmd_record() */
30 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, },
31 /* bash [bash] xmalloc() */
32 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
33 /* bash [libc] malloc() */
34 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, },
37 static struct sample fake_samples[][5] = {
39 /* perf [perf] run_command() */
40 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, },
41 /* perf [libc] malloc() */
42 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
43 /* perf [kernel] page_fault() */
44 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
45 /* perf [kernel] sys_perf_event_open() */
46 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, },
47 /* bash [libc] free() */
48 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, },
51 /* perf [libc] free() */
52 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, },
53 /* bash [libc] malloc() */
54 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, /* will be merged */
55 /* bash [bash] xfee() */
56 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, },
57 /* bash [libc] realloc() */
58 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, },
59 /* bash [kernel] page_fault() */
60 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
64 static int add_hist_entries(struct evlist *evlist, struct machine *machine)
67 struct addr_location al;
68 struct hist_entry *he;
69 struct perf_sample sample = { .period = 1, .weight = 1, };
73 * each evsel will have 10 samples - 5 common and 5 distinct.
74 * However the second evsel also has a collapsed entry for
75 * "bash [libc] malloc" so total 9 entries will be in the tree.
77 evlist__for_each_entry(evlist, evsel) {
78 struct hists *hists = evsel__hists(evsel);
80 for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) {
81 sample.cpumode = PERF_RECORD_MISC_USER;
82 sample.pid = fake_common_samples[k].pid;
83 sample.tid = fake_common_samples[k].pid;
84 sample.ip = fake_common_samples[k].ip;
86 if (machine__resolve(machine, &al, &sample) < 0)
89 he = hists__add_entry(hists, &al, NULL,
90 NULL, NULL, &sample, true);
92 addr_location__put(&al);
96 fake_common_samples[k].thread = al.thread;
97 fake_common_samples[k].map = al.map;
98 fake_common_samples[k].sym = al.sym;
101 for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) {
102 sample.pid = fake_samples[i][k].pid;
103 sample.tid = fake_samples[i][k].pid;
104 sample.ip = fake_samples[i][k].ip;
105 if (machine__resolve(machine, &al, &sample) < 0)
108 he = hists__add_entry(hists, &al, NULL,
109 NULL, NULL, &sample, true);
111 addr_location__put(&al);
115 fake_samples[i][k].thread = al.thread;
116 fake_samples[i][k].map = al.map;
117 fake_samples[i][k].sym = al.sym;
125 pr_debug("Not enough memory for adding a hist entry\n");
129 static int find_sample(struct sample *samples, size_t nr_samples,
130 struct thread *t, struct map *m, struct symbol *s)
132 while (nr_samples--) {
133 if (samples->thread == t && samples->map == m &&
141 static int __validate_match(struct hists *hists)
144 struct rb_root_cached *root;
145 struct rb_node *node;
148 * Only entries from fake_common_samples should have a pair.
150 if (hists__has(hists, need_collapse))
151 root = &hists->entries_collapsed;
153 root = hists->entries_in;
155 node = rb_first_cached(root);
157 struct hist_entry *he;
159 he = rb_entry(node, struct hist_entry, rb_node_in);
161 if (hist_entry__has_pairs(he)) {
162 if (find_sample(fake_common_samples,
163 ARRAY_SIZE(fake_common_samples),
164 he->thread, he->ms.map, he->ms.sym)) {
167 pr_debug("Can't find the matched entry\n");
172 node = rb_next(node);
175 if (count != ARRAY_SIZE(fake_common_samples)) {
176 pr_debug("Invalid count for matched entries: %zd of %zd\n",
177 count, ARRAY_SIZE(fake_common_samples));
184 static int validate_match(struct hists *leader, struct hists *other)
186 return __validate_match(leader) || __validate_match(other);
189 static int __validate_link(struct hists *hists, int idx)
192 size_t count_pair = 0;
193 size_t count_dummy = 0;
194 struct rb_root_cached *root;
195 struct rb_node *node;
198 * Leader hists (idx = 0) will have dummy entries from other,
199 * and some entries will have no pair. However every entry
200 * in other hists should have (dummy) pair.
202 if (hists__has(hists, need_collapse))
203 root = &hists->entries_collapsed;
205 root = hists->entries_in;
207 node = rb_first_cached(root);
209 struct hist_entry *he;
211 he = rb_entry(node, struct hist_entry, rb_node_in);
213 if (hist_entry__has_pairs(he)) {
214 if (!find_sample(fake_common_samples,
215 ARRAY_SIZE(fake_common_samples),
216 he->thread, he->ms.map, he->ms.sym) &&
217 !find_sample(fake_samples[idx],
218 ARRAY_SIZE(fake_samples[idx]),
219 he->thread, he->ms.map, he->ms.sym)) {
224 pr_debug("A entry from the other hists should have pair\n");
229 node = rb_next(node);
233 * Note that we have a entry collapsed in the other (idx = 1) hists.
236 if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) {
237 pr_debug("Invalid count of dummy entries: %zd of %zd\n",
238 count_dummy, ARRAY_SIZE(fake_samples[1]) - 1);
241 if (count != count_pair + ARRAY_SIZE(fake_samples[0])) {
242 pr_debug("Invalid count of total leader entries: %zd of %zd\n",
243 count, count_pair + ARRAY_SIZE(fake_samples[0]));
247 if (count != count_pair) {
248 pr_debug("Invalid count of total other entries: %zd of %zd\n",
252 if (count_dummy > 0) {
253 pr_debug("Other hists should not have dummy entries: %zd\n",
262 static int validate_link(struct hists *leader, struct hists *other)
264 return __validate_link(leader, 0) || __validate_link(other, 1);
267 static int test__hists_link(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
270 struct hists *hists, *first_hists;
271 struct machines machines;
272 struct machine *machine = NULL;
273 struct evsel *evsel, *first;
274 struct evlist *evlist = evlist__new();
279 err = parse_events(evlist, "cpu-clock", NULL);
282 err = parse_events(evlist, "task-clock", NULL);
287 /* default sort order (comm,dso,sym) will be used */
288 if (setup_sorting(NULL) < 0)
291 machines__init(&machines);
293 /* setup threads/dso/map/symbols also */
294 machine = setup_fake_machine(&machines);
299 machine__fprintf(machine, stderr);
301 /* process sample events */
302 err = add_hist_entries(evlist, machine);
306 evlist__for_each_entry(evlist, evsel) {
307 hists = evsel__hists(evsel);
308 hists__collapse_resort(hists, NULL);
311 print_hists_in(hists);
314 first = evlist__first(evlist);
315 evsel = evlist__last(evlist);
317 first_hists = evsel__hists(first);
318 hists = evsel__hists(evsel);
320 /* match common entries */
321 hists__match(first_hists, hists);
322 err = validate_match(first_hists, hists);
326 /* link common and/or dummy entries */
327 hists__link(first_hists, hists);
328 err = validate_link(first_hists, hists);
335 /* tear down everything */
336 evlist__delete(evlist);
337 reset_output_field();
338 machines__exit(&machines);
343 DEFINE_SUITE("Match and link multiple hists", hists_link);