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