Back to index

numactl  2.0.8~rc4
mbind_mig_pages.c
Go to the documentation of this file.
00001 /*
00002  * Test program to test the moving of pages using mbind.
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 <numaif.h>
00011 #include <unistd.h>
00012 #include <asm/unistd.h>
00013 
00014 unsigned int pagesize;
00015 unsigned int page_count = 32;
00016 
00017 char *page_base;
00018 char *pages;
00019 
00020 void **addr;
00021 int *status;
00022 int *nodes;
00023 int errors;
00024 int nr_nodes;
00025 
00026 struct bitmask *old_nodes;
00027 struct bitmask *new_nodes;
00028 
00029 int main(int argc, char **argv)
00030 {
00031        int i, rc;
00032 
00033        pagesize = getpagesize();
00034 
00035        nr_nodes = numa_max_node()+1;
00036 
00037        old_nodes = numa_bitmask_alloc(nr_nodes);
00038        new_nodes = numa_bitmask_alloc(nr_nodes);
00039        numa_bitmask_setbit(old_nodes, 0);
00040        numa_bitmask_setbit(new_nodes, 1);
00041 
00042        if (nr_nodes < 2) {
00043               printf("A minimum of 2 nodes is required for this test.\n");
00044               exit(1);
00045        }
00046 
00047        setbuf(stdout, NULL);
00048        printf("mbind migration test ......\n");
00049        if (argc > 1)
00050               sscanf(argv[1], "%d", &page_count);
00051 
00052        page_base = malloc((pagesize + 1) * page_count);
00053        addr = malloc(sizeof(char *) * page_count);
00054        status = malloc(sizeof(int *) * page_count);
00055        nodes = malloc(sizeof(int *) * page_count);
00056        if (!page_base || !addr || !status || !nodes) {
00057               printf("Unable to allocate memory\n");
00058               exit(1);
00059        }
00060 
00061        pages = (void *) ((((long)page_base) & ~((long)(pagesize - 1))) + pagesize);
00062 
00063        for (i = 0; i < page_count; i++) {
00064               if (i != 2)
00065                      /* We leave page 2 unallocated */
00066                      pages[ i * pagesize ] = (char) i;
00067               addr[i] = pages + i * pagesize;
00068               nodes[i] = 0;
00069               status[i] = -123;
00070        }
00071 
00072        /* Move pages toi node zero */
00073        numa_move_pages(0, page_count, addr, nodes, status, 0);
00074 
00075        printf("\nPage status before page migration\n");
00076        printf("---------------------------------\n");
00077        rc = numa_move_pages(0, page_count, addr, NULL, status, 0);
00078        if (rc < 0) {
00079               perror("move_pages");
00080               exit(1);
00081        }
00082 
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]) {
00086                      printf("Bad page state. Page %d status %d\n",i, status[i]);
00087                      exit(1);
00088               }
00089        }
00090 
00091        /* Move to node zero */
00092        printf("\nMoving pages via mbind to node 0 ...\n");
00093        rc = mbind(pages, page_count * pagesize, MPOL_BIND, old_nodes->maskp,
00094               old_nodes->size + 1, MPOL_MF_MOVE | MPOL_MF_STRICT);
00095        if (rc < 0) {
00096               perror("mbind");
00097               errors++;
00098        }
00099 
00100 
00101        printf("\nMoving pages via mbind from node 0 to 1 ...\n");
00102        rc = mbind(pages, page_count * pagesize, MPOL_BIND, new_nodes->maskp,
00103               new_nodes->size + 1, MPOL_MF_MOVE | MPOL_MF_STRICT);
00104        if (rc < 0) {
00105               perror("mbind");
00106               errors++;
00107        }
00108 
00109        numa_move_pages(0, page_count, addr, NULL, status, 0);
00110        for (i = 0; i < page_count; i++) {
00111               printf("Page %d vaddr=%lx node=%d\n", i,
00112                      (unsigned long)(pages + i * pagesize), status[i]);
00113               if (i != 2) {
00114                      if (pages[ i* pagesize ] != (char) i) {
00115                             printf("*** Page content corrupted.\n");
00116                             errors++;
00117                      } else if (status[i] != 1) {
00118                             printf("*** Page on wrong node.\n");
00119                             errors++;
00120                      }
00121               }
00122        }
00123 
00124        if (!errors)
00125               printf("Test successful.\n");
00126        else
00127               printf("%d errors.\n", errors);
00128 
00129        return errors > 0 ? 1 : 0;
00130 }
00131