Skip to content

Commit

Permalink
libperf cpumap: Use binary search in perf_cpu_map__idx() as array are…
Browse files Browse the repository at this point in the history
… sorted

Since 7074674 ("perf cpumap: Maintain cpumaps ordered and
without dups") perf_cpu_map elements are sorted in ascending order.

This patch improves perf_cpu_map__idx() by using a binary search.

Signed-off-by: Riccardo Mancini <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Ian Rogers <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lore.kernel.org/lkml/f1543c15797169c21e8b205a4a6751159180580d.1629490974.git.rickyman7@gmail.com
[ Removed 'else' after if + return, declared variables where needed ]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
Manciukic authored and acmel committed Oct 8, 2021
1 parent 47e7dd3 commit 73e40c9
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tools/lib/perf/cpumap.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,19 @@ bool perf_cpu_map__empty(const struct perf_cpu_map *map)

int perf_cpu_map__idx(struct perf_cpu_map *cpus, int cpu)
{
int i;
int low = 0, high = cpus->nr;

for (i = 0; i < cpus->nr; ++i) {
if (cpus->map[i] == cpu)
return i;
while (low < high) {
int idx = (low + high) / 2,
cpu_at_idx = cpus->map[idx];

if (cpu_at_idx == cpu)
return idx;

if (cpu_at_idx > cpu)
high = idx;
else
low = idx + 1;
}

return -1;
Expand Down

0 comments on commit 73e40c9

Please sign in to comment.