blob: 073c9c2856bc4dae2a65c3f3f90cdd9021f9acb2 [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"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030012#include <errno.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030013#include <linux/kernel.h>
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090014
15struct sample {
16 u32 pid;
17 u64 ip;
18 struct thread *thread;
19 struct map *map;
20 struct symbol *sym;
21};
22
Namhyung Kim6e344a92014-04-25 12:28:13 +090023/* For the numbers, see hists_common.c */
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090024static struct sample fake_common_samples[] = {
25 /* perf [kernel] schedule() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090026 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090027 /* perf [perf] main() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090028 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090029 /* perf [perf] cmd_record() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090030 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090031 /* bash [bash] xmalloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090032 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090033 /* bash [libc] malloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090034 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090035};
36
37static struct sample fake_samples[][5] = {
38 {
39 /* perf [perf] run_command() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090040 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090041 /* perf [libc] malloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090042 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090043 /* perf [kernel] page_fault() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090044 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090045 /* perf [kernel] sys_perf_event_open() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090046 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090047 /* bash [libc] free() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090048 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090049 },
50 {
51 /* perf [libc] free() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090052 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090053 /* bash [libc] malloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090054 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, /* will be merged */
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090055 /* bash [bash] xfee() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090056 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090057 /* bash [libc] realloc() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090058 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090059 /* bash [kernel] page_fault() */
Namhyung Kima1891aa2014-05-23 14:59:57 +090060 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090061 },
62};
63
64static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
65{
66 struct perf_evsel *evsel;
67 struct addr_location al;
68 struct hist_entry *he;
Namhyung Kimfd36f3d2015-12-23 02:06:58 +090069 struct perf_sample sample = { .period = 1, .weight = 1, };
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090070 size_t i = 0, k;
71
72 /*
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.
76 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -030077 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -030078 struct hists *hists = evsel__hists(evsel);
79
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090080 for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) {
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -030081 sample.cpumode = PERF_RECORD_MISC_USER;
Adrian Hunteref893252013-08-27 11:23:06 +030082 sample.pid = fake_common_samples[k].pid;
Namhyung Kim13ce34d2014-05-12 09:56:42 +090083 sample.tid = fake_common_samples[k].pid;
Adrian Hunteref893252013-08-27 11:23:06 +030084 sample.ip = fake_common_samples[k].ip;
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -030085
86 if (machine__resolve(machine, &al, &sample) < 0)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090087 goto out;
88
Jiri Olsa0102ef32016-06-14 20:19:21 +020089 he = hists__add_entry(hists, &al, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +090090 NULL, NULL, &sample, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030091 if (he == NULL) {
92 addr_location__put(&al);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090093 goto out;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030094 }
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +090095
96 fake_common_samples[k].thread = al.thread;
97 fake_common_samples[k].map = al.map;
98 fake_common_samples[k].sym = al.sym;
99 }
100
101 for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) {
Adrian Hunteref893252013-08-27 11:23:06 +0300102 sample.pid = fake_samples[i][k].pid;
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900103 sample.tid = fake_samples[i][k].pid;
Adrian Hunteref893252013-08-27 11:23:06 +0300104 sample.ip = fake_samples[i][k].ip;
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300105 if (machine__resolve(machine, &al, &sample) < 0)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900106 goto out;
107
Jiri Olsa0102ef32016-06-14 20:19:21 +0200108 he = hists__add_entry(hists, &al, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900109 NULL, NULL, &sample, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300110 if (he == NULL) {
111 addr_location__put(&al);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900112 goto out;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300113 }
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900114
115 fake_samples[i][k].thread = al.thread;
116 fake_samples[i][k].map = al.map;
117 fake_samples[i][k].sym = al.sym;
118 }
119 i++;
120 }
121
122 return 0;
123
124out:
125 pr_debug("Not enough memory for adding a hist entry\n");
126 return -1;
127}
128
129static int find_sample(struct sample *samples, size_t nr_samples,
130 struct thread *t, struct map *m, struct symbol *s)
131{
132 while (nr_samples--) {
133 if (samples->thread == t && samples->map == m &&
134 samples->sym == s)
135 return 1;
136 samples++;
137 }
138 return 0;
139}
140
141static int __validate_match(struct hists *hists)
142{
143 size_t count = 0;
144 struct rb_root *root;
145 struct rb_node *node;
146
147 /*
148 * Only entries from fake_common_samples should have a pair.
149 */
Jiri Olsa52225032016-05-03 13:54:42 +0200150 if (hists__has(hists, need_collapse))
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900151 root = &hists->entries_collapsed;
152 else
153 root = hists->entries_in;
154
155 node = rb_first(root);
156 while (node) {
157 struct hist_entry *he;
158
159 he = rb_entry(node, struct hist_entry, rb_node_in);
160
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)) {
165 count++;
166 } else {
167 pr_debug("Can't find the matched entry\n");
168 return -1;
169 }
170 }
171
172 node = rb_next(node);
173 }
174
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));
178 return -1;
179 }
180
181 return 0;
182}
183
184static int validate_match(struct hists *leader, struct hists *other)
185{
186 return __validate_match(leader) || __validate_match(other);
187}
188
189static int __validate_link(struct hists *hists, int idx)
190{
191 size_t count = 0;
192 size_t count_pair = 0;
193 size_t count_dummy = 0;
194 struct rb_root *root;
195 struct rb_node *node;
196
197 /*
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.
201 */
Jiri Olsa52225032016-05-03 13:54:42 +0200202 if (hists__has(hists, need_collapse))
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900203 root = &hists->entries_collapsed;
204 else
205 root = hists->entries_in;
206
207 node = rb_first(root);
208 while (node) {
209 struct hist_entry *he;
210
211 he = rb_entry(node, struct hist_entry, rb_node_in);
212
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)) {
220 count_dummy++;
221 }
222 count_pair++;
223 } else if (idx) {
224 pr_debug("A entry from the other hists should have pair\n");
225 return -1;
226 }
227
228 count++;
229 node = rb_next(node);
230 }
231
232 /*
233 * Note that we have a entry collapsed in the other (idx = 1) hists.
234 */
235 if (idx == 0) {
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);
239 return -1;
240 }
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]));
244 return -1;
245 }
246 } else {
247 if (count != count_pair) {
248 pr_debug("Invalid count of total other entries: %zd of %zd\n",
249 count, count_pair);
250 return -1;
251 }
252 if (count_dummy > 0) {
253 pr_debug("Other hists should not have dummy entries: %zd\n",
254 count_dummy);
255 return -1;
256 }
257 }
258
259 return 0;
260}
261
262static int validate_link(struct hists *leader, struct hists *other)
263{
264 return __validate_link(leader, 0) || __validate_link(other, 1);
265}
266
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300267int test__hists_link(struct test *test __maybe_unused, int subtest __maybe_unused)
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900268{
269 int err = -1;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300270 struct hists *hists, *first_hists;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300271 struct machines machines;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900272 struct machine *machine = NULL;
273 struct perf_evsel *evsel, *first;
Namhyung Kim334fe7a2013-03-11 16:43:12 +0900274 struct perf_evlist *evlist = perf_evlist__new();
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900275
276 if (evlist == NULL)
277 return -ENOMEM;
278
Jiri Olsab39b8392015-04-22 21:10:16 +0200279 err = parse_events(evlist, "cpu-clock", NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900280 if (err)
281 goto out;
Jiri Olsab39b8392015-04-22 21:10:16 +0200282 err = parse_events(evlist, "task-clock", NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900283 if (err)
284 goto out;
285
Wang Nanb0500c12016-01-11 13:48:03 +0000286 err = TEST_FAIL;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900287 /* default sort order (comm,dso,sym) will be used */
Namhyung Kim40184c42015-12-23 02:07:01 +0900288 if (setup_sorting(NULL) < 0)
Namhyung Kim55309982013-02-06 14:57:16 +0900289 goto out;
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900290
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300291 machines__init(&machines);
292
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900293 /* setup threads/dso/map/symbols also */
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300294 machine = setup_fake_machine(&machines);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900295 if (!machine)
296 goto out;
297
298 if (verbose > 1)
299 machine__fprintf(machine, stderr);
300
301 /* process sample events */
302 err = add_hist_entries(evlist, machine);
303 if (err < 0)
304 goto out;
305
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300306 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300307 hists = evsel__hists(evsel);
308 hists__collapse_resort(hists, NULL);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900309
310 if (verbose > 2)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300311 print_hists_in(hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900312 }
313
314 first = perf_evlist__first(evlist);
315 evsel = perf_evlist__last(evlist);
316
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300317 first_hists = evsel__hists(first);
318 hists = evsel__hists(evsel);
319
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900320 /* match common entries */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300321 hists__match(first_hists, hists);
322 err = validate_match(first_hists, hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900323 if (err)
324 goto out;
325
326 /* link common and/or dummy entries */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300327 hists__link(first_hists, hists);
328 err = validate_link(first_hists, hists);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900329 if (err)
330 goto out;
331
332 err = 0;
333
334out:
335 /* tear down everything */
336 perf_evlist__delete(evlist);
Namhyung Kimf21d1812014-05-12 14:43:18 +0900337 reset_output_field();
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300338 machines__exit(&machines);
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900339
340 return err;
341}