From 722926821124de1893dc869a6728fe40d619f95c Mon Sep 17 00:00:00 2001 From: Tejun Heo Date: Tue, 27 Jun 2023 13:39:32 -1000 Subject: [PATCH] scx_pair: Fix custom stride error handling scx_pair uses the default stride value of nr_cpu_ids / 2, which matches most x86 SMT configurations. However, it does allow specifying a custom stride value with -S so that e.g. neighboring CPUs can be paired up. However, not all stride values work and errors were not reported very well. This patch improves error handling so that scx_pair fails with clear error message if CPUs can't be paired up with the specified stride value. scx_pair now also prints out how CPUs are paired on startup. This should address issues #28 and #29. --- tools/sched_ext/scx_pair.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/tools/sched_ext/scx_pair.c b/tools/sched_ext/scx_pair.c index b35e4f511de6dd..4d24fcedc2cd0f 100644 --- a/tools/sched_ext/scx_pair.c +++ b/tools/sched_ext/scx_pair.c @@ -68,16 +68,37 @@ int main(int argc, char **argv) } } + printf("Pairs: "); for (i = 0; i < skel->rodata->nr_cpu_ids; i++) { - if (skel->rodata->pair_cpu[i] < 0) { - skel->rodata->pair_cpu[i] = i + stride; - skel->rodata->pair_cpu[i + stride] = i; - skel->rodata->pair_id[i] = i; - skel->rodata->pair_id[i + stride] = i; - skel->rodata->in_pair_idx[i] = 0; - skel->rodata->in_pair_idx[i + stride] = 1; + int j = (i + stride) % skel->rodata->nr_cpu_ids; + + if (skel->rodata->pair_cpu[i] >= 0) + continue; + + if (i == j) { + printf("\n"); + fprintf(stderr, "Invalid stride %d - CPU%d wants to be its own pair\n", + stride, i); + return 1; + } + + if (skel->rodata->pair_cpu[j] >= 0) { + printf("\n"); + fprintf(stderr, "Invalid stride %d - three CPUs (%d, %d, %d) want to be a pair\n", + stride, i, j, skel->rodata->pair_cpu[j]); + return 1; } + + skel->rodata->pair_cpu[i] = j; + skel->rodata->pair_cpu[j] = i; + skel->rodata->pair_id[i] = i; + skel->rodata->pair_id[j] = i; + skel->rodata->in_pair_idx[i] = 0; + skel->rodata->in_pair_idx[j] = 1; + + printf("[%d, %d] ", i, j); } + printf("\n"); assert(!scx_pair__load(skel));