blob: acf5a1301c0771eafaee8775e10ad470fd0d6b07 [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 Kimfd36f3d2015-12-23 02:06:58 +090067 struct perf_sample sample = { .period = 1, .weight = 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++) {
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -030079 sample.cpumode = PERF_RECORD_MISC_USER;
Adrian Hunteref893252013-08-27 11:23:06 +030080 sample.pid = fake_common_samples[k].pid;
Namhyung Kim13ce34d2014-05-12 09:56:42 +090081 sample.tid = fake_common_samples[k].pid;
Adrian Hunteref893252013-08-27 11:23:06 +030082 sample.ip = fake_common_samples[k].ip;
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -030083
84 if (machine__resolve(machine, &al, &sample) < 0)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090085 goto out;
86
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -030087 he = __hists__add_entry(hists, &al, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +090088 NULL, NULL, &sample, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030089 if (he == NULL) {
90 addr_location__put(&al);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090091 goto out;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030092 }
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090093
94 fake_common_samples[k].thread = al.thread;
95 fake_common_samples[k].map = al.map;
96 fake_common_samples[k].sym = al.sym;
97 }
98
99 for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) {
Adrian Hunteref893252013-08-27 11:23:06 +0300100 sample.pid = fake_samples[i][k].pid;
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900101 sample.tid = fake_samples[i][k].pid;
Adrian Hunteref893252013-08-27 11:23:06 +0300102 sample.ip = fake_samples[i][k].ip;
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300103 if (machine__resolve(machine, &al, &sample) < 0)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900104 goto out;
105
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300106 he = __hists__add_entry(hists, &al, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900107 NULL, NULL, &sample, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300108 if (he == NULL) {
109 addr_location__put(&al);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900110 goto out;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300111 }
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900112
113 fake_samples[i][k].thread = al.thread;
114 fake_samples[i][k].map = al.map;
115 fake_samples[i][k].sym = al.sym;
116 }
117 i++;
118 }
119
120 return 0;
121
122out:
123 pr_debug("Not enough memory for adding a hist entry\n");
124 return -1;
125}
126
127static int find_sample(struct sample *samples, size_t nr_samples,
128 struct thread *t, struct map *m, struct symbol *s)
129{
130 while (nr_samples--) {
131 if (samples->thread == t && samples->map == m &&
132 samples->sym == s)
133 return 1;
134 samples++;
135 }
136 return 0;
137}
138
139static int __validate_match(struct hists *hists)
140{
141 size_t count = 0;
142 struct rb_root *root;
143 struct rb_node *node;
144
145 /*
146 * Only entries from fake_common_samples should have a pair.
147 */
Jiri Olsa52225032016-05-03 13:54:42 +0200148 if (hists__has(hists, need_collapse))
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900149 root = &hists->entries_collapsed;
150 else
151 root = hists->entries_in;
152
153 node = rb_first(root);
154 while (node) {
155 struct hist_entry *he;
156
157 he = rb_entry(node, struct hist_entry, rb_node_in);
158
159 if (hist_entry__has_pairs(he)) {
160 if (find_sample(fake_common_samples,
161 ARRAY_SIZE(fake_common_samples),
162 he->thread, he->ms.map, he->ms.sym)) {
163 count++;
164 } else {
165 pr_debug("Can't find the matched entry\n");
166 return -1;
167 }
168 }
169
170 node = rb_next(node);
171 }
172
173 if (count != ARRAY_SIZE(fake_common_samples)) {
174 pr_debug("Invalid count for matched entries: %zd of %zd\n",
175 count, ARRAY_SIZE(fake_common_samples));
176 return -1;
177 }
178
179 return 0;
180}
181
182static int validate_match(struct hists *leader, struct hists *other)
183{
184 return __validate_match(leader) || __validate_match(other);
185}
186
187static int __validate_link(struct hists *hists, int idx)
188{
189 size_t count = 0;
190 size_t count_pair = 0;
191 size_t count_dummy = 0;
192 struct rb_root *root;
193 struct rb_node *node;
194
195 /*
196 * Leader hists (idx = 0) will have dummy entries from other,
197 * and some entries will have no pair. However every entry
198 * in other hists should have (dummy) pair.
199 */
Jiri Olsa52225032016-05-03 13:54:42 +0200200 if (hists__has(hists, need_collapse))
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900201 root = &hists->entries_collapsed;
202 else
203 root = hists->entries_in;
204
205 node = rb_first(root);
206 while (node) {
207 struct hist_entry *he;
208
209 he = rb_entry(node, struct hist_entry, rb_node_in);
210
211 if (hist_entry__has_pairs(he)) {
212 if (!find_sample(fake_common_samples,
213 ARRAY_SIZE(fake_common_samples),
214 he->thread, he->ms.map, he->ms.sym) &&
215 !find_sample(fake_samples[idx],
216 ARRAY_SIZE(fake_samples[idx]),
217 he->thread, he->ms.map, he->ms.sym)) {
218 count_dummy++;
219 }
220 count_pair++;
221 } else if (idx) {
222 pr_debug("A entry from the other hists should have pair\n");
223 return -1;
224 }
225
226 count++;
227 node = rb_next(node);
228 }
229
230 /*
231 * Note that we have a entry collapsed in the other (idx = 1) hists.
232 */
233 if (idx == 0) {
234 if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) {
235 pr_debug("Invalid count of dummy entries: %zd of %zd\n",
236 count_dummy, ARRAY_SIZE(fake_samples[1]) - 1);
237 return -1;
238 }
239 if (count != count_pair + ARRAY_SIZE(fake_samples[0])) {
240 pr_debug("Invalid count of total leader entries: %zd of %zd\n",
241 count, count_pair + ARRAY_SIZE(fake_samples[0]));
242 return -1;
243 }
244 } else {
245 if (count != count_pair) {
246 pr_debug("Invalid count of total other entries: %zd of %zd\n",
247 count, count_pair);
248 return -1;
249 }
250 if (count_dummy > 0) {
251 pr_debug("Other hists should not have dummy entries: %zd\n",
252 count_dummy);
253 return -1;
254 }
255 }
256
257 return 0;
258}
259
260static int validate_link(struct hists *leader, struct hists *other)
261{
262 return __validate_link(leader, 0) || __validate_link(other, 1);
263}
264
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300265int test__hists_link(int subtest __maybe_unused)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900266{
267 int err = -1;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300268 struct hists *hists, *first_hists;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300269 struct machines machines;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900270 struct machine *machine = NULL;
271 struct perf_evsel *evsel, *first;
Namhyung Kim334fe7a2013-03-11 16:43:12 +0900272 struct perf_evlist *evlist = perf_evlist__new();
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900273
274 if (evlist == NULL)
275 return -ENOMEM;
276
Jiri Olsab39b8392015-04-22 21:10:16 +0200277 err = parse_events(evlist, "cpu-clock", NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900278 if (err)
279 goto out;
Jiri Olsab39b8392015-04-22 21:10:16 +0200280 err = parse_events(evlist, "task-clock", NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900281 if (err)
282 goto out;
283
Wang Nanb0500c12016-01-11 13:48:03 +0000284 err = TEST_FAIL;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900285 /* default sort order (comm,dso,sym) will be used */
Namhyung Kim40184c42015-12-23 02:07:01 +0900286 if (setup_sorting(NULL) < 0)
Namhyung Kim55309982013-02-06 14:57:16 +0900287 goto out;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900288
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300289 machines__init(&machines);
290
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900291 /* setup threads/dso/map/symbols also */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300292 machine = setup_fake_machine(&machines);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900293 if (!machine)
294 goto out;
295
296 if (verbose > 1)
297 machine__fprintf(machine, stderr);
298
299 /* process sample events */
300 err = add_hist_entries(evlist, machine);
301 if (err < 0)
302 goto out;
303
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300304 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300305 hists = evsel__hists(evsel);
306 hists__collapse_resort(hists, NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900307
308 if (verbose > 2)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300309 print_hists_in(hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900310 }
311
312 first = perf_evlist__first(evlist);
313 evsel = perf_evlist__last(evlist);
314
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300315 first_hists = evsel__hists(first);
316 hists = evsel__hists(evsel);
317
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900318 /* match common entries */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300319 hists__match(first_hists, hists);
320 err = validate_match(first_hists, hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900321 if (err)
322 goto out;
323
324 /* link common and/or dummy entries */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300325 hists__link(first_hists, hists);
326 err = validate_link(first_hists, hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900327 if (err)
328 goto out;
329
330 err = 0;
331
332out:
333 /* tear down everything */
334 perf_evlist__delete(evlist);
Namhyung Kimf21d1812014-05-12 14:43:18 +0900335 reset_output_field();
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300336 machines__exit(&machines);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900337
338 return err;
339}