xref: /linux/tools/perf/tests/tool_pmu.c (revision 83684c4e4d62cb02b2e4d0d18963d1035439278e)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 #include "debug.h"
3 #include "evlist.h"
4 #include "parse-events.h"
5 #include "tests.h"
6 #include "tool_pmu.h"
7 
8 static int do_test(enum tool_pmu_event ev, bool with_pmu)
9 {
10 	struct evlist *evlist = evlist__new();
11 	struct evsel *evsel;
12 	struct parse_events_error err;
13 	int ret;
14 	char str[128];
15 	bool found = false;
16 
17 	if (!evlist) {
18 		pr_err("evlist allocation failed\n");
19 		return TEST_FAIL;
20 	}
21 
22 	if (with_pmu)
23 		snprintf(str, sizeof(str), "tool/%s/", tool_pmu__event_to_str(ev));
24 	else
25 		snprintf(str, sizeof(str), "%s", tool_pmu__event_to_str(ev));
26 
27 	parse_events_error__init(&err);
28 	ret = parse_events(evlist, str, &err);
29 	if (ret) {
30 		if (!tool_pmu__event_to_str(ev)) {
31 			ret = TEST_OK;
32 			goto out;
33 		}
34 
35 		pr_debug("FAILED %s:%d failed to parse event '%s', err %d\n",
36 			 __FILE__, __LINE__, str, ret);
37 		parse_events_error__print(&err, str);
38 		ret = TEST_FAIL;
39 		goto out;
40 	}
41 
42 	ret = TEST_OK;
43 	if (with_pmu ? (evlist__nr_entries(evlist) != 1)
44 		     : (evlist__nr_entries(evlist) < 1)) {
45 		pr_debug("FAILED %s:%d Unexpected number of events for '%s' of %d\n",
46 			 __FILE__, __LINE__, str, evlist__nr_entries(evlist));
47 		ret = TEST_FAIL;
48 		goto out;
49 	}
50 
51 	evlist__for_each_entry(evlist, evsel) {
52 		if (perf_pmu__is_tool(evsel->pmu)) {
53 			if (evsel->core.attr.config != ev) {
54 				pr_debug("FAILED %s:%d Unexpected config for '%s', %lld != %d\n",
55 					__FILE__, __LINE__, str, evsel->core.attr.config, ev);
56 				ret = TEST_FAIL;
57 				goto out;
58 			}
59 			found = true;
60 		}
61 	}
62 
63 	if (!found && tool_pmu__event_to_str(ev)) {
64 		pr_debug("FAILED %s:%d Didn't find tool event '%s' in parsed evsels\n",
65 			 __FILE__, __LINE__, str);
66 		ret = TEST_FAIL;
67 	}
68 
69 out:
70 	parse_events_error__exit(&err);
71 	evlist__put(evlist);
72 	return ret;
73 }
74 
75 static int test__tool_pmu_without_pmu(struct test_suite *test __maybe_unused,
76 				      int subtest __maybe_unused)
77 {
78 	int i;
79 
80 	tool_pmu__for_each_event(i) {
81 		int ret = do_test(i, /*with_pmu=*/false);
82 
83 		if (ret != TEST_OK)
84 			return ret;
85 	}
86 	return TEST_OK;
87 }
88 
89 static int test__tool_pmu_with_pmu(struct test_suite *test __maybe_unused,
90 				   int subtest __maybe_unused)
91 {
92 	int i;
93 
94 	tool_pmu__for_each_event(i) {
95 		int ret = do_test(i, /*with_pmu=*/true);
96 
97 		if (ret != TEST_OK)
98 			return ret;
99 	}
100 	return TEST_OK;
101 }
102 
103 static struct test_case tests__tool_pmu[] = {
104 	TEST_CASE("Parsing without PMU name", tool_pmu_without_pmu),
105 	TEST_CASE("Parsing with PMU name", tool_pmu_with_pmu),
106 	{	.name = NULL, }
107 };
108 
109 struct test_suite suite__tool_pmu = {
110 	.desc = "Tool PMU",
111 	.test_cases = tests__tool_pmu,
112 };
113