-
Notifications
You must be signed in to change notification settings - Fork 0
/
affinity.c
216 lines (170 loc) · 5.49 KB
/
affinity.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* clockperf
*
* Copyright (c) 2016-2021, Steven Noonan <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "prefix.h"
#include "affinity.h"
#include <stdio.h>
#define MAX_CPUS 1024
#ifdef TARGET_OS_LINUX
#include <pthread.h>
#include <sched.h>
#include <string.h>
#define CPUSET_T cpu_set_t
#define CPUSET_MASK_T __cpu_mask
#elif defined(TARGET_OS_FREEBSD)
#include <pthread.h>
#include <pthread_np.h>
#include <sys/param.h>
#include <sys/cpuset.h>
#define CPUSET_T cpuset_t
#define CPUSET_MASK_T long
#undef MAX_CPUS
#define MAX_CPUS CPU_MAXSIZE
#elif defined(TARGET_OS_SOLARIS)
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/processor.h>
#include <sys/procset.h>
#elif defined(TARGET_OS_MACOSX)
#include <sys/sysctl.h>
/*#define USE_CHUD*/
#ifdef USE_CHUD
extern int chudProcessorCount(void);
extern int utilBindThreadToCPU(int n);
extern int utilUnbindThreadFromCPU(void);
#endif
#endif
#ifdef TARGET_OS_WINDOWS
#if _WIN32_WINNT < 0x0601
typedef WORD(WINAPI *fnGetActiveProcessorGroupCount)(void);
typedef DWORD(WINAPI *fnGetActiveProcessorCount)(WORD);
typedef BOOL(WINAPI *fnSetThreadGroupAffinity)(HANDLE, const GROUP_AFFINITY *, PGROUP_AFFINITY);
static fnGetActiveProcessorGroupCount GetActiveProcessorGroupCount;
static fnGetActiveProcessorCount GetActiveProcessorCount;
static fnSetThreadGroupAffinity SetThreadGroupAffinity;
#endif
#endif
void thread_init(void)
{
#ifdef TARGET_OS_WINDOWS
#if _WIN32_WINNT < 0x0601
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
GetActiveProcessorGroupCount = (fnGetActiveProcessorGroupCount)GetProcAddress(hKernel32, "GetActiveProcessorGroupCount");
GetActiveProcessorCount = (fnGetActiveProcessorCount)GetProcAddress(hKernel32, "GetActiveProcessorCount");
SetThreadGroupAffinity = (fnSetThreadGroupAffinity)GetProcAddress(hKernel32, "SetThreadGroupAffinity");
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
#endif
#endif
}
int thread_bind(uint32_t id)
{
#ifdef TARGET_OS_WINDOWS
BOOL ret = FALSE;
HANDLE hThread = GetCurrentThread();
#if _WIN32_WINNT < 0x0601
if (is_windows7_or_greater()) {
#endif
DWORD threadsInGroup = 0;
WORD groupId, groupCount;
GROUP_AFFINITY affinity;
ZeroMemory(&affinity, sizeof(GROUP_AFFINITY));
groupCount = GetActiveProcessorGroupCount();
for (groupId = 0; groupId < groupCount; groupId++) {
threadsInGroup = GetActiveProcessorCount(groupId);
if (id < threadsInGroup)
break;
id -= threadsInGroup;
}
if (groupId < groupCount && id < threadsInGroup) {
affinity.Group = groupId;
affinity.Mask = 1ULL << id;
ret = SetThreadGroupAffinity(hThread, &affinity, NULL);
}
#if _WIN32_WINNT < 0x0601
} else {
DWORD mask;
if (id > 32)
return 1;
mask = (1 << id);
ret = SetThreadAffinityMask(hThread, mask);
}
#endif
return (ret != FALSE) ? 0 : 1;
#elif defined(TARGET_OS_LINUX) || defined(TARGET_OS_FREEBSD)
int ret;
#ifdef CPU_SET_S
size_t setsize = CPU_ALLOC_SIZE(MAX_CPUS);
CPUSET_T *set = CPU_ALLOC(MAX_CPUS);
pthread_t pth;
pth = pthread_self();
CPU_ZERO_S(setsize, set);
CPU_SET_S(id, setsize, set);
ret = pthread_setaffinity_np(pth, setsize, set);
CPU_FREE(set);
#else
size_t bits_per_set = sizeof(CPUSET_T) * 8;
size_t bits_per_subset = sizeof(CPUSET_MASK_T) * 8;
size_t setsize = sizeof(CPUSET_T) * (MAX_CPUS / bits_per_set);
size_t set_id, subset_id;
unsigned long long mask;
CPUSET_T *set = malloc(setsize);
pthread_t pth;
pth = pthread_self();
for (set_id = 0; set_id < (MAX_CPUS / bits_per_set); set_id++)
CPU_ZERO(&set[set_id]);
set_id = id / bits_per_set;
id %= bits_per_set;
subset_id = id / bits_per_subset;
id %= bits_per_subset;
mask = 1ULL << (unsigned long long)id;
((unsigned long *)set[set_id].__bits)[subset_id] |= mask;
ret = pthread_setaffinity_np(pth, setsize, set);
free(set);
#endif
return (ret == 0) ? 0 : 1;
#elif defined(TARGET_OS_SOLARIS)
/*
* This requires permissions, so can easily fail.
*/
if (processor_bind(P_LWPID, P_MYID, id, NULL) != 0) {
fprintf(stderr, "warning: failed to bind to CPU%u: %s\n",
id, strerror(errno));
return 1;
}
return 0;
#elif defined(TARGET_OS_MACOSX)
int ret = 1;
#ifdef USE_CHUD
ret = (utilBindThreadToCPU(id) == 0) ? 0 : 1;
#else
#warning "thread_bind() not implementable on macOS"
#endif
return ret == 0 ? 0 : 1;
#else
#error "thread_bind() not defined for this platform"
#endif
}
/* vim: set ts=4 sts=4 sw=4 noet: */