ieee802154: verify packet size before trying to allocate it
[linux/fpc-iii.git] / tools / perf / util / pmu.c
bloba119a5371699bd1c89575ce02de0d47655548e52
2 #include <linux/list.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <dirent.h>
8 #include "sysfs.h"
9 #include "util.h"
10 #include "pmu.h"
11 #include "parse-events.h"
13 int perf_pmu_parse(struct list_head *list, char *name);
14 extern FILE *perf_pmu_in;
16 static LIST_HEAD(pmus);
19 * Parse & process all the sysfs attributes located under
20 * the directory specified in 'dir' parameter.
22 static int pmu_format_parse(char *dir, struct list_head *head)
24 struct dirent *evt_ent;
25 DIR *format_dir;
26 int ret = 0;
28 format_dir = opendir(dir);
29 if (!format_dir)
30 return -EINVAL;
32 while (!ret && (evt_ent = readdir(format_dir))) {
33 char path[PATH_MAX];
34 char *name = evt_ent->d_name;
35 FILE *file;
37 if (!strcmp(name, ".") || !strcmp(name, ".."))
38 continue;
40 snprintf(path, PATH_MAX, "%s/%s", dir, name);
42 ret = -EINVAL;
43 file = fopen(path, "r");
44 if (!file)
45 break;
47 perf_pmu_in = file;
48 ret = perf_pmu_parse(head, name);
49 fclose(file);
52 closedir(format_dir);
53 return ret;
57 * Reading/parsing the default pmu format definition, which should be
58 * located at:
59 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
61 static int pmu_format(char *name, struct list_head *format)
63 struct stat st;
64 char path[PATH_MAX];
65 const char *sysfs;
67 sysfs = sysfs_find_mountpoint();
68 if (!sysfs)
69 return -1;
71 snprintf(path, PATH_MAX,
72 "%s/bus/event_source/devices/%s/format", sysfs, name);
74 if (stat(path, &st) < 0)
75 return -1;
77 if (pmu_format_parse(path, format))
78 return -1;
80 return 0;
84 * Reading/parsing the default pmu type value, which should be
85 * located at:
86 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
88 static int pmu_type(char *name, __u32 *type)
90 struct stat st;
91 char path[PATH_MAX];
92 const char *sysfs;
93 FILE *file;
94 int ret = 0;
96 sysfs = sysfs_find_mountpoint();
97 if (!sysfs)
98 return -1;
100 snprintf(path, PATH_MAX,
101 "%s/bus/event_source/devices/%s/type", sysfs, name);
103 if (stat(path, &st) < 0)
104 return -1;
106 file = fopen(path, "r");
107 if (!file)
108 return -EINVAL;
110 if (1 != fscanf(file, "%u", type))
111 ret = -1;
113 fclose(file);
114 return ret;
117 static struct perf_pmu *pmu_lookup(char *name)
119 struct perf_pmu *pmu;
120 LIST_HEAD(format);
121 __u32 type;
124 * The pmu data we store & need consists of the pmu
125 * type value and format definitions. Load both right
126 * now.
128 if (pmu_format(name, &format))
129 return NULL;
131 if (pmu_type(name, &type))
132 return NULL;
134 pmu = zalloc(sizeof(*pmu));
135 if (!pmu)
136 return NULL;
138 INIT_LIST_HEAD(&pmu->format);
139 list_splice(&format, &pmu->format);
140 pmu->name = strdup(name);
141 pmu->type = type;
142 return pmu;
145 static struct perf_pmu *pmu_find(char *name)
147 struct perf_pmu *pmu;
149 list_for_each_entry(pmu, &pmus, list)
150 if (!strcmp(pmu->name, name))
151 return pmu;
153 return NULL;
156 struct perf_pmu *perf_pmu__find(char *name)
158 struct perf_pmu *pmu;
161 * Once PMU is loaded it stays in the list,
162 * so we keep us from multiple reading/parsing
163 * the pmu format definitions.
165 pmu = pmu_find(name);
166 if (pmu)
167 return pmu;
169 return pmu_lookup(name);
172 static struct perf_pmu__format*
173 pmu_find_format(struct list_head *formats, char *name)
175 struct perf_pmu__format *format;
177 list_for_each_entry(format, formats, list)
178 if (!strcmp(format->name, name))
179 return format;
181 return NULL;
185 * Returns value based on the format definition (format parameter)
186 * and unformated value (value parameter).
188 * TODO maybe optimize a little ;)
190 static __u64 pmu_format_value(unsigned long *format, __u64 value)
192 unsigned long fbit, vbit;
193 __u64 v = 0;
195 for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
197 if (!test_bit(fbit, format))
198 continue;
200 if (!(value & (1llu << vbit++)))
201 continue;
203 v |= (1llu << fbit);
206 return v;
210 * Setup one of config[12] attr members based on the
211 * user input data - temr parameter.
213 static int pmu_config_term(struct list_head *formats,
214 struct perf_event_attr *attr,
215 struct parse_events__term *term)
217 struct perf_pmu__format *format;
218 __u64 *vp;
221 * Support only for hardcoded and numnerial terms.
222 * Hardcoded terms should be already in, so nothing
223 * to be done for them.
225 if (parse_events__is_hardcoded_term(term))
226 return 0;
228 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
229 return -EINVAL;
231 format = pmu_find_format(formats, term->config);
232 if (!format)
233 return -EINVAL;
235 switch (format->value) {
236 case PERF_PMU_FORMAT_VALUE_CONFIG:
237 vp = &attr->config;
238 break;
239 case PERF_PMU_FORMAT_VALUE_CONFIG1:
240 vp = &attr->config1;
241 break;
242 case PERF_PMU_FORMAT_VALUE_CONFIG2:
243 vp = &attr->config2;
244 break;
245 default:
246 return -EINVAL;
250 * XXX If we ever decide to go with string values for
251 * non-hardcoded terms, here's the place to translate
252 * them into value.
254 *vp |= pmu_format_value(format->bits, term->val.num);
255 return 0;
258 static int pmu_config(struct list_head *formats, struct perf_event_attr *attr,
259 struct list_head *head_terms)
261 struct parse_events__term *term;
263 list_for_each_entry(term, head_terms, list)
264 if (pmu_config_term(formats, attr, term))
265 return -EINVAL;
267 return 0;
271 * Configures event's 'attr' parameter based on the:
272 * 1) users input - specified in terms parameter
273 * 2) pmu format definitions - specified by pmu parameter
275 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
276 struct list_head *head_terms)
278 attr->type = pmu->type;
279 return pmu_config(&pmu->format, attr, head_terms);
282 int perf_pmu__new_format(struct list_head *list, char *name,
283 int config, unsigned long *bits)
285 struct perf_pmu__format *format;
287 format = zalloc(sizeof(*format));
288 if (!format)
289 return -ENOMEM;
291 format->name = strdup(name);
292 format->value = config;
293 memcpy(format->bits, bits, sizeof(format->bits));
295 list_add_tail(&format->list, list);
296 return 0;
299 void perf_pmu__set_format(unsigned long *bits, long from, long to)
301 long b;
303 if (!to)
304 to = from;
306 memset(bits, 0, BITS_TO_LONGS(PERF_PMU_FORMAT_BITS));
307 for (b = from; b <= to; b++)
308 set_bit(b, bits);
311 /* Simulated format definitions. */
312 static struct test_format {
313 const char *name;
314 const char *value;
315 } test_formats[] = {
316 { "krava01", "config:0-1,62-63\n", },
317 { "krava02", "config:10-17\n", },
318 { "krava03", "config:5\n", },
319 { "krava11", "config1:0,2,4,6,8,20-28\n", },
320 { "krava12", "config1:63\n", },
321 { "krava13", "config1:45-47\n", },
322 { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
323 { "krava22", "config2:8,18,48,58\n", },
324 { "krava23", "config2:28-29,38\n", },
327 #define TEST_FORMATS_CNT (sizeof(test_formats) / sizeof(struct test_format))
329 /* Simulated users input. */
330 static struct parse_events__term test_terms[] = {
332 .config = (char *) "krava01",
333 .val.num = 15,
334 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
335 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
338 .config = (char *) "krava02",
339 .val.num = 170,
340 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
341 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
344 .config = (char *) "krava03",
345 .val.num = 1,
346 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
347 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
350 .config = (char *) "krava11",
351 .val.num = 27,
352 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
353 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
356 .config = (char *) "krava12",
357 .val.num = 1,
358 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
359 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
362 .config = (char *) "krava13",
363 .val.num = 2,
364 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
365 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
368 .config = (char *) "krava21",
369 .val.num = 119,
370 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
371 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
374 .config = (char *) "krava22",
375 .val.num = 11,
376 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
377 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
380 .config = (char *) "krava23",
381 .val.num = 2,
382 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
383 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
386 #define TERMS_CNT (sizeof(test_terms) / sizeof(struct parse_events__term))
389 * Prepare format directory data, exported by kernel
390 * at /sys/bus/event_source/devices/<dev>/format.
392 static char *test_format_dir_get(void)
394 static char dir[PATH_MAX];
395 unsigned int i;
397 snprintf(dir, PATH_MAX, "/tmp/perf-pmu-test-format-XXXXXX");
398 if (!mkdtemp(dir))
399 return NULL;
401 for (i = 0; i < TEST_FORMATS_CNT; i++) {
402 static char name[PATH_MAX];
403 struct test_format *format = &test_formats[i];
404 FILE *file;
406 snprintf(name, PATH_MAX, "%s/%s", dir, format->name);
408 file = fopen(name, "w");
409 if (!file)
410 return NULL;
412 if (1 != fwrite(format->value, strlen(format->value), 1, file))
413 break;
415 fclose(file);
418 return dir;
421 /* Cleanup format directory. */
422 static int test_format_dir_put(char *dir)
424 char buf[PATH_MAX];
425 snprintf(buf, PATH_MAX, "rm -f %s/*\n", dir);
426 if (system(buf))
427 return -1;
429 snprintf(buf, PATH_MAX, "rmdir %s\n", dir);
430 return system(buf);
433 static struct list_head *test_terms_list(void)
435 static LIST_HEAD(terms);
436 unsigned int i;
438 for (i = 0; i < TERMS_CNT; i++)
439 list_add_tail(&test_terms[i].list, &terms);
441 return &terms;
444 #undef TERMS_CNT
446 int perf_pmu__test(void)
448 char *format = test_format_dir_get();
449 LIST_HEAD(formats);
450 struct list_head *terms = test_terms_list();
451 int ret;
453 if (!format)
454 return -EINVAL;
456 do {
457 struct perf_event_attr attr;
459 memset(&attr, 0, sizeof(attr));
461 ret = pmu_format_parse(format, &formats);
462 if (ret)
463 break;
465 ret = pmu_config(&formats, &attr, terms);
466 if (ret)
467 break;
469 ret = -EINVAL;
471 if (attr.config != 0xc00000000002a823)
472 break;
473 if (attr.config1 != 0x8000400000000145)
474 break;
475 if (attr.config2 != 0x0400000020041d07)
476 break;
478 ret = 0;
479 } while (0);
481 test_format_dir_put(format);
482 return ret;