#include #include #include #include #include #include #include #include #include #define SIZE 500*1024*1024 #define PAGE_SIZE 4096 // 4KB #define CHUNK_SIZE 8 * 1024 // 8KB unsigned long long tv_to_ms(struct timeval tv) { return tv.tv_sec * 1000 + tv.tv_usec / 1000; } int main() { int i; char *c; pid_t pid; unsigned long *p; struct timeval tv_b, tv_e;; p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (!p) { perror("fail to get memory"); exit(-1); } c = (unsigned char *)p; /* fill data */ for (i = 0; i < SIZE / 8; i += 8) *(p + i) = 0xffff + i; /* trigger pageout and get time of pageout */ 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))); /* read to trigger swapin and */ gettimeofday(&tv_b, NULL); for (i = 0; i < SIZE; i += PAGE_SIZE) *(c + i) = i; gettimeofday(&tv_e, NULL); printf("swp in bandwidth: %ld bytes/ms\n", SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b))); pid = fork(); if (pid) { munmap(p, SIZE); sleep(3); return 0; } madvise(p, SIZE, MADV_PAGEOUT); while (1) { sleep(10); } return 0; }