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