1 // SPDX-License-Identifier: GPL-2.0
11 #include "parse-events.h"
12 #include "hists_common.h"
14 #include <linux/kernel.h>
19 struct thread
*thread
;
24 /* For the numbers, see hists_common.c */
25 static struct sample fake_common_samples
[] = {
26 /* perf [kernel] schedule() */
27 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_KERNEL_SCHEDULE
, },
28 /* perf [perf] main() */
29 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_PERF_MAIN
, },
30 /* perf [perf] cmd_record() */
31 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_PERF_CMD_RECORD
, },
32 /* bash [bash] xmalloc() */
33 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_BASH_XMALLOC
, },
34 /* bash [libc] malloc() */
35 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_MALLOC
, },
38 static struct sample fake_samples
[][5] = {
40 /* perf [perf] run_command() */
41 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_PERF_RUN_COMMAND
, },
42 /* perf [libc] malloc() */
43 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_LIBC_MALLOC
, },
44 /* perf [kernel] page_fault() */
45 { .pid
= FAKE_PID_PERF1
, .ip
= FAKE_IP_KERNEL_PAGE_FAULT
, },
46 /* perf [kernel] sys_perf_event_open() */
47 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN
, },
48 /* bash [libc] free() */
49 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_FREE
, },
52 /* perf [libc] free() */
53 { .pid
= FAKE_PID_PERF2
, .ip
= FAKE_IP_LIBC_FREE
, },
54 /* bash [libc] malloc() */
55 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_MALLOC
, }, /* will be merged */
56 /* bash [bash] xfee() */
57 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_BASH_XFREE
, },
58 /* bash [libc] realloc() */
59 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_LIBC_REALLOC
, },
60 /* bash [kernel] page_fault() */
61 { .pid
= FAKE_PID_BASH
, .ip
= FAKE_IP_KERNEL_PAGE_FAULT
, },
65 static int add_hist_entries(struct perf_evlist
*evlist
, struct machine
*machine
)
67 struct perf_evsel
*evsel
;
68 struct addr_location al
;
69 struct hist_entry
*he
;
70 struct perf_sample sample
= { .period
= 1, .weight
= 1, };
74 * each evsel will have 10 samples - 5 common and 5 distinct.
75 * However the second evsel also has a collapsed entry for
76 * "bash [libc] malloc" so total 9 entries will be in the tree.
78 evlist__for_each_entry(evlist
, evsel
) {
79 struct hists
*hists
= evsel__hists(evsel
);
81 for (k
= 0; k
< ARRAY_SIZE(fake_common_samples
); k
++) {
82 sample
.cpumode
= PERF_RECORD_MISC_USER
;
83 sample
.pid
= fake_common_samples
[k
].pid
;
84 sample
.tid
= fake_common_samples
[k
].pid
;
85 sample
.ip
= fake_common_samples
[k
].ip
;
87 if (machine__resolve(machine
, &al
, &sample
) < 0)
90 he
= hists__add_entry(hists
, &al
, NULL
,
91 NULL
, NULL
, &sample
, true);
93 addr_location__put(&al
);
97 fake_common_samples
[k
].thread
= al
.thread
;
98 fake_common_samples
[k
].map
= al
.map
;
99 fake_common_samples
[k
].sym
= al
.sym
;
102 for (k
= 0; k
< ARRAY_SIZE(fake_samples
[i
]); k
++) {
103 sample
.pid
= fake_samples
[i
][k
].pid
;
104 sample
.tid
= fake_samples
[i
][k
].pid
;
105 sample
.ip
= fake_samples
[i
][k
].ip
;
106 if (machine__resolve(machine
, &al
, &sample
) < 0)
109 he
= hists__add_entry(hists
, &al
, NULL
,
110 NULL
, NULL
, &sample
, true);
112 addr_location__put(&al
);
116 fake_samples
[i
][k
].thread
= al
.thread
;
117 fake_samples
[i
][k
].map
= al
.map
;
118 fake_samples
[i
][k
].sym
= al
.sym
;
126 pr_debug("Not enough memory for adding a hist entry\n");
130 static int find_sample(struct sample
*samples
, size_t nr_samples
,
131 struct thread
*t
, struct map
*m
, struct symbol
*s
)
133 while (nr_samples
--) {
134 if (samples
->thread
== t
&& samples
->map
== m
&&
142 static int __validate_match(struct hists
*hists
)
145 struct rb_root
*root
;
146 struct rb_node
*node
;
149 * Only entries from fake_common_samples should have a pair.
151 if (hists__has(hists
, need_collapse
))
152 root
= &hists
->entries_collapsed
;
154 root
= hists
->entries_in
;
156 node
= rb_first(root
);
158 struct hist_entry
*he
;
160 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
162 if (hist_entry__has_pairs(he
)) {
163 if (find_sample(fake_common_samples
,
164 ARRAY_SIZE(fake_common_samples
),
165 he
->thread
, he
->ms
.map
, he
->ms
.sym
)) {
168 pr_debug("Can't find the matched entry\n");
173 node
= rb_next(node
);
176 if (count
!= ARRAY_SIZE(fake_common_samples
)) {
177 pr_debug("Invalid count for matched entries: %zd of %zd\n",
178 count
, ARRAY_SIZE(fake_common_samples
));
185 static int validate_match(struct hists
*leader
, struct hists
*other
)
187 return __validate_match(leader
) || __validate_match(other
);
190 static int __validate_link(struct hists
*hists
, int idx
)
193 size_t count_pair
= 0;
194 size_t count_dummy
= 0;
195 struct rb_root
*root
;
196 struct rb_node
*node
;
199 * Leader hists (idx = 0) will have dummy entries from other,
200 * and some entries will have no pair. However every entry
201 * in other hists should have (dummy) pair.
203 if (hists__has(hists
, need_collapse
))
204 root
= &hists
->entries_collapsed
;
206 root
= hists
->entries_in
;
208 node
= rb_first(root
);
210 struct hist_entry
*he
;
212 he
= rb_entry(node
, struct hist_entry
, rb_node_in
);
214 if (hist_entry__has_pairs(he
)) {
215 if (!find_sample(fake_common_samples
,
216 ARRAY_SIZE(fake_common_samples
),
217 he
->thread
, he
->ms
.map
, he
->ms
.sym
) &&
218 !find_sample(fake_samples
[idx
],
219 ARRAY_SIZE(fake_samples
[idx
]),
220 he
->thread
, he
->ms
.map
, he
->ms
.sym
)) {
225 pr_debug("A entry from the other hists should have pair\n");
230 node
= rb_next(node
);
234 * Note that we have a entry collapsed in the other (idx = 1) hists.
237 if (count_dummy
!= ARRAY_SIZE(fake_samples
[1]) - 1) {
238 pr_debug("Invalid count of dummy entries: %zd of %zd\n",
239 count_dummy
, ARRAY_SIZE(fake_samples
[1]) - 1);
242 if (count
!= count_pair
+ ARRAY_SIZE(fake_samples
[0])) {
243 pr_debug("Invalid count of total leader entries: %zd of %zd\n",
244 count
, count_pair
+ ARRAY_SIZE(fake_samples
[0]));
248 if (count
!= count_pair
) {
249 pr_debug("Invalid count of total other entries: %zd of %zd\n",
253 if (count_dummy
> 0) {
254 pr_debug("Other hists should not have dummy entries: %zd\n",
263 static int validate_link(struct hists
*leader
, struct hists
*other
)
265 return __validate_link(leader
, 0) || __validate_link(other
, 1);
268 int test__hists_link(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
271 struct hists
*hists
, *first_hists
;
272 struct machines machines
;
273 struct machine
*machine
= NULL
;
274 struct perf_evsel
*evsel
, *first
;
275 struct perf_evlist
*evlist
= perf_evlist__new();
280 err
= parse_events(evlist
, "cpu-clock", NULL
);
283 err
= parse_events(evlist
, "task-clock", NULL
);
288 /* default sort order (comm,dso,sym) will be used */
289 if (setup_sorting(NULL
) < 0)
292 machines__init(&machines
);
294 /* setup threads/dso/map/symbols also */
295 machine
= setup_fake_machine(&machines
);
300 machine__fprintf(machine
, stderr
);
302 /* process sample events */
303 err
= add_hist_entries(evlist
, machine
);
307 evlist__for_each_entry(evlist
, evsel
) {
308 hists
= evsel__hists(evsel
);
309 hists__collapse_resort(hists
, NULL
);
312 print_hists_in(hists
);
315 first
= perf_evlist__first(evlist
);
316 evsel
= perf_evlist__last(evlist
);
318 first_hists
= evsel__hists(first
);
319 hists
= evsel__hists(evsel
);
321 /* match common entries */
322 hists__match(first_hists
, hists
);
323 err
= validate_match(first_hists
, hists
);
327 /* link common and/or dummy entries */
328 hists__link(first_hists
, hists
);
329 err
= validate_link(first_hists
, hists
);
336 /* tear down everything */
337 perf_evlist__delete(evlist
);
338 reset_output_field();
339 machines__exit(&machines
);