-
Notifications
You must be signed in to change notification settings - Fork 671
FreeBSD, implementing binding to free cpu. #7
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,12 +68,18 @@ | |
|
|
||
| #if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__) | ||
| # include <sys/sysctl.h> | ||
| # ifdef __FreeBSD__ | ||
| # include <sys/user.h> | ||
| # include <sys/cpuset.h> | ||
| # include <pthread.h> | ||
| # include <pthread_np.h> | ||
| # endif | ||
| #endif /* __APPLE__ || __FreeBSD__ || __OpenBSD__ */ | ||
|
|
||
| /* 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 | ||
| #endif /* __linux__ */ | ||
|
|
||
|
|
@@ -405,14 +411,13 @@ static void shuffle_ptrs(void** ptrs, u32 cnt) { | |
| can be found. Assumes an upper bound of 4k CPUs. */ | ||
|
|
||
| static void bind_to_free_cpu(void) { | ||
|
|
||
| u8 cpu_used[4096] = { 0 }; | ||
| u32 i; | ||
| #ifdef __linux__ | ||
| DIR* d; | ||
| struct dirent* de; | ||
| cpu_set_t c; | ||
|
|
||
| u8 cpu_used[4096] = { 0 }; | ||
| u32 i; | ||
|
|
||
| if (cpu_core_count < 2) return; | ||
|
|
||
| if (getenv("AFL_NO_AFFINITY")) { | ||
|
|
@@ -485,6 +490,26 @@ static void bind_to_free_cpu(void) { | |
| } | ||
|
|
||
| closedir(d); | ||
| #else | ||
| struct kinfo_proc *procs; | ||
| size_t nprocs; | ||
| size_t proccount; | ||
| cpuset_t c; | ||
| int s_name[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "3" is not necessary here, better to remove |
||
| if (sysctl(s_name, 3, NULL, &nprocs, NULL, 0) < 0) return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not use magic numbers, please add a macro for calculating array length via
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure but in fact I "mimicked" a similar sysctl call above (ie to get number of cpus) I usually try to fit the general style but not pb I can change. |
||
| proccount = nprocs / sizeof(*procs); | ||
| procs = ck_alloc(nprocs); | ||
| if (sysctl(s_name, 3, NULL, &nprocs, NULL, 0) < 0) goto procs_free; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
|
||
| for (i = 0; i < proccount; i ++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove space between |
||
| if (procs[i].ki_oncpu < sizeof(cpu_used)) | ||
| cpu_used[procs[i].ki_oncpu] = 1; | ||
| } | ||
|
|
||
| procs_free: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you write this without a `goto please? Just branch on the line 502
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
| ck_free(procs); | ||
|
|
||
| #endif | ||
|
|
||
| for (i = 0; i < cpu_core_count; i++) if (!cpu_used[i]) break; | ||
|
|
||
|
|
@@ -508,8 +533,13 @@ static void bind_to_free_cpu(void) { | |
| CPU_ZERO(&c); | ||
| CPU_SET(i, &c); | ||
|
|
||
| #ifdef __linux__ | ||
| if (sched_setaffinity(0, sizeof(c), &c)) | ||
| PFATAL("sched_setaffinity failed"); | ||
| #else | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well ... I think only NetBSD has this call too in case someone wants to port it too ... but I ll change it |
||
| if (pthread_setaffinity_np(pthread_self(), sizeof(c), &c)) | ||
| PFATAL("pthread_setaffinity failed"); | ||
| #endif | ||
|
|
||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
#elif __FreeBSD__?