From nobody Tue Dec 16 07:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CEAA211488; Tue, 14 Jan 2025 21:22:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736889779; cv=none; b=HwOH+XY9bRFt9mN6FzztAIzHd1ucQWIBGjpZZMHz5SD0OF+18OnkkcZ6Jg7PvnC3sf3YivA1+Gb5Z8UCtfhFbUD3SzJ5LUQvs0K520FWZ7ayAAaZ1DXWFePlrHHUaQ6owe9DQRD7gf5sUMPfT9csXCzzyAAqcoJeNl1NhIzxwqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736889779; c=relaxed/simple; bh=WgvjHs7NLmVM3gvH/RBtwDrLLCqYxYPoraCQmLF9lZk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V9G4t/XW/sDb/qoKkdjRZmdts62wrUQxZzq7+MsxFV2PIXQtgF4QrBQ0LG6dMKbPdDmRbNVfJJ+wUC3esumdi5GsO1lfcQHVOo9lpV31KaEaZu79cocaNCRoNDhwKRse+AEjSWcuJYFjIDZbA2ttE3UhCdEjtaLvTvMdtsf/ApQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dLdZ727s; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dLdZ727s" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AEDC9C4CEE0; Tue, 14 Jan 2025 21:22:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1736889779; bh=WgvjHs7NLmVM3gvH/RBtwDrLLCqYxYPoraCQmLF9lZk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dLdZ727sGEovAM/zyhfrfqr9KciQZjXbZiaP0W+l8BCOVISe0b7tx7LHpYXApTlBP 8rF+0WNp/d4meh7TWwd+NeT1efm1aG+kfOA/tzWONSlfXm0fV9Fzka9TOubxHpam1L zIthnGq19EVt7Tb/UCSAUilpQ8krrRwDvXBp6iFW0rQpKYeViZxEjq9wCTmGNUNv3S S/i7n8SktutMwoNT0yumGMQjBtIYdTzTRWmz+83AuJd2ucUunQs3XzoCtflaJFahDE DQFJcsl7IqgIIQd2t7mJUogWmmacdJaaQYVZra7R8E3SZSOslbad/ywJHBtCXUmFYg jfgy3HxN8HlSw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Howard Chu Subject: [PATCH 2/4] perf trace: Convert syscall_stats to hashmap Date: Tue, 14 Jan 2025 13:22:54 -0800 Message-ID: <20250114212256.160730-3-namhyung@kernel.org> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog In-Reply-To: <20250114212256.160730-1-namhyung@kernel.org> References: <20250114212256.160730-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It was using a RBtree-based int-list as a hash and a custome resort logic for that. As we have hashmap, let's convert to it and add a sorting function using an array. It's also to prepare supporting system-wide syscall stats. No functional changes intended. Signed-off-by: Namhyung Kim --- tools/perf/builtin-trace.c | 121 ++++++++++++++++++++++++++++--------- 1 file changed, 92 insertions(+), 29 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 0691e817c15b4136..ad7f7fcd0d80b9df 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -39,6 +39,7 @@ #include "util/synthetic-events.h" #include "util/evlist.h" #include "util/evswitch.h" +#include "util/hashmap.h" #include "util/mmap.h" #include #include @@ -63,7 +64,6 @@ #include "print_binary.h" #include "string2.h" #include "syscalltbl.h" -#include "rb_resort.h" #include "../perf.h" #include "trace_augment.h" =20 @@ -1520,17 +1520,55 @@ struct thread_trace { struct file *table; } files; =20 - struct intlist *syscall_stats; + struct hashmap *syscall_stats; }; =20 +static size_t id_hash(long key, void *ctx __maybe_unused) +{ + return key; +} + +static bool id_equal(long key1, long key2, void *ctx __maybe_unused) +{ + return key1 =3D=3D key2; +} + +static struct hashmap *alloc_syscall_stats(void) +{ + struct hashmap *stats =3D hashmap__new(id_hash, id_equal, NULL); + + /* just for simpler error check */ + if (IS_ERR_OR_NULL(stats)) + stats =3D NULL; + return stats; +} + +static void delete_syscall_stats(struct hashmap *syscall_stats) +{ + struct hashmap_entry *pos; + size_t bkt; + + if (syscall_stats =3D=3D NULL) + return; + + hashmap__for_each_entry(syscall_stats, pos, bkt) + free(pos->pvalue); + hashmap__free(syscall_stats); +} + static struct thread_trace *thread_trace__new(struct trace *trace) { struct thread_trace *ttrace =3D zalloc(sizeof(struct thread_trace)); =20 if (ttrace) { ttrace->files.max =3D -1; - if (trace->summary) - ttrace->syscall_stats =3D intlist__new(NULL); + if (trace->summary) { + ttrace->syscall_stats =3D alloc_syscall_stats(); + if (ttrace->syscall_stats =3D=3D NULL) { + free(ttrace); + ttrace =3D NULL; + } + } } =20 return ttrace; @@ -1545,7 +1583,7 @@ static void thread_trace__delete(void *pttrace) if (!ttrace) return; =20 - intlist__delete(ttrace->syscall_stats); + delete_syscall_stats(ttrace->syscall_stats); ttrace->syscall_stats =3D NULL; thread_trace__free_files(ttrace); zfree(&ttrace->entry_str); @@ -2464,22 +2502,19 @@ struct syscall_stats { static void thread__update_stats(struct thread *thread, struct thread_trac= e *ttrace, int id, struct perf_sample *sample, long err, bool errno_summary) { - struct int_node *inode; - struct syscall_stats *stats; + struct syscall_stats *stats =3D NULL; u64 duration =3D 0; =20 - inode =3D intlist__findnew(ttrace->syscall_stats, id); - if (inode =3D=3D NULL) - return; - - stats =3D inode->priv; - if (stats =3D=3D NULL) { + if (!hashmap__find(ttrace->syscall_stats, id, &stats)) { stats =3D zalloc(sizeof(*stats)); if (stats =3D=3D NULL) return; =20 init_stats(&stats->stats); - inode->priv =3D stats; + if (hashmap__add(ttrace->syscall_stats, id, stats) < 0) { + free(stats); + return; + } } =20 if (ttrace->entry_time && sample->time > ttrace->entry_time) @@ -4618,18 +4653,45 @@ static size_t trace__fprintf_threads_header(FILE *f= p) return printed; } =20 -DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs, +struct syscall_entry { struct syscall_stats *stats; double msecs; int syscall; -) +}; + +static int entry_cmp(const void *e1, const void *e2) +{ + const struct syscall_entry *entry1 =3D e1; + const struct syscall_entry *entry2 =3D e2; + + return entry1->msecs > entry2->msecs ? -1 : 1; +} + +static struct syscall_entry *thread__sort_stats(struct thread_trace *ttrac= e) { - struct int_node *source =3D rb_entry(nd, struct int_node, rb_node); - struct syscall_stats *stats =3D source->priv; + struct syscall_entry *entry; + struct hashmap_entry *pos; + unsigned bkt, i, nr; + + nr =3D ttrace->syscall_stats->sz; + entry =3D malloc(nr * sizeof(*entry)); + if (entry =3D=3D NULL) + return NULL; =20 - entry->syscall =3D source->i; - entry->stats =3D stats; - entry->msecs =3D stats ? (u64)stats->stats.n * (avg_stats(&stats->stats= ) / NSEC_PER_MSEC) : 0; + i =3D 0; + hashmap__for_each_entry(ttrace->syscall_stats, pos, bkt) { + struct syscall_stats *ss =3D pos->pvalue; + struct stats *st =3D &ss->stats; + + entry[i].stats =3D ss; + entry[i].msecs =3D (u64)st->n * (avg_stats(st) / NSEC_PER_MSEC); + entry[i].syscall =3D pos->key; + i++; + } + assert(i =3D=3D nr); + + qsort(entry, nr, sizeof(*entry), entry_cmp); + return entry; } =20 static size_t thread__dump_stats(struct thread_trace *ttrace, @@ -4637,10 +4699,10 @@ static size_t thread__dump_stats(struct thread_trac= e *ttrace, { size_t printed =3D 0; struct syscall *sc; - struct rb_node *nd; - DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats); + struct syscall_entry *entries; =20 - if (syscall_stats =3D=3D NULL) + entries =3D thread__sort_stats(ttrace); + if (entries =3D=3D NULL) return 0; =20 printed +=3D fprintf(fp, "\n"); @@ -4649,8 +4711,9 @@ static size_t thread__dump_stats(struct thread_trace = *ttrace, printed +=3D fprintf(fp, " (msec) = (msec) (msec) (msec) (%%)\n"); printed +=3D fprintf(fp, " --------------- -------- ------ -------- --= ------- --------- --------- ------\n"); =20 - resort_rb__for_each_entry(nd, syscall_stats) { - struct syscall_stats *stats =3D syscall_stats_entry->stats; + for (size_t i =3D 0; i < ttrace->syscall_stats->sz; i++) { + struct syscall_entry *entry =3D &entries[i]; + struct syscall_stats *stats =3D entry->stats; if (stats) { double min =3D (double)(stats->stats.min) / NSEC_PER_MSEC; double max =3D (double)(stats->stats.max) / NSEC_PER_MSEC; @@ -4661,10 +4724,10 @@ static size_t thread__dump_stats(struct thread_trac= e *ttrace, pct =3D avg ? 100.0 * stddev_stats(&stats->stats) / avg : 0.0; avg /=3D NSEC_PER_MSEC; =20 - sc =3D &trace->syscalls.table[syscall_stats_entry->syscall]; + sc =3D &trace->syscalls.table[entry->syscall]; printed +=3D fprintf(fp, " %-15s", sc->name); printed +=3D fprintf(fp, " %8" PRIu64 " %6" PRIu64 " %9.3f %9.3f %9.3f", - n, stats->nr_failures, syscall_stats_entry->msecs, min, avg); + n, stats->nr_failures, entry->msecs, min, avg); printed +=3D fprintf(fp, " %9.3f %9.2f%%\n", max, pct); =20 if (trace->errno_summary && stats->nr_failures) { @@ -4678,7 +4741,7 @@ static size_t thread__dump_stats(struct thread_trace = *ttrace, } } =20 - resort_rb__delete(syscall_stats); + free(entries); printed +=3D fprintf(fp, "\n\n"); =20 return printed; --=20 2.48.0.rc2.279.g1de40edade-goog