Skip to content

Commit 3ee2255

Browse files
captain5050acmel
authored andcommitted
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 <[email protected]> Acked-by: Gautam Menghani <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Howard Chu <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent eead8a0 commit 3ee2255

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

tools/lib/perf/include/perf/threadmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int idx,
1414
LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int idx);
1515
LIBPERF_API int perf_thread_map__nr(struct perf_thread_map *threads);
1616
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);
1718

1819
LIBPERF_API struct perf_thread_map *perf_thread_map__get(struct perf_thread_map *map);
1920
LIBPERF_API void perf_thread_map__put(struct perf_thread_map *map);

tools/lib/perf/threadmap.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,15 @@ pid_t perf_thread_map__pid(struct perf_thread_map *map, int idx)
104104

105105
return map->map[idx].pid;
106106
}
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+
}

0 commit comments

Comments
 (0)