mirror of https://github.com/vasi/pixz
Only use available CPUs
Not all online CPUs may be available for the current process, especially when CPU affinity is involved. In such cases too many threads will be created, which will unnecessarily compete for CPU time. Use sched_getaffinity() (if available) to determine the correct number of threads to create.pull/107/head
parent
f1b1b5f8af
commit
6a9443d955
@ -1,5 +1,28 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef HAVE_SCHED_GETAFFINITY
|
||||
|
||||
#include <sched.h>
|
||||
#include <stdio.h>
|
||||
|
||||
size_t num_threads(void) {
|
||||
cpu_set_t cpu_set;
|
||||
CPU_ZERO(&cpu_set);
|
||||
|
||||
if (sched_getaffinity(0, sizeof cpu_set, &cpu_set) == -1)
|
||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
||||
else
|
||||
return CPU_COUNT(&cpu_set);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
size_t num_threads(void) {
|
||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue