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:12 +0800 |
commit | d898283aa449ed5284b230ec6b9e009a73a42c03 (patch) | |
tree | 3a23fa6acc06be20de3420d2d0d3531b987878b5 | |
parent | 83122be99ead58210b22f9a8a6b1f63eab73f2da (diff) | |
download | vm-scalability-d898283aa449ed5284b230ec6b9e009a73a42c03.tar.gz |
Abhinav's work on usemem_ksm.c
Signed-off-by: Fengguang Wu <fengguang.wu@intel.com>
-rw-r--r-- | usemem_ksm.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/usemem_ksm.c b/usemem_ksm.c new file mode 100644 index 0000000..0d3ff1d --- /dev/null +++ b/usemem_ksm.c @@ -0,0 +1,69 @@ +/* + * usemem_ksm.c exercises the ksm.c file in the mm + * + * It takes one argument 'size' and mmaps anonymous memory of 'size' + * + * into the process virtual address space. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <sys/mman.h> +#include <unistd.h> + +#define SLEEP_TIME (60) + +void usage(char *name) +{ + fprintf(stderr, "usage: %s SIZE\n", name); + exit(1); +} + +/* calls madvise with the the specified flag */ + +void call_madvise (unsigned long *pointer_to_address, unsigned long size, int advise) +{ + if ((madvise(pointer_to_address, size, advise)) == -1) { + fprintf(stderr, "madvise failed with error : %s\n", strerror(errno)); + munmap(pointer_to_address, size); + exit(1); + } +} + +int main(int argc, char *argv[]) +{ + /*int PS = getpagesize();*/ + + if (argc != 2) usage(argv[0]); + + unsigned long size = atoi(argv[1]); + + unsigned long *p; + + p = mmap(NULL, size, PROT_READ|PROT_WRITE, + MAP_POPULATE|MAP_ANON|MAP_PRIVATE, -1, 0); + + if (p == MAP_FAILED) { + fprintf(stderr, "anon mmap failed: %s\n", strerror(errno)); + exit(1); + } + + /* call madvise with MERGEABLE flags to enable ksm scanning */ + call_madvise(p, size, MADV_MERGEABLE); + + /* sleep for SLEEP_TIME seconds*/ + sleep(SLEEP_TIME); + + /* disable the MERGEABLE flag*/ + call_madvise(p, size, MADV_UNMERGEABLE); + + /* HUGEPAGE advised -- not related to ksm */ + call_madvise(p, size, MADV_HUGEPAGE); + + /* unmap mapped memory */ + munmap(p, size); + return 0; + +} |