diff --git a/madvise.c b/madvise.c new file mode 100644 index 0000000..1d6ad1b --- /dev/null +++ b/madvise.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +unsigned long long tv_to_ms(struct timeval tv) +{ + return tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + +void main() +{ + struct timeval tv_b, tv_e;; +#define SIZE 400*1024*1024 + void *p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (!p) { + perror("fail to get memory"); + exit(-1); + } + + madvise(p, SIZE, MADV_HUGEPAGE); + memset(p, 0x11, SIZE); /* write to get mem */ + + gettimeofday(&tv_b, NULL); + madvise(p, SIZE, MADV_PAGEOUT); + gettimeofday(&tv_e, NULL); + + printf("swp out bandwidth: %ld bytes/ms\n", + SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b))); +}