-
-
Save travisdowns/e45151402a691ec6c63ec72181875c42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <err.h> | |
#include <sys/mman.h> | |
#define DEFAULT_SIZE (40 * 1024 * 1024) | |
#define PG_SIZE 4096 | |
void failcheck(int ret, const char* what) { | |
if (ret) { | |
err(EXIT_FAILURE, "%s failed", what); | |
} else { | |
printf("%s OK\n", what); | |
} | |
} | |
int main(int argc, char **argv) { | |
size_t size = (argc == 2 ? atol(argv[1]) : DEFAULT_SIZE); | |
char *mem; | |
if (getenv("DO_MMAP")) { | |
int flags = MAP_PRIVATE | MAP_ANONYMOUS | (getenv("DO_POPULATE") ? MAP_POPULATE : 0); | |
mem = mmap(0, size, PROT_READ | PROT_WRITE, flags, -1, 0); | |
failcheck(mem == MAP_FAILED, "mmap"); | |
} else { | |
mem = malloc(size); | |
} | |
if (getenv("DO_THP")) { | |
failcheck(madvise(mem, size, MADV_HUGEPAGE), "madvise"); | |
} | |
if (getenv("DO_MADVISE")) { | |
failcheck(madvise(mem, size, MADV_WILLNEED), "madvise"); | |
} | |
if (getenv("DO_MLOCK")) { | |
failcheck(mlock(mem, size), "mlock"); | |
failcheck(munlock(mem, size), "munlock"); | |
} | |
for (volatile char *p = mem; p < mem + size; p += PG_SIZE) { | |
*p = 'z'; | |
} | |
printf("size: %6.2f MiB, pages touched: %zu\npoitner value : %p\n", | |
size / 1024. / 1024., size / PG_SIZE, mem); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment