1 // SPDX-License-Identifier: GPL-2.0
2 #include "parse-events.h"
7 #include <linux/kernel.h>
8 #include <linux/limits.h>
10 /* Simulated format definitions. */
11 static struct test_format {
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",
31 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
32 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
35 .config = (char *) "krava02",
37 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
38 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
41 .config = (char *) "krava03",
43 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
44 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
47 .config = (char *) "krava11",
49 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
50 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
53 .config = (char *) "krava12",
55 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
56 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
59 .config = (char *) "krava13",
61 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
62 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
65 .config = (char *) "krava21",
67 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
68 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
71 .config = (char *) "krava22",
73 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
74 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
77 .config = (char *) "krava23",
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];
93 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
97 for (i = 0; i < ARRAY_SIZE(test_formats); i++) {
98 static char name[PATH_MAX];
99 struct test_format *format = &test_formats[i];
102 scnprintf(name, PATH_MAX, "%s/%s", dir, format->name);
104 file = fopen(name, "w");
108 if (1 != fwrite(format->value, strlen(format->value), 1, file))
117 /* Cleanup format directory. */
118 static int test_format_dir_put(char *dir)
121 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
125 snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
129 static struct list_head *test_terms_list(void)
131 static LIST_HEAD(terms);
134 for (i = 0; i < ARRAY_SIZE(test_terms); i++)
135 list_add_tail(&test_terms[i].list, &terms);
140 int test__pmu(struct test *test __maybe_unused, int subtest __maybe_unused)
142 char *format = test_format_dir_get();
144 struct list_head *terms = test_terms_list();
151 struct perf_event_attr attr;
153 memset(&attr, 0, sizeof(attr));
155 ret = perf_pmu__format_parse(format, &formats);
159 ret = perf_pmu__config_terms("perf-pmu-test", &formats, &attr,
166 if (attr.config != 0xc00000000002a823)
168 if (attr.config1 != 0x8000400000000145)
170 if (attr.config2 != 0x0400000020041d07)
176 perf_pmu__del_formats(&formats);
177 test_format_dir_put(format);