• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/list.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <stdarg.h>
7 #include <dirent.h>
8 #include <api/fs/fs.h>
9 #include <locale.h>
10 #include "util.h"
11 #include "pmu.h"
12 #include "parse-events.h"
13 #include "cpumap.h"
14 
15 struct perf_pmu_format {
16 	char *name;
17 	int value;
18 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
19 	struct list_head list;
20 };
21 
22 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
23 
24 int perf_pmu_parse(struct list_head *list, char *name);
25 extern FILE *perf_pmu_in;
26 
27 static LIST_HEAD(pmus);
28 
29 /*
30  * Parse & process all the sysfs attributes located under
31  * the directory specified in 'dir' parameter.
32  */
perf_pmu__format_parse(char * dir,struct list_head * head)33 int perf_pmu__format_parse(char *dir, struct list_head *head)
34 {
35 	struct dirent *evt_ent;
36 	DIR *format_dir;
37 	int ret = 0;
38 
39 	format_dir = opendir(dir);
40 	if (!format_dir)
41 		return -EINVAL;
42 
43 	while (!ret && (evt_ent = readdir(format_dir))) {
44 		char path[PATH_MAX];
45 		char *name = evt_ent->d_name;
46 		FILE *file;
47 
48 		if (!strcmp(name, ".") || !strcmp(name, ".."))
49 			continue;
50 
51 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
52 
53 		ret = -EINVAL;
54 		file = fopen(path, "r");
55 		if (!file)
56 			break;
57 
58 		perf_pmu_in = file;
59 		ret = perf_pmu_parse(head, name);
60 		fclose(file);
61 	}
62 
63 	closedir(format_dir);
64 	return ret;
65 }
66 
67 /*
68  * Reading/parsing the default pmu format definition, which should be
69  * located at:
70  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
71  */
pmu_format(const char * name,struct list_head * format)72 static int pmu_format(const char *name, struct list_head *format)
73 {
74 	struct stat st;
75 	char path[PATH_MAX];
76 	const char *sysfs = sysfs__mountpoint();
77 
78 	if (!sysfs)
79 		return -1;
80 
81 	snprintf(path, PATH_MAX,
82 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
83 
84 	if (stat(path, &st) < 0)
85 		return 0;	/* no error if format does not exist */
86 
87 	if (perf_pmu__format_parse(path, format))
88 		return -1;
89 
90 	return 0;
91 }
92 
perf_pmu__parse_scale(struct perf_pmu_alias * alias,char * dir,char * name)93 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
94 {
95 	struct stat st;
96 	ssize_t sret;
97 	char scale[128];
98 	int fd, ret = -1;
99 	char path[PATH_MAX];
100 	const char *lc;
101 
102 	snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
103 
104 	fd = open(path, O_RDONLY);
105 	if (fd == -1)
106 		return -1;
107 
108 	if (fstat(fd, &st) < 0)
109 		goto error;
110 
111 	sret = read(fd, scale, sizeof(scale)-1);
112 	if (sret < 0)
113 		goto error;
114 
115 	scale[sret] = '\0';
116 	/*
117 	 * save current locale
118 	 */
119 	lc = setlocale(LC_NUMERIC, NULL);
120 
121 	/*
122 	 * force to C locale to ensure kernel
123 	 * scale string is converted correctly.
124 	 * kernel uses default C locale.
125 	 */
126 	setlocale(LC_NUMERIC, "C");
127 
128 	alias->scale = strtod(scale, NULL);
129 
130 	/* restore locale */
131 	setlocale(LC_NUMERIC, lc);
132 
133 	ret = 0;
134 error:
135 	close(fd);
136 	return ret;
137 }
138 
perf_pmu__parse_unit(struct perf_pmu_alias * alias,char * dir,char * name)139 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
140 {
141 	char path[PATH_MAX];
142 	ssize_t sret;
143 	int fd;
144 
145 	snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
146 
147 	fd = open(path, O_RDONLY);
148 	if (fd == -1)
149 		return -1;
150 
151 		sret = read(fd, alias->unit, UNIT_MAX_LEN);
152 	if (sret < 0)
153 		goto error;
154 
155 	close(fd);
156 
157 	alias->unit[sret] = '\0';
158 
159 	return 0;
160 error:
161 	close(fd);
162 	alias->unit[0] = '\0';
163 	return -1;
164 }
165 
perf_pmu__new_alias(struct list_head * list,char * dir,char * name,FILE * file)166 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
167 {
168 	struct perf_pmu_alias *alias;
169 	char buf[256];
170 	int ret;
171 
172 	ret = fread(buf, 1, sizeof(buf), file);
173 	if (ret == 0)
174 		return -EINVAL;
175 	buf[ret] = 0;
176 
177 	alias = malloc(sizeof(*alias));
178 	if (!alias)
179 		return -ENOMEM;
180 
181 	INIT_LIST_HEAD(&alias->terms);
182 	alias->scale = 1.0;
183 	alias->unit[0] = '\0';
184 
185 	ret = parse_events_terms(&alias->terms, buf);
186 	if (ret) {
187 		free(alias);
188 		return ret;
189 	}
190 
191 	alias->name = strdup(name);
192 	/*
193 	 * load unit name and scale if available
194 	 */
195 	perf_pmu__parse_unit(alias, dir, name);
196 	perf_pmu__parse_scale(alias, dir, name);
197 
198 	list_add_tail(&alias->list, list);
199 
200 	return 0;
201 }
202 
pmu_alias_info_file(char * name)203 static inline bool pmu_alias_info_file(char *name)
204 {
205 	size_t len;
206 
207 	len = strlen(name);
208 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
209 		return true;
210 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
211 		return true;
212 
213 	return false;
214 }
215 
216 /*
217  * Process all the sysfs attributes located under the directory
218  * specified in 'dir' parameter.
219  */
pmu_aliases_parse(char * dir,struct list_head * head)220 static int pmu_aliases_parse(char *dir, struct list_head *head)
221 {
222 	struct dirent *evt_ent;
223 	DIR *event_dir;
224 
225 	event_dir = opendir(dir);
226 	if (!event_dir)
227 		return -EINVAL;
228 
229 	while ((evt_ent = readdir(event_dir))) {
230 		char path[PATH_MAX];
231 		char *name = evt_ent->d_name;
232 		FILE *file;
233 
234 		if (!strcmp(name, ".") || !strcmp(name, ".."))
235 			continue;
236 
237 		/*
238 		 * skip info files parsed in perf_pmu__new_alias()
239 		 */
240 		if (pmu_alias_info_file(name))
241 			continue;
242 
243 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
244 
245 		file = fopen(path, "r");
246 		if (!file) {
247 			pr_debug("Cannot open %s\n", path);
248 			continue;
249 		}
250 
251 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
252 			pr_debug("Cannot set up %s\n", name);
253 		fclose(file);
254 	}
255 
256 	closedir(event_dir);
257 	return 0;
258 }
259 
260 /*
261  * Reading the pmu event aliases definition, which should be located at:
262  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
263  */
pmu_aliases(const char * name,struct list_head * head)264 static int pmu_aliases(const char *name, struct list_head *head)
265 {
266 	struct stat st;
267 	char path[PATH_MAX];
268 	const char *sysfs = sysfs__mountpoint();
269 
270 	if (!sysfs)
271 		return -1;
272 
273 	snprintf(path, PATH_MAX,
274 		 "%s/bus/event_source/devices/%s/events", sysfs, name);
275 
276 	if (stat(path, &st) < 0)
277 		return 0;	 /* no error if 'events' does not exist */
278 
279 	if (pmu_aliases_parse(path, head))
280 		return -1;
281 
282 	return 0;
283 }
284 
pmu_alias_terms(struct perf_pmu_alias * alias,struct list_head * terms)285 static int pmu_alias_terms(struct perf_pmu_alias *alias,
286 			   struct list_head *terms)
287 {
288 	struct parse_events_term *term, *cloned;
289 	LIST_HEAD(list);
290 	int ret;
291 
292 	list_for_each_entry(term, &alias->terms, list) {
293 		ret = parse_events_term__clone(&cloned, term);
294 		if (ret) {
295 			parse_events__free_terms(&list);
296 			return ret;
297 		}
298 		list_add_tail(&cloned->list, &list);
299 	}
300 	list_splice(&list, terms);
301 	return 0;
302 }
303 
304 /*
305  * Reading/parsing the default pmu type value, which should be
306  * located at:
307  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
308  */
pmu_type(const char * name,__u32 * type)309 static int pmu_type(const char *name, __u32 *type)
310 {
311 	struct stat st;
312 	char path[PATH_MAX];
313 	FILE *file;
314 	int ret = 0;
315 	const char *sysfs = sysfs__mountpoint();
316 
317 	if (!sysfs)
318 		return -1;
319 
320 	snprintf(path, PATH_MAX,
321 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
322 
323 	if (stat(path, &st) < 0)
324 		return -1;
325 
326 	file = fopen(path, "r");
327 	if (!file)
328 		return -EINVAL;
329 
330 	if (1 != fscanf(file, "%u", type))
331 		ret = -1;
332 
333 	fclose(file);
334 	return ret;
335 }
336 
337 /* Add all pmus in sysfs to pmu list: */
pmu_read_sysfs(void)338 static void pmu_read_sysfs(void)
339 {
340 	char path[PATH_MAX];
341 	DIR *dir;
342 	struct dirent *dent;
343 	const char *sysfs = sysfs__mountpoint();
344 
345 	if (!sysfs)
346 		return;
347 
348 	snprintf(path, PATH_MAX,
349 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
350 
351 	dir = opendir(path);
352 	if (!dir)
353 		return;
354 
355 	while ((dent = readdir(dir))) {
356 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
357 			continue;
358 		/* add to static LIST_HEAD(pmus): */
359 		perf_pmu__find(dent->d_name);
360 	}
361 
362 	closedir(dir);
363 }
364 
pmu_cpumask(const char * name)365 static struct cpu_map *pmu_cpumask(const char *name)
366 {
367 	struct stat st;
368 	char path[PATH_MAX];
369 	FILE *file;
370 	struct cpu_map *cpus;
371 	const char *sysfs = sysfs__mountpoint();
372 
373 	if (!sysfs)
374 		return NULL;
375 
376 	snprintf(path, PATH_MAX,
377 		 "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
378 
379 	if (stat(path, &st) < 0)
380 		return NULL;
381 
382 	file = fopen(path, "r");
383 	if (!file)
384 		return NULL;
385 
386 	cpus = cpu_map__read(file);
387 	fclose(file);
388 	return cpus;
389 }
390 
391 struct perf_event_attr *__attribute__((weak))
perf_pmu__get_default_config(struct perf_pmu * pmu __maybe_unused)392 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
393 {
394 	return NULL;
395 }
396 
pmu_lookup(const char * name)397 static struct perf_pmu *pmu_lookup(const char *name)
398 {
399 	struct perf_pmu *pmu;
400 	LIST_HEAD(format);
401 	LIST_HEAD(aliases);
402 	__u32 type;
403 
404 	/*
405 	 * The pmu data we store & need consists of the pmu
406 	 * type value and format definitions. Load both right
407 	 * now.
408 	 */
409 	if (pmu_format(name, &format))
410 		return NULL;
411 
412 	if (pmu_aliases(name, &aliases))
413 		return NULL;
414 
415 	if (pmu_type(name, &type))
416 		return NULL;
417 
418 	pmu = zalloc(sizeof(*pmu));
419 	if (!pmu)
420 		return NULL;
421 
422 	pmu->cpus = pmu_cpumask(name);
423 
424 	INIT_LIST_HEAD(&pmu->format);
425 	INIT_LIST_HEAD(&pmu->aliases);
426 	list_splice(&format, &pmu->format);
427 	list_splice(&aliases, &pmu->aliases);
428 	pmu->name = strdup(name);
429 	pmu->type = type;
430 	list_add_tail(&pmu->list, &pmus);
431 
432 	pmu->default_config = perf_pmu__get_default_config(pmu);
433 
434 	return pmu;
435 }
436 
pmu_find(const char * name)437 static struct perf_pmu *pmu_find(const char *name)
438 {
439 	struct perf_pmu *pmu;
440 
441 	list_for_each_entry(pmu, &pmus, list)
442 		if (!strcmp(pmu->name, name))
443 			return pmu;
444 
445 	return NULL;
446 }
447 
perf_pmu__scan(struct perf_pmu * pmu)448 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
449 {
450 	/*
451 	 * pmu iterator: If pmu is NULL, we start at the begin,
452 	 * otherwise return the next pmu. Returns NULL on end.
453 	 */
454 	if (!pmu) {
455 		pmu_read_sysfs();
456 		pmu = list_prepare_entry(pmu, &pmus, list);
457 	}
458 	list_for_each_entry_continue(pmu, &pmus, list)
459 		return pmu;
460 	return NULL;
461 }
462 
perf_pmu__find(const char * name)463 struct perf_pmu *perf_pmu__find(const char *name)
464 {
465 	struct perf_pmu *pmu;
466 
467 	/*
468 	 * Once PMU is loaded it stays in the list,
469 	 * so we keep us from multiple reading/parsing
470 	 * the pmu format definitions.
471 	 */
472 	pmu = pmu_find(name);
473 	if (pmu)
474 		return pmu;
475 
476 	return pmu_lookup(name);
477 }
478 
479 static struct perf_pmu_format *
pmu_find_format(struct list_head * formats,char * name)480 pmu_find_format(struct list_head *formats, char *name)
481 {
482 	struct perf_pmu_format *format;
483 
484 	list_for_each_entry(format, formats, list)
485 		if (!strcmp(format->name, name))
486 			return format;
487 
488 	return NULL;
489 }
490 
491 /*
492  * Sets value based on the format definition (format parameter)
493  * and unformated value (value parameter).
494  */
pmu_format_value(unsigned long * format,__u64 value,__u64 * v,bool zero)495 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
496 			     bool zero)
497 {
498 	unsigned long fbit, vbit;
499 
500 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
501 
502 		if (!test_bit(fbit, format))
503 			continue;
504 
505 		if (value & (1llu << vbit++))
506 			*v |= (1llu << fbit);
507 		else if (zero)
508 			*v &= ~(1llu << fbit);
509 	}
510 }
511 
512 /*
513  * Setup one of config[12] attr members based on the
514  * user input data - term parameter.
515  */
pmu_config_term(struct list_head * formats,struct perf_event_attr * attr,struct parse_events_term * term,bool zero)516 static int pmu_config_term(struct list_head *formats,
517 			   struct perf_event_attr *attr,
518 			   struct parse_events_term *term,
519 			   bool zero)
520 {
521 	struct perf_pmu_format *format;
522 	__u64 *vp;
523 
524 	/*
525 	 * Support only for hardcoded and numnerial terms.
526 	 * Hardcoded terms should be already in, so nothing
527 	 * to be done for them.
528 	 */
529 	if (parse_events__is_hardcoded_term(term))
530 		return 0;
531 
532 	if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
533 		return -EINVAL;
534 
535 	format = pmu_find_format(formats, term->config);
536 	if (!format)
537 		return -EINVAL;
538 
539 	switch (format->value) {
540 	case PERF_PMU_FORMAT_VALUE_CONFIG:
541 		vp = &attr->config;
542 		break;
543 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
544 		vp = &attr->config1;
545 		break;
546 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
547 		vp = &attr->config2;
548 		break;
549 	default:
550 		return -EINVAL;
551 	}
552 
553 	/*
554 	 * XXX If we ever decide to go with string values for
555 	 * non-hardcoded terms, here's the place to translate
556 	 * them into value.
557 	 */
558 	pmu_format_value(format->bits, term->val.num, vp, zero);
559 	return 0;
560 }
561 
perf_pmu__config_terms(struct list_head * formats,struct perf_event_attr * attr,struct list_head * head_terms,bool zero)562 int perf_pmu__config_terms(struct list_head *formats,
563 			   struct perf_event_attr *attr,
564 			   struct list_head *head_terms,
565 			   bool zero)
566 {
567 	struct parse_events_term *term;
568 
569 	list_for_each_entry(term, head_terms, list)
570 		if (pmu_config_term(formats, attr, term, zero))
571 			return -EINVAL;
572 
573 	return 0;
574 }
575 
576 /*
577  * Configures event's 'attr' parameter based on the:
578  * 1) users input - specified in terms parameter
579  * 2) pmu format definitions - specified by pmu parameter
580  */
perf_pmu__config(struct perf_pmu * pmu,struct perf_event_attr * attr,struct list_head * head_terms)581 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
582 		     struct list_head *head_terms)
583 {
584 	bool zero = !!pmu->default_config;
585 
586 	attr->type = pmu->type;
587 	return perf_pmu__config_terms(&pmu->format, attr, head_terms, zero);
588 }
589 
pmu_find_alias(struct perf_pmu * pmu,struct parse_events_term * term)590 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
591 					     struct parse_events_term *term)
592 {
593 	struct perf_pmu_alias *alias;
594 	char *name;
595 
596 	if (parse_events__is_hardcoded_term(term))
597 		return NULL;
598 
599 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
600 		if (term->val.num != 1)
601 			return NULL;
602 		if (pmu_find_format(&pmu->format, term->config))
603 			return NULL;
604 		name = term->config;
605 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
606 		if (strcasecmp(term->config, "event"))
607 			return NULL;
608 		name = term->val.str;
609 	} else {
610 		return NULL;
611 	}
612 
613 	list_for_each_entry(alias, &pmu->aliases, list) {
614 		if (!strcasecmp(alias->name, name))
615 			return alias;
616 	}
617 	return NULL;
618 }
619 
620 
check_unit_scale(struct perf_pmu_alias * alias,const char ** unit,double * scale)621 static int check_unit_scale(struct perf_pmu_alias *alias,
622 			    const char **unit, double *scale)
623 {
624 	/*
625 	 * Only one term in event definition can
626 	 * define unit and scale, fail if there's
627 	 * more than one.
628 	 */
629 	if ((*unit && alias->unit) ||
630 	    (*scale && alias->scale))
631 		return -EINVAL;
632 
633 	if (alias->unit)
634 		*unit = alias->unit;
635 
636 	if (alias->scale)
637 		*scale = alias->scale;
638 
639 	return 0;
640 }
641 
642 /*
643  * Find alias in the terms list and replace it with the terms
644  * defined for the alias
645  */
perf_pmu__check_alias(struct perf_pmu * pmu,struct list_head * head_terms,struct perf_pmu_info * info)646 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
647 			  struct perf_pmu_info *info)
648 {
649 	struct parse_events_term *term, *h;
650 	struct perf_pmu_alias *alias;
651 	int ret;
652 
653 	/*
654 	 * Mark unit and scale as not set
655 	 * (different from default values, see below)
656 	 */
657 	info->unit   = NULL;
658 	info->scale  = 0.0;
659 
660 	list_for_each_entry_safe(term, h, head_terms, list) {
661 		alias = pmu_find_alias(pmu, term);
662 		if (!alias)
663 			continue;
664 		ret = pmu_alias_terms(alias, &term->list);
665 		if (ret)
666 			return ret;
667 
668 		ret = check_unit_scale(alias, &info->unit, &info->scale);
669 		if (ret)
670 			return ret;
671 
672 		list_del(&term->list);
673 		free(term);
674 	}
675 
676 	/*
677 	 * if no unit or scale foundin aliases, then
678 	 * set defaults as for evsel
679 	 * unit cannot left to NULL
680 	 */
681 	if (info->unit == NULL)
682 		info->unit   = "";
683 
684 	if (info->scale == 0.0)
685 		info->scale  = 1.0;
686 
687 	return 0;
688 }
689 
perf_pmu__new_format(struct list_head * list,char * name,int config,unsigned long * bits)690 int perf_pmu__new_format(struct list_head *list, char *name,
691 			 int config, unsigned long *bits)
692 {
693 	struct perf_pmu_format *format;
694 
695 	format = zalloc(sizeof(*format));
696 	if (!format)
697 		return -ENOMEM;
698 
699 	format->name = strdup(name);
700 	format->value = config;
701 	memcpy(format->bits, bits, sizeof(format->bits));
702 
703 	list_add_tail(&format->list, list);
704 	return 0;
705 }
706 
perf_pmu__set_format(unsigned long * bits,long from,long to)707 void perf_pmu__set_format(unsigned long *bits, long from, long to)
708 {
709 	long b;
710 
711 	if (!to)
712 		to = from;
713 
714 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
715 	for (b = from; b <= to; b++)
716 		set_bit(b, bits);
717 }
718 
format_alias(char * buf,int len,struct perf_pmu * pmu,struct perf_pmu_alias * alias)719 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
720 			  struct perf_pmu_alias *alias)
721 {
722 	snprintf(buf, len, "%s/%s/", pmu->name, alias->name);
723 	return buf;
724 }
725 
format_alias_or(char * buf,int len,struct perf_pmu * pmu,struct perf_pmu_alias * alias)726 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
727 			     struct perf_pmu_alias *alias)
728 {
729 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
730 	return buf;
731 }
732 
cmp_string(const void * a,const void * b)733 static int cmp_string(const void *a, const void *b)
734 {
735 	const char * const *as = a;
736 	const char * const *bs = b;
737 	return strcmp(*as, *bs);
738 }
739 
print_pmu_events(const char * event_glob,bool name_only)740 void print_pmu_events(const char *event_glob, bool name_only)
741 {
742 	struct perf_pmu *pmu;
743 	struct perf_pmu_alias *alias;
744 	char buf[1024];
745 	int printed = 0;
746 	int len, j;
747 	char **aliases;
748 
749 	pmu = NULL;
750 	len = 0;
751 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
752 		list_for_each_entry(alias, &pmu->aliases, list)
753 			len++;
754 	aliases = malloc(sizeof(char *) * len);
755 	if (!aliases)
756 		return;
757 	pmu = NULL;
758 	j = 0;
759 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
760 		list_for_each_entry(alias, &pmu->aliases, list) {
761 			char *name = format_alias(buf, sizeof(buf), pmu, alias);
762 			bool is_cpu = !strcmp(pmu->name, "cpu");
763 
764 			if (event_glob != NULL &&
765 			    !(strglobmatch(name, event_glob) ||
766 			      (!is_cpu && strglobmatch(alias->name,
767 						       event_glob))))
768 				continue;
769 			aliases[j] = name;
770 			if (is_cpu && !name_only)
771 				aliases[j] = format_alias_or(buf, sizeof(buf),
772 							      pmu, alias);
773 			aliases[j] = strdup(aliases[j]);
774 			j++;
775 		}
776 	len = j;
777 	qsort(aliases, len, sizeof(char *), cmp_string);
778 	for (j = 0; j < len; j++) {
779 		if (name_only) {
780 			printf("%s ", aliases[j]);
781 			continue;
782 		}
783 		printf("  %-50s [Kernel PMU event]\n", aliases[j]);
784 		zfree(&aliases[j]);
785 		printed++;
786 	}
787 	if (printed)
788 		printf("\n");
789 	free(aliases);
790 }
791 
pmu_have_event(const char * pname,const char * name)792 bool pmu_have_event(const char *pname, const char *name)
793 {
794 	struct perf_pmu *pmu;
795 	struct perf_pmu_alias *alias;
796 
797 	pmu = NULL;
798 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
799 		if (strcmp(pname, pmu->name))
800 			continue;
801 		list_for_each_entry(alias, &pmu->aliases, list)
802 			if (!strcmp(alias->name, name))
803 				return true;
804 	}
805 	return false;
806 }
807 
perf_pmu__open_file(struct perf_pmu * pmu,const char * name)808 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
809 {
810 	struct stat st;
811 	char path[PATH_MAX];
812 	const char *sysfs;
813 
814 	sysfs = sysfs__mountpoint();
815 	if (!sysfs)
816 		return NULL;
817 
818 	snprintf(path, PATH_MAX,
819 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
820 
821 	if (stat(path, &st) < 0)
822 		return NULL;
823 
824 	return fopen(path, "r");
825 }
826 
perf_pmu__scan_file(struct perf_pmu * pmu,const char * name,const char * fmt,...)827 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
828 			...)
829 {
830 	va_list args;
831 	FILE *file;
832 	int ret = EOF;
833 
834 	va_start(args, fmt);
835 	file = perf_pmu__open_file(pmu, name);
836 	if (file) {
837 		ret = vfscanf(file, fmt, args);
838 		fclose(file);
839 	}
840 	va_end(args);
841 	return ret;
842 }
843