1 // SPDX-License-Identifier: GPL-2.0
10 #include "parse-events.h"
12 #include "hists_common.h"
13 #include "util/mmap.h"
15 #include <linux/kernel.h>
20 struct thread
*thread
;
25 /* For the numbers, see hists_common.c */
26 static struct sample fake_common_samples
[] = {
27 /* perf [kernel] schedule() */
28 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_KERNEL_SCHEDULE
, },
29 /* perf [perf] main() */
30 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_PERF_MAIN
, },
31 /* perf [perf] cmd_record() */
32 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_PERF_CMD_RECORD
, },
33 /* bash [bash] xmalloc() */
34 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_BASH_XMALLOC
, },
35 /* bash [libc] malloc() */
36 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_MALLOC
, },
39 static struct sample fake_samples
[][5] = {
41 /* perf [perf] run_command() */
42 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_PERF_RUN_COMMAND
, },
43 /* perf [libc] malloc() */
44 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_LIBC_MALLOC
, },
45 /* perf [kernel] page_fault() */
46 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_KERNEL_PAGE_FAULT
, },
47 /* perf [kernel] sys_perf_event_open() */
48 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN
, },
49 /* bash [libc] free() */
50 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_FREE
, },
53 /* perf [libc] free() */
54 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_LIBC_FREE
, },
55 /* bash [libc] malloc() */
56 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_MALLOC
, }, /* will be merged */
57 /* bash [bash] xfee() */
58 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_BASH_XFREE
, },
59 /* bash [libc] realloc() */
60 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_REALLOC
, },
61 /* bash [kernel] page_fault() */
62 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_KERNEL_PAGE_FAULT
, },
66 static int add_hist_entries(struct evlist
*evlist
, struct machine
*machine
)
69 struct addr_location al
;
70 struct hist_entry
*he
;
71 struct perf_sample sample
= { .period
= 1, .weight
= 1, };
74 addr_location__init(&al
);
76 * each evsel will have 10 samples - 5 common and 5 distinct.
77 * However the second evsel also has a collapsed entry for
78 * "bash [libc] malloc" so total 9 entries will be in the tree.
80 evlist__for_each_entry(evlist
, evsel
) {
81 struct hists
*hists
= evsel__hists(evsel
);
83 for (k
= 0; k
< ARRAY_SIZE(fake_common_samples
); k
++) {
84 sample
.cpumode
= PERF_RECORD_MISC_USER
;
85 sample
.pid
= fake_common_samples
[k
].pid
;
86 sample
.tid
= fake_common_samples
[k
].pid
;
87 sample
.ip
= fake_common_samples
[k
].ip
;
89 if (machine__resolve(machine
, &al
, &sample
) < 0)
92 he
= hists__add_entry(hists
, &al
, NULL
,
93 NULL
, NULL
, NULL
, &sample
, true);
98 thread__put(fake_common_samples
[k
].thread
);
99 fake_common_samples
[k
].thread
= thread__get(al
.thread
);
100 map__put(fake_common_samples
[k
].map
);
101 fake_common_samples
[k
].map
= map__get(al
.map
);
102 fake_common_samples
[k
].sym
= al
.sym
;
105 for (k
= 0; k
< ARRAY_SIZE(fake_samples
[i
]); k
++) {
106 sample
.pid
= fake_samples
[i
][k
].pid
;
107 sample
.tid
= fake_samples
[i
][k
].pid
;
108 sample
.ip
= fake_samples
[i
][k
].ip
;
109 if (machine__resolve(machine
, &al
, &sample
) < 0)
112 he
= hists__add_entry(hists
, &al
, NULL
,
113 NULL
, NULL
, NULL
, &sample
, true);
118 thread__put(fake_samples
[i
][k
].thread
);
119 fake_samples
[i
][k
].thread
= thread__get(al
.thread
);
120 map__put(fake_samples
[i
][k
].map
);
121 fake_samples
[i
][k
].map
= map__get(al
.map
);
122 fake_samples
[i
][k
].sym
= al
.sym
;
127 addr_location__exit(&al
);
130 addr_location__exit(&al
);
131 pr_debug("Not enough memory for adding a hist entry\n");
135 static void put_fake_samples(void)
139 for (i
= 0; i
< ARRAY_SIZE(fake_common_samples
); i
++)
140 map__put(fake_common_samples
[i
].map
);
141 for (i
= 0; i
< ARRAY_SIZE(fake_samples
); i
++) {
142 for (j
= 0; j
< ARRAY_SIZE(fake_samples
[0]); j
++)
143 map__put(fake_samples
[i
][j
].map
);
147 static int find_sample(struct sample
*samples
, size_t nr_samples
,
148 struct thread
*t
, struct map
*m
, struct symbol
*s
)
150 while (nr_samples
--) {
151 if (RC_CHK_EQUAL(samples
->thread
, t
) &&
152 RC_CHK_EQUAL(samples
->map
, m
) &&
160 static int __validate_match(struct hists
*hists
)
163 struct rb_root_cached
*root
;
164 struct rb_node
*node
;
167 * Only entries from fake_common_samples should have a pair.
169 if (hists__has(hists
, need_collapse
))
170 root
= &hists
->entries_collapsed
;
172 root
= hists
->entries_in
;
174 node
= rb_first_cached(root
);
176 struct hist_entry
*he
;
178 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
180 if (hist_entry__has_pairs(he
)) {
181 if (find_sample(fake_common_samples
,
182 ARRAY_SIZE(fake_common_samples
),
183 he
->thread
, he
->ms
.map
, he
->ms
.sym
)) {
186 pr_debug("Can't find the matched entry\n");
191 node
= rb_next(node
);
194 if (count
!= ARRAY_SIZE(fake_common_samples
)) {
195 pr_debug("Invalid count for matched entries: %zd of %zd\n",
196 count
, ARRAY_SIZE(fake_common_samples
));
203 static int validate_match(struct hists
*leader
, struct hists
*other
)
205 return __validate_match(leader
) || __validate_match(other
);
208 static int __validate_link(struct hists
*hists
, int idx
)
211 size_t count_pair
= 0;
212 size_t count_dummy
= 0;
213 struct rb_root_cached
*root
;
214 struct rb_node
*node
;
217 * Leader hists (idx = 0) will have dummy entries from other,
218 * and some entries will have no pair. However every entry
219 * in other hists should have (dummy) pair.
221 if (hists__has(hists
, need_collapse
))
222 root
= &hists
->entries_collapsed
;
224 root
= hists
->entries_in
;
226 node
= rb_first_cached(root
);
228 struct hist_entry
*he
;
230 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
232 if (hist_entry__has_pairs(he
)) {
233 if (!find_sample(fake_common_samples
,
234 ARRAY_SIZE(fake_common_samples
),
235 he
->thread
, he
->ms
.map
, he
->ms
.sym
) &&
236 !find_sample(fake_samples
[idx
],
237 ARRAY_SIZE(fake_samples
[idx
]),
238 he
->thread
, he
->ms
.map
, he
->ms
.sym
)) {
243 pr_debug("A entry from the other hists should have pair\n");
248 node
= rb_next(node
);
252 * Note that we have a entry collapsed in the other (idx = 1) hists.
255 if (count_dummy
!= ARRAY_SIZE(fake_samples
[1]) - 1) {
256 pr_debug("Invalid count of dummy entries: %zd of %zd\n",
257 count_dummy
, ARRAY_SIZE(fake_samples
[1]) - 1);
260 if (count
!= count_pair
+ ARRAY_SIZE(fake_samples
[0])) {
261 pr_debug("Invalid count of total leader entries: %zd of %zd\n",
262 count
, count_pair
+ ARRAY_SIZE(fake_samples
[0]));
266 if (count
!= count_pair
) {
267 pr_debug("Invalid count of total other entries: %zd of %zd\n",
271 if (count_dummy
> 0) {
272 pr_debug("Other hists should not have dummy entries: %zd\n",
281 static int validate_link(struct hists
*leader
, struct hists
*other
)
283 return __validate_link(leader
, 0) || __validate_link(other
, 1);
286 static int test__hists_link(struct test_suite
*test __maybe_unused
, int subtest __maybe_unused
)
289 struct hists
*hists
, *first_hists
;
290 struct machines machines
;
291 struct machine
*machine
= NULL
;
292 struct evsel
*evsel
, *first
;
293 struct evlist
*evlist
= evlist__new();
298 err
= parse_event(evlist
, "cpu-clock");
301 err
= parse_event(evlist
, "task-clock");
306 /* default sort order (comm,dso,sym) will be used */
307 if (setup_sorting(NULL
) < 0)
310 machines__init(&machines
);
312 /* setup threads/dso/map/symbols also */
313 machine
= setup_fake_machine(&machines
);
318 machine__fprintf(machine
, stderr
);
320 /* process sample events */
321 err
= add_hist_entries(evlist
, machine
);
325 evlist__for_each_entry(evlist
, evsel
) {
326 hists
= evsel__hists(evsel
);
327 hists__collapse_resort(hists
, NULL
);
330 print_hists_in(hists
);
333 first
= evlist__first(evlist
);
334 evsel
= evlist__last(evlist
);
336 first_hists
= evsel__hists(first
);
337 hists
= evsel__hists(evsel
);
339 /* match common entries */
340 hists__match(first_hists
, hists
);
341 err
= validate_match(first_hists
, hists
);
345 /* link common and/or dummy entries */
346 hists__link(first_hists
, hists
);
347 err
= validate_link(first_hists
, hists
);
354 /* tear down everything */
355 evlist__delete(evlist
);
356 reset_output_field();
357 machines__exit(&machines
);
363 DEFINE_SUITE("Match and link multiple hists", hists_link
);