Skip to contentSkip to conversation

perf thread_map: Deduplicate numerically equivalent PID and TID strings

PATCH Superseded Hui Su 4 participants 7 messages Sep 12 – Sep 21 perf events

lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH 1/1 perf thread_map: Deduplicate numerically equivalent PID and TID strings

thread_map__new_by_pid_str() and thread_map__new_by_tid_str() attempt to deduplicate adjacent PIDs/TIDs using prev_pid and prev_tid. However, prev_pid and prev_tid were never updated inside the loop, making the check "if (pid == prev_pid)" dead code.

Furthermore, even if prev_pid/prev_tid were updated, strlist sorts lexicographically (e.g. "010", "011", "10"), which means identical numeric values with different string representations (e.g. leading zeros) are not adjacent in the list and would never be deduplicated.

Before this fix, running duplicate TID and PID tests fails:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363297
  FAILED tests/thread-map.c:63 wrong nr for duplicate TIDs
    (nr=3, expected 1)
  test child finished with -1
  ---- end ----
  Thread map: FAILED!

Replace the ineffective prev_pid/prev_tid check with an intlist seen-set. This ensures that any numeric duplicate PID or TID is properly recognized and skipped, regardless of string formatting or order.

Add tests for numerically equivalent PID and TID strings.

After this fix:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363650
  test child finished with 0
  ---- end ----
  Thread map: Ok
Fixes b52956c961be ("perf tools: Allow multiple threads or processes in record, stat, top")
---
 tools/perf/tests/thread-map.c | 33 +++++++++++++++++++++++++++++++++
 tools/perf/util/thread_map.c  | 28 ++++++++++++++++++++--------
 2 files changed, 53 insertions(+), 8 deletions(-)
tools/perf/tests/thread-map.c +33 -0
index 877868107455..0299c8fd0a6a 100644
@@ -56,6 +56,39 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
 	TEST_ASSERT_VAL("wrong refcnt",
 			refcount_read(&map->refcnt) == 1);
 	perf_thread_map__put(map);
+
+	/* test numeric deduplication of TIDs */
+	map = thread_map__new_by_tid_str("123,0123,00123");
+	TEST_ASSERT_VAL("failed to alloc map", map);
+	TEST_ASSERT_VAL("wrong nr for duplicate TIDs", map->nr == 1);
+	TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == 123);
+	perf_thread_map__put(map);
+
+	/* test non-adjacent numeric duplicates (strlist lexicographic: 010, 011, 10) */
+	map = thread_map__new_by_tid_str("010,011,10");
+	TEST_ASSERT_VAL("failed to alloc map", map);
+	TEST_ASSERT_VAL("wrong nr for non-adjacent duplicate TIDs", map->nr == 2);
+	perf_thread_map__put(map);
+
+	/* test numeric deduplication of PIDs */
+	{
+		struct perf_thread_map *base, *dup;
+		char pid_str[64];
+
+		base = thread_map__new_by_pid(getpid());
+		TEST_ASSERT_VAL("failed to alloc baseline map", base);
+
+		snprintf(pid_str, sizeof(pid_str), "%d,0%d", getpid(), getpid());
+
+		dup = thread_map__new_str(pid_str, NULL, false);
+		TEST_ASSERT_VAL("failed to alloc duplicate pid map", dup);
+		TEST_ASSERT_VAL("wrong nr for duplicate PIDs",
+				dup->nr == base->nr);
+
+		perf_thread_map__put(dup);
+		perf_thread_map__put(base);
+	}
+
 	return 0;
 }
 
tools/perf/util/thread_map.c +20 -8
index 48c70f149e92..7b59b3f7f2e4 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include "string2.h"
 #include "strlist.h"
+#include "intlist.h"
 #include <string.h>
 #include <api/fs/fs.h>
 #include <linux/string.h>
@@ -163,12 +164,13 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 	int items, total_tasks = 0;
 	struct dirent **namelist = NULL;
 	int i, j = 0;
-	pid_t pid, prev_pid = INT_MAX;
+	pid_t pid;
 	struct str_node *pos;
 	struct strlist *slist = strlist__new(pid_str, NULL);
+	struct intlist *seen = intlist__new(NULL);
 
-	if (!slist)
-		return NULL;
+	if (!slist || !seen)
+		goto out;
 
 	strlist__for_each_entry(pos, slist) {
 		pid = strtol(pos->s, NULL, 10);
@@ -176,9 +178,12 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 		if (pid == INT_MIN || pid == INT_MAX)
 			goto out_free_threads;
 
-		if (pid == prev_pid)
+		if (intlist__has_entry(seen, (unsigned long)pid))
 			continue;
 
+		if (intlist__add(seen, (unsigned long)pid))
+			goto out_free_threads;
+
 		sprintf(name, "/proc/%d/task", pid);
 		items = scandir(name, &namelist, filter, NULL);
 		if (items <= 0)
@@ -200,6 +205,7 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 	}
 
 out:
+	intlist__delete(seen);
 	strlist__delete(slist);
 	if (threads)
 		refcount_set(&threads->refcnt, 1);
@@ -219,17 +225,19 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 {
 	struct perf_thread_map *threads = NULL, *nt;
 	int ntasks = 0;
-	pid_t tid, prev_tid = INT_MAX;
+	pid_t tid;
 	struct str_node *pos;
 	struct strlist *slist;
+	struct intlist *seen;
 
 	/* perf-stat expects threads to be generated even if tid not given */
 	if (!tid_str)
 		return perf_thread_map__new_dummy();
 
 	slist = strlist__new(tid_str, NULL);
-	if (!slist)
-		return NULL;
+	seen = intlist__new(NULL);
+	if (!slist || !seen)
+		goto out;
 
 	strlist__for_each_entry(pos, slist) {
 		tid = strtol(pos->s, NULL, 10);
@@ -237,9 +245,12 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 		if (tid == INT_MIN || tid == INT_MAX)
 			goto out_free_threads;
 
-		if (tid == prev_tid)
+		if (intlist__has_entry(seen, (unsigned long)tid))
 			continue;
 
+		if (intlist__add(seen, (unsigned long)tid))
+			goto out_free_threads;
+
 		ntasks++;
 		nt = perf_thread_map__realloc(threads, ntasks);
 
@@ -251,6 +262,7 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 		threads->nr = ntasks;
 	}
 out:
+	intlist__delete(seen);
 	strlist__delete(slist);
 	if (threads)
 		refcount_set(&threads->refcnt, 1);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing explicit inclusion of <stdio.h> for snprintf. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing explicit inclusion of <stdio.h> for snprintf.
--

commit b1526ce24c053489c0a3385a95c4492a0906a746
Author: Hui Su <[email protected]>

perf thread_map: Deduplicate numerically equivalent PID and TID strings

This commit replaces the ineffective prev_pid and prev_tid checks with an intlist seen-set to correctly deduplicate numerically equivalent PID and TID strings. It also adds new test cases to verify this behavior.

Quoted 5 lines
tools/perf/tests/thread-map.c +0 -0
index 877868107455e..0299c8fd0a6a9 100644
@@ -56,6 +56,39 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest

[ ... ]

tools/perf/tests/thread-map.c static int test__thread_map(struct test_suite *test __maybe_unused, int subtest · line 81

Quoted 9 lines
tools/perf/tests/thread-map.c +9 -0
+	/* test numeric deduplication of PIDs */
+	{
+		struct perf_thread_map *base, *dup;
+		char pid_str[64];
+
+		base = thread_map__new_by_pid(getpid());
+		TEST_ASSERT_VAL("failed to alloc baseline map", base);
+
+		snprintf(pid_str, sizeof(pid_str), "%d,0%d", getpid(), getpid());

[Severity: Low]
Does test__thread_map() in tools/perf/tests/thread-map.c need an explicit include of <stdio.h> for snprintf?

The global review guidelines mandate explicit inclusion of POSIX standard headers for all libc functions to ensure musl libc compatibility, preventing reliance on implicit inclusion. Since this introduces a new call to snprintf, strict libc implementations like musl may fail to compile without the direct header inclusion.

Quoted 12 lines
tools/perf/tests/thread-map.c +10 -0
+
+		dup = thread_map__new_str(pid_str, NULL, false);
+		TEST_ASSERT_VAL("failed to alloc duplicate pid map", dup);
+		TEST_ASSERT_VAL("wrong nr for duplicate PIDs",
+				dup->nr == base->nr);
+
+		perf_thread_map__put(dup);
+		perf_thread_map__put(base);
+	}
+
 	return 0;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw

PATCH v2 1/1 perf thread_map: Deduplicate numerically equivalent PID and TID strings

In reply to Hui Su

thread_map__new_by_pid_str() and thread_map__new_by_tid_str() attempt to deduplicate adjacent PIDs/TIDs using prev_pid and prev_tid. However, prev_pid and prev_tid were never updated inside the loop, making the check "if (pid == prev_pid)" dead code.

Furthermore, even if prev_pid/prev_tid were updated, strlist sorts lexicographically (e.g. "010", "011", "10"), which means identical numeric values with different string representations (e.g. leading zeros) are not adjacent in the list and would never be deduplicated.

Before this fix, the new tests fail at the first duplicate TID case:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363297
  FAILED tests/thread-map.c:63 wrong nr for duplicate TIDs
    (nr=3, expected 1)
  test child finished with -1
  ---- end ----
  Thread map: FAILED!

Replace the ineffective prev_pid/prev_tid check with an intlist seen-set. This ensures that any numeric duplicate PID or TID is properly recognized and skipped, regardless of string formatting or order.

Add tests for numerically equivalent PID and TID strings.

After this fix:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363650
  test child finished with 0
  ---- end ----
  Thread map: Ok
Fixes b52956c961be ("perf tools: Allow multiple threads or processes in record, stat, top")

---
Differences in v2:
- Explicitly include <stdio.h> in tools/perf/tests/thread-map.c for snprintf.
- Link to v1: https://lore.kernel.org/r/[email protected]/

 tools/perf/tests/thread-map.c | 34 ++++++++++++++++++++++++++++++++++
 tools/perf/util/thread_map.c  | 28 ++++++++++++++++++++--------
 2 files changed, 54 insertions(+), 8 deletions(-)
tools/perf/tests/thread-map.c +34 -0
index 877868107455..690ccae17c31 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -56,6 +57,39 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
 	TEST_ASSERT_VAL("wrong refcnt",
 			refcount_read(&map->refcnt) == 1);
 	perf_thread_map__put(map);
+
+	/* test numeric deduplication of TIDs */
+	map = thread_map__new_by_tid_str("123,0123,00123");
+	TEST_ASSERT_VAL("failed to alloc map", map);
+	TEST_ASSERT_VAL("wrong nr for duplicate TIDs", map->nr == 1);
+	TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == 123);
+	perf_thread_map__put(map);
+
+	/* test non-adjacent numeric duplicates (strlist lexicographic: 010, 011, 10) */
+	map = thread_map__new_by_tid_str("010,011,10");
+	TEST_ASSERT_VAL("failed to alloc map", map);
+	TEST_ASSERT_VAL("wrong nr for non-adjacent duplicate TIDs", map->nr == 2);
+	perf_thread_map__put(map);
+
+	/* test numeric deduplication of PIDs */
+	{
+		struct perf_thread_map *base, *dup;
+		char pid_str[64];
+
+		base = thread_map__new_by_pid(getpid());
+		TEST_ASSERT_VAL("failed to alloc baseline map", base);
+
+		snprintf(pid_str, sizeof(pid_str), "%d,0%d", getpid(), getpid());
+
+		dup = thread_map__new_str(pid_str, NULL, false);
+		TEST_ASSERT_VAL("failed to alloc duplicate pid map", dup);
+		TEST_ASSERT_VAL("wrong nr for duplicate PIDs",
+				dup->nr == base->nr);
+
+		perf_thread_map__put(dup);
+		perf_thread_map__put(base);
+	}
+
 	return 0;
 }
 
tools/perf/util/thread_map.c +20 -8
index 48c70f149e92..7b59b3f7f2e4 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include "string2.h"
 #include "strlist.h"
+#include "intlist.h"
 #include <string.h>
 #include <api/fs/fs.h>
 #include <linux/string.h>
@@ -163,12 +164,13 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 	int items, total_tasks = 0;
 	struct dirent **namelist = NULL;
 	int i, j = 0;
-	pid_t pid, prev_pid = INT_MAX;
+	pid_t pid;
 	struct str_node *pos;
 	struct strlist *slist = strlist__new(pid_str, NULL);
+	struct intlist *seen = intlist__new(NULL);
 
-	if (!slist)
-		return NULL;
+	if (!slist || !seen)
+		goto out;
 
 	strlist__for_each_entry(pos, slist) {
 		pid = strtol(pos->s, NULL, 10);
@@ -176,9 +178,12 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 		if (pid == INT_MIN || pid == INT_MAX)
 			goto out_free_threads;
 
-		if (pid == prev_pid)
+		if (intlist__has_entry(seen, (unsigned long)pid))
 			continue;
 
+		if (intlist__add(seen, (unsigned long)pid))
+			goto out_free_threads;
+
 		sprintf(name, "/proc/%d/task", pid);
 		items = scandir(name, &namelist, filter, NULL);
 		if (items <= 0)
@@ -200,6 +205,7 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 	}
 
 out:
+	intlist__delete(seen);
 	strlist__delete(slist);
 	if (threads)
 		refcount_set(&threads->refcnt, 1);
@@ -219,17 +225,19 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 {
 	struct perf_thread_map *threads = NULL, *nt;
 	int ntasks = 0;
-	pid_t tid, prev_tid = INT_MAX;
+	pid_t tid;
 	struct str_node *pos;
 	struct strlist *slist;
+	struct intlist *seen;
 
 	/* perf-stat expects threads to be generated even if tid not given */
 	if (!tid_str)
 		return perf_thread_map__new_dummy();
 
 	slist = strlist__new(tid_str, NULL);
-	if (!slist)
-		return NULL;
+	seen = intlist__new(NULL);
+	if (!slist || !seen)
+		goto out;
 
 	strlist__for_each_entry(pos, slist) {
 		tid = strtol(pos->s, NULL, 10);
@@ -237,9 +245,12 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 		if (tid == INT_MIN || tid == INT_MAX)
 			goto out_free_threads;
 
-		if (tid == prev_tid)
+		if (intlist__has_entry(seen, (unsigned long)tid))
 			continue;
 
+		if (intlist__add(seen, (unsigned long)tid))
+			goto out_free_threads;
+
 		ntasks++;
 		nt = perf_thread_map__realloc(threads, ntasks);
 
@@ -251,6 +262,7 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 		threads->nr = ntasks;
 	}
 out:
+	intlist__delete(seen);
 	strlist__delete(slist);
 	if (threads)
 		refcount_set(&threads->refcnt, 1);
Signature · 1 line
2.55.0
#3 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

thread_map__new_by_pid_str() and thread_map__new_by_tid_str() attempt to deduplicate adjacent PIDs/TIDs using prev_pid and prev_tid. However, prev_pid and prev_tid were never updated inside the loop, making the check "if (pid == prev_pid)" dead code.

Furthermore, even if prev_pid/prev_tid were updated, strlist sorts lexicographically (e.g. "010", "011", "10"), which means identical numeric values with different string representations (e.g. leading zeros) are not adjacent in the list and would never be deduplicated.

Before this fix, the new tests fail at the first duplicate TID case:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
[ ... ]
  ---- end ----
  Thread map: Ok
Quoted 2 lines
Fixes b52956c961be ("perf tools: Allow multiple threads or processes in record, stat, top")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v2 1/1
#4 lore raw
In reply to Hui Su
Quoted 38 lines · Hui Su

thread_map__new_by_pid_str() and thread_map__new_by_tid_str() attempt to deduplicate adjacent PIDs/TIDs using prev_pid and prev_tid. However, prev_pid and prev_tid were never updated inside the loop, making the check "if (pid == prev_pid)" dead code.

Furthermore, even if prev_pid/prev_tid were updated, strlist sorts lexicographically (e.g. "010", "011", "10"), which means identical numeric values with different string representations (e.g. leading zeros) are not adjacent in the list and would never be deduplicated.

Before this fix, the new tests fail at the first duplicate TID case:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363297
  FAILED tests/thread-map.c:63 wrong nr for duplicate TIDs
    (nr=3, expected 1)
  test child finished with -1
  ---- end ----
  Thread map: FAILED!

Replace the ineffective prev_pid/prev_tid check with an intlist seen-set. This ensures that any numeric duplicate PID or TID is properly recognized and skipped, regardless of string formatting or order.

Add tests for numerically equivalent PID and TID strings.

After this fix:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363650
  test child finished with 0
  ---- end ----
  Thread map: Ok
Quoted 2 lines
Fixes b52956c961be ("perf tools: Allow multiple threads or processes in record, stat, top")

Thanks,
Namhyung

Quoted 158 lines

---
Differences in v2:
- Explicitly include <stdio.h> in tools/perf/tests/thread-map.c for snprintf.
- Link to v1: https://lore.kernel.org/r/[email protected]/

 tools/perf/tests/thread-map.c | 34 ++++++++++++++++++++++++++++++++++
 tools/perf/util/thread_map.c  | 28 ++++++++++++++++++++--------
 2 files changed, 54 insertions(+), 8 deletions(-)
tools/perf/tests/thread-map.c +34 -0
index 877868107455..690ccae17c31 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -56,6 +57,39 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
 	TEST_ASSERT_VAL("wrong refcnt",
 			refcount_read(&map->refcnt) == 1);
 	perf_thread_map__put(map);
+
+	/* test numeric deduplication of TIDs */
+	map = thread_map__new_by_tid_str("123,0123,00123");
+	TEST_ASSERT_VAL("failed to alloc map", map);
+	TEST_ASSERT_VAL("wrong nr for duplicate TIDs", map->nr == 1);
+	TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == 123);
+	perf_thread_map__put(map);
+
+	/* test non-adjacent numeric duplicates (strlist lexicographic: 010, 011, 10) */
+	map = thread_map__new_by_tid_str("010,011,10");
+	TEST_ASSERT_VAL("failed to alloc map", map);
+	TEST_ASSERT_VAL("wrong nr for non-adjacent duplicate TIDs", map->nr == 2);
+	perf_thread_map__put(map);
+
+	/* test numeric deduplication of PIDs */
+	{
+		struct perf_thread_map *base, *dup;
+		char pid_str[64];
+
+		base = thread_map__new_by_pid(getpid());
+		TEST_ASSERT_VAL("failed to alloc baseline map", base);
+
+		snprintf(pid_str, sizeof(pid_str), "%d,0%d", getpid(), getpid());
+
+		dup = thread_map__new_str(pid_str, NULL, false);
+		TEST_ASSERT_VAL("failed to alloc duplicate pid map", dup);
+		TEST_ASSERT_VAL("wrong nr for duplicate PIDs",
+				dup->nr == base->nr);
+
+		perf_thread_map__put(dup);
+		perf_thread_map__put(base);
+	}
+
 	return 0;
 }
 
tools/perf/util/thread_map.c +20 -8
index 48c70f149e92..7b59b3f7f2e4 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include "string2.h"
 #include "strlist.h"
+#include "intlist.h"
 #include <string.h>
 #include <api/fs/fs.h>
 #include <linux/string.h>
@@ -163,12 +164,13 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 	int items, total_tasks = 0;
 	struct dirent **namelist = NULL;
 	int i, j = 0;
-	pid_t pid, prev_pid = INT_MAX;
+	pid_t pid;
 	struct str_node *pos;
 	struct strlist *slist = strlist__new(pid_str, NULL);
+	struct intlist *seen = intlist__new(NULL);
 
-	if (!slist)
-		return NULL;
+	if (!slist || !seen)
+		goto out;
 
 	strlist__for_each_entry(pos, slist) {
 		pid = strtol(pos->s, NULL, 10);
@@ -176,9 +178,12 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 		if (pid == INT_MIN || pid == INT_MAX)
 			goto out_free_threads;
 
-		if (pid == prev_pid)
+		if (intlist__has_entry(seen, (unsigned long)pid))
 			continue;
 
+		if (intlist__add(seen, (unsigned long)pid))
+			goto out_free_threads;
+
 		sprintf(name, "/proc/%d/task", pid);
 		items = scandir(name, &namelist, filter, NULL);
 		if (items <= 0)
@@ -200,6 +205,7 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
 	}
 
 out:
+	intlist__delete(seen);
 	strlist__delete(slist);
 	if (threads)
 		refcount_set(&threads->refcnt, 1);
@@ -219,17 +225,19 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 {
 	struct perf_thread_map *threads = NULL, *nt;
 	int ntasks = 0;
-	pid_t tid, prev_tid = INT_MAX;
+	pid_t tid;
 	struct str_node *pos;
 	struct strlist *slist;
+	struct intlist *seen;
 
 	/* perf-stat expects threads to be generated even if tid not given */
 	if (!tid_str)
 		return perf_thread_map__new_dummy();
 
 	slist = strlist__new(tid_str, NULL);
-	if (!slist)
-		return NULL;
+	seen = intlist__new(NULL);
+	if (!slist || !seen)
+		goto out;
 
 	strlist__for_each_entry(pos, slist) {
 		tid = strtol(pos->s, NULL, 10);
@@ -237,9 +245,12 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 		if (tid == INT_MIN || tid == INT_MAX)
 			goto out_free_threads;
 
-		if (tid == prev_tid)
+		if (intlist__has_entry(seen, (unsigned long)tid))
 			continue;
 
+		if (intlist__add(seen, (unsigned long)tid))
+			goto out_free_threads;
+
 		ntasks++;
 		nt = perf_thread_map__realloc(threads, ntasks);
 
@@ -251,6 +262,7 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 		threads->nr = ntasks;
 	}
 out:
+	intlist__delete(seen);
 	strlist__delete(slist);
 	if (threads)
 		refcount_set(&threads->refcnt, 1);
Signature · 1 line
2.55.0
reviewer
#5 lore raw
In reply to Hui Su
Quoted 113 lines · Hui Su

thread_map__new_by_pid_str() and thread_map__new_by_tid_str() attempt to deduplicate adjacent PIDs/TIDs using prev_pid and prev_tid. However, prev_pid and prev_tid were never updated inside the loop, making the check "if (pid == prev_pid)" dead code.

Furthermore, even if prev_pid/prev_tid were updated, strlist sorts lexicographically (e.g. "010", "011", "10"), which means identical numeric values with different string representations (e.g. leading zeros) are not adjacent in the list and would never be deduplicated.

Before this fix, the new tests fail at the first duplicate TID case:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363297
  FAILED tests/thread-map.c:63 wrong nr for duplicate TIDs
    (nr=3, expected 1)
  test child finished with -1
  ---- end ----
  Thread map: FAILED!

Replace the ineffective prev_pid/prev_tid check with an intlist seen-set. This ensures that any numeric duplicate PID or TID is properly recognized and skipped, regardless of string formatting or order.

Add tests for numerically equivalent PID and TID strings.

After this fix:

  $ perf test -v 35
  35: Thread map                                                      :
  --- start ---
  test child forked, pid 363650
  test child finished with 0
  ---- end ----
  Thread map: Ok
Fixes b52956c961be ("perf tools: Allow multiple threads or processes in record, stat, top")

---
Differences in v2:
- Explicitly include <stdio.h> in tools/perf/tests/thread-map.c for snprintf.
- Link to v1: https://lore.kernel.org/r/[email protected]/

 tools/perf/tests/thread-map.c | 34 ++++++++++++++++++++++++++++++++++
 tools/perf/util/thread_map.c  | 28 ++++++++++++++++++++--------
 2 files changed, 54 insertions(+), 8 deletions(-)
tools/perf/tests/thread-map.c +34 -0
index 877868107455..690ccae17c31 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -56,6 +57,39 @@ static int test__thread_map(struct test_suite *test __maybe_unused, int subtest
        TEST_ASSERT_VAL("wrong refcnt",
                        refcount_read(&map->refcnt) == 1);
        perf_thread_map__put(map);
+
+       /* test numeric deduplication of TIDs */
+       map = thread_map__new_by_tid_str("123,0123,00123");
+       TEST_ASSERT_VAL("failed to alloc map", map);
+       TEST_ASSERT_VAL("wrong nr for duplicate TIDs", map->nr == 1);
+       TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == 123);
+       perf_thread_map__put(map);
+
+       /* test non-adjacent numeric duplicates (strlist lexicographic: 010, 011, 10) */
+       map = thread_map__new_by_tid_str("010,011,10");
+       TEST_ASSERT_VAL("failed to alloc map", map);
+       TEST_ASSERT_VAL("wrong nr for non-adjacent duplicate TIDs", map->nr == 2);
+       perf_thread_map__put(map);
+
+       /* test numeric deduplication of PIDs */
+       {
+               struct perf_thread_map *base, *dup;
+               char pid_str[64];
+
+               base = thread_map__new_by_pid(getpid());
+               TEST_ASSERT_VAL("failed to alloc baseline map", base);
+
+               snprintf(pid_str, sizeof(pid_str), "%d,0%d", getpid(), getpid());
+
+               dup = thread_map__new_str(pid_str, NULL, false);
+               TEST_ASSERT_VAL("failed to alloc duplicate pid map", dup);
+               TEST_ASSERT_VAL("wrong nr for duplicate PIDs",
+                               dup->nr == base->nr);
+
+               perf_thread_map__put(dup);
+               perf_thread_map__put(base);
+       }
+
        return 0;
 }
 
tools/perf/util/thread_map.c +1 -0
index 48c70f149e92..7b59b3f7f2e4 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include "string2.h"
 #include "strlist.h"
+#include "intlist.h"
 #include <string.h>
 #include <api/fs/fs.h>
 #include <linux/string.h>

tools/perf/util/thread_map.c static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str) · line 170

Quoted 8 lines
tools/perf/util/thread_map.c +2 -1
@@ -163,12 +164,13 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
        int items, total_tasks = 0;
        struct dirent **namelist = NULL;
        int i, j = 0;
-       pid_t pid, prev_pid = INT_MAX;
+       pid_t pid;
        struct str_node *pos;
        struct strlist *slist = strlist__new(pid_str, NULL);
+       struct intlist *seen = intlist__new(NULL);

Why not parse the pid_str into the intlist and then remove duplicates?

Thanks,
Ian

Quoted 78 lines
tools/perf/util/thread_map.c +17 -8
-       if (!slist)
-               return NULL;
+       if (!slist || !seen)
+               goto out;
 
        strlist__for_each_entry(pos, slist) {
                pid = strtol(pos->s, NULL, 10);
@@ -176,9 +178,12 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
                if (pid == INT_MIN || pid == INT_MAX)
                        goto out_free_threads;
 
-               if (pid == prev_pid)
+               if (intlist__has_entry(seen, (unsigned long)pid))
                        continue;
 
+               if (intlist__add(seen, (unsigned long)pid))
+                       goto out_free_threads;
+
                sprintf(name, "/proc/%d/task", pid);
                items = scandir(name, &namelist, filter, NULL);
                if (items <= 0)
@@ -200,6 +205,7 @@ static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
        }
 
 out:
+       intlist__delete(seen);
        strlist__delete(slist);
        if (threads)
                refcount_set(&threads->refcnt, 1);
@@ -219,17 +225,19 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 {
        struct perf_thread_map *threads = NULL, *nt;
        int ntasks = 0;
-       pid_t tid, prev_tid = INT_MAX;
+       pid_t tid;
        struct str_node *pos;
        struct strlist *slist;
+       struct intlist *seen;
 
        /* perf-stat expects threads to be generated even if tid not given */
        if (!tid_str)
                return perf_thread_map__new_dummy();
 
        slist = strlist__new(tid_str, NULL);
-       if (!slist)
-               return NULL;
+       seen = intlist__new(NULL);
+       if (!slist || !seen)
+               goto out;
 
        strlist__for_each_entry(pos, slist) {
                tid = strtol(pos->s, NULL, 10);
@@ -237,9 +245,12 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
                if (tid == INT_MIN || tid == INT_MAX)
                        goto out_free_threads;
 
-               if (tid == prev_tid)
+               if (intlist__has_entry(seen, (unsigned long)tid))
                        continue;
 
+               if (intlist__add(seen, (unsigned long)tid))
+                       goto out_free_threads;
+
                ntasks++;
                nt = perf_thread_map__realloc(threads, ntasks);
 
@@ -251,6 +262,7 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
                threads->nr = ntasks;
        }
 out:
+       intlist__delete(seen);
        strlist__delete(slist);
        if (threads)
                refcount_set(&threads->refcnt, 1);
--

2.55.0

author
#6 lore raw
Quoted 1 line

Why not parse the pid_str into the intlist and then remove duplicates?

Good point. I initially kept the strlist and used a separate intlist as a seen-set to minimize the change.

One wrinkle is that intlist__new(pid_str) currently treats duplicate numeric values as an error: intlist__parse_list() propagates -EEXIST from intlist__add(). Thus, "123,0123" currently makes intlist__new() fail instead of deduplicating the values.

I think the cleaner solution is to make intlist parsing fold duplicate values, for example by using intlist__findnew(), and then iterate the intlist directly in the PID/TID paths. That removes both the extra strlist and the separate seen-set.

I'll check whether any existing intlist__new() users rely on duplicates being rejected, and whether any thread_map callers rely on the current lexicographic ordering from strlist, since intlist iteration is numeric.

I'll rework it that way for v3.

Thanks,
Hui