-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathnuma.c
More file actions
85 lines (69 loc) · 1.52 KB
/
Copy pathnuma.c
File metadata and controls
85 lines (69 loc) · 1.52 KB
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "numa.h"
#include "random.h"
#include "rnd.h"
#include "trinity.h"
/* Linux caps NUMA nodes at MAX_NUMNODES (1024 on x86_64 with current
* configs). The pool is bounded by what /sys exposes, so a small
* fixed array is plenty -- a sparse system with 8 nodes still parses
* cleanly. */
#define MAX_NUMA_NODES 1024
static int numa_node_pool[MAX_NUMA_NODES];
static unsigned int nr_numa_nodes;
void init_numa_nodes(void)
{
FILE *f;
char buf[4096];
char *p;
f = fopen("/sys/devices/system/node/online", "r");
if (f == NULL)
goto fallback;
if (fgets(buf, sizeof(buf), f) == NULL) {
fclose(f);
goto fallback;
}
fclose(f);
/* Parse cpulist syntax: "0", "0-3", "0,2,4", "0-1,3,5-7". */
p = buf;
while (*p && nr_numa_nodes < MAX_NUMA_NODES) {
char *endp;
long lo, hi;
long n;
while (*p && (isspace((unsigned char)*p) || *p == ','))
p++;
if (!*p)
break;
lo = strtol(p, &endp, 10);
if (endp == p)
break;
p = endp;
if (*p == '-') {
p++;
hi = strtol(p, &endp, 10);
if (endp == p)
break;
p = endp;
} else {
hi = lo;
}
if (lo < 0 || hi < lo)
break;
for (n = lo; n <= hi && nr_numa_nodes < MAX_NUMA_NODES; n++)
numa_node_pool[nr_numa_nodes++] = (int) n;
}
if (nr_numa_nodes == 0)
goto fallback;
return;
fallback:
numa_node_pool[0] = 0;
nr_numa_nodes = 1;
}
int random_numa_node(void)
{
if (nr_numa_nodes == 0)
return 0;
return numa_node_pool[rnd_modulo_u32(nr_numa_nodes)];
}