diff options
author | Fengguang Wu <fengguang.wu@intel.com> | 2012-11-27 20:21:57 +0800 |
---|---|---|
committer | Fengguang Wu <fengguang.wu@intel.com> | 2012-11-27 20:29:21 +0800 |
commit | 15c7945b084dfae78bf02566620e7b0c72eac464 (patch) | |
tree | ceede2500e71b9c8245e50d5d37373606660bf12 | |
parent | d4cefc51ab34423ff782693df8bd0d9fd0b5bdbe (diff) | |
download | vm-scalability-15c7945b084dfae78bf02566620e7b0c72eac464.tar.gz |
Abhinav's work on usemem_remap.c
Signed-off-by: Fengguang Wu <fengguang.wu@intel.com>
-rw-r--r-- | usemem_remap.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/usemem_remap.c b/usemem_remap.c new file mode 100644 index 0000000..95cb833 --- /dev/null +++ b/usemem_remap.c @@ -0,0 +1,66 @@ +/* + * Exercises functions in fremap.c + * + * Takes a file as argument along with its size and creates a non linear mapping in memory + * + * then reverses the page order in memory using remap_file_pages system call + */ + +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> + +int main (int argc, char *argv[]) +{ + int fd; + int pagesize = getpagesize(); + int i; + + unsigned long size; + int no_of_pages; + + /* initialize to NULL */ + unsigned char *start = NULL; + unsigned char *end; + + if (argc != 3) { + fprintf(stdout, "Usage : %s FILE SIZE\n", argv[0]); + exit(1); + } + + size = atoi(argv[2]); + no_of_pages = size / pagesize; + + if ((fd = open(argv[1], O_RDWR | O_CREAT, 0666)) == -1) { + fprintf(stderr, "error opening file\n"); + exit(1); + } + + /* create a contiguous mapping */ + if ((start = (unsigned char *) mmap(NULL, size, PROT_READ|PROT_WRITE, + MAP_SHARED|MAP_POPULATE, fd, 0)) == MAP_FAILED) { + fprintf(stderr, "%s: mmap failed: %s\n", + argv[1], strerror(errno)); + exit(1); + } + + end = (unsigned char *) (start + size); + + /* make mapping non-contiguous (reverse the page mapping order) */ + for (i = 0; i < no_of_pages / 2; i++) { + if (remap_file_pages(end - (i * pagesize), + pagesize, 0, i, MAP_SHARED|MAP_POPULATE) == -1) { + fprintf(stderr, "remap failed with error %s\n", strerror(errno)); + exit(1); + } + } + + close(fd); + + return 0; +} |