[PATCH RFC] perf bench futex: Support running benchmark while some cpus are offline

From: Pan Xinhui
Date: Mon Oct 10 2016 - 08:16:30 EST


i%ncpus might not be the correct cpu number which we want to set. If we run
command "perf bench futex all" while some cpus are not online. We will fail to
create pthreads.

Lets fix this by adding int cpu_at_index(int i) which just do a simple traverse
on the cpu_set. And return the correct cpu number.

Signed-off-by: Pan Xinhui <xinhui.pan@xxxxxxxxxxxxxxxxxx>
---
tools/perf/bench/futex-hash.c | 2 +-
tools/perf/bench/futex-lock-pi.c | 2 +-
tools/perf/bench/futex-requeue.c | 2 +-
tools/perf/bench/futex-wake-parallel.c | 2 +-
tools/perf/bench/futex-wake.c | 2 +-
tools/perf/bench/futex.h | 29 +++++++++++++++++++++++++++++
6 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/tools/perf/bench/futex-hash.c b/tools/perf/bench/futex-hash.c
index 8024cd5..3861168 100644
--- a/tools/perf/bench/futex-hash.c
+++ b/tools/perf/bench/futex-hash.c
@@ -161,7 +161,7 @@ int bench_futex_hash(int argc, const char **argv,
goto errmem;

CPU_ZERO(&cpu);
- CPU_SET(i % ncpus, &cpu);
+ CPU_SET(cpu_at_index(i), &cpu);

ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
if (ret)
diff --git a/tools/perf/bench/futex-lock-pi.c b/tools/perf/bench/futex-lock-pi.c
index 936d89d..c5548f5 100644
--- a/tools/perf/bench/futex-lock-pi.c
+++ b/tools/perf/bench/futex-lock-pi.c
@@ -127,7 +127,7 @@ static void create_threads(struct worker *w, pthread_attr_t thread_attr)
worker[i].futex = &global_futex;

CPU_ZERO(&cpu);
- CPU_SET(i % ncpus, &cpu);
+ CPU_SET(cpu_at_index(i), &cpu);

if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
diff --git a/tools/perf/bench/futex-requeue.c b/tools/perf/bench/futex-requeue.c
index f96e22e..d4531da 100644
--- a/tools/perf/bench/futex-requeue.c
+++ b/tools/perf/bench/futex-requeue.c
@@ -90,7 +90,7 @@ static void block_threads(pthread_t *w,
/* create and block all threads */
for (i = 0; i < nthreads; i++) {
CPU_ZERO(&cpu);
- CPU_SET(i % ncpus, &cpu);
+ CPU_SET(cpu_at_index(i), &cpu);

if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
diff --git a/tools/perf/bench/futex-wake-parallel.c b/tools/perf/bench/futex-wake-parallel.c
index 4a2ecd7..9821df0 100644
--- a/tools/perf/bench/futex-wake-parallel.c
+++ b/tools/perf/bench/futex-wake-parallel.c
@@ -126,7 +126,7 @@ static void block_threads(pthread_t *w, pthread_attr_t thread_attr)
/* create and block all threads */
for (i = 0; i < nblocked_threads; i++) {
CPU_ZERO(&cpu);
- CPU_SET(i % ncpus, &cpu);
+ CPU_SET(cpu_at_index(i), &cpu);

if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
diff --git a/tools/perf/bench/futex-wake.c b/tools/perf/bench/futex-wake.c
index 87d8f4f..f8eba6d 100644
--- a/tools/perf/bench/futex-wake.c
+++ b/tools/perf/bench/futex-wake.c
@@ -96,7 +96,7 @@ static void block_threads(pthread_t *w,
/* create and block all threads */
for (i = 0; i < nthreads; i++) {
CPU_ZERO(&cpu);
- CPU_SET(i % ncpus, &cpu);
+ CPU_SET(cpu_at_index(i), &cpu);

if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
diff --git a/tools/perf/bench/futex.h b/tools/perf/bench/futex.h
index b2e06d1..c7a70f0 100644
--- a/tools/perf/bench/futex.h
+++ b/tools/perf/bench/futex.h
@@ -86,6 +86,35 @@ futex_cmp_requeue(u_int32_t *uaddr, u_int32_t val, u_int32_t *uaddr2, int nr_wak
val, opflags);
}

+static inline int cpu_at_index(int i)
+{
+ static cpu_set_t cpu_set;
+ static int first_time = 1;
+ static int total_cpu;
+ int j = -1;
+
+ if (first_time) {
+ CPU_ZERO(&cpu_set);
+
+ if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) < 0)
+ return -1;
+
+ first_time = 0;
+ total_cpu = CPU_COUNT(&cpu_set);
+ }
+
+ i = i % total_cpu;
+
+ do {
+ j++;
+ if (CPU_ISSET(j, &cpu_set))
+ i--;
+ } while (i >= 0);
+
+ return j;
+}
+
+
#ifndef HAVE_PTHREAD_ATTR_SETAFFINITY_NP
#include <pthread.h>
static inline int pthread_attr_setaffinity_np(pthread_attr_t *attr,
--
2.4.11