2
Copyright (C) 2005 Andi Kleen, SuSE Labs.
4
libnuma is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; version
9
libnuma is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should find a copy of v2.1 of the GNU Lesser General Public License
15
somewhere on your Linux system; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
All calls are undefined when numa_available returns an error. */
26
static int distance_numnodes;
27
static int *distance_table;
29
static int count_numbers(char *s)
42
static void parse_numbers(char *s, int *iptr, int n)
46
for (i = 0; i < n; i++) {
47
*iptr++ = strtoul(s, &end, 0);
54
static int read_distance_table(void)
66
sprintf(fn, "/sys/devices/system/node/node%d/distance", nd);
69
if (errno == ENOENT && nd > 0)
73
len = getdelim(&line, &linelen, '\n', dfh);
79
numnodes = count_numbers(line);
80
table = calloc(numnodes * numnodes, sizeof(int));
87
parse_numbers(line, table + nd * numnodes, numnodes);
92
"Cannot parse distance information in sysfs: %s",
97
/* Update the global table pointer. Race window here with
98
other threads, but in the worst case we leak one distance
99
array one time, which is tolerable. This avoids a
100
dependency on pthreads. */
101
if (distance_table) {
105
distance_numnodes = numnodes;
106
distance_table = table;
110
int numa_distance(int a, int b)
112
if (!distance_table) {
113
int err = read_distance_table();
117
return distance_table[a * distance_numnodes + b];