#!/bin/bash # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # # This program is distributed in the hope that it would be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # Test creates several zram devices with different filesystems on them. # It fills each device with zeros and checks that compression works. # # Author: Alexey Kodanev # Modified: Naresh Kamboju global TCID := '"zram01'" global ERR_CODE := '0' source ./zram_lib.sh # Test will create the following number of zram devices: global dev_num := '1' # This is a list of parameters for zram devices. # Number of items must be equal to 'dev_num' parameter. global zram_max_streams := '"2'" # The zram sysfs node 'disksize' value can be either in bytes, # or you can use mem suffixes. But in some old kernels, mem # suffixes are not supported, for example, in RHEL6.6GA's kernel # layer, it uses strict_strtoull() to parse disksize which does # not support mem suffixes, in some newer kernels, they use # memparse() which supports mem suffixes. So here we just use # bytes to make sure everything works correctly. global zram_sizes := '"2097152'" # 2MB global zram_mem_limits := '"2M'" global zram_filesystems := '"ext4'" global zram_algs := '"lzo'" proc zram_fill_fs { var mem_free0 = $[free -m | awk 'NR==2 {print $4}] for i in [$[seq 0 $($dev_num - 1)]] { echo "fill zram$i..." var b = '0' while [ true ] { dd conv=notrunc if=/dev/zero of=zram$(i)/file \ oflag=append count=1 bs=1024 status=none \ > /dev/null !2 > !1 || break b := $($b + 1) } echo "zram$i can be filled with '$b' KB" } var mem_free1 = $[free -m | awk 'NR==2 {print $4}] var used_mem = $($mem_free0 - $mem_free1) var total_size = '0' for sm in [$zram_sizes] { var s = $[echo $sm | sed 's/M//] total_size := $($total_size + $s) } echo "zram used $(used_mem)M, zram disk sizes $(total_size)M" var v = $(100 * $total_size / $used_mem) if test $v -lt 100 { echo "FAIL compression ratio: 0.$v:1" global ERR_CODE := '-1' zram_cleanup return } echo "zram compression ratio: $[echo "scale=2; $v / 100 " | bc]:1: OK" } check_prereqs zram_load zram_max_streams zram_compress_alg zram_set_disksizes zram_set_memlimit zram_makefs zram_mount zram_fill_fs zram_cleanup zram_unload if test $ERR_CODE -ne 0 { echo "$TCID : [FAIL]" } else { echo "$TCID : [PASS]" } (CommandList children: [ (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:TCID) op:Equal rhs:{(DQ (zram01))} spids:[55])] spids: [55] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:ERR_CODE) op:Equal rhs:{(0)} spids:[60])] spids: [60] ) (C {(.)} {(./zram_lib.sh)}) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:dev_num) op:Equal rhs:{(1)} spids:[72])] spids: [72] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:zram_max_streams) op:Equal rhs:{(DQ (2))} spids:[81])] spids: [81] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:zram_sizes) op:Equal rhs:{(DQ (2097152))} spids:[108])] spids: [108] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:zram_mem_limits) op:Equal rhs:{(DQ (2M))} spids:[116])] spids: [116] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:zram_filesystems) op: Equal rhs: {(DQ (ext4))} spids: [121] ) ] spids: [121] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:zram_algs) op:Equal rhs:{(DQ (lzo))} spids:[126])] spids: [126] ) (FuncDef name: zram_fill_fs body: (BraceGroup children: [ (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:mem_free0) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [(C {(free)} {(-m)}) (C {(awk)} {(SQ <"NR==2 {print $4}">)})] negated: False ) ] ) left_token: spids: [142 154] ) } spids: [141] ) ] spids: [139] ) (ForEach iter_name: i iter_words: [ { (CommandSubPart command_list: (CommandList children: [ (C {(seq)} {(0)} { (ArithSubPart anode: (ArithBinary op_id: Arith_Minus left: (ArithWord w:{($ VSub_Name "$dev_num")}) right: (ArithWord w:{(Lit_Digits 1)}) ) spids: [169 176] ) } ) ] ) left_token: spids: [164 177] ) } ] do_arg_iter: False body: (DoGroup children: [ (C {(echo)} {(DQ ("fill zram") ($ VSub_Name "$i") (...))}) (Assignment keyword: Assign_Local pairs: [(assign_pair lhs:(LhsName name:b) op:Equal rhs:{(0)} spids:[194])] spids: [192] ) (While cond: [ (Sentence child: (C {(Lit_Other "[")} {(true)} {(Lit_Other "]")}) terminator: ) ] body: (DoGroup children: [ (AndOr children: [ (SimpleCommand words: [ {(dd)} {(Lit_VarLike "conv=") (notrunc)} {(Lit_VarLike "if=") (/dev/zero)} {(Lit_VarLike "of=") (zram) (${ VSub_Name i) (/file)} {(Lit_VarLike "oflag=") (append)} {(Lit_VarLike "count=") (1)} {(Lit_VarLike "bs=") (1024)} {(Lit_VarLike "status=") (none)} ] redirects: [ (Redir op_id: Redir_Great fd: -1 arg_word: {(/dev/null)} spids: [241] ) (Redir op_id: Redir_GreatAnd fd: 2 arg_word: {(1)} spids: [245] ) ] ) (ControlFlow token: ) ] op_id: Op_DPipe ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:b) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Plus left: (ArithWord w:{($ VSub_Name "$b")}) right: (ArithWord w:{(Lit_Digits 1)}) ) spids: [254 261] ) } spids: [253] ) ] spids: [253] ) ] spids: [207 264] ) ) (C {(echo)} { (DQ (zram) ($ VSub_Name "$i") (" can be filled with '") ($ VSub_Name "$b") ("' KB") ) } ) ] spids: [180 278] ) spids: [163 178] ) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:mem_free1) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [(C {(free)} {(-m)}) (C {(awk)} {(SQ <"NR==2 {print $4}">)})] negated: False ) ] ) left_token: spids: [285 297] ) } spids: [284] ) ] spids: [282] ) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:used_mem) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Minus left: (ArithWord w:{($ VSub_Name "$mem_free0")}) right: (ArithWord w:{($ VSub_Name "$mem_free1")}) ) spids: [303 310] ) } spids: [302] ) ] spids: [300] ) (Assignment keyword: Assign_Local pairs: [(assign_pair lhs:(LhsName name:total_size) op:Equal rhs:{(0)} spids:[316])] spids: [314] ) (ForEach iter_name: sm iter_words: [{($ VSub_Name "$zram_sizes")}] do_arg_iter: False body: (DoGroup children: [ (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:s) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(echo)} {($ VSub_Name "$sm")}) (C {(sed)} {(SQ )}) ] negated: False ) ] ) left_token: spids: [335 347] ) } spids: [334] ) ] spids: [332] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:total_size) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Plus left: (ArithWord w:{($ VSub_Name "$total_size")}) right: (ArithWord w:{($ VSub_Name "$s")}) ) spids: [351 358] ) } spids: [350] ) ] spids: [350] ) ] spids: [329 361] ) spids: [325 327] ) (C {(echo)} { (DQ ("zram used ") (${ VSub_Name used_mem) ("M, zram disk sizes ") (${ VSub_Name total_size) (M) ) } ) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:v) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Slash left: (ArithBinary op_id: Arith_Star left: (ArithWord w:{(Lit_Digits 100)}) right: (ArithWord w:{($ VSub_Name "$total_size")}) ) right: (ArithWord w:{($ VSub_Name "$used_mem")}) ) spids: [384 395] ) } spids: [383] ) ] spids: [381] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$v"))} {(-lt)} {(100)} {(Lit_Other "]")}) terminator: ) ] action: [ (C {(echo)} {(DQ ("FAIL compression ratio: 0.") ($ VSub_Name "$v") (":1"))}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ERR_CODE) op: Equal rhs: {(-1)} spids: [426] ) ] spids: [426] ) (C {(zram_cleanup)}) (ControlFlow token:) ] spids: [-1 414] ) ] spids: [-1 436] ) (C {(echo)} { (DQ ("zram compression ratio: ") (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(echo)} {(DQ ("scale=2; ") ($ VSub_Name "$v") (" / 100 "))}) (C {(bc)}) ] negated: False ) ] ) left_token: spids: [444 456] ) (":1: OK") ) } ) ] spids: [136] ) spids: [132 135] ) (C {(check_prereqs)}) (C {(zram_load)}) (C {(zram_max_streams)}) (C {(zram_compress_alg)}) (C {(zram_set_disksizes)}) (C {(zram_set_memlimit)}) (C {(zram_makefs)}) (C {(zram_mount)}) (C {(zram_fill_fs)}) (C {(zram_cleanup)}) (C {(zram_unload)}) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_Name "$ERR_CODE")} {(-ne)} {(0)} {(Lit_Other "]")}) terminator: ) ] action: [(C {(echo)} {(DQ ($ VSub_Name "$TCID") (" : [FAIL]"))})] spids: [-1 500] ) ] else_action: [(C {(echo)} {(DQ ($ VSub_Name "$TCID") (" : [PASS]"))})] spids: [510 520] ) ] )