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 __u32 type = PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NULL, 10); 350 351 if (type > PERF_PMU_TYPE_HWMON_END) { 352 pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_name); 353 return NULL; 354 } 355 356 snprintf(buf, sizeof(buf), "hwmon_%s", name); 357 fix_name(buf + 6); 358 359 hwm = zalloc(sizeof(*hwm)); 360 if (!hwm) 361 return NULL; 362 363 if (perf_pmu__init(&hwm->pmu, type, buf) != 0) { 364 perf_pmu__delete(&hwm->pmu); 365 return NULL; 366 } 367 368 hwm->hwmon_dir_fd = hwmon_dir; 369 hwm->pmu.alias_name = strdup(sysfs_name); 370 if (!hwm->pmu.alias_name) { 371 perf_pmu__delete(&hwm->pmu); 372 return NULL; 373 } 374 hwm->pmu.cpus = perf_cpu_map__new("0"); 375 if (!hwm->pmu.cpus) { 376 perf_pmu__delete(&hwm->pmu); 377 return NULL; 378 } 379 INIT_LIST_HEAD(&hwm->pmu.format); 380 INIT_LIST_HEAD(&hwm->pmu.caps); 381 hashmap__init(&hwm->events, hwmon_pmu__event_hashmap_hash, 382 hwmon_pmu__event_hashmap_equal, /*ctx=*/NULL); 383 384 list_add_tail(&hwm->pmu.list, pmus); 385 return &hwm->pmu; 386 } 387 388 void hwmon_pmu__exit(struct perf_pmu *pmu) 389 { 390 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu); 391 struct hashmap_entry *cur, *tmp; 392 size_t bkt; 393 394 hashmap__for_each_entry_safe((&hwm->events), cur, tmp, bkt) { 395 struct hwmon_pmu_event_value *value = cur->pvalue; 396 397 zfree(&value->label); 398 zfree(&value->name); 399 free(value); 400 } 401 hashmap__clear(&hwm->events); 402 close(hwm->hwmon_dir_fd); 403 } 404 405 static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, size_t out_buf_len, 406 union hwmon_pmu_event_key key, 407 const unsigned long *items, bool is_alarm) 408 { 409 size_t bit; 410 char buf[64]; 411 size_t len = 0; 412 413 for_each_set_bit(bit, items, HWMON_ITEM__MAX) { 414 int fd; 415 416 if (bit == HWMON_ITEM_LABEL || bit == HWMON_ITEM_INPUT) 417 continue; 418 419 snprintf(buf, sizeof(buf), "%s%d_%s%s", 420 hwmon_type_strs[key.type], 421 key.num, 422 hwmon_item_strs[bit], 423 is_alarm ? "_alarm" : ""); 424 fd = openat(hwm->hwmon_dir_fd, buf, O_RDONLY); 425 if (fd > 0) { 426 ssize_t read_len = read(fd, buf, sizeof(buf)); 427 428 while (read_len > 0 && buf[read_len - 1] == '\n') 429 read_len--; 430 431 if (read_len > 0) { 432 long long val; 433 434 buf[read_len] = '\0'; 435 val = strtoll(buf, /*endptr=*/NULL, 10); 436 len += snprintf(out_buf + len, out_buf_len - len, "%s%s%s=%g%s", 437 len == 0 ? " " : ", ", 438 hwmon_item_strs[bit], 439 is_alarm ? "_alarm" : "", 440 (double)val / 1000.0, 441 hwmon_units[key.type]); 442 } 443 close(fd); 444 } 445 } 446 return len; 447 } 448 449 int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb) 450 { 451 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu); 452 struct hashmap_entry *cur; 453 size_t bkt; 454 455 if (hwmon_pmu__read_events(hwm)) 456 return false; 457 458 hashmap__for_each_entry((&hwm->events), cur, bkt) { 459 static const char *const hwmon_scale_units[HWMON_TYPE_MAX] = { 460 NULL, 461 "0.001V", /* cpu */ 462 "0.001A", /* curr */ 463 "0.001J", /* energy */ 464 "1rpm", /* fan */ 465 "0.001%", /* humidity */ 466 "0.001V", /* in */ 467 NULL, /* intrusion */ 468 "0.001W", /* power */ 469 "1Hz", /* pwm */ 470 "0.001'C", /* temp */ 471 }; 472 static const char *const hwmon_desc[HWMON_TYPE_MAX] = { 473 NULL, 474 "CPU core reference voltage", /* cpu */ 475 "Current", /* curr */ 476 "Cumulative energy use", /* energy */ 477 "Fan", /* fan */ 478 "Humidity", /* humidity */ 479 "Voltage", /* in */ 480 "Chassis intrusion detection", /* intrusion */ 481 "Power use", /* power */ 482 "Pulse width modulation fan control", /* pwm */ 483 "Temperature", /* temp */ 484 }; 485 char alias_buf[64]; 486 char desc_buf[256]; 487 char encoding_buf[128]; 488 union hwmon_pmu_event_key key = { 489 .type_and_num = cur->key, 490 }; 491 struct hwmon_pmu_event_value *value = cur->pvalue; 492 struct pmu_event_info info = { 493 .pmu = pmu, 494 .name = value->name, 495 .alias = alias_buf, 496 .scale_unit = hwmon_scale_units[key.type], 497 .desc = desc_buf, 498 .long_desc = NULL, 499 .encoding_desc = encoding_buf, 500 .topic = "hwmon", 501 .pmu_name = pmu->name, 502 .event_type_desc = "Hwmon event", 503 }; 504 int ret; 505 size_t len; 506 507 len = snprintf(alias_buf, sizeof(alias_buf), "%s%d", 508 hwmon_type_strs[key.type], key.num); 509 if (!info.name) { 510 info.name = info.alias; 511 info.alias = NULL; 512 } 513 514 len = snprintf(desc_buf, sizeof(desc_buf), "%s in unit %s named %s.", 515 hwmon_desc[key.type], 516 pmu->name + 6, 517 value->label ?: info.name); 518 519 len += hwmon_pmu__describe_items(hwm, desc_buf + len, sizeof(desc_buf) - len, 520 key, value->items, /*is_alarm=*/false); 521 522 len += hwmon_pmu__describe_items(hwm, desc_buf + len, sizeof(desc_buf) - len, 523 key, value->alarm_items, /*is_alarm=*/true); 524 525 snprintf(encoding_buf, sizeof(encoding_buf), "%s/config=0x%lx/", 526 pmu->name, cur->key); 527 528 ret = cb(state, &info); 529 if (ret) 530 return ret; 531 } 532 return 0; 533 } 534 535 size_t hwmon_pmu__num_events(struct perf_pmu *pmu) 536 { 537 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu); 538 539 hwmon_pmu__read_events(hwm); 540 return hashmap__size(&hwm->events); 541 } 542 543 bool hwmon_pmu__have_event(struct perf_pmu *pmu, const char *name) 544 { 545 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu); 546 enum hwmon_type type; 547 int number; 548 union hwmon_pmu_event_key key = { .type_and_num = 0 }; 549 struct hashmap_entry *cur; 550 size_t bkt; 551 552 if (!parse_hwmon_filename(name, &type, &number, /*item=*/NULL, /*is_alarm=*/NULL)) 553 return false; 554 555 if (hwmon_pmu__read_events(hwm)) 556 return false; 557 558 key.type = type; 559 key.num = number; 560 if (hashmap_find(&hwm->events, key.type_and_num, /*value=*/NULL)) 561 return true; 562 if (key.num != -1) 563 return false; 564 /* Item is of form <type>_ which means we should match <type>_<label>. */ 565 hashmap__for_each_entry((&hwm->events), cur, bkt) { 566 struct hwmon_pmu_event_value *value = cur->pvalue; 567 568 key.type_and_num = cur->key; 569 if (key.type == type && value->name && !strcasecmp(name, value->name)) 570 return true; 571 } 572 return false; 573 } 574 575 static int hwmon_pmu__config_term(const struct hwmon_pmu *hwm, 576 struct perf_event_attr *attr, 577 struct parse_events_term *term, 578 struct parse_events_error *err) 579 { 580 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) { 581 enum hwmon_type type; 582 int number; 583 584 if (parse_hwmon_filename(term->config, &type, &number, 585 /*item=*/NULL, /*is_alarm=*/NULL)) { 586 if (number == -1) { 587 /* 588 * Item is of form <type>_ which means we should 589 * match <type>_<label>. 590 */ 591 struct hashmap_entry *cur; 592 size_t bkt; 593 594 attr->config = 0; 595 hashmap__for_each_entry((&hwm->events), cur, bkt) { 596 union hwmon_pmu_event_key key = { 597 .type_and_num = cur->key, 598 }; 599 struct hwmon_pmu_event_value *value = cur->pvalue; 600 601 if (key.type == type && value->name && 602 !strcasecmp(term->config, value->name)) { 603 attr->config = key.type_and_num; 604 break; 605 } 606 } 607 if (attr->config == 0) 608 return -EINVAL; 609 } else { 610 union hwmon_pmu_event_key key = { 611 .type_and_num = 0, 612 }; 613 614 key.type = type; 615 key.num = number; 616 attr->config = key.type_and_num; 617 } 618 return 0; 619 } 620 } 621 if (err) { 622 char *err_str; 623 624 parse_events_error__handle(err, term->err_val, 625 asprintf(&err_str, 626 "unexpected hwmon event term (%s) %s", 627 parse_events__term_type_str(term->type_term), 628 term->config) < 0 629 ? strdup("unexpected hwmon event term") 630 : err_str, 631 NULL); 632 } 633 return -EINVAL; 634 } 635 636 int hwmon_pmu__config_terms(const struct perf_pmu *pmu, 637 struct perf_event_attr *attr, 638 struct parse_events_terms *terms, 639 struct parse_events_error *err) 640 { 641 struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu); 642 struct parse_events_term *term; 643 int ret; 644 645 ret = hwmon_pmu__read_events(hwm); 646 if (ret) 647 return ret; 648 649 list_for_each_entry(term, &terms->terms, list) { 650 if (hwmon_pmu__config_term(hwm, attr, term, err)) 651 return -EINVAL; 652 } 653 654 return 0; 655 656 } 657 658 int hwmon_pmu__check_alias(struct parse_events_terms *terms, struct perf_pmu_info *info, 659 struct parse_events_error *err) 660 { 661 struct parse_events_term *term = 662 list_first_entry(&terms->terms, struct parse_events_term, list); 663 664 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) { 665 enum hwmon_type type; 666 int number; 667 668 if (parse_hwmon_filename(term->config, &type, &number, 669 /*item=*/NULL, /*is_alarm=*/NULL)) { 670 info->unit = hwmon_units[type]; 671 if (type == HWMON_TYPE_FAN || type == HWMON_TYPE_PWM || 672 type == HWMON_TYPE_INTRUSION) 673 info->scale = 1; 674 else 675 info->scale = 0.001; 676 } 677 return 0; 678 } 679 if (err) { 680 char *err_str; 681 682 parse_events_error__handle(err, term->err_val, 683 asprintf(&err_str, 684 "unexpected hwmon event term (%s) %s", 685 parse_events__term_type_str(term->type_term), 686 term->config) < 0 687 ? strdup("unexpected hwmon event term") 688 : err_str, 689 NULL); 690 } 691 return -EINVAL; 692 } 693 694 int perf_pmus__read_hwmon_pmus(struct list_head *pmus) 695 { 696 char *line = NULL; 697 struct io_dirent64 *class_hwmon_ent; 698 struct io_dir class_hwmon_dir; 699 char buf[PATH_MAX]; 700 const char *sysfs = sysfs__mountpoint(); 701 702 if (!sysfs) 703 return 0; 704 705 scnprintf(buf, sizeof(buf), "%s/class/hwmon/", sysfs); 706 io_dir__init(&class_hwmon_dir, open(buf, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); 707 708 if (class_hwmon_dir.dirfd < 0) 709 return 0; 710 711 while ((class_hwmon_ent = io_dir__readdir(&class_hwmon_dir)) != NULL) { 712 size_t line_len; 713 int hwmon_dir, name_fd; 714 struct io io; 715 716 if (class_hwmon_ent->d_type != DT_LNK) 717 continue; 718 719 scnprintf(buf, sizeof(buf), "%s/class/hwmon/%s", sysfs, class_hwmon_ent->d_name); 720 hwmon_dir = open(buf, O_DIRECTORY); 721 if (hwmon_dir == -1) { 722 pr_debug("hwmon_pmu: not a directory: '%s/class/hwmon/%s'\n", 723 sysfs, class_hwmon_ent->d_name); 724 continue; 725 } 726 name_fd = openat(hwmon_dir, "name", O_RDONLY); 727 if (name_fd == -1) { 728 pr_debug("hwmon_pmu: failure to open '%s/class/hwmon/%s/name'\n", 729 sysfs, class_hwmon_ent->d_name); 730 close(hwmon_dir); 731 continue; 732 } 733 io__init(&io, name_fd, buf, sizeof(buf)); 734 io__getline(&io, &line, &line_len); 735 if (line_len > 0 && line[line_len - 1] == '\n') 736 line[line_len - 1] = '\0'; 737 hwmon_pmu__new(pmus, hwmon_dir, class_hwmon_ent->d_name, line); 738 close(name_fd); 739 } 740 free(line); 741 close(class_hwmon_dir.dirfd); 742 return 0; 743 } 744 745 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 746 747 int evsel__hwmon_pmu_open(struct evsel *evsel, 748 struct perf_thread_map *threads, 749 int start_cpu_map_idx, int end_cpu_map_idx) 750 { 751 struct hwmon_pmu *hwm = container_of(evsel->pmu, struct hwmon_pmu, pmu); 752 union hwmon_pmu_event_key key = { 753 .type_and_num = evsel->core.attr.config, 754 }; 755 int idx = 0, thread = 0, nthreads, err = 0; 756 757 nthreads = perf_thread_map__nr(threads); 758 for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) { 759 for (thread = 0; thread < nthreads; thread++) { 760 char buf[64]; 761 int fd; 762 763 snprintf(buf, sizeof(buf), "%s%d_input", 764 hwmon_type_strs[key.type], key.num); 765 766 fd = openat(hwm->hwmon_dir_fd, buf, O_RDONLY); 767 FD(evsel, idx, thread) = fd; 768 if (fd < 0) { 769 err = -errno; 770 goto out_close; 771 } 772 } 773 } 774 return 0; 775 out_close: 776 if (err) 777 threads->err_thread = thread; 778 779 do { 780 while (--thread >= 0) { 781 if (FD(evsel, idx, thread) >= 0) 782 close(FD(evsel, idx, thread)); 783 FD(evsel, idx, thread) = -1; 784 } 785 thread = nthreads; 786 } while (--idx >= 0); 787 return err; 788 } 789 790 int evsel__hwmon_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread) 791 { 792 char buf[32]; 793 int fd; 794 ssize_t len; 795 struct perf_counts_values *count, *old_count = NULL; 796 797 if (evsel->prev_raw_counts) 798 old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); 799 800 count = perf_counts(evsel->counts, cpu_map_idx, thread); 801 fd = FD(evsel, cpu_map_idx, thread); 802 len = pread(fd, buf, sizeof(buf), 0); 803 if (len <= 0) { 804 count->lost++; 805 return -EINVAL; 806 } 807 buf[len] = '\0'; 808 if (old_count) { 809 count->val = old_count->val + strtoll(buf, NULL, 10); 810 count->run = old_count->run + 1; 811 count->ena = old_count->ena + 1; 812 } else { 813 count->val = strtoll(buf, NULL, 10); 814 count->run++; 815 count->ena++; 816 } 817 return 0; 818 } 819