1 #include "evlist.h"
2 #include "evsel.h"
3 #include "parse-events.h"
4 #include "tests.h"
5 
perf_evsel__roundtrip_cache_name_test(void)6 static int perf_evsel__roundtrip_cache_name_test(void)
7 {
8 	char name[128];
9 	int type, op, err = 0, ret = 0, i, idx;
10 	struct perf_evsel *evsel;
11 	struct perf_evlist *evlist = perf_evlist__new();
12 
13         if (evlist == NULL)
14                 return -ENOMEM;
15 
16 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
17 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
18 			/* skip invalid cache type */
19 			if (!perf_evsel__is_cache_op_valid(type, op))
20 				continue;
21 
22 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
23 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
24 									name, sizeof(name));
25 				err = parse_events(evlist, name);
26 				if (err)
27 					ret = err;
28 			}
29 		}
30 	}
31 
32 	idx = 0;
33 	evsel = perf_evlist__first(evlist);
34 
35 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
36 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
37 			/* skip invalid cache type */
38 			if (!perf_evsel__is_cache_op_valid(type, op))
39 				continue;
40 
41 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
42 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
43 									name, sizeof(name));
44 				if (evsel->idx != idx)
45 					continue;
46 
47 				++idx;
48 
49 				if (strcmp(perf_evsel__name(evsel), name)) {
50 					pr_debug("%s != %s\n", perf_evsel__name(evsel), name);
51 					ret = -1;
52 				}
53 
54 				evsel = perf_evsel__next(evsel);
55 			}
56 		}
57 	}
58 
59 	perf_evlist__delete(evlist);
60 	return ret;
61 }
62 
__perf_evsel__name_array_test(const char * names[],int nr_names)63 static int __perf_evsel__name_array_test(const char *names[], int nr_names)
64 {
65 	int i, err;
66 	struct perf_evsel *evsel;
67 	struct perf_evlist *evlist = perf_evlist__new();
68 
69         if (evlist == NULL)
70                 return -ENOMEM;
71 
72 	for (i = 0; i < nr_names; ++i) {
73 		err = parse_events(evlist, names[i]);
74 		if (err) {
75 			pr_debug("failed to parse event '%s', err %d\n",
76 				 names[i], err);
77 			goto out_delete_evlist;
78 		}
79 	}
80 
81 	err = 0;
82 	list_for_each_entry(evsel, &evlist->entries, node) {
83 		if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) {
84 			--err;
85 			pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]);
86 		}
87 	}
88 
89 out_delete_evlist:
90 	perf_evlist__delete(evlist);
91 	return err;
92 }
93 
94 #define perf_evsel__name_array_test(names) \
95 	__perf_evsel__name_array_test(names, ARRAY_SIZE(names))
96 
test__perf_evsel__roundtrip_name_test(void)97 int test__perf_evsel__roundtrip_name_test(void)
98 {
99 	int err = 0, ret = 0;
100 
101 	err = perf_evsel__name_array_test(perf_evsel__hw_names);
102 	if (err)
103 		ret = err;
104 
105 	err = perf_evsel__name_array_test(perf_evsel__sw_names);
106 	if (err)
107 		ret = err;
108 
109 	err = perf_evsel__roundtrip_cache_name_test();
110 	if (err)
111 		ret = err;
112 
113 	return ret;
114 }
115