Merge pull request #75 from devnexen/fbsd_binding_to_cpu_x

Binding to the first free cpu, porting to FreeBSD
This commit is contained in:
van Hauser 2019-10-04 10:24:41 +02:00 committed by GitHub
commit 9af6395e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 3 deletions

View File

@ -78,8 +78,15 @@
/* For systems that have sched_setaffinity; right now just Linux, but one
can hope... */
#ifdef __linux__
#if defined (__linux__) || defined(__FreeBSD__)
#define HAVE_AFFINITY 1
#if defined(__FreeBSD__)
#include <sys/cpuset.h>
#include <sys/user.h>
#include <pthread.h>
#include <pthread_np.h>
#define cpu_set_t cpuset_t
#endif
#endif /* __linux__ */
#ifndef SIMPLE_FILES

View File

@ -32,8 +32,6 @@
void bind_to_free_cpu(void) {
DIR* d;
struct dirent* de;
cpu_set_t c;
u8 cpu_used[4096] = {0};
@ -48,6 +46,9 @@ void bind_to_free_cpu(void) {
}
#if defined(__linux__)
DIR* d;
struct dirent* de;
d = opendir("/proc");
if (!d) {
@ -112,6 +113,30 @@ void bind_to_free_cpu(void) {
}
closedir(d);
#elif defined(__FreeBSD__)
struct kinfo_proc *procs;
size_t nprocs;
size_t proccount;
int s_name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
size_t s_name_l = sizeof(s_name)/sizeof(s_name[0]);
if (sysctl(s_name, s_name_l, NULL, &nprocs, NULL, 0) != 0) return;
proccount = nprocs / sizeof(*procs);
nprocs = nprocs * 4/3;
procs = ck_alloc(nprocs);
if (sysctl(s_name, s_name_l, procs, &nprocs, NULL, 0) != 0) {
ck_free(procs);
return;
}
for (i = 0; i < proccount; i ++) {
if (procs[i].ki_oncpu < sizeof(cpu_used))
cpu_used[procs[i].ki_oncpu] = 1;
}
ck_free(procs);
#endif
for (i = 0; i < cpu_core_count; ++i)
if (!cpu_used[i]) break;
@ -138,7 +163,11 @@ void bind_to_free_cpu(void) {
CPU_ZERO(&c);
CPU_SET(i, &c);
#if defined(__linux__)
if (sched_setaffinity(0, sizeof(c), &c)) PFATAL("sched_setaffinity failed");
#elif defined(__FreeBSD__)
if (pthread_setaffinity_np(pthread_self(), sizeof(c), &c)) PFATAL("pthread_setaffinity failed");
#endif
}