Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / tools / perf / tests / pmu.c
blob1e2ba26029301ffd85e1a1803eea8b8f40f134e6
1 #include "parse-events.h"
2 #include "pmu.h"
3 #include "util.h"
4 #include "tests.h"
6 /* Simulated format definitions. */
7 static 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", },
22 /* Simulated users input. */
23 static struct parse_events_term test_terms[] = {
25 .config = (char *) "krava01",
26 .val.num = 15,
27 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
28 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
31 .config = (char *) "krava02",
32 .val.num = 170,
33 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
34 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
37 .config = (char *) "krava03",
38 .val.num = 1,
39 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
40 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
43 .config = (char *) "krava11",
44 .val.num = 27,
45 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
46 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
49 .config = (char *) "krava12",
50 .val.num = 1,
51 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
52 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
55 .config = (char *) "krava13",
56 .val.num = 2,
57 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
58 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
61 .config = (char *) "krava21",
62 .val.num = 119,
63 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
64 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
67 .config = (char *) "krava22",
68 .val.num = 11,
69 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
70 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
73 .config = (char *) "krava23",
74 .val.num = 2,
75 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
76 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
81 * Prepare format directory data, exported by kernel
82 * at /sys/bus/event_source/devices/<dev>/format.
84 static char *test_format_dir_get(void)
86 static char dir[PATH_MAX];
87 unsigned int i;
89 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
90 if (!mkdtemp(dir))
91 return NULL;
93 for (i = 0; i < ARRAY_SIZE(test_formats); i++) {
94 static char name[PATH_MAX];
95 struct test_format *format = &test_formats[i];
96 FILE *file;
98 snprintf(name, PATH_MAX, "%s/%s", dir, format->name);
100 file = fopen(name, "w");
101 if (!file)
102 return NULL;
104 if (1 != fwrite(format->value, strlen(format->value), 1, file))
105 break;
107 fclose(file);
110 return dir;
113 /* Cleanup format directory. */
114 static int test_format_dir_put(char *dir)
116 char buf[PATH_MAX];
117 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
118 if (system(buf))
119 return -1;
121 snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
122 return system(buf);
125 static struct list_head *test_terms_list(void)
127 static LIST_HEAD(terms);
128 unsigned int i;
130 for (i = 0; i < ARRAY_SIZE(test_terms); i++)
131 list_add_tail(&test_terms[i].list, &terms);
133 return &terms;
136 int test__pmu(int subtest __maybe_unused)
138 char *format = test_format_dir_get();
139 LIST_HEAD(formats);
140 struct list_head *terms = test_terms_list();
141 int ret;
143 if (!format)
144 return -EINVAL;
146 do {
147 struct perf_event_attr attr;
149 memset(&attr, 0, sizeof(attr));
151 ret = perf_pmu__format_parse(format, &formats);
152 if (ret)
153 break;
155 ret = perf_pmu__config_terms(&formats, &attr, terms,
156 false, NULL);
157 if (ret)
158 break;
160 ret = -EINVAL;
162 if (attr.config != 0xc00000000002a823)
163 break;
164 if (attr.config1 != 0x8000400000000145)
165 break;
166 if (attr.config2 != 0x0400000020041d07)
167 break;
169 ret = 0;
170 } while (0);
172 test_format_dir_put(format);
173 return ret;