xref: /linux/tools/perf/util/hwmon_pmu.c (revision 1260ed77798502de9c98020040d2995008de10cc)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 #include "counts.h"
3 #include "debug.h"
4 #include "evsel.h"
5 #include "hashmap.h"
6 #include "hwmon_pmu.h"
7 #include "pmu.h"
8 #include <internal/xyarray.h>
9 #include <internal/threadmap.h>
10 #include <perf/threadmap.h>
11 #include <sys/types.h>
12 #include <assert.h>
13 #include <ctype.h>
14 #include <fcntl.h>
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <api/fs/fs.h>
19 #include <api/io.h>
20 #include <api/io_dir.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/zalloc.h>
24 
25 /** Strings that correspond to enum hwmon_type. */
26 static const char * const hwmon_type_strs[HWMON_TYPE_MAX] = {
27 	NULL,
28 	"cpu",
29 	"curr",
30 	"energy",
31 	"fan",
32 	"humidity",
33 	"in",
34 	"intrusion",
35 	"power",
36 	"pwm",
37 	"temp",
38 };
39 #define LONGEST_HWMON_TYPE_STR "intrusion"
40 
41 /** Strings that correspond to enum hwmon_item. */
42 static const char * const hwmon_item_strs[HWMON_ITEM__MAX] = {
43 	NULL,
44 	"accuracy",
45 	"alarm",
46 	"auto_channels_temp",
47 	"average",
48 	"average_highest",
49 	"average_interval",
50 	"average_interval_max",
51 	"average_interval_min",
52 	"average_lowest",
53 	"average_max",
54 	"average_min",
55 	"beep",
56 	"cap",
57 	"cap_hyst",
58 	"cap_max",
59 	"cap_min",
60 	"crit",
61 	"crit_hyst",
62 	"div",
63 	"emergency",
64 	"emergency_hist",
65 	"enable",
66 	"fault",
67 	"freq",
68 	"highest",
69 	"input",
70 	"label",
71 	"lcrit",
72 	"lcrit_hyst",
73 	"lowest",
74 	"max",
75 	"max_hyst",
76 	"min",
77 	"min_hyst",
78 	"mod",
79 	"offset",
80 	"pulses",
81 	"rated_max",
82 	"rated_min",
83 	"reset_history",
84 	"target",
85 	"type",
86 	"vid",
87 };
88 #define LONGEST_HWMON_ITEM_STR "average_interval_max"
89 
90 static const char *const hwmon_units[HWMON_TYPE_MAX] = {
91 	NULL,
92 	"V",   /* cpu */
93 	"A",   /* curr */
94 	"J",   /* energy */
95 	"rpm", /* fan */
96 	"%",   /* humidity */
97 	"V",   /* in */
98 	"",    /* intrusion */
99 	"W",   /* power */
100 	"Hz",  /* pwm */
101 	"'C",  /* temp */
102 };
103 
104 struct hwmon_pmu {
105 	struct perf_pmu pmu;
106 	struct hashmap events;
107 	int hwmon_dir_fd;
108 };
109 
110 /**
111  * struct hwmon_pmu_event_value: Value in hwmon_pmu->events.
112  *
113  * Hwmon files are of the form <type><number>_<item> and may have a suffix
114  * _alarm.
115  */
116 struct hwmon_pmu_event_value {
117 	/** @items: which item files are present. */
118 	DECLARE_BITMAP(items, HWMON_ITEM__MAX);
119 	/** @alarm_items: which item files are present. */
120 	DECLARE_BITMAP(alarm_items, HWMON_ITEM__MAX);
121 	/** @label: contents of <type><number>_label if present. */
122 	char *label;
123 	/** @name: name computed from label of the form <type>_<label>. */
124 	char *name;
125 };
126 
127 bool perf_pmu__is_hwmon(const struct perf_pmu *pmu)
128 {
129 	return pmu && pmu->type >= PERF_PMU_TYPE_HWMON_START &&
130 		pmu->type <= PERF_PMU_TYPE_HWMON_END;
131 }
132 
133 bool evsel__is_hwmon(const struct evsel *evsel)
134 {
135 	return perf_pmu__is_hwmon(evsel->pmu);
136 }
137 
138 static size_t hwmon_pmu__event_hashmap_hash(long key, void *ctx __maybe_unused)
139 {
140 	return ((union hwmon_pmu_event_key)key).type_and_num;
141 }
142 
143 static bool hwmon_pmu__event_hashmap_equal(long key1, long key2, void *ctx __maybe_unused)
144 {
145 	return ((union hwmon_pmu_event_key)key1).type_and_num ==
146 	       ((union hwmon_pmu_event_key)key2).type_and_num;
147 }
148 
149 static int hwmon_strcmp(const void *a, const void *b)
150 {
151 	const char *sa = a;
152 	const char * const *sb = b;
153 
154 	return strcmp(sa, *sb);
155 }
156 
157 bool parse_hwmon_filename(const char *filename,
158 			  enum hwmon_type *type,
159 			  int *number,
160 			  enum hwmon_item *item,
161 			  bool *alarm)
162 {
163 	char fn_type[24];
164 	const char **elem;
165 	const char *fn_item = NULL;
166 	size_t fn_item_len;
167 
168 	assert(strlen(LONGEST_HWMON_TYPE_STR) < sizeof(fn_type));
169 	strlcpy(fn_type, filename, sizeof(fn_type));
170 	for (size_t i = 0; fn_type[i] != '\0'; i++) {
171 		if (fn_type[i] >= '0' && fn_type[i] <= '9') {
172 			fn_type[i] = '\0';
173 			*number = strtoul(&filename[i], (char **)&fn_item, 10);
174 			if (*fn_item == '_')
175 				fn_item++;
176 			break;
177 		}
178 		if (fn_type[i] == '_') {
179 			fn_type[i] = '\0';
180 			*number = -1;
181 			fn_item = &filename[i + 1];
182 			break;
183 		}
184 	}
185 	if (fn_item == NULL || fn_type[0] == '\0' || (item != NULL && fn_item[0] == '\0')) {
186 		pr_debug3("hwmon_pmu: not a hwmon file '%s'\n", filename);
187 		return false;
188 	}
189 	elem = bsearch(&fn_type, hwmon_type_strs + 1, ARRAY_SIZE(hwmon_type_strs) - 1,
190 		       sizeof(hwmon_type_strs[0]), hwmon_strcmp);
191 	if (!elem) {
192 		pr_debug3("hwmon_pmu: not a hwmon type '%s' in file name '%s'\n",
193 			 fn_type, filename);
194 		return false;
195 	}
196 
197 	*type = elem - &hwmon_type_strs[0];
198 	if (!item)
199 		return true;
200 
201 	*alarm = false;
202 	fn_item_len = strlen(fn_item);
203 	if (fn_item_len > 6 && !strcmp(&fn_item[fn_item_len - 6], "_alarm")) {
204 		assert(strlen(LONGEST_HWMON_ITEM_STR) < sizeof(fn_type));
205 		strlcpy(fn_type, fn_item, fn_item_len - 5);
206 		fn_item = fn_type;
207 		*alarm = true;
208 	}
209 	elem = bsearch(fn_item, hwmon_item_strs + 1, ARRAY_SIZE(hwmon_item_strs) - 1,
210 		       sizeof(hwmon_item_strs[0]), hwmon_strcmp);
211 	if (!elem) {
212 		pr_debug3("hwmon_pmu: not a hwmon item '%s' in file name '%s'\n",
213 			 fn_item, filename);
214 		return false;
215 	}
216 	*item = elem - &hwmon_item_strs[0];
217 	return true;
218 }
219 
220 static void fix_name(char *p)
221 {
222 	char *s = strchr(p, '\n');
223 
224 	if (s)
225 		*s = '\0';
226 
227 	while (*p != '\0') {
228 		if (strchr(" :,/\n\t", *p))
229 			*p = '_';
230 		else
231 			*p = tolower(*p);
232 		p++;
233 	}
234 }
235 
236 static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
237 {
238 	int err = 0;
239 	struct hashmap_entry *cur, *tmp;
240 	size_t bkt;
241 	struct io_dirent64 *ent;
242 	struct io_dir dir;
243 
244 	if (pmu->pmu.sysfs_aliases_loaded)
245 		return 0;
246 
247 	/* Use openat so that the directory contents are refreshed. */
248 	io_dir__init(&dir, openat(pmu->hwmon_dir_fd, ".", O_CLOEXEC | O_DIRECTORY | O_RDONLY));
249 
250 	if (dir.dirfd < 0)
251 		return -ENOENT;
252 
253 	while ((ent = io_dir__readdir(&dir)) != NULL) {
254 		enum hwmon_type type;
255 		int number;
256 		enum hwmon_item item;
257 		bool alarm;
258 		union hwmon_pmu_event_key key = { .type_and_num = 0 };
259 		struct hwmon_pmu_event_value *value;
260 
261 		if (ent->d_type != DT_REG)
262 			continue;
263 
264 		if (!parse_hwmon_filename(ent->d_name, &type, &number, &item, &alarm)) {
265 			pr_debug3("Not a hwmon file '%s'\n", ent->d_name);
266 			continue;
267 		}
268 		key.num = number;
269 		key.type = type;
270 		if (!hashmap__find(&pmu->events, key.type_and_num, &value)) {
271 			value = zalloc(sizeof(*value));
272 			if (!value) {
273 				err = -ENOMEM;
274 				goto err_out;
275 			}
276 			err = hashmap__add(&pmu->events, key.type_and_num, value);
277 			if (err) {
278 				free(value);
279 				err = -ENOMEM;
280 				goto err_out;
281 			}
282 		}
283 		__set_bit(item, alarm ? value->alarm_items : value->items);
284 		if (item == HWMON_ITEM_LABEL) {
285 			char buf[128];
286 			int fd = openat(pmu->hwmon_dir_fd, ent->d_name, O_RDONLY);
287 			ssize_t read_len;
288 
289 			if (fd < 0)
290 				continue;
291 
292 			read_len = read(fd, buf, sizeof(buf));
293 
294 			while (read_len > 0 && buf[read_len - 1] == '\n')
295 				read_len--;
296 
297 			if (read_len > 0)
298 				buf[read_len] = '\0';
299 
300 			if (buf[0] == '\0') {
301 				pr_debug("hwmon_pmu: empty label file %s %s\n",
302 					 pmu->pmu.name, ent->d_name);
303 				close(fd);
304 				continue;
305 			}
306 			value->label = strdup(buf);
307 			if (!value->label) {
308 				pr_debug("hwmon_pmu: memory allocation failure\n");
309 				close(fd);
310 				continue;
311 			}
312 			snprintf(buf, sizeof(buf), "%s_%s", hwmon_type_strs[type], value->label);
313 			fix_name(buf);
314 			value->name = strdup(buf);
315 			if (!value->name)
316 				pr_debug("hwmon_pmu: memory allocation failure\n");
317 			close(fd);
318 		}
319 	}
320 	if (hashmap__size(&pmu->events) == 0)
321 		pr_debug2("hwmon_pmu: %s has no events\n", pmu->pmu.name);
322 
323 	hashmap__for_each_entry_safe((&pmu->events), cur, tmp, bkt) {
324 		union hwmon_pmu_event_key key = {
325 			.type_and_num = cur->key,
326 		};
327 		struct hwmon_pmu_event_value *value = cur->pvalue;
328 
329 		if (!test_bit(HWMON_ITEM_INPUT, value->items)) {
330 			pr_debug("hwmon_pmu: %s removing event '%s%d' that has no input file\n",
331 				pmu->pmu.name, hwmon_type_strs[key.type], key.num);
332 			hashmap__delete(&pmu->events, key.type_and_num, &key, &value);
333 			zfree(&value->label);
334 			zfree(&value->name);
335 			free(value);
336 		}
337 	}
338 	pmu->pmu.sysfs_aliases_loaded = true;
339 
340 err_out:
341 	close(dir.dirfd);
342 	return err;
343 }
344 
345 struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, int hwmon_dir, const char *sysfs_name, const char *name)
346 {
347 	char buf[32];
348 	struct hwmon_pmu *hwm;
349 
350 	hwm = zalloc(sizeof(*hwm));
351 	if (!hwm)
352 		return NULL;
353 
354 	hwm->hwmon_dir_fd = hwmon_dir;
355 	hwm->pmu.type = PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NULL, 10);
356 	if (hwm->pmu.type > PERF_PMU_TYPE_HWMON_END) {
357 		pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_name);
358 		goto err_out;
359 	}
360 	snprintf(buf, sizeof(buf), "hwmon_%s", name);
361 	fix_name(buf + 6);
362 	hwm->pmu.name = strdup(buf);
363 	if (!hwm->pmu.name)
364 		goto err_out;
365 	hwm->pmu.alias_name = strdup(sysfs_name);
366 	if (!hwm->pmu.alias_name)
367 		goto err_out;
368 	hwm->pmu.cpus = perf_cpu_map__new("0");
369 	if (!hwm->pmu.cpus)
370 		goto err_out;
371 	INIT_LIST_HEAD(&hwm->pmu.format);
372 	INIT_LIST_HEAD(&hwm->pmu.aliases);
373 	INIT_LIST_HEAD(&hwm->pmu.caps);
374 	hashmap__init(&hwm->events, hwmon_pmu__event_hashmap_hash,
375 		      hwmon_pmu__event_hashmap_equal, /*ctx=*/NULL);
376 
377 	list_add_tail(&hwm->pmu.list, pmus);
378 	return &hwm->pmu;
379 err_out:
380 	free((char *)hwm->pmu.name);
381 	free(hwm->pmu.alias_name);
382 	free(hwm);
383 	close(hwmon_dir);
384 	return NULL;
385 }
386 
387 void hwmon_pmu__exit(struct perf_pmu *pmu)
388 {
389 	struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
390 	struct hashmap_entry *cur, *tmp;
391 	size_t bkt;
392 
393 	hashmap__for_each_entry_safe((&hwm->events), cur, tmp, bkt) {
394 		struct hwmon_pmu_event_value *value = cur->pvalue;
395 
396 		zfree(&value->label);
397 		zfree(&value->name);
398 		free(value);
399 	}
400 	hashmap__clear(&hwm->events);
401 	close(hwm->hwmon_dir_fd);
402 }
403 
404 static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, size_t out_buf_len,
405 					union hwmon_pmu_event_key key,
406 					const unsigned long *items, bool is_alarm)
407 {
408 	size_t bit;
409 	char buf[64];
410 	size_t len = 0;
411 
412 	for_each_set_bit(bit, items, HWMON_ITEM__MAX) {
413 		int fd;
414 
415 		if (bit == HWMON_ITEM_LABEL || bit == HWMON_ITEM_INPUT)
416 			continue;
417 
418 		snprintf(buf, sizeof(buf), "%s%d_%s%s",
419 			hwmon_type_strs[key.type],
420 			key.num,
421 			hwmon_item_strs[bit],
422 			is_alarm ? "_alarm" : "");
423 		fd = openat(hwm->hwmon_dir_fd, buf, O_RDONLY);
424 		if (fd > 0) {
425 			ssize_t read_len = read(fd, buf, sizeof(buf));
426 
427 			while (read_len > 0 && buf[read_len - 1] == '\n')
428 				read_len--;
429 
430 			if (read_len > 0) {
431 				long long val;
432 
433 				buf[read_len] = '\0';
434 				val = strtoll(buf, /*endptr=*/NULL, 10);
435 				len += snprintf(out_buf + len, out_buf_len - len, "%s%s%s=%g%s",
436 						len == 0 ? " " : ", ",
437 						hwmon_item_strs[bit],
438 						is_alarm ? "_alarm" : "",
439 						(double)val / 1000.0,
440 						hwmon_units[key.type]);
441 			}
442 			close(fd);
443 		}
444 	}
445 	return len;
446 }
447 
448 int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb)
449 {
450 	struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
451 	struct hashmap_entry *cur;
452 	size_t bkt;
453 
454 	if (hwmon_pmu__read_events(hwm))
455 		return false;
456 
457 	hashmap__for_each_entry((&hwm->events), cur, bkt) {
458 		static const char *const hwmon_scale_units[HWMON_TYPE_MAX] = {
459 			NULL,
460 			"0.001V", /* cpu */
461 			"0.001A", /* curr */
462 			"0.001J", /* energy */
463 			"1rpm",   /* fan */
464 			"0.001%", /* humidity */
465 			"0.001V", /* in */
466 			NULL,     /* intrusion */
467 			"0.001W", /* power */
468 			"1Hz",    /* pwm */
469 			"0.001'C", /* temp */
470 		};
471 		static const char *const hwmon_desc[HWMON_TYPE_MAX] = {
472 			NULL,
473 			"CPU core reference voltage",   /* cpu */
474 			"Current",                      /* curr */
475 			"Cumulative energy use",        /* energy */
476 			"Fan",                          /* fan */
477 			"Humidity",                     /* humidity */
478 			"Voltage",                      /* in */
479 			"Chassis intrusion detection",  /* intrusion */
480 			"Power use",                    /* power */
481 			"Pulse width modulation fan control", /* pwm */
482 			"Temperature",                  /* temp */
483 		};
484 		char alias_buf[64];
485 		char desc_buf[256];
486 		char encoding_buf[128];
487 		union hwmon_pmu_event_key key = {
488 			.type_and_num = cur->key,
489 		};
490 		struct hwmon_pmu_event_value *value = cur->pvalue;
491 		struct pmu_event_info info = {
492 			.pmu = pmu,
493 			.name = value->name,
494 			.alias = alias_buf,
495 			.scale_unit = hwmon_scale_units[key.type],
496 			.desc = desc_buf,
497 			.long_desc = NULL,
498 			.encoding_desc = encoding_buf,
499 			.topic = "hwmon",
500 			.pmu_name = pmu->name,
501 			.event_type_desc = "Hwmon event",
502 		};
503 		int ret;
504 		size_t len;
505 
506 		len = snprintf(alias_buf, sizeof(alias_buf), "%s%d",
507 			       hwmon_type_strs[key.type], key.num);
508 		if (!info.name) {
509 			info.name = info.alias;
510 			info.alias = NULL;
511 		}
512 
513 		len = snprintf(desc_buf, sizeof(desc_buf), "%s in unit %s named %s.",
514 			hwmon_desc[key.type],
515 			pmu->name + 6,
516 			value->label ?: info.name);
517 
518 		len += hwmon_pmu__describe_items(hwm, desc_buf + len, sizeof(desc_buf) - len,
519 						key, value->items, /*is_alarm=*/false);
520 
521 		len += hwmon_pmu__describe_items(hwm, desc_buf + len, sizeof(desc_buf) - len,
522 						key, value->alarm_items, /*is_alarm=*/true);
523 
524 		snprintf(encoding_buf, sizeof(encoding_buf), "%s/config=0x%lx/",
525 			 pmu->name, cur->key);
526 
527 		ret = cb(state, &info);
528 		if (ret)
529 			return ret;
530 	}
531 	return 0;
532 }
533 
534 size_t hwmon_pmu__num_events(struct perf_pmu *pmu)
535 {
536 	struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
537 
538 	hwmon_pmu__read_events(hwm);
539 	return hashmap__size(&hwm->events);
540 }
541 
542 bool hwmon_pmu__have_event(struct perf_pmu *pmu, const char *name)
543 {
544 	struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
545 	enum hwmon_type type;
546 	int number;
547 	union hwmon_pmu_event_key key = { .type_and_num = 0 };
548 	struct hashmap_entry *cur;
549 	size_t bkt;
550 
551 	if (!parse_hwmon_filename(name, &type, &number, /*item=*/NULL, /*is_alarm=*/NULL))
552 		return false;
553 
554 	if (hwmon_pmu__read_events(hwm))
555 		return false;
556 
557 	key.type = type;
558 	key.num = number;
559 	if (hashmap_find(&hwm->events, key.type_and_num, /*value=*/NULL))
560 		return true;
561 	if (key.num != -1)
562 		return false;
563 	/* Item is of form <type>_ which means we should match <type>_<label>. */
564 	hashmap__for_each_entry((&hwm->events), cur, bkt) {
565 		struct hwmon_pmu_event_value *value = cur->pvalue;
566 
567 		key.type_and_num = cur->key;
568 		if (key.type == type && value->name && !strcasecmp(name, value->name))
569 			return true;
570 	}
571 	return false;
572 }
573 
574 static int hwmon_pmu__config_term(const struct hwmon_pmu *hwm,
575 				  struct perf_event_attr *attr,
576 				  struct parse_events_term *term,
577 				  struct parse_events_error *err)
578 {
579 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) {
580 		enum hwmon_type type;
581 		int number;
582 
583 		if (parse_hwmon_filename(term->config, &type, &number,
584 					 /*item=*/NULL, /*is_alarm=*/NULL)) {
585 			if (number == -1) {
586 				/*
587 				 * Item is of form <type>_ which means we should
588 				 * match <type>_<label>.
589 				 */
590 				struct hashmap_entry *cur;
591 				size_t bkt;
592 
593 				attr->config = 0;
594 				hashmap__for_each_entry((&hwm->events), cur, bkt) {
595 					union hwmon_pmu_event_key key = {
596 						.type_and_num = cur->key,
597 					};
598 					struct hwmon_pmu_event_value *value = cur->pvalue;
599 
600 					if (key.type == type && value->name &&
601 					    !strcasecmp(term->config, value->name)) {
602 						attr->config = key.type_and_num;
603 						break;
604 					}
605 				}
606 				if (attr->config == 0)
607 					return -EINVAL;
608 			} else {
609 				union hwmon_pmu_event_key key = {
610 					.type_and_num = 0,
611 				};
612 
613 				key.type = type;
614 				key.num = number;
615 				attr->config = key.type_and_num;
616 			}
617 			return 0;
618 		}
619 	}
620 	if (err) {
621 		char *err_str;
622 
623 		parse_events_error__handle(err, term->err_val,
624 					asprintf(&err_str,
625 						"unexpected hwmon event term (%s) %s",
626 						parse_events__term_type_str(term->type_term),
627 						term->config) < 0
628 					? strdup("unexpected hwmon event term")
629 					: err_str,
630 					NULL);
631 	}
632 	return -EINVAL;
633 }
634 
635 int hwmon_pmu__config_terms(const struct perf_pmu *pmu,
636 			    struct perf_event_attr *attr,
637 			    struct parse_events_terms *terms,
638 			    struct parse_events_error *err)
639 {
640 	struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
641 	struct parse_events_term *term;
642 	int ret;
643 
644 	ret = hwmon_pmu__read_events(hwm);
645 	if (ret)
646 		return ret;
647 
648 	list_for_each_entry(term, &terms->terms, list) {
649 		if (hwmon_pmu__config_term(hwm, attr, term, err))
650 			return -EINVAL;
651 	}
652 
653 	return 0;
654 
655 }
656 
657 int hwmon_pmu__check_alias(struct parse_events_terms *terms, struct perf_pmu_info *info,
658 			   struct parse_events_error *err)
659 {
660 	struct parse_events_term *term =
661 		list_first_entry(&terms->terms, struct parse_events_term, list);
662 
663 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) {
664 		enum hwmon_type type;
665 		int number;
666 
667 		if (parse_hwmon_filename(term->config, &type, &number,
668 					 /*item=*/NULL, /*is_alarm=*/NULL)) {
669 			info->unit = hwmon_units[type];
670 			if (type == HWMON_TYPE_FAN || type == HWMON_TYPE_PWM ||
671 			    type == HWMON_TYPE_INTRUSION)
672 				info->scale = 1;
673 			else
674 				info->scale = 0.001;
675 		}
676 		return 0;
677 	}
678 	if (err) {
679 		char *err_str;
680 
681 		parse_events_error__handle(err, term->err_val,
682 					asprintf(&err_str,
683 						"unexpected hwmon event term (%s) %s",
684 						parse_events__term_type_str(term->type_term),
685 						term->config) < 0
686 					? strdup("unexpected hwmon event term")
687 					: err_str,
688 					NULL);
689 	}
690 	return -EINVAL;
691 }
692 
693 int perf_pmus__read_hwmon_pmus(struct list_head *pmus)
694 {
695 	char *line = NULL;
696 	struct io_dirent64 *class_hwmon_ent;
697 	struct io_dir class_hwmon_dir;
698 	char buf[PATH_MAX];
699 	const char *sysfs = sysfs__mountpoint();
700 
701 	if (!sysfs)
702 		return 0;
703 
704 	scnprintf(buf, sizeof(buf), "%s/class/hwmon/", sysfs);
705 	io_dir__init(&class_hwmon_dir, open(buf, O_CLOEXEC | O_DIRECTORY | O_RDONLY));
706 
707 	if (class_hwmon_dir.dirfd < 0)
708 		return 0;
709 
710 	while ((class_hwmon_ent = io_dir__readdir(&class_hwmon_dir)) != NULL) {
711 		size_t line_len;
712 		int hwmon_dir, name_fd;
713 		struct io io;
714 
715 		if (class_hwmon_ent->d_type != DT_LNK)
716 			continue;
717 
718 		scnprintf(buf, sizeof(buf), "%s/class/hwmon/%s", sysfs, class_hwmon_ent->d_name);
719 		hwmon_dir = open(buf, O_DIRECTORY);
720 		if (hwmon_dir == -1) {
721 			pr_debug("hwmon_pmu: not a directory: '%s/class/hwmon/%s'\n",
722 				 sysfs, class_hwmon_ent->d_name);
723 			continue;
724 		}
725 		name_fd = openat(hwmon_dir, "name", O_RDONLY);
726 		if (name_fd == -1) {
727 			pr_debug("hwmon_pmu: failure to open '%s/class/hwmon/%s/name'\n",
728 				  sysfs, class_hwmon_ent->d_name);
729 			close(hwmon_dir);
730 			continue;
731 		}
732 		io__init(&io, name_fd, buf, sizeof(buf));
733 		io__getline(&io, &line, &line_len);
734 		if (line_len > 0 && line[line_len - 1] == '\n')
735 			line[line_len - 1] = '\0';
736 		hwmon_pmu__new(pmus, hwmon_dir, class_hwmon_ent->d_name, line);
737 		close(name_fd);
738 	}
739 	free(line);
740 	close(class_hwmon_dir.dirfd);
741 	return 0;
742 }
743 
744 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
745 
746 int evsel__hwmon_pmu_open(struct evsel *evsel,
747 			  struct perf_thread_map *threads,
748 			  int start_cpu_map_idx, int end_cpu_map_idx)
749 {
750 	struct hwmon_pmu *hwm = container_of(evsel->pmu, struct hwmon_pmu, pmu);
751 	union hwmon_pmu_event_key key = {
752 		.type_and_num = evsel->core.attr.config,
753 	};
754 	int idx = 0, thread = 0, nthreads, err = 0;
755 
756 	nthreads = perf_thread_map__nr(threads);
757 	for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) {
758 		for (thread = 0; thread < nthreads; thread++) {
759 			char buf[64];
760 			int fd;
761 
762 			snprintf(buf, sizeof(buf), "%s%d_input",
763 				 hwmon_type_strs[key.type], key.num);
764 
765 			fd = openat(hwm->hwmon_dir_fd, buf, O_RDONLY);
766 			FD(evsel, idx, thread) = fd;
767 			if (fd < 0) {
768 				err = -errno;
769 				goto out_close;
770 			}
771 		}
772 	}
773 	return 0;
774 out_close:
775 	if (err)
776 		threads->err_thread = thread;
777 
778 	do {
779 		while (--thread >= 0) {
780 			if (FD(evsel, idx, thread) >= 0)
781 				close(FD(evsel, idx, thread));
782 			FD(evsel, idx, thread) = -1;
783 		}
784 		thread = nthreads;
785 	} while (--idx >= 0);
786 	return err;
787 }
788 
789 int evsel__hwmon_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
790 {
791 	char buf[32];
792 	int fd;
793 	ssize_t len;
794 	struct perf_counts_values *count, *old_count = NULL;
795 
796 	if (evsel->prev_raw_counts)
797 		old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread);
798 
799 	count = perf_counts(evsel->counts, cpu_map_idx, thread);
800 	fd = FD(evsel, cpu_map_idx, thread);
801 	len = pread(fd, buf, sizeof(buf), 0);
802 	if (len <= 0) {
803 		count->lost++;
804 		return -EINVAL;
805 	}
806 	buf[len] = '\0';
807 	if (old_count) {
808 		count->val = old_count->val + strtoll(buf, NULL, 10);
809 		count->run = old_count->run + 1;
810 		count->ena = old_count->ena + 1;
811 	} else {
812 		count->val = strtoll(buf, NULL, 10);
813 		count->run++;
814 		count->ena++;
815 	}
816 	return 0;
817 }
818