Linux Perf Users
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	 Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	 linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 10/26] perf ftrace: Use generic hashmap for profile_hash
Date: Fri, 13 Nov 2026 13:48:34 -0800	[thread overview]
Message-ID: <20261113214850.3329699-11-irogers@google.com> (raw)
In-Reply-To: <20261113214850.3329699-1-irogers@google.com>

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@google.com>
---
 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


  parent reply	other threads:[~2026-11-13 21:49 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 21:48 [PATCH v1 00/26] perf hashmap: Fix undefined behavior, remove ERR_PTR, and add type-safe generic hashmaps Ian Rogers
2026-11-13 21:48 ` [PATCH v1 01/26] perf threads: Fix uninitialized pointer put when thread removal misses Ian Rogers
2026-11-13 21:56   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 02/26] perf annotate: Fix sym_hist_entry memory leak when zeroing histograms Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 03/26] perf stat: Fix per_pkg_mask key leak on hashmap__add failure Ian Rogers
2026-11-13 21:55   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 04/26] perf build: Don't check difference of perf and libbpf hashmap Ian Rogers
2026-11-13 21:54   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 05/26] perf hashmap: Always build perf's hashmap and avoid libbpf symbol conflict Ian Rogers
2026-11-13 21:55   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 06/26] perf hashmap: Fix strict aliasing violations in hashmap Ian Rogers
2026-11-13 21:56   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 07/26] perf hashmap: Remove errptr usage from hashmap Ian Rogers
2026-11-13 22:01   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 08/26] perf hashmap: Introduce generic types and macros Ian Rogers
2026-11-13 22:01   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 09/26] perf fncache: Use generic hashmap for fncache Ian Rogers
2026-11-13 21:56   ` sashiko-bot
2026-11-13 21:48 ` Ian Rogers [this message]
2026-11-13 21:57   ` [PATCH v1 10/26] perf ftrace: Use generic hashmap for profile_hash sashiko-bot
2026-11-13 21:48 ` [PATCH v1 11/26] perf trace: Use generic hashmap for syscall_stats Ian Rogers
2026-11-13 22:04   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 12/26] perf trace: Use generic hashmap for bpf-trace-summary Ian Rogers
2026-11-13 21:58   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 13/26] perf lock: Use generic hashmap for slab_hash Ian Rogers
2026-11-13 21:57   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 14/26] perf stat: Use generic hashmap for per_pkg_mask Ian Rogers
2026-11-13 21:57   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 15/26] perf expr: Use generic hashmap for expr_ids Ian Rogers
2026-11-13 22:02   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 16/26] perf pmu: Use generic hashmap for pmu aliases Ian Rogers
2026-11-13 22:04   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 17/26] perf hwmon_pmu: Use generic hashmap for hwmon events Ian Rogers
2026-11-13 21:58   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 18/26] perf s390: Use generic hashmap for counter name cache Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 19/26] perf threads: Use generic hashmap for thread shards Ian Rogers
2026-11-13 22:01   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 20/26] perf annotate: Use generic hashmap for sym_samples Ian Rogers
2026-11-13 22:08   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 21/26] perf annotate: Use generic hashmap for browser type_hash Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 22/26] perf annotate-data: Use generic hashmap for type_hist samples Ian Rogers
2026-11-13 22:00   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 23/26] perf inject: Use generic hashmap for aslr remap_addresses Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 24/26] perf inject: Use generic hashmap for aslr top_addresses Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 25/26] perf inject: Use generic hashmap for aslr evsel_orig_attrs Ian Rogers
2026-11-13 22:00   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 26/26] perf hashmap: Remove untyped struct hashmap API Ian Rogers
2026-11-13 22:01   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261113214850.3329699-11-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox