forked from tycho/cpuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.c
325 lines (244 loc) · 6.7 KB
/
threads.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* CPUID
*
* A simple and small tool to dump/decode CPUID information.
*
* Copyright (c) 2010-2018, Steven Noonan <steven@uplinklabs.net>
*
* 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 <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
#include "state.h"
uint32_t thread_count_native(struct cpuid_state_t *state)
{
#if defined(TARGET_OS_MACOSX)
uint32_t count;
size_t size = sizeof(count);
if (sysctlbyname("hw.ncpu", &count, &size, NULL, 0))
return 1;
return count;
#elif defined(TARGET_OS_SOLARIS)
long count;
if ((count = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
return 1;
(void)state;
return (uint32_t)count;
#else
static unsigned int i = 0;
if (i) return i;
if (thread_bind_native(state, 0) != 0) {
fprintf(stderr, "ERROR: thread_bind() doesn't appear to be working correctly.\n");
abort();
}
while (thread_bind_native(state, ++i) == 0);
return i;
#endif
}
uint32_t thread_count_stub(struct cpuid_state_t *state)
{
assert(state);
return state->cpu_logical_count;
}
uintptr_t thread_get_binding(void)
{
#ifdef TARGET_OS_WINDOWS
HANDLE hThread = GetCurrentThread();
DWORD_PTR mask = SetThreadAffinityMask(hThread, 1);
SetThreadAffinityMask(hThread, mask);
return mask;
#elif defined(TARGET_OS_LINUX) || defined(TARGET_OS_FREEBSD)
uint32_t ret, mask_id;
CPUSET_T mask;
pthread_t pth;
pth = pthread_self();
CPU_ZERO(&mask);
ret = pthread_getaffinity_np(pth, sizeof(mask), &mask);
if (ret != 0) {
fprintf(stderr, "ERROR: pthread_getaffinity_np() failed (returned %d)\n", ret);
abort();
}
mask_id = 0;
for (ret = 0; ret < 32; ret++) {
if (CPU_ISSET(ret, &mask))
mask_id |= (1 << ret);
}
return mask_id;
#elif defined(TARGET_OS_SOLARIS)
processorid_t bind;
if (processor_bind(P_LWPID, P_MYID, PBIND_QUERY, &bind) != 0) {
fprintf(stderr, "warning: failed to query LWP binding: %s\n",
strerror(errno));
return 0;
}
return 1 << bind;
#elif defined(TARGET_OS_MACOSX)
/* Hopefully this function wasn't too important. */
return 0;
#else
#error "thread_get_binding() not defined for this platform"
#endif
}
uintptr_t thread_bind_mask(uintptr_t _mask)
{
#ifdef TARGET_OS_WINDOWS
DWORD ret;
HANDLE hThread = GetCurrentThread();
ret = SetThreadAffinityMask(hThread, (DWORD_PTR)_mask);
return (ret != 0) ? 0 : 1;
#elif defined(TARGET_OS_LINUX) || defined(TARGET_OS_FREEBSD)
int ret;
CPUSET_T mask;
pthread_t pth;
pth = pthread_self();
CPU_ZERO(&mask);
for (ret = 0; ret < 64; ret++) {
if (_mask & 1)
CPU_SET(ret, &mask);
_mask >>= 1;
}
ret = pthread_setaffinity_np(pth, sizeof(mask), &mask);
return (ret == 0) ? 0 : 1;
#elif defined(TARGET_OS_SOLARIS)
(void)_mask;
/*
* Mask binding is not directly exposed, but it shouldn't matter
* significantly.
*/
return 0;
#elif defined(TARGET_OS_MACOSX)
/* We just have to pray nothing explodes too dramatically. */
return 0;
#else
#error "thread_bind_mask() not defined for this platform"
#endif
}
int thread_bind_native(__unused_variable struct cpuid_state_t *state, uint32_t id)
{
#ifdef TARGET_OS_WINDOWS
BOOL ret;
HANDLE hThread = GetCurrentThread();
#if _WIN32_WINNT >= 0x0601
GROUP_AFFINITY affinity;
ZeroMemory(&affinity, sizeof(GROUP_AFFINITY));
affinity.Group = id / 64;
affinity.Mask = 1 << (id % 64);
ret = SetThreadGroupAffinity(hThread, &affinity, NULL);
#else
DWORD mask;
if (id > 32)
return 1;
mask = (1 << id);
ret = SetThreadAffinityMask(hThread, mask);
#endif
if (state && ret != FALSE)
state->cpu_bound_index = id;
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
if (state && ret == 0)
state->cpu_bound_index = id;
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;
}
if (state)
state->cpu_bound_index = id;
return 0;
#elif defined(TARGET_OS_MACOSX)
int ret = 1;
#ifdef USE_CHUD
ret = (utilBindThreadToCPU(id) == 0) ? 0 : 1;
#endif
if (state && ret == 0)
state->cpu_bound_index = id;
return ret == 0 ? 0 : 1;
#else
#error "thread_bind_native() not defined for this platform"
#endif
}
int thread_bind_stub(struct cpuid_state_t *state, uint32_t id)
{
assert(state);
assert(id < state->cpu_logical_count);
state->cpu_bound_index = id;
return 0;
}