xref: /openbmc/linux/tools/perf/tests/parse-events.c (revision d8f7bbc947afb59c68a8574d1fe99b20cff2b1be)
1 
2 #include "parse-events.h"
3 #include "evsel.h"
4 #include "evlist.h"
5 #include "sysfs.h"
6 #include "debugfs.h"
7 #include "tests.h"
8 #include <linux/hw_breakpoint.h>
9 
10 #define TEST_ASSERT_VAL(text, cond) \
11 do { \
12 	if (!(cond)) { \
13 		pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
14 		return -1; \
15 	} \
16 } while (0)
17 
18 #define PERF_TP_SAMPLE_TYPE (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | \
19 			     PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
20 
21 static int test__checkevent_tracepoint(struct perf_evlist *evlist)
22 {
23 	struct perf_evsel *evsel = perf_evlist__first(evlist);
24 
25 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
26 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
27 	TEST_ASSERT_VAL("wrong sample_type",
28 		PERF_TP_SAMPLE_TYPE == evsel->attr.sample_type);
29 	TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
30 	return 0;
31 }
32 
33 static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist)
34 {
35 	struct perf_evsel *evsel;
36 
37 	TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
38 
39 	list_for_each_entry(evsel, &evlist->entries, node) {
40 		TEST_ASSERT_VAL("wrong type",
41 			PERF_TYPE_TRACEPOINT == evsel->attr.type);
42 		TEST_ASSERT_VAL("wrong sample_type",
43 			PERF_TP_SAMPLE_TYPE == evsel->attr.sample_type);
44 		TEST_ASSERT_VAL("wrong sample_period",
45 			1 == evsel->attr.sample_period);
46 	}
47 	return 0;
48 }
49 
50 static int test__checkevent_raw(struct perf_evlist *evlist)
51 {
52 	struct perf_evsel *evsel = perf_evlist__first(evlist);
53 
54 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
55 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
56 	TEST_ASSERT_VAL("wrong config", 0x1a == evsel->attr.config);
57 	return 0;
58 }
59 
60 static int test__checkevent_numeric(struct perf_evlist *evlist)
61 {
62 	struct perf_evsel *evsel = perf_evlist__first(evlist);
63 
64 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
65 	TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
66 	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
67 	return 0;
68 }
69 
70 static int test__checkevent_symbolic_name(struct perf_evlist *evlist)
71 {
72 	struct perf_evsel *evsel = perf_evlist__first(evlist);
73 
74 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
75 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
76 	TEST_ASSERT_VAL("wrong config",
77 			PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
78 	return 0;
79 }
80 
81 static int test__checkevent_symbolic_name_config(struct perf_evlist *evlist)
82 {
83 	struct perf_evsel *evsel = perf_evlist__first(evlist);
84 
85 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
86 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
87 	TEST_ASSERT_VAL("wrong config",
88 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
89 	TEST_ASSERT_VAL("wrong period",
90 			100000 == evsel->attr.sample_period);
91 	TEST_ASSERT_VAL("wrong config1",
92 			0 == evsel->attr.config1);
93 	TEST_ASSERT_VAL("wrong config2",
94 			1 == evsel->attr.config2);
95 	return 0;
96 }
97 
98 static int test__checkevent_symbolic_alias(struct perf_evlist *evlist)
99 {
100 	struct perf_evsel *evsel = perf_evlist__first(evlist);
101 
102 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
103 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type);
104 	TEST_ASSERT_VAL("wrong config",
105 			PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config);
106 	return 0;
107 }
108 
109 static int test__checkevent_genhw(struct perf_evlist *evlist)
110 {
111 	struct perf_evsel *evsel = perf_evlist__first(evlist);
112 
113 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
114 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
115 	TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->attr.config);
116 	return 0;
117 }
118 
119 static int test__checkevent_breakpoint(struct perf_evlist *evlist)
120 {
121 	struct perf_evsel *evsel = perf_evlist__first(evlist);
122 
123 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
124 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
125 	TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
126 	TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
127 					 evsel->attr.bp_type);
128 	TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
129 					evsel->attr.bp_len);
130 	return 0;
131 }
132 
133 static int test__checkevent_breakpoint_x(struct perf_evlist *evlist)
134 {
135 	struct perf_evsel *evsel = perf_evlist__first(evlist);
136 
137 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
138 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
139 	TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
140 	TEST_ASSERT_VAL("wrong bp_type",
141 			HW_BREAKPOINT_X == evsel->attr.bp_type);
142 	TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->attr.bp_len);
143 	return 0;
144 }
145 
146 static int test__checkevent_breakpoint_r(struct perf_evlist *evlist)
147 {
148 	struct perf_evsel *evsel = perf_evlist__first(evlist);
149 
150 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
151 	TEST_ASSERT_VAL("wrong type",
152 			PERF_TYPE_BREAKPOINT == evsel->attr.type);
153 	TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
154 	TEST_ASSERT_VAL("wrong bp_type",
155 			HW_BREAKPOINT_R == evsel->attr.bp_type);
156 	TEST_ASSERT_VAL("wrong bp_len",
157 			HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
158 	return 0;
159 }
160 
161 static int test__checkevent_breakpoint_w(struct perf_evlist *evlist)
162 {
163 	struct perf_evsel *evsel = perf_evlist__first(evlist);
164 
165 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
166 	TEST_ASSERT_VAL("wrong type",
167 			PERF_TYPE_BREAKPOINT == evsel->attr.type);
168 	TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
169 	TEST_ASSERT_VAL("wrong bp_type",
170 			HW_BREAKPOINT_W == evsel->attr.bp_type);
171 	TEST_ASSERT_VAL("wrong bp_len",
172 			HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
173 	return 0;
174 }
175 
176 static int test__checkevent_breakpoint_rw(struct perf_evlist *evlist)
177 {
178 	struct perf_evsel *evsel = perf_evlist__first(evlist);
179 
180 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
181 	TEST_ASSERT_VAL("wrong type",
182 			PERF_TYPE_BREAKPOINT == evsel->attr.type);
183 	TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
184 	TEST_ASSERT_VAL("wrong bp_type",
185 		(HW_BREAKPOINT_R|HW_BREAKPOINT_W) == evsel->attr.bp_type);
186 	TEST_ASSERT_VAL("wrong bp_len",
187 			HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
188 	return 0;
189 }
190 
191 static int test__checkevent_tracepoint_modifier(struct perf_evlist *evlist)
192 {
193 	struct perf_evsel *evsel = perf_evlist__first(evlist);
194 
195 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
196 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
197 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
198 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
199 
200 	return test__checkevent_tracepoint(evlist);
201 }
202 
203 static int
204 test__checkevent_tracepoint_multi_modifier(struct perf_evlist *evlist)
205 {
206 	struct perf_evsel *evsel;
207 
208 	TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
209 
210 	list_for_each_entry(evsel, &evlist->entries, node) {
211 		TEST_ASSERT_VAL("wrong exclude_user",
212 				!evsel->attr.exclude_user);
213 		TEST_ASSERT_VAL("wrong exclude_kernel",
214 				evsel->attr.exclude_kernel);
215 		TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
216 		TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
217 	}
218 
219 	return test__checkevent_tracepoint_multi(evlist);
220 }
221 
222 static int test__checkevent_raw_modifier(struct perf_evlist *evlist)
223 {
224 	struct perf_evsel *evsel = perf_evlist__first(evlist);
225 
226 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
227 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
228 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
229 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
230 
231 	return test__checkevent_raw(evlist);
232 }
233 
234 static int test__checkevent_numeric_modifier(struct perf_evlist *evlist)
235 {
236 	struct perf_evsel *evsel = perf_evlist__first(evlist);
237 
238 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
239 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
240 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
241 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
242 
243 	return test__checkevent_numeric(evlist);
244 }
245 
246 static int test__checkevent_symbolic_name_modifier(struct perf_evlist *evlist)
247 {
248 	struct perf_evsel *evsel = perf_evlist__first(evlist);
249 
250 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
251 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
252 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
253 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
254 
255 	return test__checkevent_symbolic_name(evlist);
256 }
257 
258 static int test__checkevent_exclude_host_modifier(struct perf_evlist *evlist)
259 {
260 	struct perf_evsel *evsel = perf_evlist__first(evlist);
261 
262 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
263 	TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
264 
265 	return test__checkevent_symbolic_name(evlist);
266 }
267 
268 static int test__checkevent_exclude_guest_modifier(struct perf_evlist *evlist)
269 {
270 	struct perf_evsel *evsel = perf_evlist__first(evlist);
271 
272 	TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
273 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
274 
275 	return test__checkevent_symbolic_name(evlist);
276 }
277 
278 static int test__checkevent_symbolic_alias_modifier(struct perf_evlist *evlist)
279 {
280 	struct perf_evsel *evsel = perf_evlist__first(evlist);
281 
282 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
283 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
284 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
285 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
286 
287 	return test__checkevent_symbolic_alias(evlist);
288 }
289 
290 static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
291 {
292 	struct perf_evsel *evsel = perf_evlist__first(evlist);
293 
294 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
295 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
296 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
297 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
298 
299 	return test__checkevent_genhw(evlist);
300 }
301 
302 static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist)
303 {
304 	struct perf_evsel *evsel = perf_evlist__first(evlist);
305 
306 
307 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
308 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
309 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
310 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
311 	TEST_ASSERT_VAL("wrong name",
312 			!strcmp(perf_evsel__name(evsel), "mem:0:u"));
313 
314 	return test__checkevent_breakpoint(evlist);
315 }
316 
317 static int test__checkevent_breakpoint_x_modifier(struct perf_evlist *evlist)
318 {
319 	struct perf_evsel *evsel = perf_evlist__first(evlist);
320 
321 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
322 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
323 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
324 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
325 	TEST_ASSERT_VAL("wrong name",
326 			!strcmp(perf_evsel__name(evsel), "mem:0:x:k"));
327 
328 	return test__checkevent_breakpoint_x(evlist);
329 }
330 
331 static int test__checkevent_breakpoint_r_modifier(struct perf_evlist *evlist)
332 {
333 	struct perf_evsel *evsel = perf_evlist__first(evlist);
334 
335 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
336 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
337 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
338 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
339 	TEST_ASSERT_VAL("wrong name",
340 			!strcmp(perf_evsel__name(evsel), "mem:0:r:hp"));
341 
342 	return test__checkevent_breakpoint_r(evlist);
343 }
344 
345 static int test__checkevent_breakpoint_w_modifier(struct perf_evlist *evlist)
346 {
347 	struct perf_evsel *evsel = perf_evlist__first(evlist);
348 
349 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
350 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
351 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
352 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
353 	TEST_ASSERT_VAL("wrong name",
354 			!strcmp(perf_evsel__name(evsel), "mem:0:w:up"));
355 
356 	return test__checkevent_breakpoint_w(evlist);
357 }
358 
359 static int test__checkevent_breakpoint_rw_modifier(struct perf_evlist *evlist)
360 {
361 	struct perf_evsel *evsel = perf_evlist__first(evlist);
362 
363 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
364 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
365 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
366 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
367 	TEST_ASSERT_VAL("wrong name",
368 			!strcmp(perf_evsel__name(evsel), "mem:0:rw:kp"));
369 
370 	return test__checkevent_breakpoint_rw(evlist);
371 }
372 
373 static int test__checkevent_pmu(struct perf_evlist *evlist)
374 {
375 
376 	struct perf_evsel *evsel = perf_evlist__first(evlist);
377 
378 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
379 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
380 	TEST_ASSERT_VAL("wrong config",    10 == evsel->attr.config);
381 	TEST_ASSERT_VAL("wrong config1",    1 == evsel->attr.config1);
382 	TEST_ASSERT_VAL("wrong config2",    3 == evsel->attr.config2);
383 	TEST_ASSERT_VAL("wrong period",  1000 == evsel->attr.sample_period);
384 
385 	return 0;
386 }
387 
388 static int test__checkevent_list(struct perf_evlist *evlist)
389 {
390 	struct perf_evsel *evsel = perf_evlist__first(evlist);
391 
392 	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
393 
394 	/* r1 */
395 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
396 	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
397 	TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
398 	TEST_ASSERT_VAL("wrong config2", 0 == evsel->attr.config2);
399 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
400 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
401 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
402 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
403 
404 	/* syscalls:sys_enter_open:k */
405 	evsel = perf_evsel__next(evsel);
406 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
407 	TEST_ASSERT_VAL("wrong sample_type",
408 		PERF_TP_SAMPLE_TYPE == evsel->attr.sample_type);
409 	TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
410 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
411 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
412 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
413 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
414 
415 	/* 1:1:hp */
416 	evsel = perf_evsel__next(evsel);
417 	TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
418 	TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
419 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
420 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
421 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
422 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
423 
424 	return 0;
425 }
426 
427 static int test__checkevent_pmu_name(struct perf_evlist *evlist)
428 {
429 	struct perf_evsel *evsel = perf_evlist__first(evlist);
430 
431 	/* cpu/config=1,name=krava/u */
432 	TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
433 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
434 	TEST_ASSERT_VAL("wrong config",  1 == evsel->attr.config);
435 	TEST_ASSERT_VAL("wrong name", !strcmp(perf_evsel__name(evsel), "krava"));
436 
437 	/* cpu/config=2/u" */
438 	evsel = perf_evsel__next(evsel);
439 	TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
440 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
441 	TEST_ASSERT_VAL("wrong config",  2 == evsel->attr.config);
442 	TEST_ASSERT_VAL("wrong name",
443 			!strcmp(perf_evsel__name(evsel), "cpu/config=2/u"));
444 
445 	return 0;
446 }
447 
448 static int test__checkevent_pmu_events(struct perf_evlist *evlist)
449 {
450 	struct perf_evsel *evsel;
451 
452 	evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
453 	TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
454 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
455 	TEST_ASSERT_VAL("wrong exclude_user",
456 			!evsel->attr.exclude_user);
457 	TEST_ASSERT_VAL("wrong exclude_kernel",
458 			evsel->attr.exclude_kernel);
459 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
460 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
461 
462 	return 0;
463 }
464 
465 static int test__checkterms_simple(struct list_head *terms)
466 {
467 	struct parse_events__term *term;
468 
469 	/* config=10 */
470 	term = list_entry(terms->next, struct parse_events__term, list);
471 	TEST_ASSERT_VAL("wrong type term",
472 			term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG);
473 	TEST_ASSERT_VAL("wrong type val",
474 			term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
475 	TEST_ASSERT_VAL("wrong val", term->val.num == 10);
476 	TEST_ASSERT_VAL("wrong config", !term->config);
477 
478 	/* config1 */
479 	term = list_entry(term->list.next, struct parse_events__term, list);
480 	TEST_ASSERT_VAL("wrong type term",
481 			term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG1);
482 	TEST_ASSERT_VAL("wrong type val",
483 			term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
484 	TEST_ASSERT_VAL("wrong val", term->val.num == 1);
485 	TEST_ASSERT_VAL("wrong config", !term->config);
486 
487 	/* config2=3 */
488 	term = list_entry(term->list.next, struct parse_events__term, list);
489 	TEST_ASSERT_VAL("wrong type term",
490 			term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG2);
491 	TEST_ASSERT_VAL("wrong type val",
492 			term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
493 	TEST_ASSERT_VAL("wrong val", term->val.num == 3);
494 	TEST_ASSERT_VAL("wrong config", !term->config);
495 
496 	/* umask=1*/
497 	term = list_entry(term->list.next, struct parse_events__term, list);
498 	TEST_ASSERT_VAL("wrong type term",
499 			term->type_term == PARSE_EVENTS__TERM_TYPE_USER);
500 	TEST_ASSERT_VAL("wrong type val",
501 			term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
502 	TEST_ASSERT_VAL("wrong val", term->val.num == 1);
503 	TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "umask"));
504 
505 	return 0;
506 }
507 
508 static int test__group1(struct perf_evlist *evlist)
509 {
510 	struct perf_evsel *evsel, *leader;
511 
512 	TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
513 
514 	/* instructions:k */
515 	evsel = leader = perf_evlist__first(evlist);
516 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
517 	TEST_ASSERT_VAL("wrong config",
518 			PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
519 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
520 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
521 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
522 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
523 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
524 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
525 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
526 
527 	/* cycles:upp */
528 	evsel = perf_evsel__next(evsel);
529 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
530 	TEST_ASSERT_VAL("wrong config",
531 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
532 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
533 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
534 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
535 	/* use of precise requires exclude_guest */
536 	TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
537 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
538 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip == 2);
539 	TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
540 
541 	return 0;
542 }
543 
544 static int test__group2(struct perf_evlist *evlist)
545 {
546 	struct perf_evsel *evsel, *leader;
547 
548 	TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
549 
550 	/* faults + :ku modifier */
551 	evsel = leader = perf_evlist__first(evlist);
552 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type);
553 	TEST_ASSERT_VAL("wrong config",
554 			PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config);
555 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
556 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
557 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
558 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
559 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
560 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
561 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
562 
563 	/* cache-references + :u modifier */
564 	evsel = perf_evsel__next(evsel);
565 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
566 	TEST_ASSERT_VAL("wrong config",
567 			PERF_COUNT_HW_CACHE_REFERENCES == evsel->attr.config);
568 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
569 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
570 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
571 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
572 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
573 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
574 	TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
575 
576 	/* cycles:k */
577 	evsel = perf_evsel__next(evsel);
578 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
579 	TEST_ASSERT_VAL("wrong config",
580 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
581 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
582 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
583 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
584 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
585 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
586 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
587 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
588 
589 	return 0;
590 }
591 
592 static int test__group3(struct perf_evlist *evlist __maybe_unused)
593 {
594 	struct perf_evsel *evsel, *leader;
595 
596 	TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->nr_entries);
597 
598 	/* group1 syscalls:sys_enter_open:H */
599 	evsel = leader = perf_evlist__first(evlist);
600 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
601 	TEST_ASSERT_VAL("wrong sample_type",
602 		PERF_TP_SAMPLE_TYPE == evsel->attr.sample_type);
603 	TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
604 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
605 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
606 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
607 	TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
608 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
609 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
610 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
611 	TEST_ASSERT_VAL("wrong group name",
612 		!strcmp(leader->group_name, "group1"));
613 
614 	/* group1 cycles:kppp */
615 	evsel = perf_evsel__next(evsel);
616 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
617 	TEST_ASSERT_VAL("wrong config",
618 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
619 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
620 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
621 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
622 	/* use of precise requires exclude_guest */
623 	TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
624 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
625 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip == 3);
626 	TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
627 	TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
628 
629 	/* group2 cycles + G modifier */
630 	evsel = leader = perf_evsel__next(evsel);
631 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
632 	TEST_ASSERT_VAL("wrong config",
633 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
634 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
635 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
636 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
637 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
638 	TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
639 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
640 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
641 	TEST_ASSERT_VAL("wrong group name",
642 		!strcmp(leader->group_name, "group2"));
643 
644 	/* group2 1:3 + G modifier */
645 	evsel = perf_evsel__next(evsel);
646 	TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
647 	TEST_ASSERT_VAL("wrong config", 3 == evsel->attr.config);
648 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
649 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
650 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
651 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
652 	TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
653 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
654 	TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
655 
656 	/* instructions:u */
657 	evsel = perf_evsel__next(evsel);
658 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
659 	TEST_ASSERT_VAL("wrong config",
660 			PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
661 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
662 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
663 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
664 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
665 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
666 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
667 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
668 
669 	return 0;
670 }
671 
672 static int test__group4(struct perf_evlist *evlist __maybe_unused)
673 {
674 	struct perf_evsel *evsel, *leader;
675 
676 	TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->nr_entries);
677 
678 	/* cycles:u + p */
679 	evsel = leader = perf_evlist__first(evlist);
680 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
681 	TEST_ASSERT_VAL("wrong config",
682 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
683 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
684 	TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
685 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
686 	/* use of precise requires exclude_guest */
687 	TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
688 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
689 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip == 1);
690 	TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
691 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
692 
693 	/* instructions:kp + p */
694 	evsel = perf_evsel__next(evsel);
695 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
696 	TEST_ASSERT_VAL("wrong config",
697 			PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
698 	TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
699 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
700 	TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
701 	/* use of precise requires exclude_guest */
702 	TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
703 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
704 	TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip == 2);
705 	TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
706 
707 	return 0;
708 }
709 
710 static int test__group5(struct perf_evlist *evlist __maybe_unused)
711 {
712 	struct perf_evsel *evsel, *leader;
713 
714 	TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->nr_entries);
715 
716 	/* cycles + G */
717 	evsel = leader = perf_evlist__first(evlist);
718 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
719 	TEST_ASSERT_VAL("wrong config",
720 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
721 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
722 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
723 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
724 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
725 	TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
726 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
727 	TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
728 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
729 
730 	/* instructions + G */
731 	evsel = perf_evsel__next(evsel);
732 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
733 	TEST_ASSERT_VAL("wrong config",
734 			PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
735 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
736 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
737 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
738 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
739 	TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
740 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
741 	TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
742 
743 	/* cycles:G */
744 	evsel = leader = perf_evsel__next(evsel);
745 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
746 	TEST_ASSERT_VAL("wrong config",
747 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
748 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
749 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
750 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
751 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
752 	TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
753 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
754 	TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
755 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
756 
757 	/* instructions:G */
758 	evsel = perf_evsel__next(evsel);
759 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
760 	TEST_ASSERT_VAL("wrong config",
761 			PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
762 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
763 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
764 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
765 	TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
766 	TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
767 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
768 	TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
769 
770 	/* cycles */
771 	evsel = perf_evsel__next(evsel);
772 	TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
773 	TEST_ASSERT_VAL("wrong config",
774 			PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config);
775 	TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
776 	TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
777 	TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
778 	TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
779 	TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
780 	TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
781 	TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
782 
783 	return 0;
784 }
785 
786 static int count_tracepoints(void)
787 {
788 	char events_path[PATH_MAX];
789 	struct dirent *events_ent;
790 	DIR *events_dir;
791 	int cnt = 0;
792 
793 	scnprintf(events_path, PATH_MAX, "%s/tracing/events",
794 		  debugfs_find_mountpoint());
795 
796 	events_dir = opendir(events_path);
797 
798 	TEST_ASSERT_VAL("Can't open events dir", events_dir);
799 
800 	while ((events_ent = readdir(events_dir))) {
801 		char sys_path[PATH_MAX];
802 		struct dirent *sys_ent;
803 		DIR *sys_dir;
804 
805 		if (!strcmp(events_ent->d_name, ".")
806 		    || !strcmp(events_ent->d_name, "..")
807 		    || !strcmp(events_ent->d_name, "enable")
808 		    || !strcmp(events_ent->d_name, "header_event")
809 		    || !strcmp(events_ent->d_name, "header_page"))
810 			continue;
811 
812 		scnprintf(sys_path, PATH_MAX, "%s/%s",
813 			  events_path, events_ent->d_name);
814 
815 		sys_dir = opendir(sys_path);
816 		TEST_ASSERT_VAL("Can't open sys dir", sys_dir);
817 
818 		while ((sys_ent = readdir(sys_dir))) {
819 			if (!strcmp(sys_ent->d_name, ".")
820 			    || !strcmp(sys_ent->d_name, "..")
821 			    || !strcmp(sys_ent->d_name, "enable")
822 			    || !strcmp(sys_ent->d_name, "filter"))
823 				continue;
824 
825 			cnt++;
826 		}
827 
828 		closedir(sys_dir);
829 	}
830 
831 	closedir(events_dir);
832 	return cnt;
833 }
834 
835 static int test__all_tracepoints(struct perf_evlist *evlist)
836 {
837 	TEST_ASSERT_VAL("wrong events count",
838 			count_tracepoints() == evlist->nr_entries);
839 
840 	return test__checkevent_tracepoint_multi(evlist);
841 }
842 
843 struct test__event_st {
844 	const char *name;
845 	__u32 type;
846 	int (*check)(struct perf_evlist *evlist);
847 };
848 
849 static struct test__event_st test__events[] = {
850 	[0] = {
851 		.name  = "syscalls:sys_enter_open",
852 		.check = test__checkevent_tracepoint,
853 	},
854 	[1] = {
855 		.name  = "syscalls:*",
856 		.check = test__checkevent_tracepoint_multi,
857 	},
858 	[2] = {
859 		.name  = "r1a",
860 		.check = test__checkevent_raw,
861 	},
862 	[3] = {
863 		.name  = "1:1",
864 		.check = test__checkevent_numeric,
865 	},
866 	[4] = {
867 		.name  = "instructions",
868 		.check = test__checkevent_symbolic_name,
869 	},
870 	[5] = {
871 		.name  = "cycles/period=100000,config2/",
872 		.check = test__checkevent_symbolic_name_config,
873 	},
874 	[6] = {
875 		.name  = "faults",
876 		.check = test__checkevent_symbolic_alias,
877 	},
878 	[7] = {
879 		.name  = "L1-dcache-load-miss",
880 		.check = test__checkevent_genhw,
881 	},
882 	[8] = {
883 		.name  = "mem:0",
884 		.check = test__checkevent_breakpoint,
885 	},
886 	[9] = {
887 		.name  = "mem:0:x",
888 		.check = test__checkevent_breakpoint_x,
889 	},
890 	[10] = {
891 		.name  = "mem:0:r",
892 		.check = test__checkevent_breakpoint_r,
893 	},
894 	[11] = {
895 		.name  = "mem:0:w",
896 		.check = test__checkevent_breakpoint_w,
897 	},
898 	[12] = {
899 		.name  = "syscalls:sys_enter_open:k",
900 		.check = test__checkevent_tracepoint_modifier,
901 	},
902 	[13] = {
903 		.name  = "syscalls:*:u",
904 		.check = test__checkevent_tracepoint_multi_modifier,
905 	},
906 	[14] = {
907 		.name  = "r1a:kp",
908 		.check = test__checkevent_raw_modifier,
909 	},
910 	[15] = {
911 		.name  = "1:1:hp",
912 		.check = test__checkevent_numeric_modifier,
913 	},
914 	[16] = {
915 		.name  = "instructions:h",
916 		.check = test__checkevent_symbolic_name_modifier,
917 	},
918 	[17] = {
919 		.name  = "faults:u",
920 		.check = test__checkevent_symbolic_alias_modifier,
921 	},
922 	[18] = {
923 		.name  = "L1-dcache-load-miss:kp",
924 		.check = test__checkevent_genhw_modifier,
925 	},
926 	[19] = {
927 		.name  = "mem:0:u",
928 		.check = test__checkevent_breakpoint_modifier,
929 	},
930 	[20] = {
931 		.name  = "mem:0:x:k",
932 		.check = test__checkevent_breakpoint_x_modifier,
933 	},
934 	[21] = {
935 		.name  = "mem:0:r:hp",
936 		.check = test__checkevent_breakpoint_r_modifier,
937 	},
938 	[22] = {
939 		.name  = "mem:0:w:up",
940 		.check = test__checkevent_breakpoint_w_modifier,
941 	},
942 	[23] = {
943 		.name  = "r1,syscalls:sys_enter_open:k,1:1:hp",
944 		.check = test__checkevent_list,
945 	},
946 	[24] = {
947 		.name  = "instructions:G",
948 		.check = test__checkevent_exclude_host_modifier,
949 	},
950 	[25] = {
951 		.name  = "instructions:H",
952 		.check = test__checkevent_exclude_guest_modifier,
953 	},
954 	[26] = {
955 		.name  = "mem:0:rw",
956 		.check = test__checkevent_breakpoint_rw,
957 	},
958 	[27] = {
959 		.name  = "mem:0:rw:kp",
960 		.check = test__checkevent_breakpoint_rw_modifier,
961 	},
962 	[28] = {
963 		.name  = "{instructions:k,cycles:upp}",
964 		.check = test__group1,
965 	},
966 	[29] = {
967 		.name  = "{faults:k,cache-references}:u,cycles:k",
968 		.check = test__group2,
969 	},
970 	[30] = {
971 		.name  = "group1{syscalls:sys_enter_open:H,cycles:kppp},group2{cycles,1:3}:G,instructions:u",
972 		.check = test__group3,
973 	},
974 	[31] = {
975 		.name  = "{cycles:u,instructions:kp}:p",
976 		.check = test__group4,
977 	},
978 	[32] = {
979 		.name  = "{cycles,instructions}:G,{cycles:G,instructions:G},cycles",
980 		.check = test__group5,
981 	},
982 	[33] = {
983 		.name  = "*:*",
984 		.check = test__all_tracepoints,
985 	},
986 };
987 
988 static struct test__event_st test__events_pmu[] = {
989 	[0] = {
990 		.name  = "cpu/config=10,config1,config2=3,period=1000/u",
991 		.check = test__checkevent_pmu,
992 	},
993 	[1] = {
994 		.name  = "cpu/config=1,name=krava/u,cpu/config=2/u",
995 		.check = test__checkevent_pmu_name,
996 	},
997 };
998 
999 struct test__term {
1000 	const char *str;
1001 	__u32 type;
1002 	int (*check)(struct list_head *terms);
1003 };
1004 
1005 static struct test__term test__terms[] = {
1006 	[0] = {
1007 		.str   = "config=10,config1,config2=3,umask=1",
1008 		.check = test__checkterms_simple,
1009 	},
1010 };
1011 
1012 static int test_event(struct test__event_st *e)
1013 {
1014 	struct perf_evlist *evlist;
1015 	int ret;
1016 
1017 	evlist = perf_evlist__new(NULL, NULL);
1018 	if (evlist == NULL)
1019 		return -ENOMEM;
1020 
1021 	ret = parse_events(evlist, e->name);
1022 	if (ret) {
1023 		pr_debug("failed to parse event '%s', err %d\n",
1024 			 e->name, ret);
1025 		return ret;
1026 	}
1027 
1028 	ret = e->check(evlist);
1029 	perf_evlist__delete(evlist);
1030 
1031 	return ret;
1032 }
1033 
1034 static int test_events(struct test__event_st *events, unsigned cnt)
1035 {
1036 	int ret1, ret2 = 0;
1037 	unsigned i;
1038 
1039 	for (i = 0; i < cnt; i++) {
1040 		struct test__event_st *e = &events[i];
1041 
1042 		pr_debug("running test %d '%s'\n", i, e->name);
1043 		ret1 = test_event(e);
1044 		if (ret1)
1045 			ret2 = ret1;
1046 	}
1047 
1048 	return ret2;
1049 }
1050 
1051 static int test_term(struct test__term *t)
1052 {
1053 	struct list_head *terms;
1054 	int ret;
1055 
1056 	terms = malloc(sizeof(*terms));
1057 	if (!terms)
1058 		return -ENOMEM;
1059 
1060 	INIT_LIST_HEAD(terms);
1061 
1062 	ret = parse_events_terms(terms, t->str);
1063 	if (ret) {
1064 		pr_debug("failed to parse terms '%s', err %d\n",
1065 			 t->str , ret);
1066 		return ret;
1067 	}
1068 
1069 	ret = t->check(terms);
1070 	parse_events__free_terms(terms);
1071 
1072 	return ret;
1073 }
1074 
1075 static int test_terms(struct test__term *terms, unsigned cnt)
1076 {
1077 	int ret = 0;
1078 	unsigned i;
1079 
1080 	for (i = 0; i < cnt; i++) {
1081 		struct test__term *t = &terms[i];
1082 
1083 		pr_debug("running test %d '%s'\n", i, t->str);
1084 		ret = test_term(t);
1085 		if (ret)
1086 			break;
1087 	}
1088 
1089 	return ret;
1090 }
1091 
1092 static int test_pmu(void)
1093 {
1094 	struct stat st;
1095 	char path[PATH_MAX];
1096 	int ret;
1097 
1098 	snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/format/",
1099 		 sysfs_find_mountpoint());
1100 
1101 	ret = stat(path, &st);
1102 	if (ret)
1103 		pr_debug("omitting PMU cpu tests\n");
1104 	return !ret;
1105 }
1106 
1107 static int test_pmu_events(void)
1108 {
1109 	struct stat st;
1110 	char path[PATH_MAX];
1111 	struct dirent *ent;
1112 	DIR *dir;
1113 	int ret;
1114 
1115 	snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/events/",
1116 		 sysfs_find_mountpoint());
1117 
1118 	ret = stat(path, &st);
1119 	if (ret) {
1120 		pr_debug("ommiting PMU cpu events tests\n");
1121 		return 0;
1122 	}
1123 
1124 	dir = opendir(path);
1125 	if (!dir) {
1126 		pr_debug("can't open pmu event dir");
1127 		return -1;
1128 	}
1129 
1130 	while (!ret && (ent = readdir(dir))) {
1131 #define MAX_NAME 100
1132 		struct test__event_st e;
1133 		char name[MAX_NAME];
1134 
1135 		if (!strcmp(ent->d_name, ".") ||
1136 		    !strcmp(ent->d_name, ".."))
1137 			continue;
1138 
1139 		snprintf(name, MAX_NAME, "cpu/event=%s/u", ent->d_name);
1140 
1141 		e.name  = name;
1142 		e.check = test__checkevent_pmu_events;
1143 
1144 		ret = test_event(&e);
1145 #undef MAX_NAME
1146 	}
1147 
1148 	closedir(dir);
1149 	return ret;
1150 }
1151 
1152 int test__parse_events(void)
1153 {
1154 	int ret1, ret2 = 0;
1155 
1156 #define TEST_EVENTS(tests)				\
1157 do {							\
1158 	ret1 = test_events(tests, ARRAY_SIZE(tests));	\
1159 	if (!ret2)					\
1160 		ret2 = ret1;				\
1161 } while (0)
1162 
1163 	TEST_EVENTS(test__events);
1164 
1165 	if (test_pmu())
1166 		TEST_EVENTS(test__events_pmu);
1167 
1168 	if (test_pmu()) {
1169 		int ret = test_pmu_events();
1170 		if (ret)
1171 			return ret;
1172 	}
1173 
1174 	ret1 = test_terms(test__terms, ARRAY_SIZE(test__terms));
1175 	if (!ret2)
1176 		ret2 = ret1;
1177 
1178 	return ret2;
1179 }
1180