[PATCH 3/4] perf test: Cover multi-record thread map synthesis
From: Leo Yan
Date: Sun Nov 15 2026 - 08:34:51 EST
Extend the thread-map synthesis test with empty maps and sizes around
the one-record and two-record limits.
Check record sizes, entry counts, continuation flags and entry order.
Reassemble each map and verify PIDs and command names, including missing
names and full-width names without a terminating NUL.
Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
---
tools/perf/tests/thread-map.c | 111 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 111 insertions(+)
diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
index 877868107455e87e526e2b336ca1ca3c7314033e..ced1a357233d68f5b81d6f2136e8cc0d0a95fc15 100644
--- a/tools/perf/tests/thread-map.c
+++ b/tools/perf/tests/thread-map.c
@@ -1,6 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
+#include <stdint.h>
+#include <linux/kernel.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/prctl.h>
@@ -8,6 +11,7 @@
#include "thread_map.h"
#include "debug.h"
#include "event.h"
+#include "tool.h"
#include "util/synthetic-events.h"
#include <perf/event.h>
#include <internal/threadmap.h>
@@ -86,9 +90,110 @@ static int process_event(const struct perf_tool *tool __maybe_unused,
return 0;
}
+struct thread_map_synthesize {
+ struct perf_tool tool;
+ struct perf_thread_map *source;
+ struct perf_thread_map *threads;
+ int offset;
+ int calls;
+};
+
+static int process_thread_map_chunk(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample __maybe_unused,
+ struct machine *machine __maybe_unused)
+{
+ struct thread_map_synthesize *data = container_of(tool, struct thread_map_synthesize, tool);
+ struct perf_record_thread_map *map = &event->thread_map;
+ const int max_nr = (UINT16_MAX - sizeof(*map)) / sizeof(map->entries[0]);
+ int nr = min(data->source->nr - data->offset, max_nr);
+ bool more = data->offset + nr < data->source->nr;
+ int ret;
+
+ data->calls++;
+ TEST_ASSERT_VAL("wrong chunk type", event->header.type == PERF_RECORD_THREAD_MAP);
+ TEST_ASSERT_VAL("wrong chunk nr", map->nr == (u64)nr);
+ TEST_ASSERT_VAL("wrong chunk size",
+ event->header.size == sizeof(*map) + nr * sizeof(map->entries[0]));
+ TEST_ASSERT_VAL("wrong continuation marker",
+ event->header.misc == (more ? PERF_RECORD_MISC_THREAD_MAP_MORE : 0));
+
+ ret = thread_map__append_event(&data->threads, map);
+ if (ret)
+ return ret;
+ data->offset += nr;
+ return 0;
+}
+
+static int check_thread_map_chunks(struct thread_map_synthesize *data)
+{
+ const int max_nr = (UINT16_MAX - sizeof(struct perf_record_thread_map)) /
+ sizeof(struct perf_record_thread_map_entry);
+ int ret;
+
+ ret = perf_event__synthesize_thread_map2(&data->tool, data->source,
+ process_thread_map_chunk, NULL);
+ TEST_ASSERT_VAL("failed to synthesize chunks", !ret);
+ TEST_ASSERT_VAL("wrong chunk count",
+ data->calls == max(1, (data->source->nr + max_nr - 1) / max_nr));
+ TEST_ASSERT_VAL("incomplete thread map", data->offset == data->source->nr);
+ TEST_ASSERT_VAL("failed to assemble thread map", data->threads);
+ TEST_ASSERT_VAL("wrong assembled nr", data->threads->nr == data->source->nr);
+ TEST_ASSERT_VAL("wrong assembled refcnt", refcount_read(&data->threads->refcnt) == 1);
+
+ for (int i = 0; i < data->source->nr; i++) {
+ const char *expected = perf_thread_map__comm(data->source, i);
+ const char *comm = perf_thread_map__comm(data->threads, i);
+
+ if (!expected)
+ expected = "";
+ TEST_ASSERT_VAL("wrong assembled pid",
+ perf_thread_map__pid(data->threads, i) ==
+ perf_thread_map__pid(data->source, i));
+ TEST_ASSERT_VAL("wrong assembled comm", comm && !strncmp(comm, expected, 16));
+ TEST_ASSERT_VAL("assembled comm not terminated",
+ strlen(comm) == strnlen(expected, 16));
+ }
+ return 0;
+}
+
+static int test_thread_map_chunks(int nr)
+{
+ struct thread_map_synthesize data = { 0 };
+ int ret = -ENOMEM;
+
+ data.source = perf_thread_map__new_array(nr, NULL);
+ if (!data.source)
+ return ret;
+
+ for (int i = 0; i < nr; i++) {
+ char comm[32];
+
+ /* Include missing comms and full-width, non-NUL-terminated wire comms. */
+ perf_thread_map__set_pid(data.source, i, nr - i);
+ if (i % 3 == 0)
+ continue;
+ if (i % 3 == 1)
+ snprintf(comm, sizeof(comm), "thread-%d", i);
+ else
+ strcpy(comm, "1234567890123456");
+ data.source->map[i].comm = strdup(comm);
+ if (!data.source->map[i].comm)
+ goto out;
+ }
+
+ ret = check_thread_map_chunks(&data);
+out:
+ perf_thread_map__put(data.threads);
+ perf_thread_map__put(data.source);
+ return ret;
+}
+
static int test__thread_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
struct perf_thread_map *threads;
+ static const int sizes[] = { 0, 1, 2729, 2730, 3000, 5458, 5459 };
+ int ret;
TEST_ASSERT_VAL("failed to set process name",
!prctl(PR_SET_NAME, NAMEUL, 0, 0, 0));
@@ -103,6 +208,12 @@ static int test__thread_map_synthesize(struct test_suite *test __maybe_unused, i
!perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
perf_thread_map__put(threads);
+ for (unsigned int i = 0; i < ARRAY_SIZE(sizes); i++) {
+ ret = test_thread_map_chunks(sizes[i]);
+ if (ret)
+ return ret;
+ }
+
return 0;
}
--
2.34.1