blob: 62efb14f3a5a7eabb0dfbc7bae31e90276ef9c54 [file] [log] [blame]
Namhyung Kim3c3cfd92014-04-25 12:28:14 +09001#include "perf.h"
2#include "util/debug.h"
3#include "util/symbol.h"
4#include "util/sort.h"
5#include "util/evsel.h"
6#include "util/evlist.h"
7#include "util/machine.h"
8#include "util/thread.h"
9#include "util/parse-events.h"
10#include "tests/tests.h"
11#include "tests/hists_common.h"
12
13struct sample {
14 u32 pid;
15 u64 ip;
16 struct thread *thread;
17 struct map *map;
18 struct symbol *sym;
Kan Liang92d424a2015-09-04 10:45:46 -040019 int socket;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090020};
21
22/* For the numbers, see hists_common.c */
23static struct sample fake_samples[] = {
24 /* perf [kernel] schedule() */
Kan Liang92d424a2015-09-04 10:45:46 -040025 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, .socket = 0 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090026 /* perf [perf] main() */
Kan Liang92d424a2015-09-04 10:45:46 -040027 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, .socket = 0 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090028 /* perf [libc] malloc() */
Kan Liang92d424a2015-09-04 10:45:46 -040029 { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, .socket = 0 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090030 /* perf [perf] main() */
Kan Liang92d424a2015-09-04 10:45:46 -040031 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, .socket = 0 }, /* will be merged */
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090032 /* perf [perf] cmd_record() */
Kan Liang92d424a2015-09-04 10:45:46 -040033 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, .socket = 1 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090034 /* perf [kernel] page_fault() */
Kan Liang92d424a2015-09-04 10:45:46 -040035 { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, .socket = 1 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090036 /* bash [bash] main() */
Kan Liang92d424a2015-09-04 10:45:46 -040037 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, .socket = 2 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090038 /* bash [bash] xmalloc() */
Kan Liang92d424a2015-09-04 10:45:46 -040039 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, .socket = 2 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090040 /* bash [libc] malloc() */
Kan Liang92d424a2015-09-04 10:45:46 -040041 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, .socket = 3 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090042 /* bash [kernel] page_fault() */
Kan Liang92d424a2015-09-04 10:45:46 -040043 { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, .socket = 3 },
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090044};
45
Namhyung Kim69bcb012013-10-30 09:40:34 +090046static int add_hist_entries(struct perf_evlist *evlist,
Arnaldo Carvalho de Melof4987842014-10-23 17:20:38 -030047 struct machine *machine)
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090048{
49 struct perf_evsel *evsel;
50 struct addr_location al;
Namhyung Kima1891aa2014-05-23 14:59:57 +090051 struct perf_sample sample = { .period = 100, };
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090052 size_t i;
53
54 /*
55 * each evsel will have 10 samples but the 4th sample
56 * (perf [perf] main) will be collapsed to an existing entry
57 * so total 9 entries will be in the tree.
58 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -030059 evlist__for_each_entry(evlist, evsel) {
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090060 for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
Namhyung Kim69bcb012013-10-30 09:40:34 +090061 struct hist_entry_iter iter = {
Namhyung Kim063bd932015-05-19 17:04:10 +090062 .evsel = evsel,
63 .sample = &sample,
Namhyung Kim69bcb012013-10-30 09:40:34 +090064 .ops = &hist_iter_normal,
65 .hide_unresolved = false,
66 };
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -030067 struct hists *hists = evsel__hists(evsel);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090068
69 /* make sure it has no filter at first */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -030070 hists->thread_filter = NULL;
71 hists->dso_filter = NULL;
72 hists->symbol_filter_str = NULL;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090073
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -030074 sample.cpumode = PERF_RECORD_MISC_USER;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090075 sample.pid = fake_samples[i].pid;
Namhyung Kim13ce34d2014-05-12 09:56:42 +090076 sample.tid = fake_samples[i].pid;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090077 sample.ip = fake_samples[i].ip;
78
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -030079 if (machine__resolve(machine, &al, &sample) < 0)
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090080 goto out;
81
Kan Liang92d424a2015-09-04 10:45:46 -040082 al.socket = fake_samples[i].socket;
Namhyung Kim063bd932015-05-19 17:04:10 +090083 if (hist_entry_iter__add(&iter, &al,
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -030084 sysctl_perf_event_max_stack, NULL) < 0) {
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030085 addr_location__put(&al);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090086 goto out;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030087 }
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090088
89 fake_samples[i].thread = al.thread;
90 fake_samples[i].map = al.map;
91 fake_samples[i].sym = al.sym;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +090092 }
93 }
94
95 return 0;
96
97out:
98 pr_debug("Not enough memory for adding a hist entry\n");
99 return TEST_FAIL;
100}
101
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300102int test__hists_filter(int subtest __maybe_unused)
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900103{
104 int err = TEST_FAIL;
105 struct machines machines;
106 struct machine *machine;
107 struct perf_evsel *evsel;
108 struct perf_evlist *evlist = perf_evlist__new();
109
110 TEST_ASSERT_VAL("No memory", evlist);
111
Jiri Olsab39b8392015-04-22 21:10:16 +0200112 err = parse_events(evlist, "cpu-clock", NULL);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900113 if (err)
114 goto out;
Jiri Olsab39b8392015-04-22 21:10:16 +0200115 err = parse_events(evlist, "task-clock", NULL);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900116 if (err)
117 goto out;
Wang Nanb0500c12016-01-11 13:48:03 +0000118 err = TEST_FAIL;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900119
120 /* default sort order (comm,dso,sym) will be used */
Namhyung Kim40184c42015-12-23 02:07:01 +0900121 if (setup_sorting(NULL) < 0)
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900122 goto out;
123
124 machines__init(&machines);
125
126 /* setup threads/dso/map/symbols also */
127 machine = setup_fake_machine(&machines);
128 if (!machine)
129 goto out;
130
131 if (verbose > 1)
132 machine__fprintf(machine, stderr);
133
134 /* process sample events */
135 err = add_hist_entries(evlist, machine);
136 if (err < 0)
137 goto out;
138
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300139 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300140 struct hists *hists = evsel__hists(evsel);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900141
142 hists__collapse_resort(hists, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +0100143 perf_evsel__output_resort(evsel, NULL);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900144
145 if (verbose > 2) {
146 pr_info("Normal histogram\n");
Namhyung Kim4e754e12014-05-12 10:06:18 +0900147 print_hists_out(hists);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900148 }
149
150 TEST_ASSERT_VAL("Invalid nr samples",
151 hists->stats.nr_events[PERF_RECORD_SAMPLE] == 10);
152 TEST_ASSERT_VAL("Invalid nr hist entries",
153 hists->nr_entries == 9);
154 TEST_ASSERT_VAL("Invalid total period",
155 hists->stats.total_period == 1000);
156 TEST_ASSERT_VAL("Unmatched nr samples",
157 hists->stats.nr_events[PERF_RECORD_SAMPLE] ==
158 hists->stats.nr_non_filtered_samples);
159 TEST_ASSERT_VAL("Unmatched nr hist entries",
160 hists->nr_entries == hists->nr_non_filtered_entries);
161 TEST_ASSERT_VAL("Unmatched total period",
162 hists->stats.total_period ==
163 hists->stats.total_non_filtered_period);
164
165 /* now applying thread filter for 'bash' */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300166 hists->thread_filter = fake_samples[9].thread;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900167 hists__filter_by_thread(hists);
168
169 if (verbose > 2) {
170 pr_info("Histogram for thread filter\n");
Namhyung Kim4e754e12014-05-12 10:06:18 +0900171 print_hists_out(hists);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900172 }
173
174 /* normal stats should be invariant */
175 TEST_ASSERT_VAL("Invalid nr samples",
176 hists->stats.nr_events[PERF_RECORD_SAMPLE] == 10);
177 TEST_ASSERT_VAL("Invalid nr hist entries",
178 hists->nr_entries == 9);
179 TEST_ASSERT_VAL("Invalid total period",
180 hists->stats.total_period == 1000);
181
182 /* but filter stats are changed */
183 TEST_ASSERT_VAL("Unmatched nr samples for thread filter",
184 hists->stats.nr_non_filtered_samples == 4);
185 TEST_ASSERT_VAL("Unmatched nr hist entries for thread filter",
186 hists->nr_non_filtered_entries == 4);
187 TEST_ASSERT_VAL("Unmatched total period for thread filter",
188 hists->stats.total_non_filtered_period == 400);
189
190 /* remove thread filter first */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300191 hists->thread_filter = NULL;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900192 hists__filter_by_thread(hists);
193
194 /* now applying dso filter for 'kernel' */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300195 hists->dso_filter = fake_samples[0].map->dso;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900196 hists__filter_by_dso(hists);
197
198 if (verbose > 2) {
199 pr_info("Histogram for dso filter\n");
Namhyung Kim4e754e12014-05-12 10:06:18 +0900200 print_hists_out(hists);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900201 }
202
203 /* normal stats should be invariant */
204 TEST_ASSERT_VAL("Invalid nr samples",
205 hists->stats.nr_events[PERF_RECORD_SAMPLE] == 10);
206 TEST_ASSERT_VAL("Invalid nr hist entries",
207 hists->nr_entries == 9);
208 TEST_ASSERT_VAL("Invalid total period",
209 hists->stats.total_period == 1000);
210
211 /* but filter stats are changed */
212 TEST_ASSERT_VAL("Unmatched nr samples for dso filter",
213 hists->stats.nr_non_filtered_samples == 3);
214 TEST_ASSERT_VAL("Unmatched nr hist entries for dso filter",
215 hists->nr_non_filtered_entries == 3);
216 TEST_ASSERT_VAL("Unmatched total period for dso filter",
217 hists->stats.total_non_filtered_period == 300);
218
219 /* remove dso filter first */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300220 hists->dso_filter = NULL;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900221 hists__filter_by_dso(hists);
222
223 /*
224 * now applying symbol filter for 'main'. Also note that
225 * there's 3 samples that have 'main' symbol but the 4th
226 * entry of fake_samples was collapsed already so it won't
227 * be counted as a separate entry but the sample count and
228 * total period will be remained.
229 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300230 hists->symbol_filter_str = "main";
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900231 hists__filter_by_symbol(hists);
232
233 if (verbose > 2) {
234 pr_info("Histogram for symbol filter\n");
Namhyung Kim4e754e12014-05-12 10:06:18 +0900235 print_hists_out(hists);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900236 }
237
238 /* normal stats should be invariant */
239 TEST_ASSERT_VAL("Invalid nr samples",
240 hists->stats.nr_events[PERF_RECORD_SAMPLE] == 10);
241 TEST_ASSERT_VAL("Invalid nr hist entries",
242 hists->nr_entries == 9);
243 TEST_ASSERT_VAL("Invalid total period",
244 hists->stats.total_period == 1000);
245
246 /* but filter stats are changed */
247 TEST_ASSERT_VAL("Unmatched nr samples for symbol filter",
248 hists->stats.nr_non_filtered_samples == 3);
249 TEST_ASSERT_VAL("Unmatched nr hist entries for symbol filter",
250 hists->nr_non_filtered_entries == 2);
251 TEST_ASSERT_VAL("Unmatched total period for symbol filter",
252 hists->stats.total_non_filtered_period == 300);
253
Kan Liang92d424a2015-09-04 10:45:46 -0400254 /* remove symbol filter first */
255 hists->symbol_filter_str = NULL;
256 hists__filter_by_symbol(hists);
257
258 /* now applying socket filters */
259 hists->socket_filter = 2;
260 hists__filter_by_socket(hists);
261
262 if (verbose > 2) {
263 pr_info("Histogram for socket filters\n");
264 print_hists_out(hists);
265 }
266
267 /* normal stats should be invariant */
268 TEST_ASSERT_VAL("Invalid nr samples",
269 hists->stats.nr_events[PERF_RECORD_SAMPLE] == 10);
270 TEST_ASSERT_VAL("Invalid nr hist entries",
271 hists->nr_entries == 9);
272 TEST_ASSERT_VAL("Invalid total period",
273 hists->stats.total_period == 1000);
274
275 /* but filter stats are changed */
276 TEST_ASSERT_VAL("Unmatched nr samples for socket filter",
277 hists->stats.nr_non_filtered_samples == 2);
278 TEST_ASSERT_VAL("Unmatched nr hist entries for socket filter",
279 hists->nr_non_filtered_entries == 2);
280 TEST_ASSERT_VAL("Unmatched total period for socket filter",
281 hists->stats.total_non_filtered_period == 200);
282
283 /* remove socket filter first */
284 hists->socket_filter = -1;
285 hists__filter_by_socket(hists);
286
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900287 /* now applying all filters at once. */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300288 hists->thread_filter = fake_samples[1].thread;
289 hists->dso_filter = fake_samples[1].map->dso;
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900290 hists__filter_by_thread(hists);
291 hists__filter_by_dso(hists);
292
293 if (verbose > 2) {
294 pr_info("Histogram for all filters\n");
Namhyung Kim4e754e12014-05-12 10:06:18 +0900295 print_hists_out(hists);
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900296 }
297
298 /* normal stats should be invariant */
299 TEST_ASSERT_VAL("Invalid nr samples",
300 hists->stats.nr_events[PERF_RECORD_SAMPLE] == 10);
301 TEST_ASSERT_VAL("Invalid nr hist entries",
302 hists->nr_entries == 9);
303 TEST_ASSERT_VAL("Invalid total period",
304 hists->stats.total_period == 1000);
305
306 /* but filter stats are changed */
307 TEST_ASSERT_VAL("Unmatched nr samples for all filter",
308 hists->stats.nr_non_filtered_samples == 2);
309 TEST_ASSERT_VAL("Unmatched nr hist entries for all filter",
310 hists->nr_non_filtered_entries == 1);
311 TEST_ASSERT_VAL("Unmatched total period for all filter",
312 hists->stats.total_non_filtered_period == 200);
313 }
314
315
316 err = TEST_OK;
317
318out:
319 /* tear down everything */
320 perf_evlist__delete(evlist);
Namhyung Kimf21d1812014-05-12 14:43:18 +0900321 reset_output_field();
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900322 machines__exit(&machines);
323
324 return err;
325}