From dc9afb27f11ac575b06b80ffe009e040ab24ad8f Mon Sep 17 00:00:00 2001 From: Wenchao Hao Date: Wed, 20 Nov 2024 09:34:50 +0800 Subject: [PATCH] init Signed-off-by: Wenchao Hao --- run.sh | 38 ++++++++++++++++++++++++++++ test_swap.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 run.sh create mode 100644 test_swap.c diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..dfe563f --- /dev/null +++ b/run.sh @@ -0,0 +1,38 @@ +echo always > /sys/kernel/mm/transparent_hugepage/hugepages-64kB/enabled + +tmpfile=$(mktemp) + +pgfault1=$(cat /proc/vmstat | grep pgfault | awk '{print $2}') + +./test_swap > $tmpfile + +pgfault2=$(cat /proc/vmstat | grep pgfault | awk '{print $2}') + +swapout=$(head -n 1 $tmpfile | awk '{print $4}') +swapin=$(tail -n 1 $tmpfile | awk '{print $4}') + +cat /sys/block/zram0/mm_stat > $tmpfile + +total_size=$(cat $tmpfile | awk '{print $1}') +used_size=$(cat $tmpfile | awk '{print $2}') +comp_ratio=`echo "scale=4; $total_size / $used_size" | bc` + + +anon_fault_alloc=$(cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/anon_fault_alloc) +anon_fault_fallback=$(cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/anon_fault_fallback) +anon_total=$(($anon_fault_fallback + $anon_fault_alloc)) +folio_alloc_success_ratio=`echo "scale=4; $anon_fault_alloc / $anon_total" | bc` + +swpout_alloc=$(cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/swpout) +swpout_fault_fallback=$(cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/swpout_fallback) +swpout_total=$(($swpout_fault_fallback + $swpout_alloc)) +swap_success_ratio=`echo "scale=4; $swpout_alloc / $swpout_total" | bc` + +echo swapout $swapout +echo swapin $swapin +echo comp_ratio $comp_ratio +echo pagefault $((pgfault2-pgfault1)) +echo folio_alloc_success_ratio $folio_alloc_success_ratio +echo swap_success_ratio $swap_success_ratio + +killall test_swap diff --git a/test_swap.c b/test_swap.c new file mode 100644 index 0000000..7d650c2 --- /dev/null +++ b/test_swap.c @@ -0,0 +1,72 @@ +#include +#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) = (unsigned long)rand() << 32 | rand(); + + /* 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); + return 0; + } + + madvise(p, SIZE, MADV_PAGEOUT); + + while (1) { + sleep(10); + } + + return 0; +}