Back to index

numactl  2.0.8~rc4
Defines | Functions | Variables
distance.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "numa.h"
#include "numaint.h"

Go to the source code of this file.

Defines

#define _GNU_SOURCE   1

Functions

static void parse_numbers (char *s, int *iptr)
static int read_distance_table (void)
int numa_distance (int a, int b)

Variables

static int distance_numnodes
static int * distance_table

Define Documentation

#define _GNU_SOURCE   1

Definition at line 19 of file distance.c.


Function Documentation

int numa_distance ( int  a,
int  b 
)

Definition at line 110 of file distance.c.

{
       if (!distance_table) {
              int err = read_distance_table();
              if (err < 0)
                     return 0;
       }
       return distance_table[a * distance_numnodes + b];
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void parse_numbers ( char *  s,
int *  iptr 
) [static]

Definition at line 29 of file distance.c.

{
       int i, d, j;
       char *end;
       int maxnode = numa_max_node();
       int numnodes = 0;

       for (i = 0; i <= maxnode; i++)
              if (numa_bitmask_isbitset(numa_nodes_ptr, i))
                     numnodes++;

       for (i = 0, j = 0; i <= maxnode; i++, j++) {
              d = strtoul(s, &end, 0);
              /* Skip unavailable nodes */
              while (j<=maxnode && !numa_bitmask_isbitset(numa_nodes_ptr, j))
                     j++;
              *(iptr+j) = d;
              if (s == end)
                     break;
              s = end;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int read_distance_table ( void  ) [static]

Definition at line 52 of file distance.c.

{
       int nd, len;
       char *line = NULL;
       size_t linelen = 0;
       int maxnode = numa_max_node() + 1;
       int *table = NULL;
       int err = -1;

       for (nd = 0;; nd++) {
              char fn[100];
              FILE *dfh;
              sprintf(fn, "/sys/devices/system/node/node%d/distance", nd);
              dfh = fopen(fn, "r");
              if (!dfh) {
                     if (errno == ENOENT && nd > 0)
                            err = 0;
                     if (!err && nd<maxnode)
                            continue;
                     else
                            break;
              }
              len = getdelim(&line, &linelen, '\n', dfh);
              fclose(dfh);
              if (len <= 0)
                     break;

              if (!table) {
                     table = calloc(maxnode * maxnode, sizeof(int));
                     if (!table) {
                            errno = ENOMEM;
                            break;
                     }
              }

              parse_numbers(line, table + nd * maxnode);
       }
       free(line);
       if (err)  {
              numa_warn(W_distance,
                       "Cannot parse distance information in sysfs: %s",
                       strerror(errno));
              free(table);
              return err;
       }
       /* Update the global table pointer.  Race window here with
          other threads, but in the worst case we leak one distance
          array one time, which is tolerable. This avoids a
          dependency on pthreads. */
       if (distance_table) {
              free(table);
              return 0;
       }
       distance_numnodes = maxnode;
       distance_table = table;
       return 0;            
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int distance_numnodes [static]

Definition at line 26 of file distance.c.

int* distance_table [static]

Definition at line 27 of file distance.c.