Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libperf threadmap: Add perf_thread_map__idx()

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>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
3ee2255c eead8a01

+13
+1
tools/lib/perf/include/perf/threadmap.h
··· 14 14 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx); 15 15 LIBPERF_API int perf_thread_map__nr(struct perf_thread_map *threads); 16 16 LIBPERF_API pid_t perf_thread_map__pid(struct perf_thread_map *map, int idx); 17 + LIBPERF_API int perf_thread_map__idx(struct perf_thread_map *map, pid_t pid); 17 18 18 19 LIBPERF_API struct perf_thread_map *perf_thread_map__get(struct perf_thread_map *map); 19 20 LIBPERF_API void perf_thread_map__put(struct perf_thread_map *map);
+12
tools/lib/perf/threadmap.c
··· 104 104 105 105 return map->map[idx].pid; 106 106 } 107 + 108 + int perf_thread_map__idx(struct perf_thread_map *threads, pid_t pid) 109 + { 110 + if (!threads) 111 + return pid == -1 ? 0 : -1; 112 + 113 + for (int i = 0; i < threads->nr; ++i) { 114 + if (threads->map[i].pid == pid) 115 + return i; 116 + } 117 + return -1; 118 + }