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