xref: /linux/tools/perf/tests/evsel-roundtrip-name.c (revision 61b7369483efb5e0a9f3b48e75fac00d46d661e0)
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 <linux/kernel.h>
8 
9 static int perf_evsel__roundtrip_cache_name_test(void)
10 {
11 	int ret = TEST_OK;
12 
13 	for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
14 		for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
15 			/* skip invalid cache type */
16 			if (!evsel__is_cache_op_valid(type, op))
17 				continue;
18 
19 			for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
20 				char name[128];
21 				struct evlist *evlist = evlist__new();
22 				struct evsel *evsel;
23 				int err;
24 
25 				if (evlist == NULL) {
26 					pr_debug("Failed to alloc evlist");
27 					return TEST_FAIL;
28 				}
29 				__evsel__hw_cache_type_op_res_name(type, op, res,
30 								name, sizeof(name));
31 
32 				err = parse_event(evlist, name);
33 				if (err) {
34 					pr_debug("Failure to parse cache event '%s' possibly as PMUs don't support it",
35 						name);
36 					evlist__delete(evlist);
37 					continue;
38 				}
39 				evlist__for_each_entry(evlist, evsel) {
40 					if (strcmp(evsel__name(evsel), name)) {
41 						pr_debug("%s != %s\n", evsel__name(evsel), name);
42 						ret = TEST_FAIL;
43 					}
44 				}
45 				evlist__delete(evlist);
46 			}
47 		}
48 	}
49 	return ret;
50 }
51 
52 static int perf_evsel__name_array_test(const char *const names[], int nr_names)
53 {
54 	int ret = TEST_OK;
55 
56 	for (int i = 0; i < nr_names; ++i) {
57 		struct evlist *evlist = evlist__new();
58 		struct evsel *evsel;
59 		int err;
60 
61 		if (evlist == NULL) {
62 			pr_debug("Failed to alloc evlist");
63 			return TEST_FAIL;
64 		}
65 		err = parse_event(evlist, names[i]);
66 		if (err) {
67 			pr_debug("failed to parse event '%s', err %d\n",
68 				 names[i], err);
69 			evlist__delete(evlist);
70 			ret = TEST_FAIL;
71 			continue;
72 		}
73 		evlist__for_each_entry(evlist, evsel) {
74 			if (strcmp(evsel__name(evsel), names[i])) {
75 				pr_debug("%s != %s\n", evsel__name(evsel), names[i]);
76 				ret = TEST_FAIL;
77 			}
78 		}
79 		evlist__delete(evlist);
80 	}
81 	return ret;
82 }
83 
84 static int test__perf_evsel__roundtrip_name_test(struct test_suite *test __maybe_unused,
85 						 int subtest __maybe_unused)
86 {
87 	int err = 0, ret = TEST_OK;
88 
89 	err = perf_evsel__name_array_test(evsel__hw_names, PERF_COUNT_HW_MAX);
90 	if (err)
91 		ret = err;
92 
93 	err = perf_evsel__name_array_test(evsel__sw_names, PERF_COUNT_SW_DUMMY + 1);
94 	if (err)
95 		ret = err;
96 
97 	err = perf_evsel__roundtrip_cache_name_test();
98 	if (err)
99 		ret = err;
100 
101 	return ret;
102 }
103 
104 DEFINE_SUITE("Roundtrip evsel->name", perf_evsel__roundtrip_name_test);
105