mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Leo Yan <leo.yan@arm.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
	 Namhyung Kim <namhyung@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
	 Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>,
	Suyash Mahar <smahar@meta.com>,  Amir Ayupov <aaupov@fb.com>
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Leo Yan <leo.yan@arm.com>
Subject: [PATCH 3/4] perf test: Cover multi-record thread map synthesis
Date: Sun, 15 Nov 2026 13:34:21 +0000	[thread overview]
Message-ID: <20261115-perf_fix_large_threads-v1-3-69c9b86b9ae5@arm.com> (raw)
In-Reply-To: <20261115-perf_fix_large_threads-v1-0-69c9b86b9ae5@arm.com>

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


  parent reply	other threads:[~2026-11-15 13:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 13:34 [PATCH 0/4] perf: Support thread maps larger than one record Leo Yan
2026-11-15 13:34 ` [PATCH 1/4] perf synthetic-events: Split oversized thread maps into records Leo Yan
2026-11-15 13:34 ` [PATCH 2/4] perf: Assemble split thread maps in script and stat report Leo Yan
2026-11-15 13:34 ` Leo Yan [this message]
2026-11-15 13:34 ` [PATCH 4/4] perf docs: Describe multi-record thread maps Leo Yan

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=20261115-perf_fix_large_threads-v1-3-69c9b86b9ae5@arm.com \
    --to=leo.yan@arm.com \
    --cc=aaupov@fb.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=smahar@meta.com \
    /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

all inboxes | Powered by JetHome®