xref: /openbmc/linux/tools/perf/tests/evsel-roundtrip-name.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cfffae2eSJiri Olsa #include "evlist.h"
3cfffae2eSJiri Olsa #include "evsel.h"
4cfffae2eSJiri Olsa #include "parse-events.h"
5cfffae2eSJiri Olsa #include "tests.h"
684f5d36fSJiri Olsa #include "debug.h"
7877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
8cfffae2eSJiri Olsa 
perf_evsel__roundtrip_cache_name_test(void)9cfffae2eSJiri Olsa static int perf_evsel__roundtrip_cache_name_test(void)
10cfffae2eSJiri Olsa {
11*a8af6e48SIan Rogers 	int ret = TEST_OK;
12*a8af6e48SIan Rogers 
13*a8af6e48SIan Rogers 	for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
14*a8af6e48SIan Rogers 		for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
15*a8af6e48SIan Rogers 			/* skip invalid cache type */
16*a8af6e48SIan Rogers 			if (!evsel__is_cache_op_valid(type, op))
17*a8af6e48SIan Rogers 				continue;
18*a8af6e48SIan Rogers 
19*a8af6e48SIan Rogers 			for (int res = 0; res < PERF_COUNT_HW_CACHE_RESULT_MAX; res++) {
20cfffae2eSJiri Olsa 				char name[128];
210f98b11cSJiri Olsa 				struct evlist *evlist = evlist__new();
22*a8af6e48SIan Rogers 				struct evsel *evsel;
23*a8af6e48SIan Rogers 				int err;
24cfffae2eSJiri Olsa 
25*a8af6e48SIan Rogers 				if (evlist == NULL) {
26*a8af6e48SIan Rogers 					pr_debug("Failed to alloc evlist");
27*a8af6e48SIan Rogers 					return TEST_FAIL;
28*a8af6e48SIan Rogers 				}
29*a8af6e48SIan Rogers 				__evsel__hw_cache_type_op_res_name(type, op, res,
30*a8af6e48SIan Rogers 								name, sizeof(name));
31cfffae2eSJiri Olsa 
32806731a9SAdrian Hunter 				err = parse_event(evlist, name);
33*a8af6e48SIan Rogers 				if (err) {
34*a8af6e48SIan Rogers 					pr_debug("Failure to parse cache event '%s' possibly as PMUs don't support it",
35*a8af6e48SIan Rogers 						name);
36*a8af6e48SIan Rogers 					evlist__delete(evlist);
37cfffae2eSJiri Olsa 					continue;
38*a8af6e48SIan Rogers 				}
39*a8af6e48SIan Rogers 				evlist__for_each_entry(evlist, evsel) {
408ab2e96dSArnaldo Carvalho de Melo 					if (strcmp(evsel__name(evsel), name)) {
418ab2e96dSArnaldo Carvalho de Melo 						pr_debug("%s != %s\n", evsel__name(evsel), name);
42*a8af6e48SIan Rogers 						ret = TEST_FAIL;
43cfffae2eSJiri Olsa 					}
44cfffae2eSJiri Olsa 				}
45c12995a5SJiri Olsa 				evlist__delete(evlist);
46*a8af6e48SIan Rogers 			}
47*a8af6e48SIan Rogers 		}
48*a8af6e48SIan Rogers 	}
49cfffae2eSJiri Olsa 	return ret;
50cfffae2eSJiri Olsa }
51cfffae2eSJiri Olsa 
perf_evsel__name_array_test(const char * const names[],int nr_names)52*a8af6e48SIan Rogers static int perf_evsel__name_array_test(const char *const names[], int nr_names)
53cfffae2eSJiri Olsa {
54*a8af6e48SIan Rogers 	int ret = TEST_OK;
55*a8af6e48SIan Rogers 
56*a8af6e48SIan Rogers 	for (int i = 0; i < nr_names; ++i) {
570f98b11cSJiri Olsa 		struct evlist *evlist = evlist__new();
58*a8af6e48SIan Rogers 		struct evsel *evsel;
59*a8af6e48SIan Rogers 		int err;
60cfffae2eSJiri Olsa 
61*a8af6e48SIan Rogers 		if (evlist == NULL) {
62*a8af6e48SIan Rogers 			pr_debug("Failed to alloc evlist");
63*a8af6e48SIan Rogers 			return TEST_FAIL;
64*a8af6e48SIan Rogers 		}
65806731a9SAdrian Hunter 		err = parse_event(evlist, names[i]);
66cfffae2eSJiri Olsa 		if (err) {
67cfffae2eSJiri Olsa 			pr_debug("failed to parse event '%s', err %d\n",
68cfffae2eSJiri Olsa 				 names[i], err);
69c12995a5SJiri Olsa 			evlist__delete(evlist);
70*a8af6e48SIan Rogers 			ret = TEST_FAIL;
71*a8af6e48SIan Rogers 			continue;
72cfffae2eSJiri Olsa 		}
73*a8af6e48SIan Rogers 		evlist__for_each_entry(evlist, evsel) {
74*a8af6e48SIan Rogers 			if (strcmp(evsel__name(evsel), names[i])) {
75*a8af6e48SIan Rogers 				pr_debug("%s != %s\n", evsel__name(evsel), names[i]);
76*a8af6e48SIan Rogers 				ret = TEST_FAIL;
77*a8af6e48SIan Rogers 			}
78*a8af6e48SIan Rogers 		}
79*a8af6e48SIan Rogers 		evlist__delete(evlist);
80*a8af6e48SIan Rogers 	}
81*a8af6e48SIan Rogers 	return ret;
82*a8af6e48SIan Rogers }
83cfffae2eSJiri Olsa 
test__perf_evsel__roundtrip_name_test(struct test_suite * test __maybe_unused,int subtest __maybe_unused)8433f44bfdSIan Rogers static int test__perf_evsel__roundtrip_name_test(struct test_suite *test __maybe_unused,
85d68f0365SIan Rogers 						 int subtest __maybe_unused)
86cfffae2eSJiri Olsa {
87*a8af6e48SIan Rogers 	int err = 0, ret = TEST_OK;
88cfffae2eSJiri Olsa 
89*a8af6e48SIan Rogers 	err = perf_evsel__name_array_test(evsel__hw_names, PERF_COUNT_HW_MAX);
90cfffae2eSJiri Olsa 	if (err)
91cfffae2eSJiri Olsa 		ret = err;
92cfffae2eSJiri Olsa 
93*a8af6e48SIan Rogers 	err = perf_evsel__name_array_test(evsel__sw_names, PERF_COUNT_SW_DUMMY + 1);
94cfffae2eSJiri Olsa 	if (err)
95cfffae2eSJiri Olsa 		ret = err;
96cfffae2eSJiri Olsa 
97cfffae2eSJiri Olsa 	err = perf_evsel__roundtrip_cache_name_test();
98cfffae2eSJiri Olsa 	if (err)
99cfffae2eSJiri Olsa 		ret = err;
100cfffae2eSJiri Olsa 
101cfffae2eSJiri Olsa 	return ret;
102cfffae2eSJiri Olsa }
103d68f0365SIan Rogers 
104d68f0365SIan Rogers DEFINE_SUITE("Roundtrip evsel->name", perf_evsel__roundtrip_name_test);
105