Compare commits

...

No commits in common. "master" and "swap" have entirely different histories.
master ... swap

11 changed files with 411 additions and 119 deletions

54
bak.c
View File

@ -1,54 +0,0 @@
#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>
unsigned long long tv_to_ms(struct timeval tv)
{
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
void main()
{
char c;
pid_t pid;
int i;
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)));
pid = fork();
if (pid) {
//scanf("%c", &c);
gettimeofday(&tv_b, NULL);
memset(p, 0x33, SIZE); /* write to get mem */
gettimeofday(&tv_e, NULL);
printf("swp in bandwidth: %ld bytes/ms\n",
SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
}
while(1) {
sleep(100);
}
}

43
bilibili.sh Normal file
View File

@ -0,0 +1,43 @@
echo > csv_bilibili.csv
for dir in /sys/kernel/mm/transparent_hugepage/hugepages-*; do
echo $dir $(cat $dir/enabled) >> csv_bilibili.csv
done
echo "dtlbmiss,itlbmiss,pagefault" >> csv_bilibili.csv
echo 3 > /proc/sys/vm/drop_caches
for i in $(seq 1 12); do
# record data background
simpleperf stat -e dTLB-load-misses,iTLB-load-misses,page-faults --app tv.danmaku.bili > result 2>&1 &
echo "start biliblii on your phone"
while true; do
if [ $(ps -ef | grep -v simpleperf | grep -c tv.danmaku.bili) -gt 1 ]; then
break
else
echo .
fi
done
for j in $(seq 1 5); do
echo $j
sleep 1
done
echo "killing biliblii"
for pid in `ps -ef | grep tv.danmaku.bili | grep -v simpleperf | grep -v grep| awk '{print $2}'`; do kill $pid; done
killall simpleperf
while true; do
if [ $(cat result | grep -c "dTLB-load-misses") -gt 0 ]; then
break
fi
sleep 1
echo '...'
done
dtlbmiss=$(cat result | grep dTLB-load-misses | awk '{print $1}' | sed 's/,//g')
itlbmiss=$(cat result | grep iTLB-load-misses | awk '{print $1}' | sed 's/,//g')
pagefault=$(cat result | grep page-faults | awk '{print $1}' | sed 's/,//g')
echo "$dtlbmiss,$itlbmiss,$pagefault"
echo "$dtlbmiss,$itlbmiss,$pagefault" >> csv_bilibili.csv
done

60
demo.c
View File

@ -1,60 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MB_BYTES (1024*1024)
int main(int argc, char **argv)
{
unsigned long *p;
unsigned long *m;
int i = 0;
int max;
const char *file_path = argv[1];
int fd = open(file_path, O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
perror("fstat");
close(fd);
return 1;
}
size_t file_size = sb.st_size;
printf("%ld, %d Kb\n", file_size, file_size/1024);
void *mapped = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mapped == MAP_FAILED) {
perror("mmap");
close(fd);
return 1;
}
m = mapped;
max = file_size / 8;
p = malloc(file_size);
for (i = 0; i < max; i++) {
*(p + i) = *(m + i);
*(m + i) = *(p + i);
}
fork();
fork();
fork();
while(1);
return 0;
}

45
douyin.sh Normal file
View File

@ -0,0 +1,45 @@
echo > csv_douyin.csv
for dir in /sys/kernel/mm/transparent_hugepage/hugepages-*; do
echo $dir $(cat $dir/enabled) >> csv_douyin.csv
done
echo "dtlbmiss,itlbmiss,pagefault" >> csv_douyin.csv
echo 3 > /proc/sys/vm/drop_caches
for i in $(seq 1 12); do
# record data background
simpleperf stat -e dTLB-load-misses,iTLB-load-misses,page-faults --app com.ss.android.ugc.aweme > result 2>&1 &
echo "start douyin on your phone"
#read tmp
while true; do
if [ $(ps -ef | grep -v simpleperf | grep -c com.ss.android.ugc.aweme) -gt 1 ]; then
break
else
echo .
fi
done
for j in $(seq 1 5); do
echo $j
sleep 1
done
echo "killing douyin"
for pid in `ps -ef | grep com.ss.android.ugc.aweme | grep -v simpleperf | grep -v grep| awk '{print $2}'`; do kill $pid; done
killall simpleperf
while true; do
if [ $(cat result | grep -c "dTLB-load-misses") -gt 0 ]; then
break
fi
sleep 1
echo '...'
done
dtlbmiss=$(cat result | grep dTLB-load-misses | awk '{print $1}' | sed 's/,//g')
itlbmiss=$(cat result | grep iTLB-load-misses | awk '{print $1}' | sed 's/,//g')
pagefault=$(cat result | grep page-faults | awk '{print $1}' | sed 's/,//g')
echo "$dtlbmiss,$itlbmiss,$pagefault"
echo "$dtlbmiss,$itlbmiss,$pagefault" >> csv_douyin.csv
done

173
multi_process_memory_test.c Normal file
View File

@ -0,0 +1,173 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#define MAX_ALLOC_SIZE (256 * 1024 * 1024) // Maximum size for a single anonymous allocation (256MB)
#define MAX_SLEEP_TIME 5 // Maximum sleep time (5 seconds)
#define MMAP_FILE "testfile.dat" // File used for mmap operations
#define DIRECT_IO_FILE "direct_io_file.dat" // File used for direct read/write operations
#define FILE_SIZE (128 * 1024 * 1024) // File size (128MB)
#define NUM_PROCESSES 4 // Number of child processes
// Generate a random size within the given limit
size_t random_size(size_t max) {
return (rand() % max) + 1;
}
void allocate_anonymous_memory() {
// Randomly allocate fragmented anonymous memory
size_t alloc_size = random_size(MAX_ALLOC_SIZE);
void *mem = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
perror("Anonymous mmap failed");
return;
}
// Touch the memory to trigger physical allocation
for (size_t i = 0; i < alloc_size; i += 4096) {
((char *)mem)[i] = rand() % 256;
}
// Lock part of the allocated memory
if (mlock(mem, alloc_size / 2) != 0) {
perror("mlock failed");
}
// Sleep for a random time
unsigned int sleep_time = random_size(MAX_SLEEP_TIME);
sleep(sleep_time);
// Unlock and release the memory
if (munmap(mem, alloc_size) != 0) {
perror("munmap failed");
}
}
void allocate_file_backed_memory() {
// Open or create the file for mmap
int fd = open(MMAP_FILE, O_CREAT | O_RDWR, 0644);
if (fd < 0) {
perror("File open failed (mmap file)");
return;
}
// Expand the file to the target size
if (ftruncate(fd, FILE_SIZE) != 0) {
perror("File truncate failed (mmap file)");
close(fd);
return;
}
// Map the file into memory
void *file_mem = mmap(NULL, FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (file_mem == MAP_FAILED) {
perror("File-backed mmap failed");
close(fd);
return;
}
// Modify the memory-mapped file region
for (size_t i = 0; i < FILE_SIZE; i += 4096) {
((char *)file_mem)[i] = rand() % 256;
}
// Sleep for a random time
unsigned int sleep_time = random_size(MAX_SLEEP_TIME);
sleep(sleep_time);
// Unmap the file-backed memory
if (munmap(file_mem, FILE_SIZE) != 0) {
perror("munmap failed");
}
close(fd);
}
void direct_file_read_write() {
// Open or create the file for direct I/O
int fd = open(DIRECT_IO_FILE, O_CREAT | O_RDWR, 0644);
if (fd < 0) {
perror("File open failed (direct I/O file)");
return;
}
// Expand the file to the target size
if (ftruncate(fd, FILE_SIZE) != 0) {
perror("File truncate failed (direct I/O file)");
close(fd);
return;
}
// Perform read/write operations on the file
char buffer[4096];
for (size_t i = 0; i < FILE_SIZE; i += sizeof(buffer)) {
// Write random data
memset(buffer, rand() % 256, sizeof(buffer));
if (write(fd, buffer, sizeof(buffer)) < 0) {
perror("File write failed (direct I/O file)");
close(fd);
return;
}
// Reset the file offset
lseek(fd, i, SEEK_SET);
// Read data back
if (read(fd, buffer, sizeof(buffer)) < 0) {
perror("File read failed (direct I/O file)");
close(fd);
return;
}
}
close(fd);
}
void child_process_task() {
srand(time(NULL) ^ getpid()); // Use a different random seed for each child process
while (1) {
// Execute anonymous memory allocation
allocate_anonymous_memory();
// Execute file-backed memory operations
allocate_file_backed_memory();
// Execute direct file read/write operations
direct_file_read_write();
// Sleep for a short time between iterations
unsigned int sleep_time = random_size(3); // Short sleep
sleep(sleep_time);
}
}
int main() {
pid_t pids[NUM_PROCESSES];
// Create multiple child processes
for (int i = 0; i < NUM_PROCESSES; i++) {
pids[i] = fork();
if (pids[i] == 0) {
// Child process executes the task
child_process_task();
exit(0); // Prevent the child process from continuing in the parent code
} else if (pids[i] < 0) {
perror("Fork failed");
exit(1);
}
}
// Parent process waits for child processes to exit
for (int i = 0; i < NUM_PROCESSES; i++) {
waitpid(pids[i], NULL, 0);
}
return 0;
}

20
perf_test.sh Normal file
View File

@ -0,0 +1,20 @@
echo > swap_result.csv
for dir in /sys/kernel/mm/transparent_hugepage/hugepages-*; do
echo $dir $(cat $dir/enabled) >> swap_result.csv
done
echo swpoutbad,swpinbad,pagefaults,itlbmiss,dtlbmiss >> swap_result.csv
for i in $(seq 1 10); do
echo $i
echo 3 > /proc/sys/vm/drop_caches
simpleperf stat -e page-faults,dTLB-loads,dTLB-load-misses,iTLB-loads,iTLB-load-misses ./test_swap > result 2>&1
killall test_swap
swpoutbad=$(cat result | grep "swp out" | awk '{print $4}' | sed 's/,//g')
swpinbad=$(cat result | grep "swp in" | awk '{print $4}' | sed 's/,//g')
pagefaults=$(cat result | grep page-faults | awk '{print $1}' | sed 's/,//g')
itlbmiss=$(cat result | grep iTLB-load-misses | awk '{print $1}' | sed 's/,//g')
dtlbmiss=$(cat result | grep dTLB-load-misses | awk '{print $1}' | sed 's/,//g')
echo $pagefaults,$dtlbmiss,$itlbmiss,$swpoutbad,$swpinbad >> swap_result.csv
done

6
run.sh
View File

@ -33,7 +33,9 @@ if [ -f /sys/block/zram0/multi_pages_debug_stat ]; then
fi
# start test
cat /sys/block/zram0/mm_stat
./test_swap > $tmpfile
cat /sys/block/zram0/mm_stat
# check data
pgfault2=$(cat /proc/vmstat | grep pgfault | awk '{print $2}')
@ -51,7 +53,10 @@ new_zram_used_size=$(cat $tmpfile | awk '{print $2}')
zram_total_size_inc=$(($new_zram_total_size-$prev_zram_total_size))
zram_used_size_inc=$(($new_zram_used_size-$prev_zram_used_size))
zram_total_size_should=524288000
comp_ratio=`echo "scale=4; $zram_total_size_inc / $zram_used_size_inc" | bc`
comp_ratio_should=`echo "scale=4; $zram_total_size_should / $zram_used_size_inc" | bc`
# calculate huge page alloc success ratio
new_anon_fault_alloc=$(cat /sys/kernel/mm/transparent_hugepage/hugepages-64kB/stats/anon_fault_alloc)
@ -100,6 +105,7 @@ fi
echo "swapout_bandwidth $bandwidth_swapout"
echo "swapin_bandwidth $bandwidth_swapin"
echo "comp_ratio $comp_ratio"
echo "comp_ratio_asume $comp_ratio_should"
echo "pagefault increased $pgfault_inc"
echo "anon_fault_success_ratio $anon_fault_success_ratio"
echo "swapout_success_ratio $swapout_success_ratio"

View File

@ -7,7 +7,6 @@
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <openssl/md5.h>
#define SIZE 500*1024*1024
#define PAGE_SIZE 4096 // 4KB
@ -43,20 +42,21 @@ int main()
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)));
printf("swp out bandwidth: %lld 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;
for (i = 0; i < SIZE / 8; i += 8)
*(p + i) = 0xffff + i;
gettimeofday(&tv_e, NULL);
printf("swp in bandwidth: %ld bytes/ms\n", SIZE/(tv_to_ms(tv_e) - tv_to_ms(tv_b)));
printf("swp in bandwidth: %lld 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;
}

44
toutiao.sh Normal file
View File

@ -0,0 +1,44 @@
echo > csv_toutiao.csv
for dir in /sys/kernel/mm/transparent_hugepage/hugepages-*; do
echo $dir $(cat $dir/enabled) >> csv_toutiao.csv
done
echo "dtlbmiss,itlbmiss,pagefault" >> csv_toutiao.csv
echo 3 > /proc/sys/vm/drop_caches
for i in $(seq 1 12); do
# record data background
simpleperf stat -e dTLB-load-misses,iTLB-load-misses,page-faults --app com.ss.android.article.news > result 2>&1 &
echo "start toutiao on your phone"
#read tmp
while true; do
if [ $(ps -ef | grep -v simpleperf | grep -c com.ss.android.article.news) -gt 1 ]; then
break
else
echo .
fi
done
for j in $(seq 1 5); do
echo $j
sleep 1
done
echo "killing toutiao"
for pid in `ps -ef | grep com.ss.android.article.news | grep -v simpleperf | grep -v grep| awk '{print $2}'`; do kill $pid; done
killall simpleperf
while true; do
if [ $(cat result | grep -c "dTLB-load-misses") -gt 0 ]; then
break
fi
sleep 1
echo '...'
done
dtlbmiss=$(cat result | grep dTLB-load-misses | awk '{print $1}' | sed 's/,//g')
itlbmiss=$(cat result | grep iTLB-load-misses | awk '{print $1}' | sed 's/,//g')
pagefault=$(cat result | grep page-faults | awk '{print $1}' | sed 's/,//g')
echo "$dtlbmiss,$itlbmiss,$pagefault"
echo "$dtlbmiss,$itlbmiss,$pagefault" >> csv_toutiao.csv
done

44
wangzhe.sh Normal file
View File

@ -0,0 +1,44 @@
echo > csv_wangzhe.csv
for dir in /sys/kernel/mm/transparent_hugepage/hugepages-*; do
echo $dir $(cat $dir/enabled) >> csv_wangzhe.csv
done
echo "dtlbmiss,itlbmiss,pagefault" >> csv_wangzhe.csv
echo 3 > /proc/sys/vm/drop_caches
for i in $(seq 1 12); do
# record data background
simpleperf stat -e dTLB-load-misses,iTLB-load-misses,page-faults --app com.tencent.tmgp.sgame > result 2>&1 &
echo "start wangzhe on your phone"
#read tmp
while true; do
if [ $(ps -ef | grep -v simpleperf | grep -c com.tencent.tmgp.sgame) -gt 1 ]; then
break
else
echo .
fi
done
for j in $(seq 1 5); do
echo $j
sleep 1
done
echo "killing wangzhe"
for pid in `ps -ef | grep com.tencent.tmgp.sgame | grep -v simpleperf | grep -v grep| awk '{print $2}'`; do kill $pid; done
killall simpleperf
while true; do
if [ $(cat result | grep -c "dTLB-load-misses") -gt 0 ]; then
break
fi
sleep 1
echo '...'
done
dtlbmiss=$(cat result | grep dTLB-load-misses | awk '{print $1}' | sed 's/,//g')
itlbmiss=$(cat result | grep iTLB-load-misses | awk '{print $1}' | sed 's/,//g')
pagefault=$(cat result | grep page-faults | awk '{print $1}' | sed 's/,//g')
echo "$dtlbmiss,$itlbmiss,$pagefault"
echo "$dtlbmiss,$itlbmiss,$pagefault" >> csv_wangzhe.csv
done

31
zram.sh Normal file
View File

@ -0,0 +1,31 @@
#!/bin/sh
sleep_time=$1
if [ -z $sleep_time ]; then
echo "set sleep time to 2"
sleep_time=2
fi
while true; do
actuall_size=$(free -h | grep Swap | awk '{print $3}')
comped_size=$(cat /sys/block/zram0/mm_stat | awk '{print $2}')
zsmalloc_size=$(cat /sys/block/zram0/mm_stat | awk '{print $3}')
if [ $actuall_size == "1G" ]; then
echo $actuall_size,$comped_size,$zsmalloc_size >> record_zram.csv
elif [ $actuall_size == "1.5G" ]; then
echo $actuall_size,$comped_size,$zsmalloc_size >> record_zram.csv
elif [ $actuall_size == "2.0G" ]; then
echo $actuall_size,$comped_size,$zsmalloc_size >> record_zram.csv
elif [ $actuall_size == "2.5G" ]; then
echo $actuall_size,$comped_size,$zsmalloc_size >> record_zram.csv
elif [ $actuall_size == "3.0G" ]; then
echo $actuall_size,$comped_size,$zsmalloc_size >> record_zram.csv
elif [ $actuall_size == "3.5G" ]; then
echo $actuall_size,$comped_size,$zsmalloc_size >> record_zram.csv
elif [ $actuall_size == "4.0G" ]; then
echo $actuall_size,$comped_size,$zsmalloc_size >> record_zram.csv
fi
sleep $sleep_time
done