Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use available CPUs #107

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -68,7 +68,7 @@ AC_SYS_LARGEFILE
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_FUNC_STRTOD
AC_CHECK_FUNCS([memchr memmove memset strerror strtol])
AC_CHECK_FUNCS([memchr memmove memset strerror strtol sched_getaffinity])
AC_CHECK_HEADER([sys/endian.h],
[
AC_CHECK_DECLS([htole64, le64toh], [], [], [
Expand Down
23 changes: 23 additions & 0 deletions src/cpu.c
@@ -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