Back to index

numactl  2.0.8~rc4
realloc_test.c
Go to the documentation of this file.
00001 #include <assert.h>
00002 #include <errno.h>
00003 #include <limits.h>
00004 #include <unistd.h>
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 #include <sys/mman.h>
00008 #include "numa.h"
00009 #include "numaif.h"
00010 
00011 #define DEFAULT_NR_PAGES    1024
00012 
00013 static int parse_int(const char *str)
00014 {
00015        char   *endptr;
00016        long   ret = strtol(str, &endptr, 0);
00017        if (*endptr != '\0') {
00018               fprintf(stderr, "[error] strtol() failed: parse error: %s\n", endptr);
00019               exit(1);
00020        }
00021 
00022        if (errno == ERANGE)
00023               fprintf(stderr, "[warning] strtol() out of range\n");
00024 
00025        if (ret > INT_MAX || ret < INT_MIN) {
00026               fprintf(stderr, "[warning] parse_int() out of range\n");
00027               ret = (ret > 0) ? INT_MAX : INT_MIN;
00028        }
00029 
00030        return (int) ret;
00031 }
00032 
00033 int main(int argc, char **argv)
00034 {
00035        char   *mem;
00036        int           page_size = numa_pagesize();
00037        int           node = 0;
00038        int           nr_pages = DEFAULT_NR_PAGES;
00039 
00040        if (numa_available() < 0) {
00041               fprintf(stderr, "numa is not available");
00042               exit(1);
00043        }
00044 
00045        if (argc > 1)
00046               node = parse_int(argv[1]);
00047        if (argc > 2)
00048               nr_pages = parse_int(argv[2]);
00049        
00050        mem = numa_alloc_onnode(page_size, node);
00051 
00052        /* Store the policy of the newly allocated area */
00053        unsigned long nodemask;
00054        int                         mode;
00055        int                         nr_nodes = numa_num_possible_nodes();
00056        if (get_mempolicy(&mode, &nodemask, nr_nodes, mem,
00057                                      MPOL_F_NODE | MPOL_F_ADDR) < 0) {
00058               perror("get_mempolicy() failed");
00059               exit(1);
00060        }
00061 
00062        /* Print some info */
00063        printf("Page size: %d\n", page_size);
00064        printf("Pages realloc'ed: %d\n", nr_pages);
00065        printf("Allocate data in node: %d\n", node);
00066 
00067        int i;
00068        int nr_inplace = 0;
00069        int nr_moved   = 0;
00070        for (i = 0; i < nr_pages; i++) {
00071               /* Enlarge mem with one more page */
00072               char   *new_mem = numa_realloc(mem, (i+1)*page_size, (i+2)*page_size);
00073               if (!new_mem) {
00074                      perror("numa_realloc() failed");
00075                      exit(1);
00076               }
00077 
00078               if (new_mem == mem)
00079                      ++nr_inplace;
00080               else
00081                      ++nr_moved;
00082               mem = new_mem;
00083 
00084               /* Check the policy of the realloc'ed area */
00085               unsigned long realloc_nodemask;
00086               int                         realloc_mode;
00087               if (get_mempolicy(&realloc_mode, &realloc_nodemask,
00088                                             nr_nodes, mem, MPOL_F_NODE | MPOL_F_ADDR) < 0) {
00089                      perror("get_mempolicy() failed");
00090                      exit(1);
00091               }
00092 
00093               assert(realloc_nodemask == nodemask &&
00094                         realloc_mode == mode && "policy changed");
00095        }
00096 
00097        /* Shrink to the original size */
00098        mem = numa_realloc(mem, (nr_pages + 1)*page_size, page_size);
00099        if (!mem) {
00100               perror("numa_realloc() failed");
00101               exit(1);
00102        }
00103 
00104        numa_free(mem, page_size);
00105        printf("In-place reallocs: %d\n", nr_inplace);
00106        printf("Moved reallocs: %d\n", nr_moved);
00107        return 0;
00108 }