Commit | Line | Data |
---|---|---|
3c3cfd99 NK |
1 | #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 | ||
13 | struct sample { | |
14 | u32 pid; | |
15 | u64 ip; | |
16 | struct thread *thread; | |
17 | struct map *map; | |
18 | struct symbol *sym; | |
92d424ae | 19 | int socket; |
3c3cfd99 NK |
20 | }; |
21 | ||
22 | /* For the numbers, see hists_common.c */ | |
23 | static struct sample fake_samples[] = { | |
24 | /* perf [kernel] schedule() */ | |
92d424ae | 25 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, .socket = 0 }, |
3c3cfd99 | 26 | /* perf [perf] main() */ |
92d424ae | 27 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_MAIN, .socket = 0 }, |
3c3cfd99 | 28 | /* perf [libc] malloc() */ |
92d424ae | 29 | { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, .socket = 0 }, |
3c3cfd99 | 30 | /* perf [perf] main() */ |
92d424ae | 31 | { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, .socket = 0 }, /* will be merged */ |
3c3cfd99 | 32 | /* perf [perf] cmd_record() */ |
92d424ae | 33 | { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, .socket = 1 }, |
3c3cfd99 | 34 | /* perf [kernel] page_fault() */ |
92d424ae | 35 | { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_PAGE_FAULT, .socket = 1 }, |
3c3cfd99 | 36 | /* bash [bash] main() */ |
92d424ae | 37 | { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_MAIN, .socket = 2 }, |
3c3cfd99 | 38 | /* bash [bash] xmalloc() */ |
92d424ae | 39 | { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, .socket = 2 }, |
3c3cfd99 | 40 | /* bash [libc] malloc() */ |
92d424ae | 41 | { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, .socket = 3 }, |
3c3cfd99 | 42 | /* bash [kernel] page_fault() */ |
92d424ae | 43 | { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, .socket = 3 }, |
3c3cfd99 NK |
44 | }; |
45 | ||
69bcb019 | 46 | static int add_hist_entries(struct perf_evlist *evlist, |
f498784c | 47 | struct machine *machine) |
3c3cfd99 NK |
48 | { |
49 | struct perf_evsel *evsel; | |
50 | struct addr_location al; | |
a1891aa4 | 51 | struct perf_sample sample = { .period = 100, }; |
3c3cfd99 NK |
52 | 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 | */ | |
59 | evlist__for_each(evlist, evsel) { | |
60 | for (i = 0; i < ARRAY_SIZE(fake_samples); i++) { | |
69bcb019 | 61 | struct hist_entry_iter iter = { |
063bd936 NK |
62 | .evsel = evsel, |
63 | .sample = &sample, | |
69bcb019 NK |
64 | .ops = &hist_iter_normal, |
65 | .hide_unresolved = false, | |
66 | }; | |
4ea062ed | 67 | struct hists *hists = evsel__hists(evsel); |
3c3cfd99 NK |
68 | |
69 | /* make sure it has no filter at first */ | |
4ea062ed ACM |
70 | hists->thread_filter = NULL; |
71 | hists->dso_filter = NULL; | |
72 | hists->symbol_filter_str = NULL; | |
3c3cfd99 | 73 | |
473398a2 | 74 | sample.cpumode = PERF_RECORD_MISC_USER; |
3c3cfd99 | 75 | sample.pid = fake_samples[i].pid; |
13ce34df | 76 | sample.tid = fake_samples[i].pid; |
3c3cfd99 NK |
77 | sample.ip = fake_samples[i].ip; |
78 | ||
bb3eb566 | 79 | if (machine__resolve(machine, &al, &sample) < 0) |
3c3cfd99 NK |
80 | goto out; |
81 | ||
92d424ae | 82 | al.socket = fake_samples[i].socket; |
063bd936 | 83 | if (hist_entry_iter__add(&iter, &al, |
4cb93446 | 84 | sysctl_perf_event_max_stack, NULL) < 0) { |
b91fc39f | 85 | addr_location__put(&al); |
3c3cfd99 | 86 | goto out; |
b91fc39f | 87 | } |
3c3cfd99 NK |
88 | |
89 | fake_samples[i].thread = al.thread; | |
90 | fake_samples[i].map = al.map; | |
91 | fake_samples[i].sym = al.sym; | |
3c3cfd99 NK |
92 | } |
93 | } | |
94 | ||
95 | return 0; | |
96 | ||
97 | out: | |
98 | pr_debug("Not enough memory for adding a hist entry\n"); | |
99 | return TEST_FAIL; | |
100 | } | |
101 | ||
721a1f53 | 102 | int test__hists_filter(int subtest __maybe_unused) |
3c3cfd99 NK |
103 | { |
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 | ||
b39b8393 | 112 | err = parse_events(evlist, "cpu-clock", NULL); |
3c3cfd99 NK |
113 | if (err) |
114 | goto out; | |
b39b8393 | 115 | err = parse_events(evlist, "task-clock", NULL); |
3c3cfd99 NK |
116 | if (err) |
117 | goto out; | |
b0500c16 | 118 | err = TEST_FAIL; |
3c3cfd99 NK |
119 | |
120 | /* default sort order (comm,dso,sym) will be used */ | |
40184c46 | 121 | if (setup_sorting(NULL) < 0) |
3c3cfd99 NK |
122 | 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 | ||
139 | evlist__for_each(evlist, evsel) { | |
4ea062ed | 140 | struct hists *hists = evsel__hists(evsel); |
3c3cfd99 NK |
141 | |
142 | hists__collapse_resort(hists, NULL); | |
452ce03b | 143 | perf_evsel__output_resort(evsel, NULL); |
3c3cfd99 NK |
144 | |
145 | if (verbose > 2) { | |
146 | pr_info("Normal histogram\n"); | |
4e754e1c | 147 | print_hists_out(hists); |
3c3cfd99 NK |
148 | } |
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' */ | |
4ea062ed | 166 | hists->thread_filter = fake_samples[9].thread; |
3c3cfd99 NK |
167 | hists__filter_by_thread(hists); |
168 | ||
169 | if (verbose > 2) { | |
170 | pr_info("Histogram for thread filter\n"); | |
4e754e1c | 171 | print_hists_out(hists); |
3c3cfd99 NK |
172 | } |
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 */ | |
4ea062ed | 191 | hists->thread_filter = NULL; |
3c3cfd99 NK |
192 | hists__filter_by_thread(hists); |
193 | ||
194 | /* now applying dso filter for 'kernel' */ | |
4ea062ed | 195 | hists->dso_filter = fake_samples[0].map->dso; |
3c3cfd99 NK |
196 | hists__filter_by_dso(hists); |
197 | ||
198 | if (verbose > 2) { | |
199 | pr_info("Histogram for dso filter\n"); | |
4e754e1c | 200 | print_hists_out(hists); |
3c3cfd99 NK |
201 | } |
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 */ | |
4ea062ed | 220 | hists->dso_filter = NULL; |
3c3cfd99 NK |
221 | 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 | */ | |
4ea062ed | 230 | hists->symbol_filter_str = "main"; |
3c3cfd99 NK |
231 | hists__filter_by_symbol(hists); |
232 | ||
233 | if (verbose > 2) { | |
234 | pr_info("Histogram for symbol filter\n"); | |
4e754e1c | 235 | print_hists_out(hists); |
3c3cfd99 NK |
236 | } |
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 | ||
92d424ae KL |
254 | /* 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 | ||
3c3cfd99 | 287 | /* now applying all filters at once. */ |
4ea062ed ACM |
288 | hists->thread_filter = fake_samples[1].thread; |
289 | hists->dso_filter = fake_samples[1].map->dso; | |
3c3cfd99 NK |
290 | hists__filter_by_thread(hists); |
291 | hists__filter_by_dso(hists); | |
292 | ||
293 | if (verbose > 2) { | |
294 | pr_info("Histogram for all filters\n"); | |
4e754e1c | 295 | print_hists_out(hists); |
3c3cfd99 NK |
296 | } |
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 | ||
318 | out: | |
319 | /* tear down everything */ | |
320 | perf_evlist__delete(evlist); | |
f21d1815 | 321 | reset_output_field(); |
3c3cfd99 NK |
322 | machines__exit(&machines); |
323 | ||
324 | return err; | |
325 | } |