Skip to content

Commit d18da7e

Browse files
xuyang0410shuahkh
authored andcommitted
selftests/zram01.sh: Fix compression ratio calculation
zram01 uses `free -m` to measure zram memory usage. The results are no sense because they are polluted by all running processes on the system. We Should only calculate the free memory delta for the current process. So use the third field of /sys/block/zram<id>/mm_stat to measure memory usage instead. The file is available since kernel 4.1. orig_data_size(first): uncompressed size of data stored in this disk. compr_data_size(second): compressed size of data stored in this disk mem_used_total(third): the amount of memory allocated for this disk Also remove useless zram cleanup call in zram_fill_fs and so we don't need to cleanup zram twice if fails. Signed-off-by: Yang Xu <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent fc4eb48 commit d18da7e

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

tools/testing/selftests/zram/zram01.sh

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ zram_algs="lzo"
3333

3434
zram_fill_fs()
3535
{
36-
local mem_free0=$(free -m | awk 'NR==2 {print $4}')
37-
3836
for i in $(seq 0 $(($dev_num - 1))); do
3937
echo "fill zram$i..."
4038
local b=0
@@ -45,29 +43,17 @@ zram_fill_fs()
4543
b=$(($b + 1))
4644
done
4745
echo "zram$i can be filled with '$b' KB"
48-
done
4946

50-
local mem_free1=$(free -m | awk 'NR==2 {print $4}')
51-
local used_mem=$(($mem_free0 - $mem_free1))
47+
local mem_used_total=`awk '{print $3}' "/sys/block/zram$i/mm_stat"`
48+
local v=$((100 * 1024 * $b / $mem_used_total))
49+
if [ "$v" -lt 100 ]; then
50+
echo "FAIL compression ratio: 0.$v:1"
51+
ERR_CODE=-1
52+
return
53+
fi
5254

53-
local total_size=0
54-
for sm in $zram_sizes; do
55-
local s=$(echo $sm | sed 's/M//')
56-
total_size=$(($total_size + $s))
55+
echo "zram compression ratio: $(echo "scale=2; $v / 100 " | bc):1: OK"
5756
done
58-
59-
echo "zram used ${used_mem}M, zram disk sizes ${total_size}M"
60-
61-
local v=$((100 * $total_size / $used_mem))
62-
63-
if [ "$v" -lt 100 ]; then
64-
echo "FAIL compression ratio: 0.$v:1"
65-
ERR_CODE=-1
66-
zram_cleanup
67-
return
68-
fi
69-
70-
echo "zram compression ratio: $(echo "scale=2; $v / 100 " | bc):1: OK"
7157
}
7258

7359
check_prereqs

0 commit comments

Comments
 (0)