]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
libperf threadmap: Add perf_thread_map__idx()
authorIan Rogers <irogers@google.com>
Mon, 19 May 2025 19:51:39 +0000 (12:51 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 21 May 2025 18:07:13 +0000 (15:07 -0300)
Allow computation of thread map index from a PID.

Note, with a 'struct perf_cpu_map' the sorted nature allows for a binary
search to compute the index which isn't currently possible with a
'struct perf_thread_map' as they aren't guaranteed sorted.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Gautam Menghani <gautam@linux.ibm.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250519195148.1708988-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/perf/include/perf/threadmap.h
tools/lib/perf/threadmap.c

index 8b40e7777cea7d9ce435f66bd4050a88924eca61..44deb815b8173710831863013f5261feac4ada13 100644 (file)
@@ -14,6 +14,7 @@ LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx,
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
 LIBPERF_API int perf_thread_map__nr(struct perf_thread_map *threads);
 LIBPERF_API pid_t perf_thread_map__pid(struct perf_thread_map *map, int idx);
+LIBPERF_API int perf_thread_map__idx(struct perf_thread_map *map, pid_t pid);
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__get(struct perf_thread_map *map);
 LIBPERF_API void perf_thread_map__put(struct perf_thread_map *map);
index 3ca9ba4987fc0a6bc9fa0901e8f219f3e5c24bce..db431b036f5780f4f16cd2d93d6931e187f5a3ed 100644 (file)
@@ -104,3 +104,15 @@ pid_t perf_thread_map__pid(struct perf_thread_map *map, int idx)
 
        return map->map[idx].pid;
 }
+
+int perf_thread_map__idx(struct perf_thread_map *threads, pid_t pid)
+{
+       if (!threads)
+               return pid == -1 ? 0 : -1;
+
+       for (int i = 0; i < threads->nr; ++i) {
+               if (threads->map[i].pid == pid)
+                       return i;
+       }
+       return -1;
+}