Signed-off-by: Wenchao Hao <haowenchao22@gmail.com>
This commit is contained in:
Wenchao Hao 2024-11-20 09:34:50 +08:00
commit dc9afb27f1
2 changed files with 110 additions and 0 deletions

38
run.sh Normal file
View File

@ -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

72
test_swap.c Normal file
View File

@ -0,0 +1,72 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <openssl/md5.h>
#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;
}