From d8540fc53386cd7ff270085aca621e32772832ba Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Sun, 10 Jan 2010 14:24:31 -0500 Subject: [PATCH] Support earlier glibc sched_setaffinity Signed-off-by: Mathieu Desnoyers Signed-off-by: Pedro Alves --- configure.ac | 83 ++++++++++++++++++++++++++++++++++++++ tests/api_gcc.h | 14 +++++++ tests/api_x86.h | 14 +++++++ tests/test_mutex.c | 13 ++++++ tests/test_perthreadlock.c | 13 ++++++ tests/test_qsbr.c | 13 ++++++ tests/test_qsbr_gc.c | 14 +++++++ tests/test_rwlock.c | 14 +++++++ tests/test_rwlock_timing.c | 1 + tests/test_urcu.c | 14 +++++++ tests/test_urcu_assign.c | 14 +++++++ tests/test_urcu_bp.c | 14 +++++++ tests/test_urcu_defer.c | 14 +++++++ tests/test_urcu_gc.c | 14 +++++++ 14 files changed, 249 insertions(+) diff --git a/configure.ac b/configure.ac index 28409d9..6122978 100644 --- a/configure.ac +++ b/configure.ac @@ -119,6 +119,89 @@ else fi ] +# From the sched_setaffinity(2)'s man page: +# ~~~~ +# The CPU affinity system calls were introduced in Linux kernel 2.5.8. +# The library interfaces were introduced in glibc 2.3. Initially, the +# glibc interfaces included a cpusetsize argument. In glibc 2.3.3, +# the cpuset size argument was removed, but this argument was +# restored in glibc 2.3.4. +# ~~~~ + +# In addition to that, some vendors ported the system call to 2.4 +# kernels. + +# Furthermore, when the function first appeared, the MASK argument was +# an unsigned long pointer, while later it was made into a cpu_set_t +# pointer. Systems that have the cpu_set_t version also should have +# the CPU_ZERO, CPU_SET, etc. macros. + +# All this mess means we have to cater for at least 3 different +# sched_setaffinity prototypes: + +# ~~~~ +# int sched_setaffinity (pid_t pid, unsigned int len, unsigned long *mask); +# int sched_setaffinity (pid_t __pid, size_t __cpusetsize, const cpu_set_t *__cpuset); +# int sched_setaffinity (pid_t __pid, const cpu_set_t *__mask); +# ~~~~ + +dnl Since we define _GNU_SOURCE in the sources, must do so too in the +dnl autoconf tests, as defining _GNU_SOURCE or not exposes +dnl sched_setaffinity bits differently. +saved_CFLAGS=$CFLAGS +CFLAGS="$CFLAGS -D_GNU_SOURCE" + +# First check if the function is available at all. +AC_CHECK_FUNCS( + [sched_setaffinity], + [ # Okay, we have it. Check if also have cpu_set_t. If we don't, + # then we have the first version using unsigned long, and no + # CPU_ZERO, etc. macros. If we do have cpu_set_t, we may have the + # version with 2 or 3 arguments. In that case, CPU_ZERO, etc., + # should also be present, but we confirm nonetheless. + + AC_CHECK_TYPES( + [cpu_set_t], + [ # We do have it. Confirm that we have CPU_ZERO, and it actually works. + AC_MSG_CHECKING([whether CPU_ZERO works]) + AH_TEMPLATE([HAVE_CPU_ZERO], + [Define to 1 if we have CPU_ZERO and if it works]) + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [#define _GNU_SOURCE + #include ], + [cpu_set_t foo; CPU_ZERO (&foo);]) + ], + [ # Works! + AC_DEFINE(HAVE_CPU_ZERO, 1) + AC_MSG_RESULT([yes]) + ], + [AC_MSG_RESULT([no])] + ) + + # Check how many arguments does sched_setaffinity take. + # Should be 3 or 2. + AC_MSG_CHECKING([how many arguments sched_setaffinity takes]) + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [#include ], + [cpu_set_t foo; sched_setaffinity (0, sizeof (foo), &foo);]) + ], + [sched_set_affinity_args=3], + [sched_set_affinity_args=2]) + AC_DEFINE_UNQUOTED(SCHED_SETAFFINITY_ARGS, $sched_set_affinity_args, + [Define to sched_setaffinity's number of arguments.]) + AC_MSG_RESULT([$sched_set_affinity_args]) + ], + [ # No cpu_set_t, always 3 args. + AC_DEFINE(SCHED_SETAFFINITY_ARGS, 3) ], + [#include ] + ) + ] +) + +CFLAGS=$saved_CFLAGS + AC_CONFIG_FILES([ Makefile tests/Makefile diff --git a/tests/api_gcc.h b/tests/api_gcc.h index 872c873..17487d5 100644 --- a/tests/api_gcc.h +++ b/tests/api_gcc.h @@ -2,6 +2,8 @@ #ifndef _INCLUDE_API_H #define _INCLUDE_API_H +#include "../config.h" + /* * common.h: Common Linux kernel-isms. * @@ -461,13 +463,25 @@ static void wait_all_threads(void) } } +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void run_on(int cpu) { +#if HAVE_SCHED_SETAFFINITY cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/api_x86.h b/tests/api_x86.h index 05fc05a..eba0613 100644 --- a/tests/api_x86.h +++ b/tests/api_x86.h @@ -3,6 +3,8 @@ #ifndef _INCLUDE_API_H #define _INCLUDE_API_H +#include "../config.h" + /* * common.h: Common Linux kernel-isms. * @@ -522,13 +524,25 @@ static void wait_all_threads(void) } } +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void run_on(int cpu) { +#if HAVE_SCHED_SETAFFINITY cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_mutex.c b/tests/test_mutex.c index 89fe4b1..7fb036d 100644 --- a/tests/test_mutex.c +++ b/tests/test_mutex.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -98,6 +99,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -107,6 +114,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -120,7 +128,12 @@ static void set_affinity(void) } CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_perthreadlock.c b/tests/test_perthreadlock.c index 1b9445f..e47315b 100644 --- a/tests/test_perthreadlock.c +++ b/tests/test_perthreadlock.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -102,6 +103,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -111,6 +118,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -124,7 +132,12 @@ static void set_affinity(void) } CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_qsbr.c b/tests/test_qsbr.c index 5284a48..7a68ae8 100644 --- a/tests/test_qsbr.c +++ b/tests/test_qsbr.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -96,6 +97,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -105,6 +112,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -118,7 +126,12 @@ static void set_affinity(void) } CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_qsbr_gc.c b/tests/test_qsbr_gc.c index 12430d0..c480461 100644 --- a/tests/test_qsbr_gc.c +++ b/tests/test_qsbr_gc.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -101,6 +102,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -110,6 +117,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -121,9 +129,15 @@ static void set_affinity(void) perror("Error in pthread mutex unlock"); exit(-1); } + CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_rwlock.c b/tests/test_rwlock.c index 837b800..b893100 100644 --- a/tests/test_rwlock.c +++ b/tests/test_rwlock.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -98,6 +99,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -107,6 +114,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -118,9 +126,15 @@ static void set_affinity(void) perror("Error in pthread mutex unlock"); exit(-1); } + CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_rwlock_timing.c b/tests/test_rwlock_timing.c index 869017e..c354de7 100644 --- a/tests/test_rwlock_timing.c +++ b/tests/test_rwlock_timing.c @@ -20,6 +20,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#define _GNU_SOURCE #include #include #include diff --git a/tests/test_urcu.c b/tests/test_urcu.c index 2f8cd16..0cde0b3 100644 --- a/tests/test_urcu.c +++ b/tests/test_urcu.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -96,6 +97,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -105,6 +112,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -116,9 +124,15 @@ static void set_affinity(void) perror("Error in pthread mutex unlock"); exit(-1); } + CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_urcu_assign.c b/tests/test_urcu_assign.c index 83b8f2c..d38361f 100644 --- a/tests/test_urcu_assign.c +++ b/tests/test_urcu_assign.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -96,6 +97,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -105,6 +112,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -116,9 +124,15 @@ static void set_affinity(void) perror("Error in pthread mutex unlock"); exit(-1); } + CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_urcu_bp.c b/tests/test_urcu_bp.c index baf23dc..5ba6e49 100644 --- a/tests/test_urcu_bp.c +++ b/tests/test_urcu_bp.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -96,6 +97,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -105,6 +112,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -116,9 +124,15 @@ static void set_affinity(void) perror("Error in pthread mutex unlock"); exit(-1); } + CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_urcu_defer.c b/tests/test_urcu_defer.c index 72c598b..10cd250 100644 --- a/tests/test_urcu_defer.c +++ b/tests/test_urcu_defer.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -97,6 +98,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -106,6 +113,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -117,9 +125,15 @@ static void set_affinity(void) perror("Error in pthread mutex unlock"); exit(-1); } + CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* diff --git a/tests/test_urcu_gc.c b/tests/test_urcu_gc.c index 6d4cde3..4e6f965 100644 --- a/tests/test_urcu_gc.c +++ b/tests/test_urcu_gc.c @@ -21,6 +21,7 @@ */ #define _GNU_SOURCE +#include "../config.h" #include #include #include @@ -105,6 +106,12 @@ static int use_affinity = 0; pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; +#ifndef HAVE_CPU_SET_T +typedef unsigned long cpu_set_t; +# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) +# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) +#endif + static void set_affinity(void) { cpu_set_t mask; @@ -114,6 +121,7 @@ static void set_affinity(void) if (!use_affinity) return; +#if HAVE_SCHED_SETAFFINITY ret = pthread_mutex_lock(&affinity_mutex); if (ret) { perror("Error in pthread mutex lock"); @@ -125,9 +133,15 @@ static void set_affinity(void) perror("Error in pthread mutex unlock"); exit(-1); } + CPU_ZERO(&mask); CPU_SET(cpu, &mask); +#if SCHED_SETAFFINITY_ARGS == 2 + sched_setaffinity(0, &mask); +#else sched_setaffinity(0, sizeof(mask), &mask); +#endif +#endif /* HAVE_SCHED_SETAFFINITY */ } /* -- 2.34.1