Coverage Report

Created: 2026-12-01 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/numactl/distance.c
Line
Count
Source
1
/* Discover distances
2
   Copyright (C) 2005 Andi Kleen, SuSE Labs.
3
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
7
   2.1.
8
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.
13
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
17
18
   All calls are undefined when numa_available returns an error. */
19
#define _GNU_SOURCE
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <errno.h>
23
#include "numa.h"
24
#include "numaint.h"
25
26
static int distance_numnodes;
27
static int *distance_table;
28
29
void numa_distance_cleanup(void)
30
0
{
31
0
  free(distance_table);
32
0
  distance_table = NULL;
33
0
  distance_numnodes = 0;
34
0
}
35
36
static void parse_numbers(char *s, int *iptr)
37
0
{
38
0
  int i, d, j;
39
0
  char *end;
40
0
  int maxnode = numa_max_node();
41
42
0
  for (i = 0, j = 0; i <= maxnode; i++, j++) {
43
0
    d = strtoul(s, &end, 0);
44
    /* Skip unavailable nodes */
45
0
    while (j<=maxnode && !numa_bitmask_isbitset(numa_nodes_ptr, j))
46
0
      j++;
47
0
    if (s == end || j > maxnode)
48
0
      break;
49
0
    *(iptr+j) = d;
50
0
    s = end;
51
0
  }
52
0
}
53
54
static int read_distance_table(void)
55
0
{
56
0
  int nd, len;
57
0
  char *line = NULL;
58
0
  size_t linelen = 0;
59
0
  int maxnode = numa_max_node() + 1;
60
0
  int *table = NULL;
61
0
  int err = -1;
62
63
0
  for (nd = 0;; nd++) {
64
0
    char fn[100];
65
0
    FILE *dfh;
66
0
    sprintf(fn, "/sys/devices/system/node/node%d/distance", nd);
67
0
    dfh = fopen(fn, "r");
68
0
    if (!dfh) {
69
0
      if (errno == ENOENT)
70
0
        err = 0;
71
0
      if (!err && nd<maxnode)
72
0
        continue;
73
0
      else
74
0
        break;
75
0
    }
76
    /* Node past what numa_init() sized the table for, no row for it */
77
0
    if (nd >= maxnode) {
78
0
      fclose(dfh);
79
0
      err = 0;
80
0
      break;
81
0
    }
82
0
    len = getdelim(&line, &linelen, '\n', dfh);
83
0
    fclose(dfh);
84
0
    if (len <= 0)
85
0
      break;
86
87
0
    if (!table) {
88
0
      table = calloc(maxnode * maxnode, sizeof(int));
89
0
      if (!table) {
90
0
        errno = ENOMEM;
91
0
        break;
92
0
      }
93
0
    }
94
95
0
    parse_numbers(line, table + nd * maxnode);
96
0
  }
97
0
  free(line);
98
0
  if (err)  {
99
0
    numa_warn(W_distance,
100
0
        "Cannot parse distance information in sysfs: %s",
101
0
        strerror(errno));
102
0
    free(table);
103
0
    return err;
104
0
  }
105
  /* Update the global table pointer.  Race window here with
106
     other threads, but in the worst case we leak one distance
107
     array one time, which is tolerable. This avoids a
108
     dependency on pthreads. */
109
0
  if (distance_table) {
110
0
    free(table);
111
0
    return 0;
112
0
  }
113
0
  distance_numnodes = maxnode;
114
0
  distance_table = table;
115
0
  return 0;
116
0
}
117
118
int numa_distance(int a, int b)
119
0
{
120
0
  if (!distance_table) {
121
0
    int err = read_distance_table();
122
0
    if ((err < 0) || (!distance_table))
123
0
      return 0;
124
0
  }
125
0
  if ((unsigned)a >= distance_numnodes || (unsigned)b >= distance_numnodes)
126
0
    return 0;
127
0
  return distance_table[a * distance_numnodes + b];
128
0
}