-
Notifications
You must be signed in to change notification settings - Fork 16
/
affinity.hpp
125 lines (101 loc) · 3.26 KB
/
affinity.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#define _GNU_SOURCE
#include <pthread.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#ifdef __LINUX__
#include <sched.h>
#endif
#ifdef __APPLE__
#include <mach/thread_policy.h>
#include <mach/task_info.h>
#include <sys/sysctl.h>
#include <mach/thread_policy.h>
#include <mach/thread_act.h>
// OSX code adapted from
// http://yyshen.github.io/2015/01/18/binding_threads_to_cores_osx.html
// these macros and methods don't exist on OSX
#define SYSCTL_CORE_COUNT "machdep.cpu.core_count"
typedef struct cpu_set {
uint32_t count;
} cpu_set_t;
static inline void
CPU_ZERO(cpu_set_t *cs) { cs->count = 0; }
static inline void
CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); }
static inline int
CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); }
static inline int
CPU_COUNT(cpu_set_t *cs) { return __builtin_popcount(cs->count); }
#define CPU_SETSIZE 32
static int sched_getaffinity(pid_t pid, size_t cpu_size, cpu_set_t *cpu_set)
{
int32_t core_count = 0;
size_t len = sizeof(core_count);
int ret = sysctlbyname(SYSCTL_CORE_COUNT, &core_count, &len, 0, 0);
if (ret) {
return -1;
}
cpu_set->count = 0;
for (int i = 0; i < core_count; i++) {
cpu_set->count |= (1 << i);
}
return 0;
}
static int pthread_setaffinity_np(pthread_t thread, size_t cpu_size,
cpu_set_t *cpu_set)
{
thread_port_t mach_thread;
int core = 0;
for (core = 0; core < 8 * cpu_size; core++) {
if (CPU_ISSET(core, cpu_set)) break;
}
thread_affinity_policy_data_t policy = { core };
mach_thread = pthread_mach_thread_np(thread);
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, 1);
return 0;
}
#endif
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
static int bind_to_core(int core) {
/* bind the calling thread to the requested core
*
* The use of this method is for better NUMA utilization. The
* default NUMA policy is local, where memory is allocated on the NUMA node
* relative to the core if possible. The intention with this method is to
* bind to a core first, and then allocate memory. A beneficial side effect
* is that threads should not hop between cores either.
*
* This method is cgroup safe.
*/
// https://stackoverflow.com/a/11583550/19741
// http://blog.saliya.org/2015/07/get-and-set-process-affinity-in-c.html
pthread_t thread = pthread_self();
pid_t pid = getpid();
cpu_set_t current_set, new_set;
int j, ret;
CPU_ZERO(¤t_set);
CPU_ZERO(&new_set);
ret = sched_getaffinity(pid, sizeof(current_set), ¤t_set);
// find which core in our cpu_set corresponds to the callers
// request
int target = -1;
for(j = 0; j < CPU_SETSIZE; j++) {
if(CPU_ISSET(j, ¤t_set)) {
target++;
}
if(target == core)
break;
}
if(target != core) {
fprintf(stderr, "Unable to bind this thread to core %d. Are sufficient processors available?", thread);
return -1;
}
CPU_SET(j, &new_set);
int serr = pthread_setaffinity_np(thread, sizeof(new_set), &new_set);
return serr;
}