From 50b5860bcdc7dfb448c98c913203184e339756d7 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Fri, 28 Feb 2014 15:08:25 -0800 Subject: [PATCH] cpus_allowed_policy fixups - Make 'split' roundrobin the CPUs, if we have more threads than CPUs in the set. - Properly use a CPU index, don't assume a sequential set of CPUs. Signed-off-by: Jens Axboe --- options.c | 17 ++++++++++++----- os/os-freebsd.h | 1 + os/os-linux.h | 1 + os/os-solaris.h | 25 +++++++++++++++++++++++++ os/os-windows.h | 5 +++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/options.c b/options.c index c1a8f323..4ff4c9b4 100644 --- a/options.c +++ b/options.c @@ -394,16 +394,23 @@ static int str_exitall_cb(void) } #ifdef FIO_HAVE_CPU_AFFINITY -int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu) +int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index) { + unsigned int i, index, cpus_in_mask; const long max_cpu = cpus_online(); - unsigned int i; + cpus_in_mask = fio_cpu_count(mask); + cpu_index = cpu_index % cpus_in_mask; + + index = 0; for (i = 0; i < max_cpu; i++) { - if (cpu != i) { - fio_cpu_clear(mask, i); + if (!fio_cpu_isset(mask, i)) continue; - } + + if (cpu_index != index) + fio_cpu_clear(mask, i); + + index++; } return fio_cpu_count(mask); diff --git a/os/os-freebsd.h b/os/os-freebsd.h index 402792a0..e35c8354 100644 --- a/os/os-freebsd.h +++ b/os/os-freebsd.h @@ -32,6 +32,7 @@ typedef cpuset_t os_cpu_mask_t; #define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask)) #define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask)) +#define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask)) #define fio_cpu_count(maks) CPU_COUNT((mask)) static inline int fio_cpuset_init(os_cpu_mask_t *mask) diff --git a/os/os-linux.h b/os/os-linux.h index 3ed8c2ef..ef80ce2b 100644 --- a/os/os-linux.h +++ b/os/os-linux.h @@ -61,6 +61,7 @@ typedef struct drand48_data os_random_state_t; #define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask)) #define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask)) +#define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask)) #define fio_cpu_count(maks) CPU_COUNT((mask)) static inline int fio_cpuset_init(os_cpu_mask_t *mask) diff --git a/os/os-solaris.h b/os/os-solaris.h index 7a0a3f0b..c8896b8c 100644 --- a/os/os-solaris.h +++ b/os/os-solaris.h @@ -103,6 +103,31 @@ static inline int fio_set_odirect(int fd) #define fio_cpu_clear(mask, cpu) pset_assign(PS_NONE, (cpu), NULL) #define fio_cpu_set(mask, cpu) pset_assign(*(mask), (cpu), NULL) +static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu) +{ + const unsigned int max_cpus = cpus_online(); + processorid_t *cpus; + int i, ret; + + cpus = malloc(sizeof(*cpus) * max_cpus); + + if (pset_info(*mask, NULL, &num_cpus, cpus) < 0) { + free(cpus); + return 0; + } + + ret = 0; + for (i = 0; i < max_cpus; i++) { + if (cpus[i] == cpu) { + ret = 1; + break; + } + } + + free(cpus); + return ret; +} + static inline int fio_cpuset_init(os_cpu_mask_t *mask) { if (pset_create(mask) < 0) diff --git a/os/os-windows.h b/os/os-windows.h index 243edc67..49f96068 100644 --- a/os/os-windows.h +++ b/os/os-windows.h @@ -215,6 +215,11 @@ static inline void fio_cpu_set(os_cpu_mask_t *mask, int cpu) *mask |= 1 << cpu; } +static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu) +{ + return (*mask & (1U << cpu)); +} + static inline int fio_cpu_count(os_cpu_mask_t *mask) { return hweight64(*mask); -- 2.25.1