| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 | 
 | 2 | #include <linux/list.h> | 
 | 3 | #include <linux/compiler.h> | 
 | 4 | #include <sys/types.h> | 
 | 5 | #include <errno.h> | 
 | 6 | #include <fcntl.h> | 
 | 7 | #include <sys/stat.h> | 
 | 8 | #include <unistd.h> | 
 | 9 | #include <stdio.h> | 
 | 10 | #include <stdbool.h> | 
 | 11 | #include <stdarg.h> | 
 | 12 | #include <dirent.h> | 
 | 13 | #include <api/fs/fs.h> | 
 | 14 | #include <locale.h> | 
 | 15 | #include <regex.h> | 
 | 16 | #include "util.h" | 
 | 17 | #include "pmu.h" | 
 | 18 | #include "parse-events.h" | 
 | 19 | #include "cpumap.h" | 
 | 20 | #include "header.h" | 
 | 21 | #include "pmu-events/pmu-events.h" | 
 | 22 | #include "cache.h" | 
 | 23 | #include "string2.h" | 
 | 24 |  | 
 | 25 | struct perf_pmu_format { | 
 | 26 | 	char *name; | 
 | 27 | 	int value; | 
 | 28 | 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); | 
 | 29 | 	struct list_head list; | 
 | 30 | }; | 
 | 31 |  | 
 | 32 | #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/" | 
 | 33 |  | 
 | 34 | int perf_pmu_parse(struct list_head *list, char *name); | 
 | 35 | extern FILE *perf_pmu_in; | 
 | 36 |  | 
 | 37 | static LIST_HEAD(pmus); | 
 | 38 |  | 
 | 39 | /* | 
 | 40 |  * Parse & process all the sysfs attributes located under | 
 | 41 |  * the directory specified in 'dir' parameter. | 
 | 42 |  */ | 
 | 43 | int perf_pmu__format_parse(char *dir, struct list_head *head) | 
 | 44 | { | 
 | 45 | 	struct dirent *evt_ent; | 
 | 46 | 	DIR *format_dir; | 
 | 47 | 	int ret = 0; | 
 | 48 |  | 
 | 49 | 	format_dir = opendir(dir); | 
 | 50 | 	if (!format_dir) | 
 | 51 | 		return -EINVAL; | 
 | 52 |  | 
 | 53 | 	while (!ret && (evt_ent = readdir(format_dir))) { | 
 | 54 | 		char path[PATH_MAX]; | 
 | 55 | 		char *name = evt_ent->d_name; | 
 | 56 | 		FILE *file; | 
 | 57 |  | 
 | 58 | 		if (!strcmp(name, ".") || !strcmp(name, "..")) | 
 | 59 | 			continue; | 
 | 60 |  | 
 | 61 | 		snprintf(path, PATH_MAX, "%s/%s", dir, name); | 
 | 62 |  | 
 | 63 | 		ret = -EINVAL; | 
 | 64 | 		file = fopen(path, "r"); | 
 | 65 | 		if (!file) | 
 | 66 | 			break; | 
 | 67 |  | 
 | 68 | 		perf_pmu_in = file; | 
 | 69 | 		ret = perf_pmu_parse(head, name); | 
 | 70 | 		fclose(file); | 
 | 71 | 	} | 
 | 72 |  | 
 | 73 | 	closedir(format_dir); | 
 | 74 | 	return ret; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | /* | 
 | 78 |  * Reading/parsing the default pmu format definition, which should be | 
 | 79 |  * located at: | 
 | 80 |  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. | 
 | 81 |  */ | 
 | 82 | static int pmu_format(const char *name, struct list_head *format) | 
 | 83 | { | 
 | 84 | 	struct stat st; | 
 | 85 | 	char path[PATH_MAX]; | 
 | 86 | 	const char *sysfs = sysfs__mountpoint(); | 
 | 87 |  | 
 | 88 | 	if (!sysfs) | 
 | 89 | 		return -1; | 
 | 90 |  | 
 | 91 | 	snprintf(path, PATH_MAX, | 
 | 92 | 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); | 
 | 93 |  | 
 | 94 | 	if (stat(path, &st) < 0) | 
 | 95 | 		return 0;	/* no error if format does not exist */ | 
 | 96 |  | 
 | 97 | 	if (perf_pmu__format_parse(path, format)) | 
 | 98 | 		return -1; | 
 | 99 |  | 
 | 100 | 	return 0; | 
 | 101 | } | 
 | 102 |  | 
 | 103 | static int convert_scale(const char *scale, char **end, double *sval) | 
 | 104 | { | 
 | 105 | 	char *lc; | 
 | 106 | 	int ret = 0; | 
 | 107 |  | 
 | 108 | 	/* | 
 | 109 | 	 * save current locale | 
 | 110 | 	 */ | 
 | 111 | 	lc = setlocale(LC_NUMERIC, NULL); | 
 | 112 |  | 
 | 113 | 	/* | 
 | 114 | 	 * The lc string may be allocated in static storage, | 
 | 115 | 	 * so get a dynamic copy to make it survive setlocale | 
 | 116 | 	 * call below. | 
 | 117 | 	 */ | 
 | 118 | 	lc = strdup(lc); | 
 | 119 | 	if (!lc) { | 
 | 120 | 		ret = -ENOMEM; | 
 | 121 | 		goto out; | 
 | 122 | 	} | 
 | 123 |  | 
 | 124 | 	/* | 
 | 125 | 	 * force to C locale to ensure kernel | 
 | 126 | 	 * scale string is converted correctly. | 
 | 127 | 	 * kernel uses default C locale. | 
 | 128 | 	 */ | 
 | 129 | 	setlocale(LC_NUMERIC, "C"); | 
 | 130 |  | 
 | 131 | 	*sval = strtod(scale, end); | 
 | 132 |  | 
 | 133 | out: | 
 | 134 | 	/* restore locale */ | 
 | 135 | 	setlocale(LC_NUMERIC, lc); | 
 | 136 | 	free(lc); | 
 | 137 | 	return ret; | 
 | 138 | } | 
 | 139 |  | 
 | 140 | static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) | 
 | 141 | { | 
 | 142 | 	struct stat st; | 
 | 143 | 	ssize_t sret; | 
 | 144 | 	char scale[128]; | 
 | 145 | 	int fd, ret = -1; | 
 | 146 | 	char path[PATH_MAX]; | 
 | 147 |  | 
 | 148 | 	scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name); | 
 | 149 |  | 
 | 150 | 	fd = open(path, O_RDONLY); | 
 | 151 | 	if (fd == -1) | 
 | 152 | 		return -1; | 
 | 153 |  | 
 | 154 | 	if (fstat(fd, &st) < 0) | 
 | 155 | 		goto error; | 
 | 156 |  | 
 | 157 | 	sret = read(fd, scale, sizeof(scale)-1); | 
 | 158 | 	if (sret < 0) | 
 | 159 | 		goto error; | 
 | 160 |  | 
 | 161 | 	if (scale[sret - 1] == '\n') | 
 | 162 | 		scale[sret - 1] = '\0'; | 
 | 163 | 	else | 
 | 164 | 		scale[sret] = '\0'; | 
 | 165 |  | 
 | 166 | 	ret = convert_scale(scale, NULL, &alias->scale); | 
 | 167 | error: | 
 | 168 | 	close(fd); | 
 | 169 | 	return ret; | 
 | 170 | } | 
 | 171 |  | 
 | 172 | static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) | 
 | 173 | { | 
 | 174 | 	char path[PATH_MAX]; | 
 | 175 | 	ssize_t sret; | 
 | 176 | 	int fd; | 
 | 177 |  | 
 | 178 | 	scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name); | 
 | 179 |  | 
 | 180 | 	fd = open(path, O_RDONLY); | 
 | 181 | 	if (fd == -1) | 
 | 182 | 		return -1; | 
 | 183 |  | 
 | 184 | 	sret = read(fd, alias->unit, UNIT_MAX_LEN); | 
 | 185 | 	if (sret < 0) | 
 | 186 | 		goto error; | 
 | 187 |  | 
 | 188 | 	close(fd); | 
 | 189 |  | 
 | 190 | 	if (alias->unit[sret - 1] == '\n') | 
 | 191 | 		alias->unit[sret - 1] = '\0'; | 
 | 192 | 	else | 
 | 193 | 		alias->unit[sret] = '\0'; | 
 | 194 |  | 
 | 195 | 	return 0; | 
 | 196 | error: | 
 | 197 | 	close(fd); | 
 | 198 | 	alias->unit[0] = '\0'; | 
 | 199 | 	return -1; | 
 | 200 | } | 
 | 201 |  | 
 | 202 | static int | 
 | 203 | perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) | 
 | 204 | { | 
 | 205 | 	char path[PATH_MAX]; | 
 | 206 | 	int fd; | 
 | 207 |  | 
 | 208 | 	scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); | 
 | 209 |  | 
 | 210 | 	fd = open(path, O_RDONLY); | 
 | 211 | 	if (fd == -1) | 
 | 212 | 		return -1; | 
 | 213 |  | 
 | 214 | 	close(fd); | 
 | 215 |  | 
 | 216 | 	alias->per_pkg = true; | 
 | 217 | 	return 0; | 
 | 218 | } | 
 | 219 |  | 
 | 220 | static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, | 
 | 221 | 				    char *dir, char *name) | 
 | 222 | { | 
 | 223 | 	char path[PATH_MAX]; | 
 | 224 | 	int fd; | 
 | 225 |  | 
 | 226 | 	scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); | 
 | 227 |  | 
 | 228 | 	fd = open(path, O_RDONLY); | 
 | 229 | 	if (fd == -1) | 
 | 230 | 		return -1; | 
 | 231 |  | 
 | 232 | 	alias->snapshot = true; | 
 | 233 | 	close(fd); | 
 | 234 | 	return 0; | 
 | 235 | } | 
 | 236 |  | 
 | 237 | static void perf_pmu_assign_str(char *name, const char *field, char **old_str, | 
 | 238 | 				char **new_str) | 
 | 239 | { | 
 | 240 | 	if (!*old_str) | 
 | 241 | 		goto set_new; | 
 | 242 |  | 
 | 243 | 	if (*new_str) {	/* Have new string, check with old */ | 
 | 244 | 		if (strcasecmp(*old_str, *new_str)) | 
 | 245 | 			pr_debug("alias %s differs in field '%s'\n", | 
 | 246 | 				 name, field); | 
 | 247 | 		zfree(old_str); | 
 | 248 | 	} else		/* Nothing new --> keep old string */ | 
 | 249 | 		return; | 
 | 250 | set_new: | 
 | 251 | 	*old_str = *new_str; | 
 | 252 | 	*new_str = NULL; | 
 | 253 | } | 
 | 254 |  | 
 | 255 | static void perf_pmu_update_alias(struct perf_pmu_alias *old, | 
 | 256 | 				  struct perf_pmu_alias *newalias) | 
 | 257 | { | 
 | 258 | 	perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); | 
 | 259 | 	perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, | 
 | 260 | 			    &newalias->long_desc); | 
 | 261 | 	perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); | 
 | 262 | 	perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr, | 
 | 263 | 			    &newalias->metric_expr); | 
 | 264 | 	perf_pmu_assign_str(old->name, "metric_name", &old->metric_name, | 
 | 265 | 			    &newalias->metric_name); | 
 | 266 | 	perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); | 
 | 267 | 	old->scale = newalias->scale; | 
 | 268 | 	old->per_pkg = newalias->per_pkg; | 
 | 269 | 	old->snapshot = newalias->snapshot; | 
 | 270 | 	memcpy(old->unit, newalias->unit, sizeof(old->unit)); | 
 | 271 | } | 
 | 272 |  | 
 | 273 | /* Delete an alias entry. */ | 
 | 274 | static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) | 
 | 275 | { | 
 | 276 | 	zfree(&newalias->name); | 
 | 277 | 	zfree(&newalias->desc); | 
 | 278 | 	zfree(&newalias->long_desc); | 
 | 279 | 	zfree(&newalias->topic); | 
 | 280 | 	zfree(&newalias->str); | 
 | 281 | 	zfree(&newalias->metric_expr); | 
 | 282 | 	zfree(&newalias->metric_name); | 
 | 283 | 	parse_events_terms__purge(&newalias->terms); | 
 | 284 | 	free(newalias); | 
 | 285 | } | 
 | 286 |  | 
 | 287 | /* Merge an alias, search in alias list. If this name is already | 
 | 288 |  * present merge both of them to combine all information. | 
 | 289 |  */ | 
 | 290 | static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, | 
 | 291 | 				 struct list_head *alist) | 
 | 292 | { | 
 | 293 | 	struct perf_pmu_alias *a; | 
 | 294 |  | 
 | 295 | 	list_for_each_entry(a, alist, list) { | 
 | 296 | 		if (!strcasecmp(newalias->name, a->name)) { | 
 | 297 | 			perf_pmu_update_alias(a, newalias); | 
 | 298 | 			perf_pmu_free_alias(newalias); | 
 | 299 | 			return true; | 
 | 300 | 		} | 
 | 301 | 	} | 
 | 302 | 	return false; | 
 | 303 | } | 
 | 304 |  | 
 | 305 | static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, | 
 | 306 | 				 char *desc, char *val, | 
 | 307 | 				 char *long_desc, char *topic, | 
 | 308 | 				 char *unit, char *perpkg, | 
 | 309 | 				 char *metric_expr, | 
 | 310 | 				 char *metric_name) | 
 | 311 | { | 
 | 312 | 	struct parse_events_term *term; | 
 | 313 | 	struct perf_pmu_alias *alias; | 
 | 314 | 	int ret; | 
 | 315 | 	int num; | 
 | 316 | 	char newval[256]; | 
 | 317 |  | 
 | 318 | 	alias = malloc(sizeof(*alias)); | 
 | 319 | 	if (!alias) | 
 | 320 | 		return -ENOMEM; | 
 | 321 |  | 
 | 322 | 	INIT_LIST_HEAD(&alias->terms); | 
 | 323 | 	alias->scale = 1.0; | 
 | 324 | 	alias->unit[0] = '\0'; | 
 | 325 | 	alias->per_pkg = false; | 
 | 326 | 	alias->snapshot = false; | 
 | 327 |  | 
 | 328 | 	ret = parse_events_terms(&alias->terms, val); | 
 | 329 | 	if (ret) { | 
 | 330 | 		pr_err("Cannot parse alias %s: %d\n", val, ret); | 
 | 331 | 		free(alias); | 
 | 332 | 		return ret; | 
 | 333 | 	} | 
 | 334 |  | 
 | 335 | 	/* Scan event and remove leading zeroes, spaces, newlines, some | 
 | 336 | 	 * platforms have terms specified as | 
 | 337 | 	 * event=0x0091 (read from files ../<PMU>/events/<FILE> | 
 | 338 | 	 * and terms specified as event=0x91 (read from JSON files). | 
 | 339 | 	 * | 
 | 340 | 	 * Rebuild string to make alias->str member comparable. | 
 | 341 | 	 */ | 
 | 342 | 	memset(newval, 0, sizeof(newval)); | 
 | 343 | 	ret = 0; | 
 | 344 | 	list_for_each_entry(term, &alias->terms, list) { | 
 | 345 | 		if (ret) | 
 | 346 | 			ret += scnprintf(newval + ret, sizeof(newval) - ret, | 
 | 347 | 					 ","); | 
 | 348 | 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) | 
 | 349 | 			ret += scnprintf(newval + ret, sizeof(newval) - ret, | 
 | 350 | 					 "%s=%#x", term->config, term->val.num); | 
 | 351 | 		else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) | 
 | 352 | 			ret += scnprintf(newval + ret, sizeof(newval) - ret, | 
 | 353 | 					 "%s=%s", term->config, term->val.str); | 
 | 354 | 	} | 
 | 355 |  | 
 | 356 | 	alias->name = strdup(name); | 
 | 357 | 	if (dir) { | 
 | 358 | 		/* | 
 | 359 | 		 * load unit name and scale if available | 
 | 360 | 		 */ | 
 | 361 | 		perf_pmu__parse_unit(alias, dir, name); | 
 | 362 | 		perf_pmu__parse_scale(alias, dir, name); | 
 | 363 | 		perf_pmu__parse_per_pkg(alias, dir, name); | 
 | 364 | 		perf_pmu__parse_snapshot(alias, dir, name); | 
 | 365 | 	} | 
 | 366 |  | 
 | 367 | 	alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; | 
 | 368 | 	alias->metric_name = metric_name ? strdup(metric_name): NULL; | 
 | 369 | 	alias->desc = desc ? strdup(desc) : NULL; | 
 | 370 | 	alias->long_desc = long_desc ? strdup(long_desc) : | 
 | 371 | 				desc ? strdup(desc) : NULL; | 
 | 372 | 	alias->topic = topic ? strdup(topic) : NULL; | 
 | 373 | 	if (unit) { | 
 | 374 | 		if (convert_scale(unit, &unit, &alias->scale) < 0) | 
 | 375 | 			return -1; | 
 | 376 | 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit); | 
 | 377 | 	} | 
 | 378 | 	alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; | 
 | 379 | 	alias->str = strdup(newval); | 
 | 380 |  | 
 | 381 | 	if (!perf_pmu_merge_alias(alias, list)) | 
 | 382 | 		list_add_tail(&alias->list, list); | 
 | 383 |  | 
 | 384 | 	return 0; | 
 | 385 | } | 
 | 386 |  | 
 | 387 | static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) | 
 | 388 | { | 
 | 389 | 	char buf[256]; | 
 | 390 | 	int ret; | 
 | 391 |  | 
 | 392 | 	ret = fread(buf, 1, sizeof(buf), file); | 
 | 393 | 	if (ret == 0) | 
 | 394 | 		return -EINVAL; | 
 | 395 |  | 
 | 396 | 	buf[ret] = 0; | 
 | 397 |  | 
 | 398 | 	/* Remove trailing newline from sysfs file */ | 
 | 399 | 	rtrim(buf); | 
 | 400 |  | 
 | 401 | 	return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL, | 
 | 402 | 				     NULL, NULL, NULL); | 
 | 403 | } | 
 | 404 |  | 
 | 405 | static inline bool pmu_alias_info_file(char *name) | 
 | 406 | { | 
 | 407 | 	size_t len; | 
 | 408 |  | 
 | 409 | 	len = strlen(name); | 
 | 410 | 	if (len > 5 && !strcmp(name + len - 5, ".unit")) | 
 | 411 | 		return true; | 
 | 412 | 	if (len > 6 && !strcmp(name + len - 6, ".scale")) | 
 | 413 | 		return true; | 
 | 414 | 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) | 
 | 415 | 		return true; | 
 | 416 | 	if (len > 9 && !strcmp(name + len - 9, ".snapshot")) | 
 | 417 | 		return true; | 
 | 418 |  | 
 | 419 | 	return false; | 
 | 420 | } | 
 | 421 |  | 
 | 422 | /* | 
 | 423 |  * Process all the sysfs attributes located under the directory | 
 | 424 |  * specified in 'dir' parameter. | 
 | 425 |  */ | 
 | 426 | static int pmu_aliases_parse(char *dir, struct list_head *head) | 
 | 427 | { | 
 | 428 | 	struct dirent *evt_ent; | 
 | 429 | 	DIR *event_dir; | 
 | 430 |  | 
 | 431 | 	event_dir = opendir(dir); | 
 | 432 | 	if (!event_dir) | 
 | 433 | 		return -EINVAL; | 
 | 434 |  | 
 | 435 | 	while ((evt_ent = readdir(event_dir))) { | 
 | 436 | 		char path[PATH_MAX]; | 
 | 437 | 		char *name = evt_ent->d_name; | 
 | 438 | 		FILE *file; | 
 | 439 |  | 
 | 440 | 		if (!strcmp(name, ".") || !strcmp(name, "..")) | 
 | 441 | 			continue; | 
 | 442 |  | 
 | 443 | 		/* | 
 | 444 | 		 * skip info files parsed in perf_pmu__new_alias() | 
 | 445 | 		 */ | 
 | 446 | 		if (pmu_alias_info_file(name)) | 
 | 447 | 			continue; | 
 | 448 |  | 
 | 449 | 		scnprintf(path, PATH_MAX, "%s/%s", dir, name); | 
 | 450 |  | 
 | 451 | 		file = fopen(path, "r"); | 
 | 452 | 		if (!file) { | 
 | 453 | 			pr_debug("Cannot open %s\n", path); | 
 | 454 | 			continue; | 
 | 455 | 		} | 
 | 456 |  | 
 | 457 | 		if (perf_pmu__new_alias(head, dir, name, file) < 0) | 
 | 458 | 			pr_debug("Cannot set up %s\n", name); | 
 | 459 | 		fclose(file); | 
 | 460 | 	} | 
 | 461 |  | 
 | 462 | 	closedir(event_dir); | 
 | 463 | 	return 0; | 
 | 464 | } | 
 | 465 |  | 
 | 466 | /* | 
 | 467 |  * Reading the pmu event aliases definition, which should be located at: | 
 | 468 |  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. | 
 | 469 |  */ | 
 | 470 | static int pmu_aliases(const char *name, struct list_head *head) | 
 | 471 | { | 
 | 472 | 	struct stat st; | 
 | 473 | 	char path[PATH_MAX]; | 
 | 474 | 	const char *sysfs = sysfs__mountpoint(); | 
 | 475 |  | 
 | 476 | 	if (!sysfs) | 
 | 477 | 		return -1; | 
 | 478 |  | 
 | 479 | 	snprintf(path, PATH_MAX, | 
 | 480 | 		 "%s/bus/event_source/devices/%s/events", sysfs, name); | 
 | 481 |  | 
 | 482 | 	if (stat(path, &st) < 0) | 
 | 483 | 		return 0;	 /* no error if 'events' does not exist */ | 
 | 484 |  | 
 | 485 | 	if (pmu_aliases_parse(path, head)) | 
 | 486 | 		return -1; | 
 | 487 |  | 
 | 488 | 	return 0; | 
 | 489 | } | 
 | 490 |  | 
 | 491 | static int pmu_alias_terms(struct perf_pmu_alias *alias, | 
 | 492 | 			   struct list_head *terms) | 
 | 493 | { | 
 | 494 | 	struct parse_events_term *term, *cloned; | 
 | 495 | 	LIST_HEAD(list); | 
 | 496 | 	int ret; | 
 | 497 |  | 
 | 498 | 	list_for_each_entry(term, &alias->terms, list) { | 
 | 499 | 		ret = parse_events_term__clone(&cloned, term); | 
 | 500 | 		if (ret) { | 
 | 501 | 			parse_events_terms__purge(&list); | 
 | 502 | 			return ret; | 
 | 503 | 		} | 
 | 504 | 		/* | 
 | 505 | 		 * Weak terms don't override command line options, | 
 | 506 | 		 * which we don't want for implicit terms in aliases. | 
 | 507 | 		 */ | 
 | 508 | 		cloned->weak = true; | 
 | 509 | 		list_add_tail(&cloned->list, &list); | 
 | 510 | 	} | 
 | 511 | 	list_splice(&list, terms); | 
 | 512 | 	return 0; | 
 | 513 | } | 
 | 514 |  | 
 | 515 | /* | 
 | 516 |  * Reading/parsing the default pmu type value, which should be | 
 | 517 |  * located at: | 
 | 518 |  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. | 
 | 519 |  */ | 
 | 520 | static int pmu_type(const char *name, __u32 *type) | 
 | 521 | { | 
 | 522 | 	struct stat st; | 
 | 523 | 	char path[PATH_MAX]; | 
 | 524 | 	FILE *file; | 
 | 525 | 	int ret = 0; | 
 | 526 | 	const char *sysfs = sysfs__mountpoint(); | 
 | 527 |  | 
 | 528 | 	if (!sysfs) | 
 | 529 | 		return -1; | 
 | 530 |  | 
 | 531 | 	snprintf(path, PATH_MAX, | 
 | 532 | 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); | 
 | 533 |  | 
 | 534 | 	if (stat(path, &st) < 0) | 
 | 535 | 		return -1; | 
 | 536 |  | 
 | 537 | 	file = fopen(path, "r"); | 
 | 538 | 	if (!file) | 
 | 539 | 		return -EINVAL; | 
 | 540 |  | 
 | 541 | 	if (1 != fscanf(file, "%u", type)) | 
 | 542 | 		ret = -1; | 
 | 543 |  | 
 | 544 | 	fclose(file); | 
 | 545 | 	return ret; | 
 | 546 | } | 
 | 547 |  | 
 | 548 | /* Add all pmus in sysfs to pmu list: */ | 
 | 549 | static void pmu_read_sysfs(void) | 
 | 550 | { | 
 | 551 | 	char path[PATH_MAX]; | 
 | 552 | 	DIR *dir; | 
 | 553 | 	struct dirent *dent; | 
 | 554 | 	const char *sysfs = sysfs__mountpoint(); | 
 | 555 |  | 
 | 556 | 	if (!sysfs) | 
 | 557 | 		return; | 
 | 558 |  | 
 | 559 | 	snprintf(path, PATH_MAX, | 
 | 560 | 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); | 
 | 561 |  | 
 | 562 | 	dir = opendir(path); | 
 | 563 | 	if (!dir) | 
 | 564 | 		return; | 
 | 565 |  | 
 | 566 | 	while ((dent = readdir(dir))) { | 
 | 567 | 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) | 
 | 568 | 			continue; | 
 | 569 | 		/* add to static LIST_HEAD(pmus): */ | 
 | 570 | 		perf_pmu__find(dent->d_name); | 
 | 571 | 	} | 
 | 572 |  | 
 | 573 | 	closedir(dir); | 
 | 574 | } | 
 | 575 |  | 
 | 576 | static struct cpu_map *__pmu_cpumask(const char *path) | 
 | 577 | { | 
 | 578 | 	FILE *file; | 
 | 579 | 	struct cpu_map *cpus; | 
 | 580 |  | 
 | 581 | 	file = fopen(path, "r"); | 
 | 582 | 	if (!file) | 
 | 583 | 		return NULL; | 
 | 584 |  | 
 | 585 | 	cpus = cpu_map__read(file); | 
 | 586 | 	fclose(file); | 
 | 587 | 	return cpus; | 
 | 588 | } | 
 | 589 |  | 
 | 590 | /* | 
 | 591 |  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) | 
 | 592 |  * may have a "cpus" file. | 
 | 593 |  */ | 
 | 594 | #define CPUS_TEMPLATE_UNCORE	"%s/bus/event_source/devices/%s/cpumask" | 
 | 595 | #define CPUS_TEMPLATE_CPU	"%s/bus/event_source/devices/%s/cpus" | 
 | 596 |  | 
 | 597 | static struct cpu_map *pmu_cpumask(const char *name) | 
 | 598 | { | 
 | 599 | 	char path[PATH_MAX]; | 
 | 600 | 	struct cpu_map *cpus; | 
 | 601 | 	const char *sysfs = sysfs__mountpoint(); | 
 | 602 | 	const char *templates[] = { | 
 | 603 | 		CPUS_TEMPLATE_UNCORE, | 
 | 604 | 		CPUS_TEMPLATE_CPU, | 
 | 605 | 		NULL | 
 | 606 | 	}; | 
 | 607 | 	const char **template; | 
 | 608 |  | 
 | 609 | 	if (!sysfs) | 
 | 610 | 		return NULL; | 
 | 611 |  | 
 | 612 | 	for (template = templates; *template; template++) { | 
 | 613 | 		snprintf(path, PATH_MAX, *template, sysfs, name); | 
 | 614 | 		cpus = __pmu_cpumask(path); | 
 | 615 | 		if (cpus) | 
 | 616 | 			return cpus; | 
 | 617 | 	} | 
 | 618 |  | 
 | 619 | 	return NULL; | 
 | 620 | } | 
 | 621 |  | 
 | 622 | static bool pmu_is_uncore(const char *name) | 
 | 623 | { | 
 | 624 | 	char path[PATH_MAX]; | 
 | 625 | 	struct cpu_map *cpus; | 
 | 626 | 	const char *sysfs = sysfs__mountpoint(); | 
 | 627 |  | 
 | 628 | 	snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name); | 
 | 629 | 	cpus = __pmu_cpumask(path); | 
 | 630 | 	cpu_map__put(cpus); | 
 | 631 |  | 
 | 632 | 	return !!cpus; | 
 | 633 | } | 
 | 634 |  | 
 | 635 | /* | 
 | 636 |  *  PMU CORE devices have different name other than cpu in sysfs on some | 
 | 637 |  *  platforms. | 
 | 638 |  *  Looking for possible sysfs files to identify the arm core device. | 
 | 639 |  */ | 
 | 640 | static int is_arm_pmu_core(const char *name) | 
 | 641 | { | 
 | 642 | 	struct stat st; | 
 | 643 | 	char path[PATH_MAX]; | 
 | 644 | 	const char *sysfs = sysfs__mountpoint(); | 
 | 645 |  | 
 | 646 | 	if (!sysfs) | 
 | 647 | 		return 0; | 
 | 648 |  | 
 | 649 | 	/* Look for cpu sysfs (specific to arm) */ | 
 | 650 | 	scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus", | 
 | 651 | 				sysfs, name); | 
 | 652 | 	if (stat(path, &st) == 0) | 
 | 653 | 		return 1; | 
 | 654 |  | 
 | 655 | 	return 0; | 
 | 656 | } | 
 | 657 |  | 
 | 658 | /* | 
 | 659 |  * Return the CPU id as a raw string. | 
 | 660 |  * | 
 | 661 |  * Each architecture should provide a more precise id string that | 
 | 662 |  * can be use to match the architecture's "mapfile". | 
 | 663 |  */ | 
 | 664 | char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused) | 
 | 665 | { | 
 | 666 | 	return NULL; | 
 | 667 | } | 
 | 668 |  | 
 | 669 | /* Return zero when the cpuid from the mapfile.csv matches the | 
 | 670 |  * cpuid string generated on this platform. | 
 | 671 |  * Otherwise return non-zero. | 
 | 672 |  */ | 
 | 673 | int strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) | 
 | 674 | { | 
 | 675 | 	regex_t re; | 
 | 676 | 	regmatch_t pmatch[1]; | 
 | 677 | 	int match; | 
 | 678 |  | 
 | 679 | 	if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) { | 
 | 680 | 		/* Warn unable to generate match particular string. */ | 
 | 681 | 		pr_info("Invalid regular expression %s\n", mapcpuid); | 
 | 682 | 		return 1; | 
 | 683 | 	} | 
 | 684 |  | 
 | 685 | 	match = !regexec(&re, cpuid, 1, pmatch, 0); | 
 | 686 | 	regfree(&re); | 
 | 687 | 	if (match) { | 
 | 688 | 		size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); | 
 | 689 |  | 
 | 690 | 		/* Verify the entire string matched. */ | 
 | 691 | 		if (match_len == strlen(cpuid)) | 
 | 692 | 			return 0; | 
 | 693 | 	} | 
 | 694 | 	return 1; | 
 | 695 | } | 
 | 696 |  | 
 | 697 | static char *perf_pmu__getcpuid(struct perf_pmu *pmu) | 
 | 698 | { | 
 | 699 | 	char *cpuid; | 
 | 700 | 	static bool printed; | 
 | 701 |  | 
 | 702 | 	cpuid = getenv("PERF_CPUID"); | 
 | 703 | 	if (cpuid) | 
 | 704 | 		cpuid = strdup(cpuid); | 
 | 705 | 	if (!cpuid) | 
 | 706 | 		cpuid = get_cpuid_str(pmu); | 
 | 707 | 	if (!cpuid) | 
 | 708 | 		return NULL; | 
 | 709 |  | 
 | 710 | 	if (!printed) { | 
 | 711 | 		pr_debug("Using CPUID %s\n", cpuid); | 
 | 712 | 		printed = true; | 
 | 713 | 	} | 
 | 714 | 	return cpuid; | 
 | 715 | } | 
 | 716 |  | 
 | 717 | struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu) | 
 | 718 | { | 
 | 719 | 	struct pmu_events_map *map; | 
 | 720 | 	char *cpuid = perf_pmu__getcpuid(pmu); | 
 | 721 | 	int i; | 
 | 722 |  | 
 | 723 | 	/* on some platforms which uses cpus map, cpuid can be NULL for | 
 | 724 | 	 * PMUs other than CORE PMUs. | 
 | 725 | 	 */ | 
 | 726 | 	if (!cpuid) | 
 | 727 | 		return NULL; | 
 | 728 |  | 
 | 729 | 	i = 0; | 
 | 730 | 	for (;;) { | 
 | 731 | 		map = &pmu_events_map[i++]; | 
 | 732 | 		if (!map->table) { | 
 | 733 | 			map = NULL; | 
 | 734 | 			break; | 
 | 735 | 		} | 
 | 736 |  | 
 | 737 | 		if (!strcmp_cpuid_str(map->cpuid, cpuid)) | 
 | 738 | 			break; | 
 | 739 | 	} | 
 | 740 | 	free(cpuid); | 
 | 741 | 	return map; | 
 | 742 | } | 
 | 743 |  | 
 | 744 | /* | 
 | 745 |  * From the pmu_events_map, find the table of PMU events that corresponds | 
 | 746 |  * to the current running CPU. Then, add all PMU events from that table | 
 | 747 |  * as aliases. | 
 | 748 |  */ | 
 | 749 | static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) | 
 | 750 | { | 
 | 751 | 	int i; | 
 | 752 | 	struct pmu_events_map *map; | 
 | 753 | 	const char *name = pmu->name; | 
 | 754 |  | 
 | 755 | 	map = perf_pmu__find_map(pmu); | 
 | 756 | 	if (!map) | 
 | 757 | 		return; | 
 | 758 |  | 
 | 759 | 	/* | 
 | 760 | 	 * Found a matching PMU events table. Create aliases | 
 | 761 | 	 */ | 
 | 762 | 	i = 0; | 
 | 763 | 	while (1) { | 
 | 764 | 		const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu"; | 
 | 765 | 		struct pmu_event *pe = &map->table[i++]; | 
 | 766 | 		const char *pname = pe->pmu ? pe->pmu : cpu_name; | 
 | 767 |  | 
 | 768 | 		if (!pe->name) { | 
 | 769 | 			if (pe->metric_group || pe->metric_name) | 
 | 770 | 				continue; | 
 | 771 | 			break; | 
 | 772 | 		} | 
 | 773 |  | 
 | 774 | 		/* | 
 | 775 | 		 * uncore alias may be from different PMU | 
 | 776 | 		 * with common prefix | 
 | 777 | 		 */ | 
 | 778 | 		if (pmu_is_uncore(name) && | 
 | 779 | 		    !strncmp(pname, name, strlen(pname))) | 
 | 780 | 			goto new_alias; | 
 | 781 |  | 
 | 782 | 		if (strcmp(pname, name)) | 
 | 783 | 			continue; | 
 | 784 |  | 
 | 785 | new_alias: | 
 | 786 | 		/* need type casts to override 'const' */ | 
 | 787 | 		__perf_pmu__new_alias(head, NULL, (char *)pe->name, | 
 | 788 | 				(char *)pe->desc, (char *)pe->event, | 
 | 789 | 				(char *)pe->long_desc, (char *)pe->topic, | 
 | 790 | 				(char *)pe->unit, (char *)pe->perpkg, | 
 | 791 | 				(char *)pe->metric_expr, | 
 | 792 | 				(char *)pe->metric_name); | 
 | 793 | 	} | 
 | 794 | } | 
 | 795 |  | 
 | 796 | struct perf_event_attr * __weak | 
 | 797 | perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) | 
 | 798 | { | 
 | 799 | 	return NULL; | 
 | 800 | } | 
 | 801 |  | 
 | 802 | static struct perf_pmu *pmu_lookup(const char *name) | 
 | 803 | { | 
 | 804 | 	struct perf_pmu *pmu; | 
 | 805 | 	LIST_HEAD(format); | 
 | 806 | 	LIST_HEAD(aliases); | 
 | 807 | 	__u32 type; | 
 | 808 |  | 
 | 809 | 	/* | 
 | 810 | 	 * The pmu data we store & need consists of the pmu | 
 | 811 | 	 * type value and format definitions. Load both right | 
 | 812 | 	 * now. | 
 | 813 | 	 */ | 
 | 814 | 	if (pmu_format(name, &format)) | 
 | 815 | 		return NULL; | 
 | 816 |  | 
 | 817 | 	/* | 
 | 818 | 	 * Check the type first to avoid unnecessary work. | 
 | 819 | 	 */ | 
 | 820 | 	if (pmu_type(name, &type)) | 
 | 821 | 		return NULL; | 
 | 822 |  | 
 | 823 | 	if (pmu_aliases(name, &aliases)) | 
 | 824 | 		return NULL; | 
 | 825 |  | 
 | 826 | 	pmu = zalloc(sizeof(*pmu)); | 
 | 827 | 	if (!pmu) | 
 | 828 | 		return NULL; | 
 | 829 |  | 
 | 830 | 	pmu->cpus = pmu_cpumask(name); | 
 | 831 | 	pmu->name = strdup(name); | 
 | 832 | 	pmu->type = type; | 
 | 833 | 	pmu->is_uncore = pmu_is_uncore(name); | 
 | 834 | 	pmu_add_cpu_aliases(&aliases, pmu); | 
 | 835 |  | 
 | 836 | 	INIT_LIST_HEAD(&pmu->format); | 
 | 837 | 	INIT_LIST_HEAD(&pmu->aliases); | 
 | 838 | 	list_splice(&format, &pmu->format); | 
 | 839 | 	list_splice(&aliases, &pmu->aliases); | 
 | 840 | 	list_add_tail(&pmu->list, &pmus); | 
 | 841 |  | 
 | 842 | 	pmu->default_config = perf_pmu__get_default_config(pmu); | 
 | 843 |  | 
 | 844 | 	return pmu; | 
 | 845 | } | 
 | 846 |  | 
 | 847 | static struct perf_pmu *pmu_find(const char *name) | 
 | 848 | { | 
 | 849 | 	struct perf_pmu *pmu; | 
 | 850 |  | 
 | 851 | 	list_for_each_entry(pmu, &pmus, list) | 
 | 852 | 		if (!strcmp(pmu->name, name)) | 
 | 853 | 			return pmu; | 
 | 854 |  | 
 | 855 | 	return NULL; | 
 | 856 | } | 
 | 857 |  | 
 | 858 | struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) | 
 | 859 | { | 
 | 860 | 	/* | 
 | 861 | 	 * pmu iterator: If pmu is NULL, we start at the begin, | 
 | 862 | 	 * otherwise return the next pmu. Returns NULL on end. | 
 | 863 | 	 */ | 
 | 864 | 	if (!pmu) { | 
 | 865 | 		pmu_read_sysfs(); | 
 | 866 | 		pmu = list_prepare_entry(pmu, &pmus, list); | 
 | 867 | 	} | 
 | 868 | 	list_for_each_entry_continue(pmu, &pmus, list) | 
 | 869 | 		return pmu; | 
 | 870 | 	return NULL; | 
 | 871 | } | 
 | 872 |  | 
 | 873 | struct perf_pmu *perf_pmu__find(const char *name) | 
 | 874 | { | 
 | 875 | 	struct perf_pmu *pmu; | 
 | 876 |  | 
 | 877 | 	/* | 
 | 878 | 	 * Once PMU is loaded it stays in the list, | 
 | 879 | 	 * so we keep us from multiple reading/parsing | 
 | 880 | 	 * the pmu format definitions. | 
 | 881 | 	 */ | 
 | 882 | 	pmu = pmu_find(name); | 
 | 883 | 	if (pmu) | 
 | 884 | 		return pmu; | 
 | 885 |  | 
 | 886 | 	return pmu_lookup(name); | 
 | 887 | } | 
 | 888 |  | 
 | 889 | static struct perf_pmu_format * | 
 | 890 | pmu_find_format(struct list_head *formats, const char *name) | 
 | 891 | { | 
 | 892 | 	struct perf_pmu_format *format; | 
 | 893 |  | 
 | 894 | 	list_for_each_entry(format, formats, list) | 
 | 895 | 		if (!strcmp(format->name, name)) | 
 | 896 | 			return format; | 
 | 897 |  | 
 | 898 | 	return NULL; | 
 | 899 | } | 
 | 900 |  | 
 | 901 | __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) | 
 | 902 | { | 
 | 903 | 	struct perf_pmu_format *format = pmu_find_format(formats, name); | 
 | 904 | 	__u64 bits = 0; | 
 | 905 | 	int fbit; | 
 | 906 |  | 
 | 907 | 	if (!format) | 
 | 908 | 		return 0; | 
 | 909 |  | 
 | 910 | 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) | 
 | 911 | 		bits |= 1ULL << fbit; | 
 | 912 |  | 
 | 913 | 	return bits; | 
 | 914 | } | 
 | 915 |  | 
 | 916 | /* | 
 | 917 |  * Sets value based on the format definition (format parameter) | 
 | 918 |  * and unformated value (value parameter). | 
 | 919 |  */ | 
 | 920 | static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, | 
 | 921 | 			     bool zero) | 
 | 922 | { | 
 | 923 | 	unsigned long fbit, vbit; | 
 | 924 |  | 
 | 925 | 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { | 
 | 926 |  | 
 | 927 | 		if (!test_bit(fbit, format)) | 
 | 928 | 			continue; | 
 | 929 |  | 
 | 930 | 		if (value & (1llu << vbit++)) | 
 | 931 | 			*v |= (1llu << fbit); | 
 | 932 | 		else if (zero) | 
 | 933 | 			*v &= ~(1llu << fbit); | 
 | 934 | 	} | 
 | 935 | } | 
 | 936 |  | 
 | 937 | static __u64 pmu_format_max_value(const unsigned long *format) | 
 | 938 | { | 
 | 939 | 	int w; | 
 | 940 |  | 
 | 941 | 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); | 
 | 942 | 	if (!w) | 
 | 943 | 		return 0; | 
 | 944 | 	if (w < 64) | 
 | 945 | 		return (1ULL << w) - 1; | 
 | 946 | 	return -1; | 
 | 947 | } | 
 | 948 |  | 
 | 949 | /* | 
 | 950 |  * Term is a string term, and might be a param-term. Try to look up it's value | 
 | 951 |  * in the remaining terms. | 
 | 952 |  * - We have a term like "base-or-format-term=param-term", | 
 | 953 |  * - We need to find the value supplied for "param-term" (with param-term named | 
 | 954 |  *   in a config string) later on in the term list. | 
 | 955 |  */ | 
 | 956 | static int pmu_resolve_param_term(struct parse_events_term *term, | 
 | 957 | 				  struct list_head *head_terms, | 
 | 958 | 				  __u64 *value) | 
 | 959 | { | 
 | 960 | 	struct parse_events_term *t; | 
 | 961 |  | 
 | 962 | 	list_for_each_entry(t, head_terms, list) { | 
 | 963 | 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { | 
 | 964 | 			if (!strcmp(t->config, term->config)) { | 
 | 965 | 				t->used = true; | 
 | 966 | 				*value = t->val.num; | 
 | 967 | 				return 0; | 
 | 968 | 			} | 
 | 969 | 		} | 
 | 970 | 	} | 
 | 971 |  | 
 | 972 | 	if (verbose > 0) | 
 | 973 | 		printf("Required parameter '%s' not specified\n", term->config); | 
 | 974 |  | 
 | 975 | 	return -1; | 
 | 976 | } | 
 | 977 |  | 
 | 978 | static char *pmu_formats_string(struct list_head *formats) | 
 | 979 | { | 
 | 980 | 	struct perf_pmu_format *format; | 
 | 981 | 	char *str = NULL; | 
 | 982 | 	struct strbuf buf = STRBUF_INIT; | 
 | 983 | 	unsigned i = 0; | 
 | 984 |  | 
 | 985 | 	if (!formats) | 
 | 986 | 		return NULL; | 
 | 987 |  | 
 | 988 | 	/* sysfs exported terms */ | 
 | 989 | 	list_for_each_entry(format, formats, list) | 
 | 990 | 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) | 
 | 991 | 			goto error; | 
 | 992 |  | 
 | 993 | 	str = strbuf_detach(&buf, NULL); | 
 | 994 | error: | 
 | 995 | 	strbuf_release(&buf); | 
 | 996 |  | 
 | 997 | 	return str; | 
 | 998 | } | 
 | 999 |  | 
 | 1000 | /* | 
 | 1001 |  * Setup one of config[12] attr members based on the | 
 | 1002 |  * user input data - term parameter. | 
 | 1003 |  */ | 
 | 1004 | static int pmu_config_term(struct list_head *formats, | 
 | 1005 | 			   struct perf_event_attr *attr, | 
 | 1006 | 			   struct parse_events_term *term, | 
 | 1007 | 			   struct list_head *head_terms, | 
 | 1008 | 			   bool zero, struct parse_events_error *err) | 
 | 1009 | { | 
 | 1010 | 	struct perf_pmu_format *format; | 
 | 1011 | 	__u64 *vp; | 
 | 1012 | 	__u64 val, max_val; | 
 | 1013 |  | 
 | 1014 | 	/* | 
 | 1015 | 	 * If this is a parameter we've already used for parameterized-eval, | 
 | 1016 | 	 * skip it in normal eval. | 
 | 1017 | 	 */ | 
 | 1018 | 	if (term->used) | 
 | 1019 | 		return 0; | 
 | 1020 |  | 
 | 1021 | 	/* | 
 | 1022 | 	 * Hardcoded terms should be already in, so nothing | 
 | 1023 | 	 * to be done for them. | 
 | 1024 | 	 */ | 
 | 1025 | 	if (parse_events__is_hardcoded_term(term)) | 
 | 1026 | 		return 0; | 
 | 1027 |  | 
 | 1028 | 	format = pmu_find_format(formats, term->config); | 
 | 1029 | 	if (!format) { | 
 | 1030 | 		if (verbose > 0) | 
 | 1031 | 			printf("Invalid event/parameter '%s'\n", term->config); | 
 | 1032 | 		if (err) { | 
 | 1033 | 			char *pmu_term = pmu_formats_string(formats); | 
 | 1034 |  | 
 | 1035 | 			err->idx  = term->err_term; | 
 | 1036 | 			err->str  = strdup("unknown term"); | 
 | 1037 | 			err->help = parse_events_formats_error_string(pmu_term); | 
 | 1038 | 			free(pmu_term); | 
 | 1039 | 		} | 
 | 1040 | 		return -EINVAL; | 
 | 1041 | 	} | 
 | 1042 |  | 
 | 1043 | 	switch (format->value) { | 
 | 1044 | 	case PERF_PMU_FORMAT_VALUE_CONFIG: | 
 | 1045 | 		vp = &attr->config; | 
 | 1046 | 		break; | 
 | 1047 | 	case PERF_PMU_FORMAT_VALUE_CONFIG1: | 
 | 1048 | 		vp = &attr->config1; | 
 | 1049 | 		break; | 
 | 1050 | 	case PERF_PMU_FORMAT_VALUE_CONFIG2: | 
 | 1051 | 		vp = &attr->config2; | 
 | 1052 | 		break; | 
 | 1053 | 	default: | 
 | 1054 | 		return -EINVAL; | 
 | 1055 | 	} | 
 | 1056 |  | 
 | 1057 | 	/* | 
 | 1058 | 	 * Either directly use a numeric term, or try to translate string terms | 
 | 1059 | 	 * using event parameters. | 
 | 1060 | 	 */ | 
 | 1061 | 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { | 
 | 1062 | 		if (term->no_value && | 
 | 1063 | 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { | 
 | 1064 | 			if (err) { | 
 | 1065 | 				err->idx = term->err_val; | 
 | 1066 | 				err->str = strdup("no value assigned for term"); | 
 | 1067 | 			} | 
 | 1068 | 			return -EINVAL; | 
 | 1069 | 		} | 
 | 1070 |  | 
 | 1071 | 		val = term->val.num; | 
 | 1072 | 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { | 
 | 1073 | 		if (strcmp(term->val.str, "?")) { | 
 | 1074 | 			if (verbose > 0) { | 
 | 1075 | 				pr_info("Invalid sysfs entry %s=%s\n", | 
 | 1076 | 						term->config, term->val.str); | 
 | 1077 | 			} | 
 | 1078 | 			if (err) { | 
 | 1079 | 				err->idx = term->err_val; | 
 | 1080 | 				err->str = strdup("expected numeric value"); | 
 | 1081 | 			} | 
 | 1082 | 			return -EINVAL; | 
 | 1083 | 		} | 
 | 1084 |  | 
 | 1085 | 		if (pmu_resolve_param_term(term, head_terms, &val)) | 
 | 1086 | 			return -EINVAL; | 
 | 1087 | 	} else | 
 | 1088 | 		return -EINVAL; | 
 | 1089 |  | 
 | 1090 | 	max_val = pmu_format_max_value(format->bits); | 
 | 1091 | 	if (val > max_val) { | 
 | 1092 | 		if (err) { | 
 | 1093 | 			err->idx = term->err_val; | 
 | 1094 | 			if (asprintf(&err->str, | 
 | 1095 | 				     "value too big for format, maximum is %llu", | 
 | 1096 | 				     (unsigned long long)max_val) < 0) | 
 | 1097 | 				err->str = strdup("value too big for format"); | 
 | 1098 | 			return -EINVAL; | 
 | 1099 | 		} | 
 | 1100 | 		/* | 
 | 1101 | 		 * Assume we don't care if !err, in which case the value will be | 
 | 1102 | 		 * silently truncated. | 
 | 1103 | 		 */ | 
 | 1104 | 	} | 
 | 1105 |  | 
 | 1106 | 	pmu_format_value(format->bits, val, vp, zero); | 
 | 1107 | 	return 0; | 
 | 1108 | } | 
 | 1109 |  | 
 | 1110 | int perf_pmu__config_terms(struct list_head *formats, | 
 | 1111 | 			   struct perf_event_attr *attr, | 
 | 1112 | 			   struct list_head *head_terms, | 
 | 1113 | 			   bool zero, struct parse_events_error *err) | 
 | 1114 | { | 
 | 1115 | 	struct parse_events_term *term; | 
 | 1116 |  | 
 | 1117 | 	list_for_each_entry(term, head_terms, list) { | 
 | 1118 | 		if (pmu_config_term(formats, attr, term, head_terms, | 
 | 1119 | 				    zero, err)) | 
 | 1120 | 			return -EINVAL; | 
 | 1121 | 	} | 
 | 1122 |  | 
 | 1123 | 	return 0; | 
 | 1124 | } | 
 | 1125 |  | 
 | 1126 | /* | 
 | 1127 |  * Configures event's 'attr' parameter based on the: | 
 | 1128 |  * 1) users input - specified in terms parameter | 
 | 1129 |  * 2) pmu format definitions - specified by pmu parameter | 
 | 1130 |  */ | 
 | 1131 | int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, | 
 | 1132 | 		     struct list_head *head_terms, | 
 | 1133 | 		     struct parse_events_error *err) | 
 | 1134 | { | 
 | 1135 | 	bool zero = !!pmu->default_config; | 
 | 1136 |  | 
 | 1137 | 	attr->type = pmu->type; | 
 | 1138 | 	return perf_pmu__config_terms(&pmu->format, attr, head_terms, | 
 | 1139 | 				      zero, err); | 
 | 1140 | } | 
 | 1141 |  | 
 | 1142 | static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, | 
 | 1143 | 					     struct parse_events_term *term) | 
 | 1144 | { | 
 | 1145 | 	struct perf_pmu_alias *alias; | 
 | 1146 | 	char *name; | 
 | 1147 |  | 
 | 1148 | 	if (parse_events__is_hardcoded_term(term)) | 
 | 1149 | 		return NULL; | 
 | 1150 |  | 
 | 1151 | 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { | 
 | 1152 | 		if (term->val.num != 1) | 
 | 1153 | 			return NULL; | 
 | 1154 | 		if (pmu_find_format(&pmu->format, term->config)) | 
 | 1155 | 			return NULL; | 
 | 1156 | 		name = term->config; | 
 | 1157 | 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { | 
 | 1158 | 		if (strcasecmp(term->config, "event")) | 
 | 1159 | 			return NULL; | 
 | 1160 | 		name = term->val.str; | 
 | 1161 | 	} else { | 
 | 1162 | 		return NULL; | 
 | 1163 | 	} | 
 | 1164 |  | 
 | 1165 | 	list_for_each_entry(alias, &pmu->aliases, list) { | 
 | 1166 | 		if (!strcasecmp(alias->name, name)) | 
 | 1167 | 			return alias; | 
 | 1168 | 	} | 
 | 1169 | 	return NULL; | 
 | 1170 | } | 
 | 1171 |  | 
 | 1172 |  | 
 | 1173 | static int check_info_data(struct perf_pmu_alias *alias, | 
 | 1174 | 			   struct perf_pmu_info *info) | 
 | 1175 | { | 
 | 1176 | 	/* | 
 | 1177 | 	 * Only one term in event definition can | 
 | 1178 | 	 * define unit, scale and snapshot, fail | 
 | 1179 | 	 * if there's more than one. | 
 | 1180 | 	 */ | 
 | 1181 | 	if ((info->unit && alias->unit[0]) || | 
 | 1182 | 	    (info->scale && alias->scale) || | 
 | 1183 | 	    (info->snapshot && alias->snapshot)) | 
 | 1184 | 		return -EINVAL; | 
 | 1185 |  | 
 | 1186 | 	if (alias->unit[0]) | 
 | 1187 | 		info->unit = alias->unit; | 
 | 1188 |  | 
 | 1189 | 	if (alias->scale) | 
 | 1190 | 		info->scale = alias->scale; | 
 | 1191 |  | 
 | 1192 | 	if (alias->snapshot) | 
 | 1193 | 		info->snapshot = alias->snapshot; | 
 | 1194 |  | 
 | 1195 | 	return 0; | 
 | 1196 | } | 
 | 1197 |  | 
 | 1198 | /* | 
 | 1199 |  * Find alias in the terms list and replace it with the terms | 
 | 1200 |  * defined for the alias | 
 | 1201 |  */ | 
 | 1202 | int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, | 
 | 1203 | 			  struct perf_pmu_info *info) | 
 | 1204 | { | 
 | 1205 | 	struct parse_events_term *term, *h; | 
 | 1206 | 	struct perf_pmu_alias *alias; | 
 | 1207 | 	int ret; | 
 | 1208 |  | 
 | 1209 | 	info->per_pkg = false; | 
 | 1210 |  | 
 | 1211 | 	/* | 
 | 1212 | 	 * Mark unit and scale as not set | 
 | 1213 | 	 * (different from default values, see below) | 
 | 1214 | 	 */ | 
 | 1215 | 	info->unit     = NULL; | 
 | 1216 | 	info->scale    = 0.0; | 
 | 1217 | 	info->snapshot = false; | 
 | 1218 | 	info->metric_expr = NULL; | 
 | 1219 | 	info->metric_name = NULL; | 
 | 1220 |  | 
 | 1221 | 	list_for_each_entry_safe(term, h, head_terms, list) { | 
 | 1222 | 		alias = pmu_find_alias(pmu, term); | 
 | 1223 | 		if (!alias) | 
 | 1224 | 			continue; | 
 | 1225 | 		ret = pmu_alias_terms(alias, &term->list); | 
 | 1226 | 		if (ret) | 
 | 1227 | 			return ret; | 
 | 1228 |  | 
 | 1229 | 		ret = check_info_data(alias, info); | 
 | 1230 | 		if (ret) | 
 | 1231 | 			return ret; | 
 | 1232 |  | 
 | 1233 | 		if (alias->per_pkg) | 
 | 1234 | 			info->per_pkg = true; | 
 | 1235 | 		info->metric_expr = alias->metric_expr; | 
 | 1236 | 		info->metric_name = alias->metric_name; | 
 | 1237 |  | 
 | 1238 | 		list_del(&term->list); | 
 | 1239 | 		free(term); | 
 | 1240 | 	} | 
 | 1241 |  | 
 | 1242 | 	/* | 
 | 1243 | 	 * if no unit or scale foundin aliases, then | 
 | 1244 | 	 * set defaults as for evsel | 
 | 1245 | 	 * unit cannot left to NULL | 
 | 1246 | 	 */ | 
 | 1247 | 	if (info->unit == NULL) | 
 | 1248 | 		info->unit   = ""; | 
 | 1249 |  | 
 | 1250 | 	if (info->scale == 0.0) | 
 | 1251 | 		info->scale  = 1.0; | 
 | 1252 |  | 
 | 1253 | 	return 0; | 
 | 1254 | } | 
 | 1255 |  | 
 | 1256 | int perf_pmu__new_format(struct list_head *list, char *name, | 
 | 1257 | 			 int config, unsigned long *bits) | 
 | 1258 | { | 
 | 1259 | 	struct perf_pmu_format *format; | 
 | 1260 |  | 
 | 1261 | 	format = zalloc(sizeof(*format)); | 
 | 1262 | 	if (!format) | 
 | 1263 | 		return -ENOMEM; | 
 | 1264 |  | 
 | 1265 | 	format->name = strdup(name); | 
 | 1266 | 	format->value = config; | 
 | 1267 | 	memcpy(format->bits, bits, sizeof(format->bits)); | 
 | 1268 |  | 
 | 1269 | 	list_add_tail(&format->list, list); | 
 | 1270 | 	return 0; | 
 | 1271 | } | 
 | 1272 |  | 
 | 1273 | void perf_pmu__set_format(unsigned long *bits, long from, long to) | 
 | 1274 | { | 
 | 1275 | 	long b; | 
 | 1276 |  | 
 | 1277 | 	if (!to) | 
 | 1278 | 		to = from; | 
 | 1279 |  | 
 | 1280 | 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); | 
 | 1281 | 	for (b = from; b <= to; b++) | 
 | 1282 | 		set_bit(b, bits); | 
 | 1283 | } | 
 | 1284 |  | 
 | 1285 | static int sub_non_neg(int a, int b) | 
 | 1286 | { | 
 | 1287 | 	if (b > a) | 
 | 1288 | 		return 0; | 
 | 1289 | 	return a - b; | 
 | 1290 | } | 
 | 1291 |  | 
 | 1292 | static char *format_alias(char *buf, int len, struct perf_pmu *pmu, | 
 | 1293 | 			  struct perf_pmu_alias *alias) | 
 | 1294 | { | 
 | 1295 | 	struct parse_events_term *term; | 
 | 1296 | 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); | 
 | 1297 |  | 
 | 1298 | 	list_for_each_entry(term, &alias->terms, list) { | 
 | 1299 | 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) | 
 | 1300 | 			used += snprintf(buf + used, sub_non_neg(len, used), | 
 | 1301 | 					",%s=%s", term->config, | 
 | 1302 | 					term->val.str); | 
 | 1303 | 	} | 
 | 1304 |  | 
 | 1305 | 	if (sub_non_neg(len, used) > 0) { | 
 | 1306 | 		buf[used] = '/'; | 
 | 1307 | 		used++; | 
 | 1308 | 	} | 
 | 1309 | 	if (sub_non_neg(len, used) > 0) { | 
 | 1310 | 		buf[used] = '\0'; | 
 | 1311 | 		used++; | 
 | 1312 | 	} else | 
 | 1313 | 		buf[len - 1] = '\0'; | 
 | 1314 |  | 
 | 1315 | 	return buf; | 
 | 1316 | } | 
 | 1317 |  | 
 | 1318 | static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, | 
 | 1319 | 			     struct perf_pmu_alias *alias) | 
 | 1320 | { | 
 | 1321 | 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); | 
 | 1322 | 	return buf; | 
 | 1323 | } | 
 | 1324 |  | 
 | 1325 | struct sevent { | 
 | 1326 | 	char *name; | 
 | 1327 | 	char *desc; | 
 | 1328 | 	char *topic; | 
 | 1329 | 	char *str; | 
 | 1330 | 	char *pmu; | 
 | 1331 | 	char *metric_expr; | 
 | 1332 | 	char *metric_name; | 
 | 1333 | }; | 
 | 1334 |  | 
 | 1335 | static int cmp_sevent(const void *a, const void *b) | 
 | 1336 | { | 
 | 1337 | 	const struct sevent *as = a; | 
 | 1338 | 	const struct sevent *bs = b; | 
 | 1339 |  | 
 | 1340 | 	/* Put extra events last */ | 
 | 1341 | 	if (!!as->desc != !!bs->desc) | 
 | 1342 | 		return !!as->desc - !!bs->desc; | 
 | 1343 | 	if (as->topic && bs->topic) { | 
 | 1344 | 		int n = strcmp(as->topic, bs->topic); | 
 | 1345 |  | 
 | 1346 | 		if (n) | 
 | 1347 | 			return n; | 
 | 1348 | 	} | 
 | 1349 | 	return strcmp(as->name, bs->name); | 
 | 1350 | } | 
 | 1351 |  | 
 | 1352 | static void wordwrap(char *s, int start, int max, int corr) | 
 | 1353 | { | 
 | 1354 | 	int column = start; | 
 | 1355 | 	int n; | 
 | 1356 |  | 
 | 1357 | 	while (*s) { | 
 | 1358 | 		int wlen = strcspn(s, " \t"); | 
 | 1359 |  | 
 | 1360 | 		if (column + wlen >= max && column > start) { | 
 | 1361 | 			printf("\n%*s", start, ""); | 
 | 1362 | 			column = start + corr; | 
 | 1363 | 		} | 
 | 1364 | 		n = printf("%s%.*s", column > start ? " " : "", wlen, s); | 
 | 1365 | 		if (n <= 0) | 
 | 1366 | 			break; | 
 | 1367 | 		s += wlen; | 
 | 1368 | 		column += n; | 
 | 1369 | 		s = ltrim(s); | 
 | 1370 | 	} | 
 | 1371 | } | 
 | 1372 |  | 
 | 1373 | void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, | 
 | 1374 | 			bool long_desc, bool details_flag) | 
 | 1375 | { | 
 | 1376 | 	struct perf_pmu *pmu; | 
 | 1377 | 	struct perf_pmu_alias *alias; | 
 | 1378 | 	char buf[1024]; | 
 | 1379 | 	int printed = 0; | 
 | 1380 | 	int len, j; | 
 | 1381 | 	struct sevent *aliases; | 
 | 1382 | 	int numdesc = 0; | 
 | 1383 | 	int columns = pager_get_columns(); | 
 | 1384 | 	char *topic = NULL; | 
 | 1385 |  | 
 | 1386 | 	pmu = NULL; | 
 | 1387 | 	len = 0; | 
 | 1388 | 	while ((pmu = perf_pmu__scan(pmu)) != NULL) { | 
 | 1389 | 		list_for_each_entry(alias, &pmu->aliases, list) | 
 | 1390 | 			len++; | 
 | 1391 | 		if (pmu->selectable) | 
 | 1392 | 			len++; | 
 | 1393 | 	} | 
 | 1394 | 	aliases = zalloc(sizeof(struct sevent) * len); | 
 | 1395 | 	if (!aliases) | 
 | 1396 | 		goto out_enomem; | 
 | 1397 | 	pmu = NULL; | 
 | 1398 | 	j = 0; | 
 | 1399 | 	while ((pmu = perf_pmu__scan(pmu)) != NULL) { | 
 | 1400 | 		list_for_each_entry(alias, &pmu->aliases, list) { | 
 | 1401 | 			char *name = alias->desc ? alias->name : | 
 | 1402 | 				format_alias(buf, sizeof(buf), pmu, alias); | 
 | 1403 | 			bool is_cpu = !strcmp(pmu->name, "cpu"); | 
 | 1404 |  | 
 | 1405 | 			if (event_glob != NULL && | 
 | 1406 | 			    !(strglobmatch_nocase(name, event_glob) || | 
 | 1407 | 			      (!is_cpu && strglobmatch_nocase(alias->name, | 
 | 1408 | 						       event_glob)) || | 
 | 1409 | 			      (alias->topic && | 
 | 1410 | 			       strglobmatch_nocase(alias->topic, event_glob)))) | 
 | 1411 | 				continue; | 
 | 1412 |  | 
 | 1413 | 			if (is_cpu && !name_only && !alias->desc) | 
 | 1414 | 				name = format_alias_or(buf, sizeof(buf), pmu, alias); | 
 | 1415 |  | 
 | 1416 | 			aliases[j].name = name; | 
 | 1417 | 			if (is_cpu && !name_only && !alias->desc) | 
 | 1418 | 				aliases[j].name = format_alias_or(buf, | 
 | 1419 | 								  sizeof(buf), | 
 | 1420 | 								  pmu, alias); | 
 | 1421 | 			aliases[j].name = strdup(aliases[j].name); | 
 | 1422 | 			if (!aliases[j].name) | 
 | 1423 | 				goto out_enomem; | 
 | 1424 |  | 
 | 1425 | 			aliases[j].desc = long_desc ? alias->long_desc : | 
 | 1426 | 						alias->desc; | 
 | 1427 | 			aliases[j].topic = alias->topic; | 
 | 1428 | 			aliases[j].str = alias->str; | 
 | 1429 | 			aliases[j].pmu = pmu->name; | 
 | 1430 | 			aliases[j].metric_expr = alias->metric_expr; | 
 | 1431 | 			aliases[j].metric_name = alias->metric_name; | 
 | 1432 | 			j++; | 
 | 1433 | 		} | 
 | 1434 | 		if (pmu->selectable && | 
 | 1435 | 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { | 
 | 1436 | 			char *s; | 
 | 1437 | 			if (asprintf(&s, "%s//", pmu->name) < 0) | 
 | 1438 | 				goto out_enomem; | 
 | 1439 | 			aliases[j].name = s; | 
 | 1440 | 			j++; | 
 | 1441 | 		} | 
 | 1442 | 	} | 
 | 1443 | 	len = j; | 
 | 1444 | 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent); | 
 | 1445 | 	for (j = 0; j < len; j++) { | 
 | 1446 | 		/* Skip duplicates */ | 
 | 1447 | 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) | 
 | 1448 | 			continue; | 
 | 1449 | 		if (name_only) { | 
 | 1450 | 			printf("%s ", aliases[j].name); | 
 | 1451 | 			continue; | 
 | 1452 | 		} | 
 | 1453 | 		if (aliases[j].desc && !quiet_flag) { | 
 | 1454 | 			if (numdesc++ == 0) | 
 | 1455 | 				printf("\n"); | 
 | 1456 | 			if (aliases[j].topic && (!topic || | 
 | 1457 | 					strcmp(topic, aliases[j].topic))) { | 
 | 1458 | 				printf("%s%s:\n", topic ? "\n" : "", | 
 | 1459 | 						aliases[j].topic); | 
 | 1460 | 				topic = aliases[j].topic; | 
 | 1461 | 			} | 
 | 1462 | 			printf("  %-50s\n", aliases[j].name); | 
 | 1463 | 			printf("%*s", 8, "["); | 
 | 1464 | 			wordwrap(aliases[j].desc, 8, columns, 0); | 
 | 1465 | 			printf("]\n"); | 
 | 1466 | 			if (details_flag) { | 
 | 1467 | 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); | 
 | 1468 | 				if (aliases[j].metric_name) | 
 | 1469 | 					printf(" MetricName: %s", aliases[j].metric_name); | 
 | 1470 | 				if (aliases[j].metric_expr) | 
 | 1471 | 					printf(" MetricExpr: %s", aliases[j].metric_expr); | 
 | 1472 | 				putchar('\n'); | 
 | 1473 | 			} | 
 | 1474 | 		} else | 
 | 1475 | 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name); | 
 | 1476 | 		printed++; | 
 | 1477 | 	} | 
 | 1478 | 	if (printed && pager_in_use()) | 
 | 1479 | 		printf("\n"); | 
 | 1480 | out_free: | 
 | 1481 | 	for (j = 0; j < len; j++) | 
 | 1482 | 		zfree(&aliases[j].name); | 
 | 1483 | 	zfree(&aliases); | 
 | 1484 | 	return; | 
 | 1485 |  | 
 | 1486 | out_enomem: | 
 | 1487 | 	printf("FATAL: not enough memory to print PMU events\n"); | 
 | 1488 | 	if (aliases) | 
 | 1489 | 		goto out_free; | 
 | 1490 | } | 
 | 1491 |  | 
 | 1492 | bool pmu_have_event(const char *pname, const char *name) | 
 | 1493 | { | 
 | 1494 | 	struct perf_pmu *pmu; | 
 | 1495 | 	struct perf_pmu_alias *alias; | 
 | 1496 |  | 
 | 1497 | 	pmu = NULL; | 
 | 1498 | 	while ((pmu = perf_pmu__scan(pmu)) != NULL) { | 
 | 1499 | 		if (strcmp(pname, pmu->name)) | 
 | 1500 | 			continue; | 
 | 1501 | 		list_for_each_entry(alias, &pmu->aliases, list) | 
 | 1502 | 			if (!strcmp(alias->name, name)) | 
 | 1503 | 				return true; | 
 | 1504 | 	} | 
 | 1505 | 	return false; | 
 | 1506 | } | 
 | 1507 |  | 
 | 1508 | static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) | 
 | 1509 | { | 
 | 1510 | 	struct stat st; | 
 | 1511 | 	char path[PATH_MAX]; | 
 | 1512 | 	const char *sysfs; | 
 | 1513 |  | 
 | 1514 | 	sysfs = sysfs__mountpoint(); | 
 | 1515 | 	if (!sysfs) | 
 | 1516 | 		return NULL; | 
 | 1517 |  | 
 | 1518 | 	snprintf(path, PATH_MAX, | 
 | 1519 | 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); | 
 | 1520 |  | 
 | 1521 | 	if (stat(path, &st) < 0) | 
 | 1522 | 		return NULL; | 
 | 1523 |  | 
 | 1524 | 	return fopen(path, "r"); | 
 | 1525 | } | 
 | 1526 |  | 
 | 1527 | int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, | 
 | 1528 | 			...) | 
 | 1529 | { | 
 | 1530 | 	va_list args; | 
 | 1531 | 	FILE *file; | 
 | 1532 | 	int ret = EOF; | 
 | 1533 |  | 
 | 1534 | 	va_start(args, fmt); | 
 | 1535 | 	file = perf_pmu__open_file(pmu, name); | 
 | 1536 | 	if (file) { | 
 | 1537 | 		ret = vfscanf(file, fmt, args); | 
 | 1538 | 		fclose(file); | 
 | 1539 | 	} | 
 | 1540 | 	va_end(args); | 
 | 1541 | 	return ret; | 
 | 1542 | } |