2 #include <linux/list.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
;
28 format_dir
= opendir(dir
);
32 while (!ret
&& (evt_ent
= readdir(format_dir
))) {
34 char *name
= evt_ent
->d_name
;
37 if (!strcmp(name
, ".") || !strcmp(name
, ".."))
40 snprintf(path
, PATH_MAX
, "%s/%s", dir
, name
);
43 file
= fopen(path
, "r");
48 ret
= perf_pmu_parse(head
, name
);
57 * Reading/parsing the default pmu format definition, which should be
59 * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
61 static int pmu_format(char *name
, struct list_head
*format
)
67 sysfs
= sysfs_find_mountpoint();
71 snprintf(path
, PATH_MAX
,
72 "%s/bus/event_source/devices/%s/format", sysfs
, name
);
74 if (stat(path
, &st
) < 0)
77 if (pmu_format_parse(path
, format
))
84 * Reading/parsing the default pmu type value, which should be
86 * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
88 static int pmu_type(char *name
, __u32
*type
)
96 sysfs
= sysfs_find_mountpoint();
100 snprintf(path
, PATH_MAX
,
101 "%s/bus/event_source/devices/%s/type", sysfs
, name
);
103 if (stat(path
, &st
) < 0)
106 file
= fopen(path
, "r");
110 if (1 != fscanf(file
, "%u", type
))
117 static struct perf_pmu
*pmu_lookup(char *name
)
119 struct perf_pmu
*pmu
;
124 * The pmu data we store & need consists of the pmu
125 * type value and format definitions. Load both right
128 if (pmu_format(name
, &format
))
131 if (pmu_type(name
, &type
))
134 pmu
= zalloc(sizeof(*pmu
));
138 INIT_LIST_HEAD(&pmu
->format
);
139 list_splice(&format
, &pmu
->format
);
140 pmu
->name
= strdup(name
);
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
))
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
);
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
))
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
;
195 for (fbit
= 0, vbit
= 0; fbit
< PERF_PMU_FORMAT_BITS
; fbit
++) {
197 if (!test_bit(fbit
, format
))
200 if (!(value
& (1llu << vbit
++)))
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
;
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
))
228 if (term
->type_val
!= PARSE_EVENTS__TERM_TYPE_NUM
)
231 format
= pmu_find_format(formats
, term
->config
);
235 switch (format
->value
) {
236 case PERF_PMU_FORMAT_VALUE_CONFIG
:
239 case PERF_PMU_FORMAT_VALUE_CONFIG1
:
242 case PERF_PMU_FORMAT_VALUE_CONFIG2
:
250 * XXX If we ever decide to go with string values for
251 * non-hardcoded terms, here's the place to translate
254 *vp
|= pmu_format_value(format
->bits
, term
->val
.num
);
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
))
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
));
291 format
->name
= strdup(name
);
292 format
->value
= config
;
293 memcpy(format
->bits
, bits
, sizeof(format
->bits
));
295 list_add_tail(&format
->list
, list
);
299 void perf_pmu__set_format(unsigned long *bits
, long from
, long to
)
306 memset(bits
, 0, BITS_TO_LONGS(PERF_PMU_FORMAT_BITS
));
307 for (b
= from
; b
<= to
; b
++)
311 /* Simulated format definitions. */
312 static struct test_format
{
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",
334 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
335 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
338 .config
= (char *) "krava02",
340 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
341 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
344 .config
= (char *) "krava03",
346 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
347 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
350 .config
= (char *) "krava11",
352 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
353 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
356 .config
= (char *) "krava12",
358 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
359 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
362 .config
= (char *) "krava13",
364 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
365 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
368 .config
= (char *) "krava21",
370 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
371 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
374 .config
= (char *) "krava22",
376 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
377 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
380 .config
= (char *) "krava23",
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
];
397 snprintf(dir
, PATH_MAX
, "/tmp/perf-pmu-test-format-XXXXXX");
401 for (i
= 0; i
< TEST_FORMATS_CNT
; i
++) {
402 static char name
[PATH_MAX
];
403 struct test_format
*format
= &test_formats
[i
];
406 snprintf(name
, PATH_MAX
, "%s/%s", dir
, format
->name
);
408 file
= fopen(name
, "w");
412 if (1 != fwrite(format
->value
, strlen(format
->value
), 1, file
))
421 /* Cleanup format directory. */
422 static int test_format_dir_put(char *dir
)
425 snprintf(buf
, PATH_MAX
, "rm -f %s/*\n", dir
);
429 snprintf(buf
, PATH_MAX
, "rmdir %s\n", dir
);
433 static struct list_head
*test_terms_list(void)
435 static LIST_HEAD(terms
);
438 for (i
= 0; i
< TERMS_CNT
; i
++)
439 list_add_tail(&test_terms
[i
].list
, &terms
);
446 int perf_pmu__test(void)
448 char *format
= test_format_dir_get();
450 struct list_head
*terms
= test_terms_list();
457 struct perf_event_attr attr
;
459 memset(&attr
, 0, sizeof(attr
));
461 ret
= pmu_format_parse(format
, &formats
);
465 ret
= pmu_config(&formats
, &attr
, terms
);
471 if (attr
.config
!= 0xc00000000002a823)
473 if (attr
.config1
!= 0x8000400000000145)
475 if (attr
.config2
!= 0x0400000020041d07)
481 test_format_dir_put(format
);