Back to index

numactl  2.0.8~rc4
move_pages.c
Go to the documentation of this file.
00001 /*
00002  * Test program to test the moving of individual pages in a process.
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 <asm/unistd.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 int main(int argc, char **argv)
00026 {
00027        int i, rc;
00028 
00029        pagesize = getpagesize();
00030 
00031        nr_nodes = numa_max_node();
00032 
00033        if (nr_nodes < 2) {
00034               printf("A minimum of 2 nodes is required for this test.\n");
00035               exit(1);
00036        }
00037 
00038        setbuf(stdout, NULL);
00039        printf("move_pages() test ......\n");
00040        if (argc > 1)
00041               sscanf(argv[1], "%d", &page_count);
00042 
00043        printf("pages=%d (%s)\n", page_count, argv[1]);
00044 
00045        page_base = malloc((pagesize + 1) * page_count);
00046        addr = malloc(sizeof(char *) * page_count);
00047        status = malloc(sizeof(int *) * page_count);
00048        nodes = malloc(sizeof(int *) * page_count);
00049        if (!page_base || !addr || !status || !nodes) {
00050               printf("Unable to allocate memory\n");
00051               exit(1);
00052        }
00053 
00054        pages = (void *) ((((long)page_base) & ~((long)(pagesize - 1))) + pagesize);
00055 
00056        for (i = 0; i < page_count; i++) {
00057               if (i != 2)
00058                      /* We leave page 2 unallocated */
00059                      pages[ i * pagesize ] = (char) i;
00060               addr[i] = pages + i * pagesize;
00061               nodes[i] = (i % nr_nodes);
00062               status[i] = -123;
00063        }
00064 
00065        printf("\nMoving pages to start node ...\n");
00066        rc = numa_move_pages(0, page_count, addr, NULL, status, 0);
00067        if (rc < 0)
00068               perror("move_pages");
00069 
00070        for (i = 0; i < page_count; i++)
00071               printf("Page %d vaddr=%p node=%d\n", i, pages + i * pagesize, status[i]);
00072 
00073        printf("\nMoving pages to target nodes ...\n");
00074        rc = numa_move_pages(0, page_count, addr, nodes, status, 0);
00075 
00076        if (rc < 0) {
00077               perror("move_pages");
00078               errors++;
00079        }
00080 
00081        for (i = 0; i < page_count; i++) {
00082               if (i != 2) {
00083                      if (pages[ i* pagesize ] != (char) i)
00084                             errors++;
00085                      else if (nodes[i] != (i % nr_nodes))
00086                             errors++;
00087               }
00088        }
00089 
00090        for (i = 0; i < page_count; i++) {
00091               printf("Page %d vaddr=%lx node=%d\n", i,
00092                      (unsigned long)(pages + i * pagesize), status[i]);
00093        }
00094 
00095        if (!errors)
00096               printf("Test successful.\n");
00097        else
00098               printf("%d errors.\n", errors);
00099 
00100        return errors > 0 ? 1 : 0;
00101 }
00102