[PATCH v1 10/26] perf ftrace: Use generic hashmap for profile_hash
From: Ian Rogers
Date: Fri Nov 13 2026 - 16:50:41 EST
Convert perf_ftrace::profile_hash to DECLARE_HASHMAP(ftrace_profile_map,
char *, struct ftrace_profile_data *) and use the typed hashmap__* and
hashmap__for_each() wrappers.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-ftrace.c | 43 ++++++++++++++++++++++---------------
tools/perf/util/ftrace.h | 7 ++++--
2 files changed, 31 insertions(+), 19 deletions(-)
diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index ac0107f5b891..02b4bf12451e 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -1228,7 +1228,7 @@ static int prepare_func_profile(struct perf_ftrace *ftrace)
ftrace->graph_tail = 1;
ftrace->graph_verbose = 0;
- ftrace->profile_hash = hashmap__new(profile_hash, profile_equal, NULL);
+ ftrace->profile_hash = ftrace_profile_map__new(profile_hash, profile_equal, NULL);
return ftrace->profile_hash ? 0 : -ENOMEM;
}
@@ -1347,17 +1347,22 @@ enum perf_ftrace_profile_sort_key {
static enum perf_ftrace_profile_sort_key profile_sort = PFP_SORT_TOTAL;
+struct ftrace_profile_entry {
+ char *name;
+ struct ftrace_profile_data *data;
+};
+
static int cmp_profile_data(const void *a, const void *b)
{
- const struct hashmap_entry *e1 = *(const struct hashmap_entry **)a;
- const struct hashmap_entry *e2 = *(const struct hashmap_entry **)b;
- struct ftrace_profile_data *p1 = e1->pvalue;
- struct ftrace_profile_data *p2 = e2->pvalue;
+ const struct ftrace_profile_entry *e1 = a;
+ const struct ftrace_profile_entry *e2 = b;
+ struct ftrace_profile_data *p1 = e1->data;
+ struct ftrace_profile_data *p2 = e2->data;
double v1, v2;
switch (profile_sort) {
case PFP_SORT_NAME:
- return strcmp(e1->pkey, e2->pkey);
+ return strcmp(e1->name, e2->name);
case PFP_SORT_AVG:
v1 = p1->st.mean;
v2 = p2->st.mean;
@@ -1386,8 +1391,10 @@ static int cmp_profile_data(const void *a, const void *b)
static void print_profile_result(struct perf_ftrace *ftrace)
{
- struct hashmap_entry *entry, **profile;
- size_t i, nr, bkt;
+ struct ftrace_profile_entry *profile;
+ size_t i, nr;
+ char *key;
+ struct ftrace_profile_data *val;
nr = hashmap__size(ftrace->profile_hash);
if (nr == 0)
@@ -1400,20 +1407,22 @@ static void print_profile_result(struct perf_ftrace *ftrace)
}
i = 0;
- hashmap__for_each_entry(ftrace->profile_hash, entry, bkt)
- profile[i++] = entry;
+ hashmap__for_each(ftrace->profile_hash, key, val) {
+ profile[i].name = key;
+ profile[i].data = val;
+ i++;
+ }
assert(i == nr);
- //cmp_profile_data(profile[0], profile[1]);
qsort(profile, nr, sizeof(*profile), cmp_profile_data);
printf("# %10s %10s %10s %10s %s\n",
"Total (us)", "Avg (us)", "Max (us)", "Count", "Function");
for (i = 0; i < nr; i++) {
- const char *name = profile[i]->pkey;
- struct ftrace_profile_data *p = profile[i]->pvalue;
+ const char *name = profile[i].name;
+ struct ftrace_profile_data *p = profile[i].data;
printf("%12.3f %10.3f %6"PRIu64".%03"PRIu64" %10.0f %s\n",
p->st.n * p->st.mean / 1000, p->st.mean / 1000,
@@ -1422,12 +1431,12 @@ static void print_profile_result(struct perf_ftrace *ftrace)
free(profile);
- hashmap__for_each_entry(ftrace->profile_hash, entry, bkt) {
- free((char *)entry->pkey);
- free(entry->pvalue);
+ hashmap__for_each(ftrace->profile_hash, key, val) {
+ free(key);
+ free(val);
}
- hashmap__free(ftrace->profile_hash);
+ ftrace_profile_map__free(ftrace->profile_hash);
ftrace->profile_hash = NULL;
}
diff --git a/tools/perf/util/ftrace.h b/tools/perf/util/ftrace.h
index 950f2efafad2..5ed52e66b8cb 100644
--- a/tools/perf/util/ftrace.h
+++ b/tools/perf/util/ftrace.h
@@ -3,12 +3,15 @@
#include <linux/list.h>
+#include "hashmap.h"
#include "target.h"
struct evlist;
-struct hashamp;
+struct ftrace_profile_data;
struct stats;
+DECLARE_HASHMAP(ftrace_profile_map, char *, struct ftrace_profile_data *);
+
struct perf_ftrace {
struct evlist *evlist;
struct target target;
@@ -18,7 +21,7 @@ struct perf_ftrace {
struct list_head graph_funcs;
struct list_head nograph_funcs;
struct list_head event_pair;
- struct hashmap *profile_hash;
+ struct ftrace_profile_map *profile_hash;
unsigned long percpu_buffer_size;
bool inherit;
bool use_nsec;
--
2.56.1.703.gf848ed807c-goog