Back to index

numactl  2.0.8~rc4
Defines | Enumerations | Functions | Variables
memhog.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <string.h>
#include <stdbool.h>
#include "numa.h"
#include "numaif.h"
#include "util.h"

Go to the source code of this file.

Defines

#define terr(x)   perror(x)
#define MADV_NOHUGEPAGE   15

Enumerations

enum  { UNIT = 10*1024*1024 }

Functions

void usage (void)
void hog (void *map)
int main (int ac, char **av)

Variables

int repeat = 1
long length

Define Documentation

#define MADV_NOHUGEPAGE   15

Definition at line 35 of file memhog.c.

#define terr (   x)    perror(x)

Definition at line 28 of file memhog.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
UNIT 

Definition at line 30 of file memhog.c.

     {
       UNIT = 10*1024*1024,
};

Function Documentation

void hog ( void *  map)

Definition at line 51 of file memhog.c.

{
       long i;
       for (i = 0;  i < length; i += UNIT) {
              long left = length - i;
              if (left > UNIT)
                     left = UNIT;
              putchar('.');
              fflush(stdout);
              memset(map + i, 0xff, left);
       }
       putchar('\n');
}

Here is the caller graph for this function:

int main ( int  ac,
char **  av 
)

Definition at line 65 of file memhog.c.

{
       char *map;
       struct bitmask *nodes, *gnodes;
       int policy, gpolicy;
       int ret = 0;
       int loose = 0;
       int i;
       int fd = -1;
       bool disable_hugepage = false;

       nodes = numa_allocate_nodemask();
       gnodes = numa_allocate_nodemask();

       while (av[1] && av[1][0] == '-') {
              switch (av[1][1]) {
              case 'f':
                     fd = open(av[1]+2, O_RDWR);
                     if (fd < 0)
                            perror(av[1]+2);
                     break; 
              case 'r':
                     repeat = atoi(av[1] + 2);
                     break;
              case 'H':
                     disable_hugepage = true;
                     break;
              default:      
                     usage();
              }
              av++;         
       }
       
       if (!av[1]) usage();

       length = memsize(av[1]);
       if (av[2] && numa_available() < 0) {
              printf("Kernel doesn't support NUMA policy\n");
              exit(1);
       } else
              loose = 1;
       policy = parse_policy(av[2], av[3]);
       if (policy != MPOL_DEFAULT)
              nodes = numa_parse_nodestring(av[3]);
        if (!nodes) {
              printf ("<%s> is invalid\n", av[3]);
              exit(1);
       }
       
       if (fd >= 0)
              map = mmap(NULL,length,PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
       else   
              map = mmap(NULL, length, PROT_READ|PROT_WRITE,
                               MAP_PRIVATE|MAP_ANONYMOUS,
                               0, 0);
       if (map == (char*)-1)
              err("mmap");
       
       if (mbind(map, length, policy, nodes->maskp, nodes->size, 0) < 0)
              terr("mbind");

       if (disable_hugepage)
              madvise(map, length, MADV_NOHUGEPAGE);
       
       gpolicy = -1;
       if (get_mempolicy(&gpolicy, gnodes->maskp, gnodes->size, map, MPOL_F_ADDR) < 0)
              terr("get_mempolicy");
       if (!loose && policy != gpolicy) {
              ret = 1;
              printf("policy %d gpolicy %d\n", policy, gpolicy);
       }
       if (!loose && !numa_bitmask_equal(gnodes, nodes)) {
              printf("nodes differ %lx, %lx!\n",
                     gnodes->maskp[0], nodes->maskp[0]);
              ret = 1;
       }

       for (i = 0; i < repeat; i++)
              hog(map);
       exit(ret);
}

Here is the call 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);
}

Here is the caller graph for this function:


Variable Documentation

long length

Definition at line 49 of file memhog.c.

int repeat = 1

Definition at line 38 of file memhog.c.