Back to index

numactl  2.0.8~rc4
migrate_pages.c
Go to the documentation of this file.
00001 /*
00002  * Test program to test the moving of a processes pages.
00003  *
00004  * (C) 2006 Silicon Graphics, Inc.
00005  *            Christoph Lameter <clameter@sgi.com>
00006  */
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <numa.h>
00010 #include <unistd.h>
00011 #include <errno.h>
00012 
00013 unsigned int pagesize;
00014 unsigned int page_count = 32;
00015 
00016 char *page_base;
00017 char *pages;
00018 
00019 void **addr;
00020 int *status;
00021 int *nodes;
00022 int errors;
00023 int nr_nodes;
00024 
00025 struct bitmask *old_nodes;
00026 struct bitmask *new_nodes;
00027 
00028 int main(int argc, char **argv)
00029 {
00030        int i, rc;
00031 
00032        pagesize = getpagesize();
00033 
00034        nr_nodes = numa_max_node()+1;
00035 
00036        old_nodes = numa_bitmask_alloc(nr_nodes);
00037         new_nodes = numa_bitmask_alloc(nr_nodes);
00038         numa_bitmask_setbit(old_nodes, 1);
00039         numa_bitmask_setbit(new_nodes, 0);
00040 
00041        if (nr_nodes < 2) {
00042               printf("A minimum of 2 nodes is required for this test.\n");
00043               exit(1);
00044        }
00045 
00046        setbuf(stdout, NULL);
00047        printf("migrate_pages() test ......\n");
00048        if (argc > 1)
00049               sscanf(argv[1], "%d", &page_count);
00050 
00051        page_base = malloc((pagesize + 1) * page_count);
00052        addr = malloc(sizeof(char *) * page_count);
00053        status = malloc(sizeof(int *) * page_count);
00054        nodes = malloc(sizeof(int *) * page_count);
00055        if (!page_base || !addr || !status || !nodes) {
00056               printf("Unable to allocate memory\n");
00057               exit(1);
00058        }
00059 
00060        pages = (void *) ((((long)page_base) & ~((long)(pagesize - 1))) + pagesize);
00061 
00062        for (i = 0; i < page_count; i++) {
00063               if (i != 2)
00064                      /* We leave page 2 unallocated */
00065                      pages[ i * pagesize ] = (char) i;
00066               addr[i] = pages + i * pagesize;
00067               nodes[i] = 1;
00068               status[i] = -123;
00069        }
00070 
00071        /* Move to starting node */
00072        rc = numa_move_pages(0, page_count, addr, nodes, status, 0);
00073        if (rc < 0 && errno != ENOENT) {
00074               perror("move_pages");
00075               exit(1);
00076        }
00077 
00078        /* Verify correct startup locations */
00079        printf("Page location at the beginning of the test\n");
00080        printf("------------------------------------------\n");
00081 
00082        numa_move_pages(0, page_count, addr, NULL, status, 0);
00083        for (i = 0; i < page_count; i++) {
00084               printf("Page %d vaddr=%p node=%d\n", i, pages + i * pagesize, status[i]);
00085               if (i != 2 && status[i] != 1) {
00086                      printf("Bad page state before migrate_pages. Page %d status %d\n",i, status[i]);
00087                      exit(1);
00088               }
00089        }
00090 
00091        /* Move to node zero */
00092        numa_move_pages(0, page_count, addr, nodes, status, 0);
00093 
00094        printf("\nMigrating the current processes pages ...\n");
00095        rc = numa_migrate_pages(0, old_nodes, new_nodes);
00096 
00097        if (rc < 0) {
00098               perror("numa_migrate_pages failed");
00099               errors++;
00100        }
00101 
00102        /* Get page state after migration */
00103        numa_move_pages(0, page_count, addr, NULL, status, 0);
00104        for (i = 0; i < page_count; i++) {
00105               printf("Page %d vaddr=%lx node=%d\n", i,
00106                      (unsigned long)(pages + i * pagesize), status[i]);
00107               if (i != 2) {
00108                      if (pages[ i* pagesize ] != (char) i) {
00109                             printf("*** Page contents corrupted.\n");
00110                             errors++;
00111                      } else if (status[i]) {
00112                             printf("*** Page on the wrong node\n");
00113                             errors++;
00114                      }
00115               }
00116        }
00117 
00118        if (!errors)
00119               printf("Test successful.\n");
00120        else
00121               printf("%d errors.\n", errors);
00122 
00123        return errors > 0 ? 1 : 0;
00124 }
00125