aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/tests/pmu.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/tests/pmu.c')
-rw-r--r--tools/perf/tests/pmu.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c
new file mode 100644
index 000000000000..a5f379863b8f
--- /dev/null
+++ b/tools/perf/tests/pmu.c
@@ -0,0 +1,178 @@
1#include "parse-events.h"
2#include "pmu.h"
3#include "util.h"
4#include "tests.h"
5
6/* Simulated format definitions. */
7static struct test_format {
8 const char *name;
9 const char *value;
10} test_formats[] = {
11 { "krava01", "config:0-1,62-63\n", },
12 { "krava02", "config:10-17\n", },
13 { "krava03", "config:5\n", },
14 { "krava11", "config1:0,2,4,6,8,20-28\n", },
15 { "krava12", "config1:63\n", },
16 { "krava13", "config1:45-47\n", },
17 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
18 { "krava22", "config2:8,18,48,58\n", },
19 { "krava23", "config2:28-29,38\n", },
20};
21
22#define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format))
23
24/* Simulated users input. */
25static 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#define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term))
82
83/*
84 * Prepare format directory data, exported by kernel
85 * at /sys/bus/event_source/devices/<dev>/format.
86 */
87static char *test_format_dir_get(void)
88{
89 static char dir[PATH_MAX];
90 unsigned int i;
91
92 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
93 if (!mkdtemp(dir))
94 return NULL;
95
96 for (i = 0; i < TEST_FORMATS_CNT; i++) {
97 static char name[PATH_MAX];
98 struct test_format *format = &test_formats[i];
99 FILE *file;
100
101 snprintf(name, PATH_MAX, "%s/%s", dir, format->name);
102
103 file = fopen(name, "w");
104 if (!file)
105 return NULL;
106
107 if (1 != fwrite(format->value, strlen(format->value), 1, file))
108 break;
109
110 fclose(file);
111 }
112
113 return dir;
114}
115
116/* Cleanup format directory. */
117static int test_format_dir_put(char *dir)
118{
119 char buf[PATH_MAX];
120 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
121 if (system(buf))
122 return -1;
123
124 snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
125 return system(buf);
126}
127
128static struct list_head *test_terms_list(void)
129{
130 static LIST_HEAD(terms);
131 unsigned int i;
132
133 for (i = 0; i < TERMS_CNT; i++)
134 list_add_tail(&test_terms[i].list, &terms);
135
136 return &terms;
137}
138
139#undef TERMS_CNT
140
141int test__pmu(void)
142{
143 char *format = test_format_dir_get();
144 LIST_HEAD(formats);
145 struct list_head *terms = test_terms_list();
146 int ret;
147
148 if (!format)
149 return -EINVAL;
150
151 do {
152 struct perf_event_attr attr;
153
154 memset(&attr, 0, sizeof(attr));
155
156 ret = perf_pmu__format_parse(format, &formats);
157 if (ret)
158 break;
159
160 ret = perf_pmu__config_terms(&formats, &attr, terms);
161 if (ret)
162 break;
163
164 ret = -EINVAL;
165
166 if (attr.config != 0xc00000000002a823)
167 break;
168 if (attr.config1 != 0x8000400000000145)
169 break;
170 if (attr.config2 != 0x0400000020041d07)
171 break;
172
173 ret = 0;
174 } while (0);
175
176 test_format_dir_put(format);
177 return ret;
178}