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 <dirent.h>
15 #include <fcntl.h>
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <api/fs/fs.h>
20 #include <api/io.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
perf_pmu__is_hwmon(const struct perf_pmu * pmu)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
evsel__is_hwmon(const struct evsel * evsel)133 bool evsel__is_hwmon(const struct evsel *evsel)
134 {
135 return perf_pmu__is_hwmon(evsel->pmu);
136 }
137
hwmon_pmu__event_hashmap_hash(long key,void * ctx __maybe_unused)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
hwmon_pmu__event_hashmap_equal(long key1,long key2,void * ctx __maybe_unused)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
hwmon_strcmp(const void * a,const void * b)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
parse_hwmon_filename(const char * filename,enum hwmon_type * type,int * number,enum hwmon_item * item,bool * alarm)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
fix_name(char * p)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
hwmon_pmu__read_events(struct hwmon_pmu * pmu)236 static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
237 {
238 DIR *dir;
239 struct dirent *ent;
240 int dup_fd, err = 0;
241 struct hashmap_entry *cur, *tmp;
242 size_t bkt;
243
244 if (pmu->pmu.sysfs_aliases_loaded)
245 return 0;
246
247 /*
248 * Use a dup-ed fd as closedir will close it. Use openat so that the
249 * directory contents are refreshed.
250 */
251 dup_fd = openat(pmu->hwmon_dir_fd, ".", O_DIRECTORY);
252
253 if (dup_fd == -1)
254 return -ENOMEM;
255
256 dir = fdopendir(dup_fd);
257 if (!dir) {
258 close(dup_fd);
259 return -ENOMEM;
260 }
261
262 while ((ent = readdir(dir)) != NULL) {
263 enum hwmon_type type;
264 int number;
265 enum hwmon_item item;
266 bool alarm;
267 union hwmon_pmu_event_key key = { .type_and_num = 0 };
268 struct hwmon_pmu_event_value *value;
269
270 if (ent->d_type != DT_REG)
271 continue;
272
273 if (!parse_hwmon_filename(ent->d_name, &type, &number, &item, &alarm)) {
274 pr_debug3("Not a hwmon file '%s'\n", ent->d_name);
275 continue;
276 }
277 key.num = number;
278 key.type = type;
279 if (!hashmap__find(&pmu->events, key.type_and_num, &value)) {
280 value = zalloc(sizeof(*value));
281 if (!value) {
282 err = -ENOMEM;
283 goto err_out;
284 }
285 err = hashmap__add(&pmu->events, key.type_and_num, value);
286 if (err) {
287 free(value);
288 err = -ENOMEM;
289 goto err_out;
290 }
291 }
292 __set_bit(item, alarm ? value->alarm_items : value->items);
293 if (item == HWMON_ITEM_LABEL) {
294 char buf[128];
295 int fd = openat(pmu->hwmon_dir_fd, ent->d_name, O_RDONLY);
296 ssize_t read_len;
297
298 if (fd < 0)
299 continue;
300
301 read_len = read(fd, buf, sizeof(buf));
302
303 while (read_len > 0 && buf[read_len - 1] == '\n')
304 read_len--;
305
306 if (read_len > 0)
307 buf[read_len] = '\0';
308
309 if (buf[0] == '\0') {
310 pr_debug("hwmon_pmu: empty label file %s %s\n",
311 pmu->pmu.name, ent->d_name);
312 close(fd);
313 continue;
314 }
315 value->label = strdup(buf);
316 if (!value->label) {
317 pr_debug("hwmon_pmu: memory allocation failure\n");
318 close(fd);
319 continue;
320 }
321 snprintf(buf, sizeof(buf), "%s_%s", hwmon_type_strs[type], value->label);
322 fix_name(buf);
323 value->name = strdup(buf);
324 if (!value->name)
325 pr_debug("hwmon_pmu: memory allocation failure\n");
326 close(fd);
327 }
328 }
329 if (hashmap__size(&pmu->events) == 0)
330 pr_debug2("hwmon_pmu: %s has no events\n", pmu->pmu.name);
331
332 hashmap__for_each_entry_safe((&pmu->events), cur, tmp, bkt) {
333 union hwmon_pmu_event_key key = {
334 .type_and_num = cur->key,
335 };
336 struct hwmon_pmu_event_value *value = cur->pvalue;
337
338 if (!test_bit(HWMON_ITEM_INPUT, value->items)) {
339 pr_debug("hwmon_pmu: %s removing event '%s%d' that has no input file\n",
340 pmu->pmu.name, hwmon_type_strs[key.type], key.num);
341 hashmap__delete(&pmu->events, key.type_and_num, &key, &value);
342 zfree(&value->label);
343 zfree(&value->name);
344 free(value);
345 }
346 }
347 pmu->pmu.sysfs_aliases_loaded = true;
348
349 err_out:
350 closedir(dir);
351 return err;
352 }
353
hwmon_pmu__new(struct list_head * pmus,int hwmon_dir,const char * sysfs_name,const char * name)354 struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, int hwmon_dir, const char *sysfs_name, const char *name)
355 {
356 char buf[32];
357 struct hwmon_pmu *hwm;
358
359 hwm = zalloc(sizeof(*hwm));
360 if (!hwm)
361 return NULL;
362
363 hwm->hwmon_dir_fd = hwmon_dir;
364 hwm->pmu.type = PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NULL, 10);
365 if (hwm->pmu.type > PERF_PMU_TYPE_HWMON_END) {
366 pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_name);
367 goto err_out;
368 }
369 snprintf(buf, sizeof(buf), "hwmon_%s", name);
370 fix_name(buf + 6);
371 hwm->pmu.name = strdup(buf);
372 if (!hwm->pmu.name)
373 goto err_out;
374 hwm->pmu.alias_name = strdup(sysfs_name);
375 if (!hwm->pmu.alias_name)
376 goto err_out;
377 hwm->pmu.cpus = perf_cpu_map__new("0");
378 if (!hwm->pmu.cpus)
379 goto err_out;
380 INIT_LIST_HEAD(&hwm->pmu.format);
381 INIT_LIST_HEAD(&hwm->pmu.aliases);
382 INIT_LIST_HEAD(&hwm->pmu.caps);
383 hashmap__init(&hwm->events, hwmon_pmu__event_hashmap_hash,
384 hwmon_pmu__event_hashmap_equal, /*ctx=*/NULL);
385
386 list_add_tail(&hwm->pmu.list, pmus);
387 return &hwm->pmu;
388 err_out:
389 free((char *)hwm->pmu.name);
390 free(hwm->pmu.alias_name);
391 free(hwm);
392 close(hwmon_dir);
393 return NULL;
394 }
395
hwmon_pmu__exit(struct perf_pmu * pmu)396 void hwmon_pmu__exit(struct perf_pmu *pmu)
397 {
398 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
399 struct hashmap_entry *cur, *tmp;
400 size_t bkt;
401
402 hashmap__for_each_entry_safe((&hwm->events), cur, tmp, bkt) {
403 struct hwmon_pmu_event_value *value = cur->pvalue;
404
405 zfree(&value->label);
406 zfree(&value->name);
407 free(value);
408 }
409 hashmap__clear(&hwm->events);
410 close(hwm->hwmon_dir_fd);
411 }
412
hwmon_pmu__describe_items(struct hwmon_pmu * hwm,char * out_buf,size_t out_buf_len,union hwmon_pmu_event_key key,const unsigned long * items,bool is_alarm)413 static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, size_t out_buf_len,
414 union hwmon_pmu_event_key key,
415 const unsigned long *items, bool is_alarm)
416 {
417 size_t bit;
418 char buf[64];
419 size_t len = 0;
420
421 for_each_set_bit(bit, items, HWMON_ITEM__MAX) {
422 int fd;
423
424 if (bit == HWMON_ITEM_LABEL || bit == HWMON_ITEM_INPUT)
425 continue;
426
427 snprintf(buf, sizeof(buf), "%s%d_%s%s",
428 hwmon_type_strs[key.type],
429 key.num,
430 hwmon_item_strs[bit],
431 is_alarm ? "_alarm" : "");
432 fd = openat(hwm->hwmon_dir_fd, buf, O_RDONLY);
433 if (fd > 0) {
434 ssize_t read_len = read(fd, buf, sizeof(buf));
435
436 while (read_len > 0 && buf[read_len - 1] == '\n')
437 read_len--;
438
439 if (read_len > 0) {
440 long long val;
441
442 buf[read_len] = '\0';
443 val = strtoll(buf, /*endptr=*/NULL, 10);
444 len += snprintf(out_buf + len, out_buf_len - len, "%s%s%s=%g%s",
445 len == 0 ? " " : ", ",
446 hwmon_item_strs[bit],
447 is_alarm ? "_alarm" : "",
448 (double)val / 1000.0,
449 hwmon_units[key.type]);
450 }
451 close(fd);
452 }
453 }
454 return len;
455 }
456
hwmon_pmu__for_each_event(struct perf_pmu * pmu,void * state,pmu_event_callback cb)457 int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb)
458 {
459 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
460 struct hashmap_entry *cur;
461 size_t bkt;
462
463 if (hwmon_pmu__read_events(hwm))
464 return false;
465
466 hashmap__for_each_entry((&hwm->events), cur, bkt) {
467 static const char *const hwmon_scale_units[HWMON_TYPE_MAX] = {
468 NULL,
469 "0.001V", /* cpu */
470 "0.001A", /* curr */
471 "0.001J", /* energy */
472 "1rpm", /* fan */
473 "0.001%", /* humidity */
474 "0.001V", /* in */
475 NULL, /* intrusion */
476 "0.001W", /* power */
477 "1Hz", /* pwm */
478 "0.001'C", /* temp */
479 };
480 static const char *const hwmon_desc[HWMON_TYPE_MAX] = {
481 NULL,
482 "CPU core reference voltage", /* cpu */
483 "Current", /* curr */
484 "Cumulative energy use", /* energy */
485 "Fan", /* fan */
486 "Humidity", /* humidity */
487 "Voltage", /* in */
488 "Chassis intrusion detection", /* intrusion */
489 "Power use", /* power */
490 "Pulse width modulation fan control", /* pwm */
491 "Temperature", /* temp */
492 };
493 char alias_buf[64];
494 char desc_buf[256];
495 char encoding_buf[128];
496 union hwmon_pmu_event_key key = {
497 .type_and_num = cur->key,
498 };
499 struct hwmon_pmu_event_value *value = cur->pvalue;
500 struct pmu_event_info info = {
501 .pmu = pmu,
502 .name = value->name,
503 .alias = alias_buf,
504 .scale_unit = hwmon_scale_units[key.type],
505 .desc = desc_buf,
506 .long_desc = NULL,
507 .encoding_desc = encoding_buf,
508 .topic = "hwmon",
509 .pmu_name = pmu->name,
510 .event_type_desc = "Hwmon event",
511 };
512 int ret;
513 size_t len;
514
515 len = snprintf(alias_buf, sizeof(alias_buf), "%s%d",
516 hwmon_type_strs[key.type], key.num);
517 if (!info.name) {
518 info.name = info.alias;
519 info.alias = NULL;
520 }
521
522 len = snprintf(desc_buf, sizeof(desc_buf), "%s in unit %s named %s.",
523 hwmon_desc[key.type],
524 pmu->name + 6,
525 value->label ?: info.name);
526
527 len += hwmon_pmu__describe_items(hwm, desc_buf + len, sizeof(desc_buf) - len,
528 key, value->items, /*is_alarm=*/false);
529
530 len += hwmon_pmu__describe_items(hwm, desc_buf + len, sizeof(desc_buf) - len,
531 key, value->alarm_items, /*is_alarm=*/true);
532
533 snprintf(encoding_buf, sizeof(encoding_buf), "%s/config=0x%lx/",
534 pmu->name, cur->key);
535
536 ret = cb(state, &info);
537 if (ret)
538 return ret;
539 }
540 return 0;
541 }
542
hwmon_pmu__num_events(struct perf_pmu * pmu)543 size_t hwmon_pmu__num_events(struct perf_pmu *pmu)
544 {
545 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
546
547 hwmon_pmu__read_events(hwm);
548 return hashmap__size(&hwm->events);
549 }
550
hwmon_pmu__have_event(struct perf_pmu * pmu,const char * name)551 bool hwmon_pmu__have_event(struct perf_pmu *pmu, const char *name)
552 {
553 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
554 enum hwmon_type type;
555 int number;
556 union hwmon_pmu_event_key key = { .type_and_num = 0 };
557 struct hashmap_entry *cur;
558 size_t bkt;
559
560 if (!parse_hwmon_filename(name, &type, &number, /*item=*/NULL, /*is_alarm=*/NULL))
561 return false;
562
563 if (hwmon_pmu__read_events(hwm))
564 return false;
565
566 key.type = type;
567 key.num = number;
568 if (hashmap_find(&hwm->events, key.type_and_num, /*value=*/NULL))
569 return true;
570 if (key.num != -1)
571 return false;
572 /* Item is of form <type>_ which means we should match <type>_<label>. */
573 hashmap__for_each_entry((&hwm->events), cur, bkt) {
574 struct hwmon_pmu_event_value *value = cur->pvalue;
575
576 key.type_and_num = cur->key;
577 if (key.type == type && value->name && !strcasecmp(name, value->name))
578 return true;
579 }
580 return false;
581 }
582
hwmon_pmu__config_term(const struct hwmon_pmu * hwm,struct perf_event_attr * attr,struct parse_events_term * term,struct parse_events_error * err)583 static int hwmon_pmu__config_term(const struct hwmon_pmu *hwm,
584 struct perf_event_attr *attr,
585 struct parse_events_term *term,
586 struct parse_events_error *err)
587 {
588 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) {
589 enum hwmon_type type;
590 int number;
591
592 if (parse_hwmon_filename(term->config, &type, &number,
593 /*item=*/NULL, /*is_alarm=*/NULL)) {
594 if (number == -1) {
595 /*
596 * Item is of form <type>_ which means we should
597 * match <type>_<label>.
598 */
599 struct hashmap_entry *cur;
600 size_t bkt;
601
602 attr->config = 0;
603 hashmap__for_each_entry((&hwm->events), cur, bkt) {
604 union hwmon_pmu_event_key key = {
605 .type_and_num = cur->key,
606 };
607 struct hwmon_pmu_event_value *value = cur->pvalue;
608
609 if (key.type == type && value->name &&
610 !strcasecmp(term->config, value->name)) {
611 attr->config = key.type_and_num;
612 break;
613 }
614 }
615 if (attr->config == 0)
616 return -EINVAL;
617 } else {
618 union hwmon_pmu_event_key key = {
619 .type_and_num = 0,
620 };
621
622 key.type = type;
623 key.num = number;
624 attr->config = key.type_and_num;
625 }
626 return 0;
627 }
628 }
629 if (err) {
630 char *err_str;
631
632 parse_events_error__handle(err, term->err_val,
633 asprintf(&err_str,
634 "unexpected hwmon event term (%s) %s",
635 parse_events__term_type_str(term->type_term),
636 term->config) < 0
637 ? strdup("unexpected hwmon event term")
638 : err_str,
639 NULL);
640 }
641 return -EINVAL;
642 }
643
hwmon_pmu__config_terms(const struct perf_pmu * pmu,struct perf_event_attr * attr,struct parse_events_terms * terms,struct parse_events_error * err)644 int hwmon_pmu__config_terms(const struct perf_pmu *pmu,
645 struct perf_event_attr *attr,
646 struct parse_events_terms *terms,
647 struct parse_events_error *err)
648 {
649 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
650 struct parse_events_term *term;
651 int ret;
652
653 ret = hwmon_pmu__read_events(hwm);
654 if (ret)
655 return ret;
656
657 list_for_each_entry(term, &terms->terms, list) {
658 if (hwmon_pmu__config_term(hwm, attr, term, err))
659 return -EINVAL;
660 }
661
662 return 0;
663
664 }
665
hwmon_pmu__check_alias(struct parse_events_terms * terms,struct perf_pmu_info * info,struct parse_events_error * err)666 int hwmon_pmu__check_alias(struct parse_events_terms *terms, struct perf_pmu_info *info,
667 struct parse_events_error *err)
668 {
669 struct parse_events_term *term =
670 list_first_entry(&terms->terms, struct parse_events_term, list);
671
672 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) {
673 enum hwmon_type type;
674 int number;
675
676 if (parse_hwmon_filename(term->config, &type, &number,
677 /*item=*/NULL, /*is_alarm=*/NULL)) {
678 info->unit = hwmon_units[type];
679 if (type == HWMON_TYPE_FAN || type == HWMON_TYPE_PWM ||
680 type == HWMON_TYPE_INTRUSION)
681 info->scale = 1;
682 else
683 info->scale = 0.001;
684 }
685 return 0;
686 }
687 if (err) {
688 char *err_str;
689
690 parse_events_error__handle(err, term->err_val,
691 asprintf(&err_str,
692 "unexpected hwmon event term (%s) %s",
693 parse_events__term_type_str(term->type_term),
694 term->config) < 0
695 ? strdup("unexpected hwmon event term")
696 : err_str,
697 NULL);
698 }
699 return -EINVAL;
700 }
701
perf_pmus__read_hwmon_pmus(struct list_head * pmus)702 int perf_pmus__read_hwmon_pmus(struct list_head *pmus)
703 {
704 char *line = NULL;
705 DIR *class_hwmon_dir;
706 struct dirent *class_hwmon_ent;
707 char buf[PATH_MAX];
708 const char *sysfs = sysfs__mountpoint();
709
710 if (!sysfs)
711 return 0;
712
713 scnprintf(buf, sizeof(buf), "%s/class/hwmon/", sysfs);
714 class_hwmon_dir = opendir(buf);
715 if (!class_hwmon_dir)
716 return 0;
717
718 while ((class_hwmon_ent = readdir(class_hwmon_dir)) != NULL) {
719 size_t line_len;
720 int hwmon_dir, name_fd;
721 struct io io;
722
723 if (class_hwmon_ent->d_type != DT_LNK)
724 continue;
725
726 scnprintf(buf, sizeof(buf), "%s/class/hwmon/%s", sysfs, class_hwmon_ent->d_name);
727 hwmon_dir = open(buf, O_DIRECTORY);
728 if (hwmon_dir == -1) {
729 pr_debug("hwmon_pmu: not a directory: '%s/class/hwmon/%s'\n",
730 sysfs, class_hwmon_ent->d_name);
731 continue;
732 }
733 name_fd = openat(hwmon_dir, "name", O_RDONLY);
734 if (name_fd == -1) {
735 pr_debug("hwmon_pmu: failure to open '%s/class/hwmon/%s/name'\n",
736 sysfs, class_hwmon_ent->d_name);
737 close(hwmon_dir);
738 continue;
739 }
740 io__init(&io, name_fd, buf, sizeof(buf));
741 io__getline(&io, &line, &line_len);
742 if (line_len > 0 && line[line_len - 1] == '\n')
743 line[line_len - 1] = '\0';
744 hwmon_pmu__new(pmus, hwmon_dir, class_hwmon_ent->d_name, line);
745 close(name_fd);
746 }
747 free(line);
748 closedir(class_hwmon_dir);
749 return 0;
750 }
751
752 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
753
evsel__hwmon_pmu_open(struct evsel * evsel,struct perf_thread_map * threads,int start_cpu_map_idx,int end_cpu_map_idx)754 int evsel__hwmon_pmu_open(struct evsel *evsel,
755 struct perf_thread_map *threads,
756 int start_cpu_map_idx, int end_cpu_map_idx)
757 {
758 struct hwmon_pmu *hwm = container_of(evsel->pmu, struct hwmon_pmu, pmu);
759 union hwmon_pmu_event_key key = {
760 .type_and_num = evsel->core.attr.config,
761 };
762 int idx = 0, thread = 0, nthreads, err = 0;
763
764 nthreads = perf_thread_map__nr(threads);
765 for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) {
766 for (thread = 0; thread < nthreads; thread++) {
767 char buf[64];
768 int fd;
769
770 snprintf(buf, sizeof(buf), "%s%d_input",
771 hwmon_type_strs[key.type], key.num);
772
773 fd = openat(hwm->hwmon_dir_fd, buf, O_RDONLY);
774 FD(evsel, idx, thread) = fd;
775 if (fd < 0) {
776 err = -errno;
777 goto out_close;
778 }
779 }
780 }
781 return 0;
782 out_close:
783 if (err)
784 threads->err_thread = thread;
785
786 do {
787 while (--thread >= 0) {
788 if (FD(evsel, idx, thread) >= 0)
789 close(FD(evsel, idx, thread));
790 FD(evsel, idx, thread) = -1;
791 }
792 thread = nthreads;
793 } while (--idx >= 0);
794 return err;
795 }
796
evsel__hwmon_pmu_read(struct evsel * evsel,int cpu_map_idx,int thread)797 int evsel__hwmon_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread)
798 {
799 char buf[32];
800 int fd;
801 ssize_t len;
802 struct perf_counts_values *count, *old_count = NULL;
803
804 if (evsel->prev_raw_counts)
805 old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread);
806
807 count = perf_counts(evsel->counts, cpu_map_idx, thread);
808 fd = FD(evsel, cpu_map_idx, thread);
809 len = pread(fd, buf, sizeof(buf), 0);
810 if (len <= 0) {
811 count->lost++;
812 return -EINVAL;
813 }
814 buf[len] = '\0';
815 if (old_count) {
816 count->val = old_count->val + strtoll(buf, NULL, 10);
817 count->run = old_count->run + 1;
818 count->ena = old_count->ena + 1;
819 } else {
820 count->val = strtoll(buf, NULL, 10);
821 count->run++;
822 count->ena++;
823 }
824 return 0;
825 }
826