blob: 34c61e4d3352fbfd6984d5b30c05e6d32bf2d64c [file] [log] [blame]
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +09001#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"
Namhyung Kim6e344a92014-04-25 12:28:13 +090011#include "hists_common.h"
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090012
13struct sample {
14 u32 pid;
15 u64 ip;
16 struct thread *thread;
17 struct map *map;
18 struct symbol *sym;
19};
20
Namhyung Kim6e344a92014-04-25 12:28:13 +090021/* For the numbers, see hists_common.c */
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090022static struct sample fake_common_samples[] = {
23 /* perf [kernel] schedule() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090024 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090025 /* perf [perf] main() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090026 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090027 /* perf [perf] cmd_record() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090028 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090029 /* bash [bash] xmalloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090030 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090031 /* bash [libc] malloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090032 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090033};
34
35static struct sample fake_samples[][5] = {
36 {
37 /* perf [perf] run_command() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090038 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090039 /* perf [libc] malloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090040 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090041 /* perf [kernel] page_fault() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090042 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090043 /* perf [kernel] sys_perf_event_open() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090044 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090045 /* bash [libc] free() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090046 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090047 },
48 {
49 /* perf [libc] free() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090050 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090051 /* bash [libc] malloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090052 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, /* will be merged */
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090053 /* bash [bash] xfee() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090054 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090055 /* bash [libc] realloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090056 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090057 /* bash [kernel] page_fault() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090058 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090059 },
60};
61
62static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
63{
64 struct perf_evsel *evsel;
65 struct addr_location al;
66 struct hist_entry *he;
Namhyung Kima1891aa2014-05-23 14:59:57 +090067 struct perf_sample sample = { .period = 1, };
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090068 size_t i = 0, k;
69
70 /*
71 * each evsel will have 10 samples - 5 common and 5 distinct.
72 * However the second evsel also has a collapsed entry for
73 * "bash [libc] malloc" so total 9 entries will be in the tree.
74 */
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030075 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -030076 struct hists *hists = evsel__hists(evsel);
77
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090078 for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) {
79 const union perf_event event = {
Adrian Hunteref893252013-08-27 11:23:06 +030080 .header = {
81 .misc = PERF_RECORD_MISC_USER,
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090082 },
83 };
84
Adrian Hunteref893252013-08-27 11:23:06 +030085 sample.pid = fake_common_samples[k].pid;
Namhyung Kim13ce34d2014-05-12 09:56:42 +090086 sample.tid = fake_common_samples[k].pid;
Adrian Hunteref893252013-08-27 11:23:06 +030087 sample.ip = fake_common_samples[k].ip;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090088 if (perf_event__preprocess_sample(&event, machine, &al,
Adrian Huntere44baa32013-08-08 14:32:25 +030089 &sample) < 0)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090090 goto out;
91
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -030092 he = __hists__add_entry(hists, &al, NULL,
Namhyung Kima0b51af2012-09-11 13:34:27 +090093 NULL, NULL, 1, 1, 0, true);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090094 if (he == NULL)
95 goto out;
96
97 fake_common_samples[k].thread = al.thread;
98 fake_common_samples[k].map = al.map;
99 fake_common_samples[k].sym = al.sym;
100 }
101
102 for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) {
103 const union perf_event event = {
Adrian Hunteref893252013-08-27 11:23:06 +0300104 .header = {
105 .misc = PERF_RECORD_MISC_USER,
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900106 },
107 };
108
Adrian Hunteref893252013-08-27 11:23:06 +0300109 sample.pid = fake_samples[i][k].pid;
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900110 sample.tid = fake_samples[i][k].pid;
Adrian Hunteref893252013-08-27 11:23:06 +0300111 sample.ip = fake_samples[i][k].ip;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900112 if (perf_event__preprocess_sample(&event, machine, &al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300113 &sample) < 0)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900114 goto out;
115
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300116 he = __hists__add_entry(hists, &al, NULL,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900117 NULL, NULL, 1, 1, 0, true);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900118 if (he == NULL)
119 goto out;
120
121 fake_samples[i][k].thread = al.thread;
122 fake_samples[i][k].map = al.map;
123 fake_samples[i][k].sym = al.sym;
124 }
125 i++;
126 }
127
128 return 0;
129
130out:
131 pr_debug("Not enough memory for adding a hist entry\n");
132 return -1;
133}
134
135static int find_sample(struct sample *samples, size_t nr_samples,
136 struct thread *t, struct map *m, struct symbol *s)
137{
138 while (nr_samples--) {
139 if (samples->thread == t && samples->map == m &&
140 samples->sym == s)
141 return 1;
142 samples++;
143 }
144 return 0;
145}
146
147static int __validate_match(struct hists *hists)
148{
149 size_t count = 0;
150 struct rb_root *root;
151 struct rb_node *node;
152
153 /*
154 * Only entries from fake_common_samples should have a pair.
155 */
156 if (sort__need_collapse)
157 root = &hists->entries_collapsed;
158 else
159 root = hists->entries_in;
160
161 node = rb_first(root);
162 while (node) {
163 struct hist_entry *he;
164
165 he = rb_entry(node, struct hist_entry, rb_node_in);
166
167 if (hist_entry__has_pairs(he)) {
168 if (find_sample(fake_common_samples,
169 ARRAY_SIZE(fake_common_samples),
170 he->thread, he->ms.map, he->ms.sym)) {
171 count++;
172 } else {
173 pr_debug("Can't find the matched entry\n");
174 return -1;
175 }
176 }
177
178 node = rb_next(node);
179 }
180
181 if (count != ARRAY_SIZE(fake_common_samples)) {
182 pr_debug("Invalid count for matched entries: %zd of %zd\n",
183 count, ARRAY_SIZE(fake_common_samples));
184 return -1;
185 }
186
187 return 0;
188}
189
190static int validate_match(struct hists *leader, struct hists *other)
191{
192 return __validate_match(leader) || __validate_match(other);
193}
194
195static int __validate_link(struct hists *hists, int idx)
196{
197 size_t count = 0;
198 size_t count_pair = 0;
199 size_t count_dummy = 0;
200 struct rb_root *root;
201 struct rb_node *node;
202
203 /*
204 * Leader hists (idx = 0) will have dummy entries from other,
205 * and some entries will have no pair. However every entry
206 * in other hists should have (dummy) pair.
207 */
208 if (sort__need_collapse)
209 root = &hists->entries_collapsed;
210 else
211 root = hists->entries_in;
212
213 node = rb_first(root);
214 while (node) {
215 struct hist_entry *he;
216
217 he = rb_entry(node, struct hist_entry, rb_node_in);
218
219 if (hist_entry__has_pairs(he)) {
220 if (!find_sample(fake_common_samples,
221 ARRAY_SIZE(fake_common_samples),
222 he->thread, he->ms.map, he->ms.sym) &&
223 !find_sample(fake_samples[idx],
224 ARRAY_SIZE(fake_samples[idx]),
225 he->thread, he->ms.map, he->ms.sym)) {
226 count_dummy++;
227 }
228 count_pair++;
229 } else if (idx) {
230 pr_debug("A entry from the other hists should have pair\n");
231 return -1;
232 }
233
234 count++;
235 node = rb_next(node);
236 }
237
238 /*
239 * Note that we have a entry collapsed in the other (idx = 1) hists.
240 */
241 if (idx == 0) {
242 if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) {
243 pr_debug("Invalid count of dummy entries: %zd of %zd\n",
244 count_dummy, ARRAY_SIZE(fake_samples[1]) - 1);
245 return -1;
246 }
247 if (count != count_pair + ARRAY_SIZE(fake_samples[0])) {
248 pr_debug("Invalid count of total leader entries: %zd of %zd\n",
249 count, count_pair + ARRAY_SIZE(fake_samples[0]));
250 return -1;
251 }
252 } else {
253 if (count != count_pair) {
254 pr_debug("Invalid count of total other entries: %zd of %zd\n",
255 count, count_pair);
256 return -1;
257 }
258 if (count_dummy > 0) {
259 pr_debug("Other hists should not have dummy entries: %zd\n",
260 count_dummy);
261 return -1;
262 }
263 }
264
265 return 0;
266}
267
268static int validate_link(struct hists *leader, struct hists *other)
269{
270 return __validate_link(leader, 0) || __validate_link(other, 1);
271}
272
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900273int test__hists_link(void)
274{
275 int err = -1;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300276 struct hists *hists, *first_hists;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300277 struct machines machines;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900278 struct machine *machine = NULL;
279 struct perf_evsel *evsel, *first;
Namhyung Kim334fe7a2013-03-11 16:43:12 +0900280 struct perf_evlist *evlist = perf_evlist__new();
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900281
282 if (evlist == NULL)
283 return -ENOMEM;
284
Jiri Olsab39b8392015-04-22 21:10:16 +0200285 err = parse_events(evlist, "cpu-clock", NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900286 if (err)
287 goto out;
Jiri Olsab39b8392015-04-22 21:10:16 +0200288 err = parse_events(evlist, "task-clock", NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900289 if (err)
290 goto out;
291
292 /* default sort order (comm,dso,sym) will be used */
Namhyung Kim55309982013-02-06 14:57:16 +0900293 if (setup_sorting() < 0)
294 goto out;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900295
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300296 machines__init(&machines);
297
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900298 /* setup threads/dso/map/symbols also */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300299 machine = setup_fake_machine(&machines);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900300 if (!machine)
301 goto out;
302
303 if (verbose > 1)
304 machine__fprintf(machine, stderr);
305
306 /* process sample events */
307 err = add_hist_entries(evlist, machine);
308 if (err < 0)
309 goto out;
310
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300311 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300312 hists = evsel__hists(evsel);
313 hists__collapse_resort(hists, NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900314
315 if (verbose > 2)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300316 print_hists_in(hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900317 }
318
319 first = perf_evlist__first(evlist);
320 evsel = perf_evlist__last(evlist);
321
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300322 first_hists = evsel__hists(first);
323 hists = evsel__hists(evsel);
324
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900325 /* match common entries */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300326 hists__match(first_hists, hists);
327 err = validate_match(first_hists, hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900328 if (err)
329 goto out;
330
331 /* link common and/or dummy entries */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300332 hists__link(first_hists, hists);
333 err = validate_link(first_hists, hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900334 if (err)
335 goto out;
336
337 err = 0;
338
339out:
340 /* tear down everything */
341 perf_evlist__delete(evlist);
Namhyung Kimf21d1812014-05-12 14:43:18 +0900342 reset_output_field();
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300343 machines__exit(&machines);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900344
345 return err;
346}