-
-
Save travisdowns/c4a5d134beb47312e705eb3af91ade14 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
# compile with: | |
# cc -O1 -Wall pagefaults.c -o pagefaults | |
# run with (for 1 GB region): | |
# perf stat ./pagefaults 1000000000 | |
#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 = malloc(size); | |
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