diff options
author | Simon Marlow <marlowsd@gmail.com> | 2009-03-18 15:47:19 +0000 |
---|---|---|
committer | Simon Marlow <marlowsd@gmail.com> | 2009-03-18 15:47:19 +0000 |
commit | 31caec794c3978d55d79f715f21fb72948c9f300 (patch) | |
tree | 475474eb40efe84d3696177054dea9867c0f9261 /rts/posix/OSThreads.c | |
parent | 7583384214ed6aa4a90d77c5975728a9b06149f2 (diff) | |
download | haskell-31caec794c3978d55d79f715f21fb72948c9f300.tar.gz |
Set thread affinity with +RTS -qa (only on Linux so far)
Diffstat (limited to 'rts/posix/OSThreads.c')
-rw-r--r-- | rts/posix/OSThreads.c | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index 6eb2d2bc8e..f15fc95c3a 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -7,8 +7,9 @@ * * --------------------------------------------------------------------------*/ -#if defined(DEBUG) && defined(__linux__) +#if defined(__linux__) /* We want GNU extensions in DEBUG mode for mutex error checking */ +/* We also want the affinity API, which requires _GNU_SOURCE */ #define _GNU_SOURCE #endif @@ -26,6 +27,10 @@ #error pthreads.h is required for the threaded RTS on Posix platforms #endif +#if defined(HAVE_SCHED_H) +#include <sched.h> +#endif + /* * This (allegedly) OS threads independent layer was initially * abstracted away from code that used Pthreads, so the functions @@ -197,6 +202,26 @@ getNumberOfProcessors (void) return nproc; } +// Schedules the thread to run on CPU n of m. m may be less than the +// number of physical CPUs, in which case, the thread will be allowed +// to run on CPU n, n+m, n+2m etc. +void +setThreadAffinity (nat n, nat m) +{ +#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY) + nat nproc; + cpu_set_t cs; + nat i; + + nproc = getNumberOfProcessors(); + CPU_ZERO(&cs); + for (i = n; i < nproc; i+=m) { + CPU_SET(n, &cs); + } + sched_setaffinity(0, sizeof(cpu_set_t), &cs); +#endif +} + #else /* !defined(THREADED_RTS) */ int |