-
Notifications
You must be signed in to change notification settings - Fork 36
/
zram-test.sh
executable file
·137 lines (100 loc) · 3.06 KB
/
zram-test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
set -e
trap 'echo "[ERROR] Error in line $LINENO when executing: $BASH_COMMAND"' ERR
modprobe zram
function make_zram() {
NAME="$1"
ALGO="$2"
MOUNTPOINT="$3"
DEV=/dev/$NAME
umount "$MOUNTPOINT" &>/dev/null || true
if ! [[ -e $DEV ]]; then
cat /sys/class/zram-control/hot_add
fi
# reset the device to ensure we can set the parameters
for i in {1..9}; do
if echo 1 > "/sys/block/$NAME/reset"; then
break
fi
sleep 1
done
# https://github.com/lz4/lz4?tab=readme-ov-file#benchmarks
# Core i7-9700K single thread
# Compressor Ratio Compression Decompression
# LZ4 default (v1.9.0) 2.101 780 MB/s 4970 MB/s
# Zstandard 1.4.0 -1 2.883 515 MB/s 1380 MB/s
#
# on a pi4, lz4 in compression has even higher relative speed over zstd
# use zstd for now even though it's slower
#echo lz4 > "/sys/block/$NAME/comp_algorithm"
echo "$ALGO" > "/sys/block/$NAME/comp_algorithm"
# use 1/4 of memory
use=$(( $(grep -e MemTotal /proc/meminfo | tr -s ' ' | cut -d' ' -f2) / 4 ))
# use at least 256M for systems with small memory
min=$(( 256 * 1024 ))
if (( use < min )); then
size=$min
else
size=$use
fi
# zram maximum memory usage
echo $(( size ))K > "/sys/block/$NAME/mem_limit"
# disk size
# let's make this 3x the memory limit in case we get good compression
echo $(( 4 * size ))K > "/sys/block/$NAME/disksize"
mkfs.ext2 "$DEV" &>/dev/null
mkdir -p "$MOUNTPOINT"
mount "$DEV" "$MOUNTPOINT"
}
function reset_cache() {
sync
#echo 3 > /proc/sys/vm/drop_caches
}
function test_zram_algo() {
ALGO="$1"
SIZE=100
MP=/test_zram_algo
ZRAM=zram1
make_zram $ZRAM "$ALGO" $MP
ZF=/$MP/testfile
reset_cache
echo
echo "$ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO $ALGO"
echo -n "from /run to zram: "
dd if=$TF of=$ZF bs=1M oflag=dsync 2>&1 | grep -v records
reset_cache
echo 1 > /proc/sys/vm/drop_caches
sleep 1
echo -n "from zram to /run: "
dd if=$ZF of=$TF bs=1M oflag=dsync 2>&1 | grep -v records
reset_cache
zramctl | grep $ZRAM | awk '{ print "uncompressed: ", $4, " compressed: ",$5 }'
umount $MP || true
echo 1 > "/sys/block/$ZRAM/reset"
}
function test_algos() {
test_zram_algo zstd
test_zram_algo lz4
test_zram_algo lzo
#test_zram_algo lzo-rle
}
DF=/tmp/zram_td_100.zst
if ! [[ -f $DF ]]; then
wget -O $DF https://github.com/wiedehopf/adsb-wiki/releases/download/compression_test/zram_td_100.zst
fi
TF=/run/zram_test_file
echo
echo "Testing using first 100MB of chrome binary:"
zstd -f -d $DF -o $TF -q
test_algos
if [[ $1 == all ]]; then
echo
echo "Testing using 100MB random data"
dd if=/dev/urandom of=$TF bs=1M count=100 status=none
test_algos
echo
echo "Testing using 100MB zeroes"
dd if=/dev/zero of=$TF bs=1M count=100 status=none
test_algos
fi
rm -f "$TF"