Back to index

numactl  2.0.8~rc4
Defines | Enumerations | Functions | Variables
numamon.c File Reference
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/fcntl.h>

Go to the source code of this file.

Defines

#define _LARGE_FILE_SOURCE   1
#define _GNU_SOURCE   1
#define err(x)   perror(x),exit(1)
#define PERFEVTSEL0   0xc0010000
#define PERFEVTSEL1   0xc0010001
#define PERFEVTSEL2   0xc0010002
#define PERFEVTSEL3   0xc0010003
#define PERFCTR0   0xc0010004
#define PERFCTR1   0xc0010005
#define PERFCTR2   0xc0010006
#define PERFCTR3   0xc0010007
#define EVENT   0xe9
#define PERFEVTSEL_EN   (1 << 22)
#define PERFEVTSEL_OS   (1 << 17)
#define PERFEVTSEL_USR   (1 << 16)
#define BASE   (EVENT | PERFEVTSEL_EN | PERFEVTSEL_OS | PERFEVTSEL_USR)
#define MAXCPU   8

Enumerations

enum  { LOCALLOCAL = 0, LOCALREMOTE = 1, REMOTELOCAL = 2 }

Functions

void usage (void)
void Vprintf (char *fmt,...)
unsigned long long rdmsr (int cpu, unsigned long msr)
void wrmsr (int cpu, unsigned long msr, unsigned long long value)
int cpufilter (int cpu)
void checkcounter (int cpu, int clear)
void setup (int clear)
void printf_padded (int pad, char *fmt,...)
void print_header (void)
void print_cpu (int cpu)
void dumpall (void)
void checkk8 (void)
int main (int ac, char **av)

Variables

static int mem [] = { [LOCALLOCAL] = 0xa8, [LOCALREMOTE] = 0x98, [REMOTELOCAL] = 0x68 }
static int io [] = { [LOCALLOCAL] = 0xa4, [LOCALREMOTE] = 0x94, [REMOTELOCAL] = 0x64 }
static int * masks = mem
int force = 0
int msrfd [MAXCPU]
int delay
int absolute
char * cfilter
int verbose

Define Documentation

#define _GNU_SOURCE   1

Definition at line 22 of file numamon.c.

#define _LARGE_FILE_SOURCE   1

Definition at line 21 of file numamon.c.

Definition at line 55 of file numamon.c.

#define err (   x)    perror(x),exit(1)

Definition at line 38 of file numamon.c.

#define EVENT   0xe9

Definition at line 50 of file numamon.c.

#define MAXCPU   8

Definition at line 57 of file numamon.c.

#define PERFCTR0   0xc0010004

Definition at line 45 of file numamon.c.

#define PERFCTR1   0xc0010005

Definition at line 46 of file numamon.c.

#define PERFCTR2   0xc0010006

Definition at line 47 of file numamon.c.

#define PERFCTR3   0xc0010007

Definition at line 48 of file numamon.c.

#define PERFEVTSEL0   0xc0010000

Definition at line 40 of file numamon.c.

#define PERFEVTSEL1   0xc0010001

Definition at line 41 of file numamon.c.

#define PERFEVTSEL2   0xc0010002

Definition at line 42 of file numamon.c.

#define PERFEVTSEL3   0xc0010003

Definition at line 43 of file numamon.c.

#define PERFEVTSEL_EN   (1 << 22)

Definition at line 51 of file numamon.c.

#define PERFEVTSEL_OS   (1 << 17)

Definition at line 52 of file numamon.c.

#define PERFEVTSEL_USR   (1 << 16)

Definition at line 53 of file numamon.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
LOCALLOCAL 
LOCALREMOTE 
REMOTELOCAL 

Definition at line 33 of file numamon.c.

{ LOCALLOCAL = 0, LOCALREMOTE = 1, REMOTELOCAL = 2 };

Function Documentation

void checkcounter ( int  cpu,
int  clear 
)

Definition at line 119 of file numamon.c.

{
       int i;
       for (i = 1; i < 4; i++) {
              int clear_this = clear;
              unsigned long long evtsel = rdmsr(cpu, PERFEVTSEL0 + i);
              Vprintf("%d: %x %Lx\n", cpu, PERFEVTSEL0 + i, evtsel);
              if (!(evtsel & PERFEVTSEL_EN)) {
                     Vprintf("reinit %d\n", cpu);
                     wrmsr(cpu, PERFEVTSEL0 + i, BASE | masks[i - 1]);
                     clear_this = 1;
              } else if (evtsel == (BASE | (masks[i-1] << 8))) {
                     /* everything fine */
              } else if (force) {
                     Vprintf("reinit force %d\n", cpu);
                     wrmsr(cpu, PERFEVTSEL0 + i, BASE | (masks[i - 1] << 8));
                     clear_this = 1;
              } else {
                     fprintf(stderr, "perfctr %d cpu %d already used with %Lx\n",
                            i, cpu, evtsel);
                     fprintf(stderr, "Consider using -f if you know what you're doing.\n");
                     exit(1);
              }
              if (clear_this) {
                     Vprintf("clearing %d\n", cpu);
                     wrmsr(cpu, PERFCTR0 + i, 0);
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void checkk8 ( void  )

Definition at line 241 of file numamon.c.

{
       char *line = NULL;
       size_t size = 0;
       int bad = 0;
       FILE *f = fopen("/proc/cpuinfo", "r");
       if (!f)
              return;       
       while (getline(&line, &size, f) > 0) {
              if (!strncmp("vendor_id", line, 9)) {
                     if (!strstr(line, "AMD"))
                            bad++;
              }
              if (!strncmp("cpu family", line, 10)) {
                     char *s = line + strcspn(line,":");
                     int family;
                     if (*s == ':') ++s;
                     family = strtoul(s, NULL, 0);
                     if (family != 15)
                            bad++;
              }
       }
       if (bad) {
              printf("not a opteron cpu\n");
              exit(1);
       }
       free(line);
       fclose(f);
}

Here is the caller graph for this function:

int cpufilter ( int  cpu)

Definition at line 95 of file numamon.c.

{
       long num;
       char *end;
       char *s;
       
       if (!cfilter)
              return 1;     
       for (s = cfilter;;) {
              num = strtoul(s, &end, 0);
              if (end == s)
                     usage();
              if (cpu == num)
                     return 1;
              if (*end == ',')
                     s = end+1;
              else if (*end == 0)
                     break;
              else
                     usage();
       }
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void dumpall ( void  )

Definition at line 220 of file numamon.c.

{
       int cnt = 0;
       int cpu;
       print_header();
       for (;;) {
              for (cpu = 0; cpu < MAXCPU; ++cpu) {
                     if (msrfd[cpu] < 0)
                            continue;
                     print_cpu(cpu);
              }
              if (!delay)
                     break;
              sleep(delay);
              if (++cnt > 40) {
                     cnt = 0;
                     print_header();
              }
       }             
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  ac,
char **  av 
)

Definition at line 285 of file numamon.c.

{
       int opt;
       checkk8();
       while ((opt = getopt(ac,av,"ifscmaC:v")) != -1) {
              switch (opt) {
              case 'f':
                     force = 1;
                     break;
              case 'c':
                     setup(1);
                     exit(0);
              case 's':
                     setup(0);
                     exit(0);
              case 'm':
                     masks = mem;
                     break;
              case 'i':
                     masks = io;
                     break;
              case 'a':
                     absolute = 1;
                     break;
              case 'C':
                     cfilter = optarg;
                     break;
              case 'v':
                     verbose = 1;
                     break;
              default:
                     usage();
              }
       }
       if (av[optind]) {
              char *end;
              delay = strtoul(av[optind], &end, 10);
              if (*end)
                     usage();
              if (av[optind+1])
                     usage();
       }

       setup(0);
       dumpall();
       return 0;
}

Here is the call graph for this function:

void print_cpu ( int  cpu)

Definition at line 204 of file numamon.c.

{
       int i;
       static unsigned long long lastval[4];
       printf_padded(4, "%d", cpu);
       for (i = 1; i < 4; i++) {
              unsigned long long val = rdmsr(cpu, PERFCTR0 + i);
              if (absolute)
                     printf_padded(16, "%Lu", val);
              else
                     printf_padded(16, "%Lu", val - lastval[i]);             
              lastval[i] = val;
       }
       putchar('\n');
}

Here is the call graph for this function:

Here is the caller graph for this function:

void print_header ( void  )

Definition at line 195 of file numamon.c.

{
       printf_padded(4, "CPU ");
       printf_padded(16, "LOCAL");
       printf_padded(16, "LOCAL->REMOTE");
       printf_padded(16, "REMOTE->LOCAL");
       putchar('\n');
}

Here is the call graph for this function:

Here is the caller graph for this function:

void printf_padded ( int  pad,
char *  fmt,
  ... 
)

Definition at line 185 of file numamon.c.

{
       char buf[pad + 1];
       va_list ap;
       va_start(ap, fmt);
       vsnprintf(buf, pad, fmt, ap);
       printf("%-*s", pad, buf);
       va_end(ap);
}

Here is the caller graph for this function:

unsigned long long rdmsr ( int  cpu,
unsigned long  msr 
)

Definition at line 77 of file numamon.c.

{
       unsigned long long val;
       if (pread(msrfd[cpu], &val, 8, msr) != 8) {
              fprintf(stderr, "rdmsr of %lx failed: %s\n", msr, strerror(errno));
              exit(1);
       }
       return val;
}

Here is the caller graph for this function:

void setup ( int  clear)

Definition at line 149 of file numamon.c.

{
       DIR *dir;
       struct dirent *d;
       int numcpus = 0;

       memset(msrfd, -1, sizeof(msrfd));
       dir = opendir("/dev/cpu");
       if (!dir)
              err("cannot open /dev/cpu");
       while ((d = readdir(dir)) != NULL) {
              char buf[64];
              char *end;
              long cpunum = strtoul(d->d_name, &end, 0);
              if (*end != 0)
                     continue;
              if (cpunum > MAXCPU) {
                     fprintf(stderr, "too many cpus %ld %s\n", cpunum, d->d_name);
                     continue;
              }
              if (!cpufilter(cpunum))
                     continue;
              snprintf(buf, 63, "/dev/cpu/%ld/msr", cpunum);
              msrfd[cpunum] = open64(buf, O_RDWR);
              if (msrfd[cpunum] < 0)
                     continue;
              numcpus++;
              checkcounter(cpunum, clear);
       }
       closedir(dir);              
       if (numcpus == 0) {
              fprintf(stderr, "No CPU found using MSR driver.\n");
              exit(1);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void usage ( void  )

Definition at line 40 of file memhog.c.

{
       printf("memhog [-rNUM] size[kmg] [policy [nodeset]]\n");
       printf("-rNUM repeat memset NUM times\n");
       printf("-H disable transparent hugepages\n");
       print_policies();
       exit(1);
}
void Vprintf ( char *  fmt,
  ... 
)

Definition at line 68 of file numamon.c.

{
       va_list ap;
       va_start(ap,fmt);
       if (verbose)
              vfprintf(stderr,fmt,ap);
       va_end(ap);
}

Here is the caller graph for this function:

void wrmsr ( int  cpu,
unsigned long  msr,
unsigned long long  value 
)

Definition at line 87 of file numamon.c.

{
       if (pwrite(msrfd[cpu], &value, 8, msr) != 8) {
              fprintf(stderr, "wdmsr of %lx failed: %s\n", msr, strerror(errno));
              exit(1);
       }
}

Here is the caller graph for this function:


Variable Documentation

int absolute

Definition at line 62 of file numamon.c.

char* cfilter

Definition at line 63 of file numamon.c.

int delay

Definition at line 61 of file numamon.c.

int force = 0

Definition at line 59 of file numamon.c.

int io[] = { [LOCALLOCAL] = 0xa4, [LOCALREMOTE] = 0x94, [REMOTELOCAL] = 0x64 } [static]

Definition at line 35 of file numamon.c.

int* masks = mem [static]

Definition at line 36 of file numamon.c.

int mem[] = { [LOCALLOCAL] = 0xa8, [LOCALREMOTE] = 0x98, [REMOTELOCAL] = 0x68 } [static]

Definition at line 34 of file numamon.c.

int msrfd[MAXCPU]

Definition at line 60 of file numamon.c.

int verbose

Definition at line 64 of file numamon.c.