[PATCH v2 5/7] perf: Assemble split thread maps in script and stat report
From: Leo Yan
Date: Fri Dec 04 2026 - 06:24:14 EST
Add thread_map__append_event() to assemble records in order, and reuse
it for single-record construction. Free any command names copied by a
failed append.
Keep the map private until the final record arrives. Publish it to the
evlist and allocate counters once both the complete thread map and CPU
map are available. This avoids reallocating a map already shared with
the evlist.
Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
---
tools/perf/builtin-script.c | 46 ++++++++++++++++++++++++++++++++++-----
tools/perf/builtin-stat.c | 52 +++++++++++++++++++++++++++++++++++++++-----
tools/perf/util/thread_map.c | 51 +++++++++++++++++++++++++++++++++----------
tools/perf/util/thread_map.h | 2 ++
4 files changed, 128 insertions(+), 23 deletions(-)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 88aaf9cfd132a7aa80ae240edf8268e59af9cc47..e9fc3c7b1864789b3f99a1f0534c0d6a10e0ed9f 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -162,6 +162,7 @@ struct perf_script {
bool show_cgroup_events;
bool show_text_poke_events;
bool allocated;
+ bool threads_is_appending;
bool per_event_dump;
bool stitch_lbr;
struct evswitch evswitch;
@@ -3416,6 +3417,10 @@ static int __cmd_script(struct perf_script *script)
}
ret = perf_session__process_events(script->session);
+ if (!ret && !session_done() && script->threads_is_appending) {
+ pr_err("Incomplete thread map: missing final record.\n");
+ ret = -EINVAL;
+ }
if (script->per_event_dump)
perf_script__exit_per_event_dump_stats(script);
@@ -3940,13 +3945,34 @@ static void script__setup_sample_type(struct perf_script *script)
}
}
-static int process_stat_round_event(const struct perf_tool *tool __maybe_unused,
+static int process_stat_event(const struct perf_tool *tool, struct perf_session *session,
+ union perf_event *event)
+{
+ const struct perf_script *script = container_of(tool, struct perf_script, tool);
+
+ if (!script->allocated) {
+ pr_err("Maps are not allocated: threads_is_appending=%d\n",
+ script->threads_is_appending);
+ return -EINVAL;
+ }
+
+ return perf_event__process_stat_event(tool, session, event);
+}
+
+static int process_stat_round_event(const struct perf_tool *tool,
struct perf_session *session,
union perf_event *event)
{
+ const struct perf_script *script = container_of(tool, struct perf_script, tool);
struct perf_record_stat_round *round = &event->stat_round;
struct evsel *counter;
+ if (!script->allocated) {
+ pr_err("Maps are not allocated: threads_is_appending=%d\n",
+ script->threads_is_appending);
+ return -EINVAL;
+ }
+
evlist__for_each_entry(session->evlist, counter) {
perf_stat_process_counter(&stat_config, counter);
process_stat(counter, round->time);
@@ -3975,6 +4001,10 @@ static int set_maps(struct perf_script *script)
{
struct evlist *evlist = script->session->evlist;
+ /* Appending can move the map, so share it only after the final record. */
+ if (script->threads_is_appending)
+ return 0;
+
if (!script->cpus || !script->threads)
return 0;
@@ -3996,18 +4026,22 @@ int process_thread_map_event(const struct perf_tool *tool,
union perf_event *event)
{
struct perf_script *script = container_of(tool, struct perf_script, tool);
+ int ret;
if (dump_trace)
perf_event__fprintf_thread_map(event, stdout);
- if (script->threads) {
+ if (script->threads && !script->threads_is_appending) {
pr_warning("Extra thread map event, ignoring.\n");
return 0;
}
- script->threads = thread_map__new_event(&event->thread_map);
- if (!script->threads)
- return -ENOMEM;
+ ret = thread_map__append_event(&script->threads, &event->thread_map);
+ if (ret)
+ return ret;
+
+ script->threads_is_appending =
+ event->header.misc & PERF_RECORD_MISC_THREAD_MAP_MORE;
return set_maps(script);
}
@@ -4624,7 +4658,7 @@ int cmd_script(int argc, const char **argv)
script.tool.auxtrace_info = perf_script__process_auxtrace_info;
script.tool.auxtrace = perf_event__process_auxtrace;
script.tool.auxtrace_error = perf_event__process_auxtrace_error;
- script.tool.stat = perf_event__process_stat_event;
+ script.tool.stat = process_stat_event;
script.tool.stat_round = process_stat_round_event;
script.tool.stat_config = process_stat_config_event;
script.tool.thread_map = process_thread_map_event;
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 7b2025c37e87cff4ddcf7da8c643db008bdaed54..a00594b2210929e1856a744a9c2b00dcc7f9d631 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -147,6 +147,7 @@ struct perf_stat {
u64 bytes_written;
struct perf_tool tool;
bool maps_allocated;
+ bool threads_is_appending;
struct perf_cpu_map *cpus;
struct perf_thread_map *threads;
enum aggr_mode aggr_mode;
@@ -2234,16 +2235,37 @@ static int __cmd_record(const struct option stat_options[], struct opt_aggr_mode
return argc;
}
-static int process_stat_round_event(const struct perf_tool *tool __maybe_unused,
+static int process_stat_event(const struct perf_tool *tool, struct perf_session *session,
+ union perf_event *event)
+{
+ const struct perf_stat *st = container_of(tool, struct perf_stat, tool);
+
+ if (!st->maps_allocated) {
+ pr_err("Maps are not allocated: threads_is_appending=%d\n",
+ st->threads_is_appending);
+ return -EINVAL;
+ }
+
+ return perf_event__process_stat_event(tool, session, event);
+}
+
+static int process_stat_round_event(const struct perf_tool *tool,
struct perf_session *session,
union perf_event *event)
{
+ const struct perf_stat *st = container_of(tool, struct perf_stat, tool);
struct perf_record_stat_round *stat_round = &event->stat_round;
struct timespec tsh, *ts = NULL;
struct perf_env *env = perf_session__env(session);
const char **argv = env->cmdline_argv;
int argc = env->nr_cmdline;
+ if (!st->maps_allocated) {
+ pr_err("Maps are not allocated: threads_is_appending=%d\n",
+ st->threads_is_appending);
+ return -EINVAL;
+ }
+
process_counters();
if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
@@ -2266,6 +2288,12 @@ int process_stat_config_event(const struct perf_tool *tool,
{
struct perf_stat *st = container_of(tool, struct perf_stat, tool);
+ if (!st->maps_allocated) {
+ pr_err("Maps are not allocated: threads_is_appending=%d\n",
+ st->threads_is_appending);
+ return -EINVAL;
+ }
+
perf_event__read_stat_config(&stat_config, &event->stat_config);
if (perf_cpu_map__is_empty(st->cpus)) {
@@ -2293,6 +2321,10 @@ int process_stat_config_event(const struct perf_tool *tool,
static int set_maps(struct perf_stat *st)
{
+ /* Appending can move the map, so share it only after the final record. */
+ if (st->threads_is_appending)
+ return 0;
+
if (!st->cpus || !st->threads)
return 0;
@@ -2314,15 +2346,19 @@ int process_thread_map_event(const struct perf_tool *tool,
union perf_event *event)
{
struct perf_stat *st = container_of(tool, struct perf_stat, tool);
+ int ret;
- if (st->threads) {
+ if (st->threads && !st->threads_is_appending) {
pr_warning("Extra thread map event, ignoring.\n");
return 0;
}
- st->threads = thread_map__new_event(&event->thread_map);
- if (!st->threads)
- return -ENOMEM;
+ ret = thread_map__append_event(&st->threads, &event->thread_map);
+ if (ret)
+ return ret;
+
+ st->threads_is_appending =
+ event->header.misc & PERF_RECORD_MISC_THREAD_MAP_MORE;
return set_maps(st);
}
@@ -2405,7 +2441,7 @@ static int __cmd_report(int argc, const char **argv)
perf_stat.tool.thread_map = process_thread_map_event;
perf_stat.tool.cpu_map = process_cpu_map_event;
perf_stat.tool.stat_config = process_stat_config_event;
- perf_stat.tool.stat = perf_event__process_stat_event;
+ perf_stat.tool.stat = process_stat_event;
perf_stat.tool.stat_round = process_stat_round_event;
/*
@@ -2424,6 +2460,10 @@ static int __cmd_report(int argc, const char **argv)
evsel_list = session->evlist;
ret = perf_session__process_events(session);
+ if (!ret && !session_done() && perf_stat.threads_is_appending) {
+ pr_err("Incomplete thread map: missing final record.\n");
+ ret = -EINVAL;
+ }
perf_thread_map__put(perf_stat.threads);
perf_cpu_map__put(perf_stat.cpus);
diff --git a/tools/perf/util/thread_map.c b/tools/perf/util/thread_map.c
index 918ab318023205e48ccc18d9a0dd675c1b77f1ed..4b3e0cd022fb79635b914610f32cb0c9c161568f 100644
--- a/tools/perf/util/thread_map.c
+++ b/tools/perf/util/thread_map.c
@@ -336,28 +336,57 @@ void thread_map__read_comms(struct perf_thread_map *threads)
comm_init(threads, i);
}
-static void thread_map__copy_event(struct perf_thread_map *threads,
- struct perf_record_thread_map *event)
+int thread_map__append_event(struct perf_thread_map **map,
+ const struct perf_record_thread_map *event)
{
- unsigned i;
+ struct perf_thread_map *threads = *map;
+ bool new_map = !threads;
+ u64 old_nr = threads ? threads->nr : 0, nr, i;
- threads->nr = (int) event->nr;
+ /*
+ * Trust event->nr to describe chunks of a valid thread map, whose total
+ * count and allocation size are representable without overflow.
+ */
+ nr = old_nr + event->nr;
+
+ threads = perf_thread_map__realloc(threads, nr);
+ if (!threads)
+ return -ENOMEM;
+
+ if (new_map) {
+ threads->nr = 0;
+ refcount_set(&threads->refcnt, 1);
+ }
+ *map = threads;
for (i = 0; i < event->nr; i++) {
- perf_thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
- threads->map[i].comm = strndup(event->entries[i].comm, 16);
+ u64 idx = old_nr + i;
+
+ perf_thread_map__set_pid(threads, idx,
+ (pid_t) event->entries[i].pid);
+ threads->map[idx].comm = strndup(event->entries[i].comm,
+ sizeof(event->entries[i].comm));
+ if (!threads->map[idx].comm)
+ goto out_free;
}
+ threads->nr = nr;
- refcount_set(&threads->refcnt, 1);
+ return 0;
+
+out_free:
+ while (i--)
+ zfree(&threads->map[old_nr + i].comm);
+ return -ENOMEM;
}
struct perf_thread_map *thread_map__new_event(struct perf_record_thread_map *event)
{
- struct perf_thread_map *threads;
+ struct perf_thread_map *threads = NULL;
- threads = thread_map__alloc(event->nr);
- if (threads)
- thread_map__copy_event(threads, event);
+ if (thread_map__append_event(&threads, event)) {
+ perf_thread_map__put(threads);
+ return NULL;
+ }
return threads;
}
diff --git a/tools/perf/util/thread_map.h b/tools/perf/util/thread_map.h
index d700a22b7ae4663aca843baf80f7e5f522d6e607..7ddb9dd3fe9f2525cc7ba358bd6294a293cac910 100644
--- a/tools/perf/util/thread_map.h
+++ b/tools/perf/util/thread_map.h
@@ -13,6 +13,8 @@ struct perf_thread_map *thread_map__new_by_pid(pid_t pid);
struct perf_thread_map *thread_map__new_by_tid(pid_t tid);
struct perf_thread_map *thread_map__new(pid_t pid, pid_t tid);
struct perf_thread_map *thread_map__new_event(struct perf_record_thread_map *event);
+int thread_map__append_event(struct perf_thread_map **map,
+ const struct perf_record_thread_map *event);
struct perf_thread_map *thread_map__new_str(const char *pid,
const char *tid, bool all_threads);
--
2.34.1