From a78d53518fee4f499ec37bbbc0ff0fcb20af67ea Mon Sep 17 00:00:00 2001 From: Wenchao Hao Date: Tue, 19 Nov 2024 19:56:45 +0800 Subject: [PATCH] add Signed-off-by: Wenchao Hao --- test_swap.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 test_swap.c diff --git a/test_swap.c b/test_swap.c new file mode 100644 index 0000000..17ffa61 --- /dev/null +++ b/test_swap.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SIZE 19*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; + 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) = 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))); + + return 0; +}