]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
selftests/zram01.sh: Fix compression ratio calculation
authorYang Xu <xuyang2018.jy@fujitsu.com>
Thu, 27 Jan 2022 09:11:36 +0000 (17:11 +0800)
committerPaolo Pisati <paolo.pisati@canonical.com>
Mon, 7 Mar 2022 10:44:04 +0000 (11:44 +0100)
BugLink: https://bugs.launchpad.net/bugs/1963890
[ Upstream commit d18da7ec3719559d6e74937266d0416e6c7e0b31 ]

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 <xuyang2018.jy@fujitsu.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
tools/testing/selftests/zram/zram01.sh

index 114863d9fb8768c906732d7ce780e3f6ea73e44c..e9e9eb777e2c78f8cbafd33582c00a043b4640f7 100755 (executable)
@@ -33,8 +33,6 @@ zram_algs="lzo"
 
 zram_fill_fs()
 {
-       local mem_free0=$(free -m | awk 'NR==2 {print $4}')
-
        for i in $(seq 0 $(($dev_num - 1))); do
                echo "fill zram$i..."
                local b=0
@@ -45,29 +43,17 @@ zram_fill_fs()
                        b=$(($b + 1))
                done
                echo "zram$i can be filled with '$b' KB"
-       done
 
-       local mem_free1=$(free -m | awk 'NR==2 {print $4}')
-       local used_mem=$(($mem_free0 - $mem_free1))
+               local mem_used_total=`awk '{print $3}' "/sys/block/zram$i/mm_stat"`
+               local v=$((100 * 1024 * $b / $mem_used_total))
+               if [ "$v" -lt 100 ]; then
+                        echo "FAIL compression ratio: 0.$v:1"
+                        ERR_CODE=-1
+                        return
+               fi
 
-       local total_size=0
-       for sm in $zram_sizes; do
-               local s=$(echo $sm | sed 's/M//')
-               total_size=$(($total_size + $s))
+               echo "zram compression ratio: $(echo "scale=2; $v / 100 " | bc):1: OK"
        done
-
-       echo "zram used ${used_mem}M, zram disk sizes ${total_size}M"
-
-       local v=$((100 * $total_size / $used_mem))
-
-       if [ "$v" -lt 100 ]; then
-               echo "FAIL compression ratio: 0.$v:1"
-               ERR_CODE=-1
-               zram_cleanup
-               return
-       fi
-
-       echo "zram compression ratio: $(echo "scale=2; $v / 100 " | bc):1: OK"
 }
 
 check_prereqs