Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / tests / pmu.c
blob5c11fe2b30406815f3baaa5946f1d040f5db89d3
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 <stdio.h>
7 #include <linux/kernel.h>
8 #include <linux/limits.h>
10 /* Simulated format definitions. */
11 static struct test_format {
12 const char *name;
13 const char *value;
14 } test_formats[] = {
15 { "krava01", "config:0-1,62-63\n", },
16 { "krava02", "config:10-17\n", },
17 { "krava03", "config:5\n", },
18 { "krava11", "config1:0,2,4,6,8,20-28\n", },
19 { "krava12", "config1:63\n", },
20 { "krava13", "config1:45-47\n", },
21 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
22 { "krava22", "config2:8,18,48,58\n", },
23 { "krava23", "config2:28-29,38\n", },
26 /* Simulated users input. */
27 static struct parse_events_term test_terms[] = {
29 .config = (char *) "krava01",
30 .val.num = 15,
31 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
32 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
35 .config = (char *) "krava02",
36 .val.num = 170,
37 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
38 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
41 .config = (char *) "krava03",
42 .val.num = 1,
43 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
44 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
47 .config = (char *) "krava11",
48 .val.num = 27,
49 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
50 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
53 .config = (char *) "krava12",
54 .val.num = 1,
55 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
56 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
59 .config = (char *) "krava13",
60 .val.num = 2,
61 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
62 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
65 .config = (char *) "krava21",
66 .val.num = 119,
67 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
68 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
71 .config = (char *) "krava22",
72 .val.num = 11,
73 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
74 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
77 .config = (char *) "krava23",
78 .val.num = 2,
79 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
80 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
85 * Prepare format directory data, exported by kernel
86 * at /sys/bus/event_source/devices/<dev>/format.
88 static char *test_format_dir_get(void)
90 static char dir[PATH_MAX];
91 unsigned int i;
93 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
94 if (!mkdtemp(dir))
95 return NULL;
97 for (i = 0; i < ARRAY_SIZE(test_formats); i++) {
98 static char name[PATH_MAX];
99 struct test_format *format = &test_formats[i];
100 FILE *file;
102 scnprintf(name, PATH_MAX, "%s/%s", dir, format->name);
104 file = fopen(name, "w");
105 if (!file)
106 return NULL;
108 if (1 != fwrite(format->value, strlen(format->value), 1, file))
109 break;
111 fclose(file);
114 return dir;
117 /* Cleanup format directory. */
118 static int test_format_dir_put(char *dir)
120 char buf[PATH_MAX];
121 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
122 if (system(buf))
123 return -1;
125 snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
126 return system(buf);
129 static struct list_head *test_terms_list(void)
131 static LIST_HEAD(terms);
132 unsigned int i;
134 for (i = 0; i < ARRAY_SIZE(test_terms); i++)
135 list_add_tail(&test_terms[i].list, &terms);
137 return &terms;
140 int test__pmu(struct test *test __maybe_unused, int subtest __maybe_unused)
142 char *format = test_format_dir_get();
143 LIST_HEAD(formats);
144 struct list_head *terms = test_terms_list();
145 int ret;
147 if (!format)
148 return -EINVAL;
150 do {
151 struct perf_event_attr attr;
153 memset(&attr, 0, sizeof(attr));
155 ret = perf_pmu__format_parse(format, &formats);
156 if (ret)
157 break;
159 ret = perf_pmu__config_terms("perf-pmu-test", &formats, &attr,
160 terms, false, NULL);
161 if (ret)
162 break;
164 ret = -EINVAL;
166 if (attr.config != 0xc00000000002a823)
167 break;
168 if (attr.config1 != 0x8000400000000145)
169 break;
170 if (attr.config2 != 0x0400000020041d07)
171 break;
173 ret = 0;
174 } while (0);
176 test_format_dir_put(format);
177 return ret;