GNU Linux-libre 4.19.286-gnu1
[releases.git] / tools / testing / selftests / zram / zram01.sh
1 #!/bin/bash
2 # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of
7 # the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it would be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # Test creates several zram devices with different filesystems on them.
15 # It fills each device with zeros and checks that compression works.
16 #
17 # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
18 # Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
19
20 TCID="zram01"
21 ERR_CODE=0
22
23 . ./zram_lib.sh
24
25 # Test will create the following number of zram devices:
26 dev_num=1
27 # This is a list of parameters for zram devices.
28 # Number of items must be equal to 'dev_num' parameter.
29 zram_max_streams="2"
30
31 # The zram sysfs node 'disksize' value can be either in bytes,
32 # or you can use mem suffixes. But in some old kernels, mem
33 # suffixes are not supported, for example, in RHEL6.6GA's kernel
34 # layer, it uses strict_strtoull() to parse disksize which does
35 # not support mem suffixes, in some newer kernels, they use
36 # memparse() which supports mem suffixes. So here we just use
37 # bytes to make sure everything works correctly.
38 zram_sizes="2097152" # 2MB
39 zram_mem_limits="2M"
40 zram_filesystems="ext4"
41 zram_algs="lzo"
42
43 zram_fill_fs()
44 {
45         for i in $(seq $dev_start $dev_end); do
46                 echo "fill zram$i..."
47                 local b=0
48                 while [ true ]; do
49                         dd conv=notrunc if=/dev/zero of=zram${i}/file \
50                                 oflag=append count=1 bs=1024 status=none \
51                                 > /dev/null 2>&1 || break
52                         b=$(($b + 1))
53                 done
54                 echo "zram$i can be filled with '$b' KB"
55
56                 local mem_used_total=`awk '{print $3}' "/sys/block/zram$i/mm_stat"`
57                 local v=$((100 * 1024 * $b / $mem_used_total))
58                 if [ "$v" -lt 100 ]; then
59                          echo "FAIL compression ratio: 0.$v:1"
60                          ERR_CODE=-1
61                          return
62                 fi
63
64                 echo "zram compression ratio: $(echo "scale=2; $v / 100 " | bc):1: OK"
65         done
66 }
67
68 check_prereqs
69 zram_load
70 zram_max_streams
71 zram_compress_alg
72 zram_set_disksizes
73 zram_set_memlimit
74 zram_makefs
75 zram_mount
76
77 zram_fill_fs
78 zram_cleanup
79
80 if [ $ERR_CODE -ne 0 ]; then
81         echo "$TCID : [FAIL]"
82 else
83         echo "$TCID : [PASS]"
84 fi