[PATCH v1 17/26] perf hwmon_pmu: Use generic hashmap for hwmon events

From: Ian Rogers

Date: Fri Nov 13 2026 - 18:20:55 EST


Convert hwmon_pmu::events in util/hwmon_pmu.c to
DECLARE_HASHMAP(hwmon_pmu_events_map, long, struct hwmon_pmu_event_value *)
and use hashmap__for_each{,_safe}().

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/hwmon_pmu.c | 57 +++++++++++++++++--------------------
1 file changed, 26 insertions(+), 31 deletions(-)

diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c
index c150fc0d7bd3..95049f5fead3 100644
--- a/tools/perf/util/hwmon_pmu.c
+++ b/tools/perf/util/hwmon_pmu.c
@@ -101,9 +101,12 @@ static const char *const hwmon_units[HWMON_TYPE_MAX] = {
"'C", /* temp */
};

+struct hwmon_pmu_event_value;
+DECLARE_HASHMAP(hwmon_pmu_events_map, long, struct hwmon_pmu_event_value *);
+
struct hwmon_pmu {
struct perf_pmu pmu;
- struct hashmap events;
+ struct hwmon_pmu_events_map events;
char *hwmon_dir;
};

@@ -237,8 +240,8 @@ static void fix_name(char *p)
static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
{
int err = 0;
- struct hashmap_entry *cur, *tmp;
- size_t bkt;
+ long key_val;
+ struct hwmon_pmu_event_value *value;
struct io_dirent64 *ent;
struct io_dir dir;

@@ -257,7 +260,6 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
enum hwmon_item item;
bool alarm;
union hwmon_pmu_event_key key = { .type_and_num = 0 };
- struct hwmon_pmu_event_value *value;

if (ent->d_type != DT_REG)
continue;
@@ -324,16 +326,15 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
if (hashmap__size(&pmu->events) == 0)
pr_debug2("hwmon_pmu: %s has no events\n", pmu->pmu.name);

- hashmap__for_each_entry_safe((&pmu->events), cur, tmp, bkt) {
+ hashmap__for_each_safe(&pmu->events, key_val, value) {
union hwmon_pmu_event_key key = {
- .type_and_num = cur->key,
+ .type_and_num = key_val,
};
- struct hwmon_pmu_event_value *value = cur->pvalue;

if (!test_bit(HWMON_ITEM_INPUT, value->items)) {
pr_debug("hwmon_pmu: %s removing event '%s%d' that has no input file\n",
pmu->pmu.name, hwmon_type_strs[key.type], key.num);
- hashmap__delete(&pmu->events, key.type_and_num, &key, &value);
+ hashmap__delete(&pmu->events, key_val, NULL, NULL);
zfree(&value->label);
zfree(&value->name);
free(value);
@@ -397,12 +398,10 @@ struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, const char *hwmon_dir,
void hwmon_pmu__exit(struct perf_pmu *pmu)
{
struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
- struct hashmap_entry *cur, *tmp;
- size_t bkt;
-
- hashmap__for_each_entry_safe((&hwm->events), cur, tmp, bkt) {
- struct hwmon_pmu_event_value *value = cur->pvalue;
+ long key;
+ struct hwmon_pmu_event_value *value;

+ hashmap__for_each_safe(&hwm->events, key, value) {
zfree(&value->label);
zfree(&value->name);
free(value);
@@ -463,13 +462,13 @@ static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, si
int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb)
{
struct hwmon_pmu *hwm = container_of(pmu, struct hwmon_pmu, pmu);
- struct hashmap_entry *cur;
- size_t bkt;
+ long key_val;
+ struct hwmon_pmu_event_value *value;

if (hwmon_pmu__read_events(hwm))
return false;

- hashmap__for_each_entry((&hwm->events), cur, bkt) {
+ hashmap__for_each(&hwm->events, key_val, value) {
static const char *const hwmon_scale_units[HWMON_TYPE_MAX] = {
NULL,
"0.001V", /* cpu */
@@ -500,9 +499,8 @@ int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callb
char desc_buf[256];
char encoding_buf[128];
union hwmon_pmu_event_key key = {
- .type_and_num = cur->key,
+ .type_and_num = key_val,
};
- struct hwmon_pmu_event_value *value = cur->pvalue;
struct pmu_event_info info = {
.pmu = pmu,
.name = value->name,
@@ -537,7 +535,7 @@ int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callb
key, value->alarm_items, /*is_alarm=*/true);

snprintf(encoding_buf, sizeof(encoding_buf), "%s/config=0x%lx/",
- pmu->name, cur->key);
+ pmu->name, key_val);

ret = cb(state, &info);
if (ret)
@@ -560,8 +558,8 @@ bool hwmon_pmu__have_event(struct perf_pmu *pmu, const char *name)
enum hwmon_type type;
int number;
union hwmon_pmu_event_key key = { .type_and_num = 0 };
- struct hashmap_entry *cur;
- size_t bkt;
+ long key_val;
+ struct hwmon_pmu_event_value *value;

if (!parse_hwmon_filename(name, &type, &number, /*item=*/NULL, /*is_alarm=*/NULL))
return false;
@@ -571,15 +569,13 @@ bool hwmon_pmu__have_event(struct perf_pmu *pmu, const char *name)

key.type = type;
key.num = number;
- if (hashmap__find(&hwm->events, key.type_and_num, /*value=*/NULL))
+ if (hashmap__find(&hwm->events, key.type_and_num, /*value_ptr=*/NULL))
return true;
if (key.num != -1)
return false;
/* Item is of form <type>_ which means we should match <type>_<label>. */
- hashmap__for_each_entry((&hwm->events), cur, bkt) {
- struct hwmon_pmu_event_value *value = cur->pvalue;
-
- key.type_and_num = cur->key;
+ hashmap__for_each(&hwm->events, key_val, value) {
+ key.type_and_num = key_val;
if (key.type == type && value->name && !strcasecmp(name, value->name))
return true;
}
@@ -602,15 +598,14 @@ static int hwmon_pmu__config_term(const struct hwmon_pmu *hwm,
* Item is of form <type>_ which means we should
* match <type>_<label>.
*/
- struct hashmap_entry *cur;
- size_t bkt;
+ long key_val;
+ struct hwmon_pmu_event_value *value;

attr->config = 0;
- hashmap__for_each_entry((&hwm->events), cur, bkt) {
+ hashmap__for_each(&hwm->events, key_val, value) {
union hwmon_pmu_event_key key = {
- .type_and_num = cur->key,
+ .type_and_num = key_val,
};
- struct hwmon_pmu_event_value *value = cur->pvalue;

if (key.type == type && value->name &&
!strcasecmp(term->config, value->name)) {
--
2.56.1.703.gf848ed807c-goog