-
Notifications
You must be signed in to change notification settings - Fork 0
/
NUMA_memnode.c
367 lines (303 loc) · 8.32 KB
/
NUMA_memnode.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <numaif.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>
#include <sys/param.h>
#include "libNUMA.h"
int
NUMA_memnode_system_mask (size_t destsize, memnode_set_t *dest)
{
/* Iterate over the node information. */
DIR *dir = opendir ("/sys/devices/system/node");
if (dir == NULL)
return -1;
MEMNODE_ZERO_S (destsize, dest);
int result = 0;
struct dirent64 *d;
while ((d = readdir64 (dir)) != NULL)
/* NB: the sysfs has d_type support. */
if (d->d_type == DT_DIR && strncmp (d->d_name, "node", 4) == 0)
{
char *endp;
unsigned long int nodeidx = strtoul (d->d_name + 4, &endp, 10);
if (nodeidx != ULONG_MAX && endp != d->d_name + 4 && *endp == '\0')
{
if (dest != NULL && MEMNODE_SET_S (nodeidx, destsize, dest) == 0)
{
result = -1;
errno = ERANGE;
break;
}
++result;
}
}
closedir (dir);
return result;
}
int
NUMA_memnode_system_count (void)
{
return NUMA_memnode_system_mask (0, NULL);
}
int
NUMA_memnode_self_mask (size_t destsize, memnode_set_t *dest)
{
// XXX When the kernel knows about the special use of 0 use it instead.
pid_t pid = getpid ();
/* Try with the default cpu_set_t first. */
cpu_set_t s;
if (sched_getaffinity (pid, sizeof (s), &s) < 0)
{
cpu_set_t *ds = NULL;
size_t dslen = sizeof (s);
int result = -1;
while (errno == EINVAL)
{
/* Don't worry about overflows here. malloc will return
ENOMEM long before that. */
dslen *= 2;
cpu_set_t *newds = realloc (ds, dslen);
if (newds == NULL)
{
free (ds);
return -1;
}
ds = newds;
if (sched_getaffinity (getpid (), dslen, ds) >= 0)
{
result = NUMA_cpu_to_memnode (dslen, ds, destsize, dest);
break;
}
}
free (ds);
return result;
}
return NUMA_cpu_to_memnode (sizeof (s), &s, destsize, dest);
}
int
NUMA_memnode_self_current_idx (void)
{
int node;
#ifdef __x86_64__
/* getcpu is a vsyscall. */
register unsigned long int rax __asm__ ("rax") = 0xffffffffff600800ul;
register long int rcx __asm ("rcx");
register void *rdx __asm ("rdx") = NULL;
register void *rdi __asm ("rdi") = NULL;
register void *rsi __asm ("rsi") = &node;
register long int r8 __asm ("r8");
register long int r9 __asm ("r9");
register long int r10 __asm ("r10");
register long int r11 __asm ("r11");
asm volatile ("call *%%rax"
: "=a" (rax), "=c" (rcx), "=d" (rdx), "=D" (rdi), "=S" (rsi),
"=r" (r8), "=r" (r9), "=r" (r10), "=r" (r11)
: "2" (rdx), "3" (rdi), "4" (rsi));
if (rax != 0)
{
errno = -rax;
return -1;
}
#else
if (syscall (__NR_getcpu, NULL, &node, NULL) < 0)
return -1;
#endif
return node;
}
int
NUMA_memnode_self_current_mask (size_t destsize, memnode_set_t *dest)
{
int idx = NUMA_memnode_self_current_idx ();
if (idx < 0)
return -1;
MEMNODE_SET_S (idx, destsize, dest);
return idx;
}
int
NUMA_cpu_to_memnode (size_t cpusetsize, const cpu_set_t *cpuset,
size_t memnodesize, memnode_set_t *memnodeset)
{
/* Iterate over the node information and check whether any of the
local CPUs is listed in CPUSET. */
DIR *dir = opendir ("/sys/devices/system/node");
if (dir == NULL)
return -1;
MEMNODE_ZERO_S (memnodesize, memnodeset);
int result = 0;
int dfd = dirfd (dir);
struct dirent64 *d;
while ((d = readdir64 (dir)) != NULL)
/* NB: the sysfs has d_type support. */
if (d->d_type == DT_DIR && strncmp (d->d_name, "node", 4) == 0)
{
char *endp;
unsigned long int nodeidx = strtoul (d->d_name + 4, &endp, 10);
if (nodeidx == ULONG_MAX || endp == d->d_name + 4 || *endp != '\0')
continue;
/* Now read this directory for the cpu* links. */
int fd = openat (dfd, d->d_name, O_RDONLY|O_DIRECTORY);
if (fd == -1)
continue;
DIR *dir2 = fdopendir (fd);
if (dir2 == NULL)
{
close (fd);
continue;
}
struct dirent64 *d2;
while ((d2 = readdir64 (dir2)) != NULL)
if (d2->d_type == DT_LNK && strncmp (d2->d_name, "cpu", 3) == 0)
{
unsigned long int nr = strtoul (d2->d_name + 3, &endp, 10);
if (nr != ULONG_MAX && endp != d2->d_name + 3 && *endp == '\0'
&& CPU_ISSET_S (nr, cpusetsize, cpuset))
{
/* The CPU is in the set. Mark the memory node as used. */
if (MEMNODE_SET_S (nodeidx, memnodesize, memnodeset) == 0)
{
closedir (dir2);
errno = ERANGE;
result = 1;
goto out;
}
++result;
break;
}
}
closedir (dir2);
}
out:
closedir (dir);
return result;
}
int
NUMA_memnode_to_cpu (size_t memnodesize, const memnode_set_t *memnodeset,
size_t cpusetsize, cpu_set_t *cpuset)
{
/* Iterate over the node information and set the bits for all the
CPUs in memory nodes listed in MEMNODESET. */
DIR *dir = opendir ("/sys/devices/system/node");
if (dir == NULL)
return -1;
CPU_ZERO_S (cpusetsize, cpuset);
int result = 0;
int dfd = dirfd (dir);
struct dirent64 *d;
while ((d = readdir64 (dir)) != NULL)
/* NB: the sysfs has d_type support. */
if (d->d_type == DT_DIR && strncmp (d->d_name, "node", 4) == 0)
{
char *endp;
unsigned long int nodeidx = strtoul (d->d_name + 4, &endp, 10);
if (nodeidx == ULONG_MAX || endp == d->d_name + 4 || *endp != '\0'
|| ! MEMNODE_ISSET_S (nodeidx, memnodesize, memnodeset))
continue;
/* Now read this directory for the cpu* links. */
int fd = openat (dfd, d->d_name, O_RDONLY|O_DIRECTORY);
if (fd == -1)
continue;
DIR *dir2 = fdopendir (fd);
if (dir2 == NULL)
{
close (fd);
continue;
}
struct dirent64 *d2;
while ((d2 = readdir64 (dir2)) != NULL)
if (d2->d_type == DT_LNK && strncmp (d2->d_name, "cpu", 3) == 0)
{
unsigned long int nr = strtoul (d2->d_name + 3, &endp, 10);
if (nr != ULONG_MAX && endp != d2->d_name + 3 && *endp == '\0')
{
/* Precaution: maybe a CPU is listed for more than
one node. We do not want to count it twice. */
if (! CPU_ISSET_S (nr, cpusetsize, cpuset)
&& CPU_SET_S (nr, cpusetsize, cpuset) == 0)
{
closedir (dir2);
errno = ERANGE;
result = 1;
goto out;
}
++result;
}
}
closedir (dir2);
}
out:
closedir (dir);
return result;
}
int
NUMA_memnode_count (size_t setsize, const memnode_set_t *set)
{
int s = 0;
const __memnode_mask *p = set->__bits;
const __memnode_mask *end = &set->__bits[setsize / sizeof (__memnode_mask)];
while (p < end)
{
__memnode_mask l = *p++;
if (l == 0)
continue;
#if LONG_BIT > 32
l = (l & 0x5555555555555555ul) + ((l >> 1) & 0x5555555555555555ul);
l = (l & 0x3333333333333333ul) + ((l >> 2) & 0x3333333333333333ul);
l = (l & 0x0f0f0f0f0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0f0f0f0f0ful);
l = (l & 0x00ff00ff00ff00fful) + ((l >> 8) & 0x00ff00ff00ff00fful);
l = (l & 0x0000ffff0000fffful) + ((l >> 16) & 0x0000ffff0000fffful);
l = (l & 0x00000000fffffffful) + ((l >> 32) & 0x00000000fffffffful);
#else
l = (l & 0x55555555ul) + ((l >> 1) & 0x55555555ul);
l = (l & 0x33333333ul) + ((l >> 2) & 0x33333333ul);
l = (l & 0x0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0ful);
l = (l & 0x00ff00fful) + ((l >> 8) & 0x00ff00fful);
l = (l & 0x0000fffful) + ((l >> 16) & 0x0000fffful);
#endif
s += l;
}
return s;
}
int
NUMA_mem_get_node_idx (void *addr)
{
int res;
if (syscall (__NR_get_mempolicy, &res, NULL, (size_t) 0, addr,
(unsigned long int) MPOL_F_ADDR) < 0)
return -1;
return res;
}
int
NUMA_mem_get_node_mask (void *addr, size_t size, size_t destsize,
memnode_set_t *dest)
{
/* We have to iterate over the entire address range. This can be
wasteful in case we deal with large pages. But there is no
alternative so far. */
size_t ps = sysconf (_SC_PAGESIZE);
int result = 0;
while (size > 0)
{
int res;
if (syscall (__NR_get_mempolicy, &res, NULL, (size_t) 0, addr,
(unsigned long int) MPOL_F_ADDR) < 0)
return -1;
if (dest && !MEMNODE_ISSET_S (res, destsize, dest))
{
if (MEMNODE_SET_S (res, destsize, dest) == 0)
{
errno = ERANGE;
return -1;
}
++result;
}
addr = (char *) addr + ps;
size -= MIN (size, ps);
}
return result;
}