1#!/bin/sh 2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved. 3# Copyright (c) 2019-2021 Petr Vorel <pvorel@suse.cz> 4# Author: Alexey Kodanev <alexey.kodanev@oracle.com> 5# 6# Test creates several zram devices with different filesystems on them. 7# It fills each device with zeros and checks that compression works. 8 9TST_CNT=7 10TST_TESTFUNC="do_test" 11TST_NEEDS_CMDS="awk bc dd" 12. zram_lib.sh 13TST_SETUP="setup" 14 15check_space_for_btrfs() 16{ 17 local ram_size 18 19 ram_size=$(awk '/MemTotal:/ {print $2}' /proc/meminfo) 20 if [ "$ram_size" -lt 1048576 ]; then 21 tst_res TINFO "not enough space for Btrfs" 22 return 1 23 fi 24 return 0 25} 26 27# List of parameters for zram devices. 28# For each number the test creates own zram device. 29# NOTE about size: 30# The zram sysfs node 'disksize' value can be either in bytes, 31# or you can use mem suffixes. But in some old kernels, mem 32# suffixes are not supported, for example, in RHEL6.6GA's kernel 33# layer, it uses strict_strtoull() to parse disksize which does 34# not support mem suffixes, in some newer kernels, they use 35# memparse() which supports mem suffixes. So here we just use 36# bytes to make sure everything works correctly. 37initialize_vars() 38{ 39 local fs limit size stream=-1 40 dev_num=0 41 42 for fs in $(tst_supported_fs); do 43 [ "$fs" = "tmpfs" ] && continue 44 size="26214400" 45 limit="25M" 46 if [ "$fs" = "btrfs" ]; then 47 check_space_for_btrfs || continue 48 size="402653184" 49 limit="$((size/1024/1024))M" 50 fi 51 52 stream=$((stream+3)) 53 dev_num=$((dev_num+1)) 54 zram_filesystems="$zram_filesystems $fs" 55 zram_mem_limits="$zram_mem_limits $limit" 56 zram_sizes="$zram_sizes $size" 57 zram_max_streams="$zram_max_streams $stream" 58 done 59 60 [ $dev_num -eq 0 ] && \ 61 tst_brk TCONF "no suitable filesystem" 62} 63 64setup() 65{ 66 initialize_vars 67 zram_load 68} 69 70zram_makefs() 71{ 72 local i=$dev_start 73 local fs 74 75 for fs in $zram_filesystems; do 76 tst_res TINFO "make $fs filesystem on /dev/zram$i" 77 mkfs.$fs /dev/zram$i > err.log 2>&1 78 if [ $? -ne 0 ]; then 79 cat err.log 80 tst_brk TFAIL "failed to make $fs on /dev/zram$i" 81 fi 82 83 i=$(($i + 1)) 84 done 85 86 tst_res TPASS "zram_makefs succeeded" 87} 88 89zram_mount() 90{ 91 local i=0 92 93 for i in $(seq $dev_start $dev_end); do 94 tst_res TINFO "mount /dev/zram$i" 95 mkdir zram$i 96 ROD mount /dev/zram$i zram$i 97 dev_mounted=$i 98 done 99 100 tst_res TPASS "mount of zram device(s) succeeded" 101} 102 103zram_fill_fs() 104{ 105 for i in $(seq $dev_start $dev_end); do 106 tst_res TINFO "filling zram$i (it can take long time)" 107 local b=0 108 while true; do 109 dd conv=notrunc if=/dev/zero of=zram${i}/file \ 110 oflag=append count=1 bs=1024 status=none \ 111 >/dev/null 2>err.txt || break 112 b=$(($b + 1)) 113 done 114 if [ $b -eq 0 ]; then 115 [ -s err.txt ] && tst_res TWARN "dd error: $(cat err.txt)" 116 tst_brk TBROK "cannot fill zram $i" 117 fi 118 tst_res TPASS "zram$i was filled with '$b' KB" 119 120 if [ ! -f "/sys/block/zram$i/mm_stat" ]; then 121 if [ $i -eq 0 ]; then 122 tst_res TCONF "zram compression ratio test requires zram mm_stat sysfs file" 123 fi 124 125 continue 126 fi 127 128 local mem_used_total=`awk '{print $3}' "/sys/block/zram$i/mm_stat"` 129 local v=$((100 * 1024 * $b / $mem_used_total)) 130 local r=`echo "scale=2; $v / 100 " | bc` 131 132 if [ "$v" -lt 100 ]; then 133 tst_res TFAIL "compression ratio: $r:1" 134 break 135 fi 136 137 tst_res TPASS "compression ratio: $r:1" 138 done 139} 140 141do_test() 142{ 143 case $1 in 144 1) zram_max_streams;; 145 2) zram_compress_alg;; 146 3) zram_set_disksizes;; 147 4) zram_set_memlimit;; 148 5) zram_makefs;; 149 6) zram_mount;; 150 7) zram_fill_fs;; 151 esac 152} 153 154tst_run 155