Skip to content

Commit

Permalink
selftests/bpf: Ensure that next_cpu() returns a valid CPU number
Browse files Browse the repository at this point in the history
When using option -a without --prod-affinity or --cons-affinity, if the
number of producers and consumers is greater than the number of online
CPUs, the benchmark will fail to run as shown below:

  $ getconf _NPROCESSORS_ONLN
  8
  $ ./bench bpf-loop -a -p9
  Setting up benchmark 'bpf-loop'...
  setting affinity to CPU #8 failed: -22

Fix it by returning the remainder of next_cpu divided by the number of
online CPUs in next_cpu().

Signed-off-by: Hou Tao <houtao1@huawei.com>
Link: https://lore.kernel.org/r/20230613080921.1623219-4-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Hou Tao authored and Alexei Starovoitov committed Jun 19, 2023
1 parent ea400d1 commit da77ae2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tools/testing/selftests/bpf/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ static int next_cpu(struct cpu_set *cpu_set)
exit(1);
}

return cpu_set->next_cpu++;
return cpu_set->next_cpu++ % env.nr_cpus;
}

static struct bench_state {
Expand Down Expand Up @@ -659,6 +659,7 @@ static void collect_measurements(long delta_ns) {

int main(int argc, char **argv)
{
env.nr_cpus = get_nprocs();
parse_cmdline_args_init(argc, argv);

if (env.list) {
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct env {
bool quiet;
int consumer_cnt;
int producer_cnt;
int nr_cpus;
struct cpu_set prod_cpus;
struct cpu_set cons_cpus;
};
Expand Down

0 comments on commit da77ae2

Please sign in to comment.