xref: /openbmc/linux/tools/perf/tests/pmu.c (revision 9134211f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "parse-events.h"
3 #include "pmu.h"
4 #include "tests.h"
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <linux/kernel.h>
9 #include <linux/limits.h>
10 
11 /* Simulated format definitions. */
12 static struct test_format {
13 	const char *name;
14 	const char *value;
15 } test_formats[] = {
16 	{ "krava01", "config:0-1,62-63\n", },
17 	{ "krava02", "config:10-17\n", },
18 	{ "krava03", "config:5\n", },
19 	{ "krava11", "config1:0,2,4,6,8,20-28\n", },
20 	{ "krava12", "config1:63\n", },
21 	{ "krava13", "config1:45-47\n", },
22 	{ "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
23 	{ "krava22", "config2:8,18,48,58\n", },
24 	{ "krava23", "config2:28-29,38\n", },
25 };
26 
27 /* Simulated users input. */
28 static struct parse_events_term test_terms[] = {
29 	{
30 		.config    = (char *) "krava01",
31 		.val.num   = 15,
32 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
33 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
34 	},
35 	{
36 		.config    = (char *) "krava02",
37 		.val.num   = 170,
38 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
39 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
40 	},
41 	{
42 		.config    = (char *) "krava03",
43 		.val.num   = 1,
44 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
45 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
46 	},
47 	{
48 		.config    = (char *) "krava11",
49 		.val.num   = 27,
50 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
51 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
52 	},
53 	{
54 		.config    = (char *) "krava12",
55 		.val.num   = 1,
56 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
57 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
58 	},
59 	{
60 		.config    = (char *) "krava13",
61 		.val.num   = 2,
62 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
63 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
64 	},
65 	{
66 		.config    = (char *) "krava21",
67 		.val.num   = 119,
68 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
69 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
70 	},
71 	{
72 		.config    = (char *) "krava22",
73 		.val.num   = 11,
74 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
75 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
76 	},
77 	{
78 		.config    = (char *) "krava23",
79 		.val.num   = 2,
80 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
81 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
82 	},
83 };
84 
85 /*
86  * Prepare format directory data, exported by kernel
87  * at /sys/bus/event_source/devices/<dev>/format.
88  */
89 static char *test_format_dir_get(char *dir, size_t sz)
90 {
91 	unsigned int i;
92 
93 	snprintf(dir, sz, "/tmp/perf-pmu-test-format-XXXXXX");
94 	if (!mkdtemp(dir))
95 		return NULL;
96 
97 	for (i = 0; i < ARRAY_SIZE(test_formats); i++) {
98 		char name[PATH_MAX];
99 		struct test_format *format = &test_formats[i];
100 		FILE *file;
101 
102 		scnprintf(name, PATH_MAX, "%s/%s", dir, format->name);
103 
104 		file = fopen(name, "w");
105 		if (!file)
106 			return NULL;
107 
108 		if (1 != fwrite(format->value, strlen(format->value), 1, file))
109 			break;
110 
111 		fclose(file);
112 	}
113 
114 	return dir;
115 }
116 
117 /* Cleanup format directory. */
118 static int test_format_dir_put(char *dir)
119 {
120 	char buf[PATH_MAX + 20];
121 
122 	snprintf(buf, sizeof(buf), "rm -f %s/*\n", dir);
123 	if (system(buf))
124 		return -1;
125 
126 	snprintf(buf, sizeof(buf), "rmdir %s\n", dir);
127 	return system(buf);
128 }
129 
130 static struct list_head *test_terms_list(void)
131 {
132 	static LIST_HEAD(terms);
133 	unsigned int i;
134 
135 	for (i = 0; i < ARRAY_SIZE(test_terms); i++)
136 		list_add_tail(&test_terms[i].list, &terms);
137 
138 	return &terms;
139 }
140 
141 static int test__pmu(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
142 {
143 	char dir[PATH_MAX];
144 	char *format = test_format_dir_get(dir, sizeof(dir));
145 	LIST_HEAD(formats);
146 	struct list_head *terms = test_terms_list();
147 	int ret;
148 
149 	if (!format)
150 		return -EINVAL;
151 
152 	do {
153 		struct perf_event_attr attr;
154 		int fd;
155 
156 		memset(&attr, 0, sizeof(attr));
157 
158 		fd = open(format, O_DIRECTORY);
159 		if (fd < 0) {
160 			ret = fd;
161 			break;
162 		}
163 		ret = perf_pmu__format_parse(fd, &formats);
164 		if (ret)
165 			break;
166 
167 		ret = perf_pmu__config_terms("perf-pmu-test", &formats, &attr,
168 					     terms, false, NULL);
169 		if (ret)
170 			break;
171 
172 		ret = -EINVAL;
173 
174 		if (attr.config  != 0xc00000000002a823)
175 			break;
176 		if (attr.config1 != 0x8000400000000145)
177 			break;
178 		if (attr.config2 != 0x0400000020041d07)
179 			break;
180 
181 		ret = 0;
182 	} while (0);
183 
184 	perf_pmu__del_formats(&formats);
185 	test_format_dir_put(format);
186 	return ret;
187 }
188 
189 DEFINE_SUITE("Parse perf pmu format", pmu);
190