04d12c4ff23b7bdd1ffe71a418c463ad93e08753
[xfstests-dev.git] / common / filter
1 ##/bin/bash
2 #
3 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation.
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 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
18 #
19 # standard filters
20 #
21
22 # Checks that given_value is in range of correct_value +/- tolerance.
23 # Tolerance can be an absolute value or a percentage of the correct value
24 # (see examples with tolerances below).
25 # Outputs suitable message to stdout if it's not in range.
26 #
27 # A verbose option, -v, may be used as the LAST argument
28
29 # e.g. 
30 # foo: 0.0298 = 0.03 +/- 5%
31 # _within_tolerance "foo" 0.0298 0.03 5%  
32
33 # foo: 0.0298 = 0.03 +/- 0.01
34 # _within_tolerance "foo" 0.0298 0.03 0.01
35 #
36 # foo: 0.0298 = 0.03 -0.01 +0.002
37 # _within_tolerance "foo" 0.0298 0.03 0.01 0.002
38 #
39 # foo: verbose output of 0.0298 = 0.03 +/- 5% 
40 # _within_tolerance "foo" 0.0298 0.03 5% -v 
41 _within_tolerance()
42 {
43   _name=$1
44   _given_val=$2
45   _correct_val=$3
46   _mintol=$4
47   _maxtol=$_mintol
48   _verbose=0
49   _debug=false
50
51   # maxtol arg is optional
52   # verbose arg is optional
53   if [ $# -ge 5 ]
54   then 
55      if [ "$5" = "-v" ]
56      then
57         _verbose=1
58      else
59         _maxtol=$5
60      fi
61   fi
62   if [ $# -ge 6 ]
63   then
64      [ "$6" = "-v" ] && _verbose=1
65   fi
66
67   # find min with or without %
68   _mintolerance=`echo $_mintol | sed -e 's/%//'` 
69   if [ $_mintol = $_mintolerance ]
70   then 
71       _min=`echo "scale=5; $_correct_val-$_mintolerance" | bc`
72   else
73       _min=`echo "scale=5; $_correct_val-$_mintolerance*0.01*$_correct_val" | bc`
74   fi
75
76   # find max with or without %
77   _maxtolerance=`echo $_maxtol | sed -e 's/%//'` 
78   if [ $_maxtol = $_maxtolerance ]
79   then 
80       _max=`echo "scale=5; $_correct_val+$_maxtolerance" | bc`
81   else
82       _max=`echo "scale=5; $_correct_val+$_maxtolerance*0.01*$_correct_val" | bc`
83   fi
84
85   $_debug && echo "min = $_min"
86   $_debug && echo "max = $_max"
87
88   cat <<EOF >$tmp.bc.1
89 scale=5;
90 if ($_min <= $_given_val) 1;
91 if ($_min > $_given_val) 0; 
92 EOF
93
94   cat <<EOF >$tmp.bc.2
95 scale=5;
96 if ($_given_val <= $_max) 1;
97 if ($_given_val > $_max) 0;
98 EOF
99
100   _above_min=`bc <$tmp.bc.1`
101   _below_max=`bc <$tmp.bc.2`
102
103   rm -f $tmp.bc.[12]
104
105   _in_range=`expr $_above_min \& $_below_max` 
106
107   # fix up min, max precision for output
108   # can vary for 5.3, 6.2
109
110   # remove any trailing zeroes from min, max if they have fractional parts
111   _min=`echo $_min | sed -e '/\./s/0*$//' -e 's/\.$//'`
112   _max=`echo $_max | sed -e '/\./s/0*$//' -e 's/\.$//'`
113
114   if [ $_in_range -eq 1 ] 
115   then
116         [ $_verbose -eq 1 ] && echo $_name is in range
117         return 0
118   else
119         [ $_verbose -eq 1 ] && echo $_name has value of $_given_val
120         [ $_verbose -eq 1 ] && echo $_name is NOT in range $_min .. $_max       
121         return 1
122   fi
123 }
124
125 # ctime(3) dates
126 #
127 _filter_date()
128 {
129     sed \
130         -e 's/[A-Z][a-z][a-z] [A-z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$/DATE/'
131 }
132
133 # prints filtered output on stdout, values (use eval) on stderr
134 # Non XFS filesystems always return a 4k block size and a 256 byte inode.
135 _filter_mkfs()
136 {
137     case $FSTYP in
138     xfs)
139         ;;
140     *)
141         cat - >/dev/null
142         perl -e 'print STDERR "dbsize=4096\nisize=256\n"'
143         return ;;
144     esac
145
146     set -
147     perl -ne '
148     if (/^meta-data=([\w,|\/.-]+)\s+isize=(\d+)\s+agcount=(\d+), agsize=(\d+) blks/) {
149         print STDERR "ddev=$1\nisize=$2\nagcount=$3\nagsize=$4\n";
150         print STDOUT "meta-data=DDEV isize=XXX agcount=N, agsize=XXX blks\n";
151     }
152     if (/^\s+=\s+sectsz=(\d+)\s+attr=(\d+)/) {
153         print STDERR "sectsz=$1\nattr=$2\n";
154     }
155     if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+), imaxpct=(\d+)/) {
156         print STDERR "dbsize=$1\ndblocks=$2\nimaxpct=$3\n";
157         print STDOUT "data     = bsize=XXX blocks=XXX, imaxpct=PCT\n";
158     }
159     if (/^\s+=\s+sunit=(\d+)\s+swidth=(\d+) blks/) {
160         print STDERR "sunit=$1\nswidth=$2\nunwritten=1\n";
161         print STDOUT "         = sunit=XXX swidth=XXX, unwritten=X\n";
162     }
163     if (/^naming\s+=version\s+(\d+)\s+bsize=(\d+)/) {
164         print STDERR "dirversion=$1\ndirbsize=$2\n";
165         print STDOUT "naming   =VERN bsize=XXX\n";
166     }
167     if (/^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+),\s+version=(\d+)/ ||
168         /^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+)/) {
169         print STDERR "ldev=\"$1\"\nlbsize=$2\nlblocks=$3\nlversion=$4\n";
170         print STDOUT "log      =LDEV bsize=XXX blocks=XXX\n";
171     }
172     if (/^\s+=\s+sectsz=(\d+)\s+sunit=(\d+) blks/) {
173         print STDERR "logsectsz=$1\nlogsunit=$2\n\n";
174     }
175     if (/^realtime\s+=([\w|\/.-]+)\s+extsz=(\d+)\s+blocks=(\d+), rtextents=(\d+)/) {
176         print STDERR "rtdev=$1\nrtextsz=$2\nrtblocks=$3\nrtextents=$4\n";
177         print STDOUT "realtime =RDEV extsz=XXX blocks=XXX, rtextents=XXX\n";
178     }'
179 }
180
181
182 # prints the bits we care about in growfs
183
184 _filter_growfs()
185 {
186     perl -ne '
187     if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+), imaxpct=(\d+)/) {
188         print "xfs_growfs --BlockSize=$1 --Blocks=$2\n";
189     }
190     elsif (/^data/) {
191         print;
192     }'
193 }
194
195 _filter_dd()
196 {
197     $AWK_PROG '
198         /records in/                { next }
199         /records out/               { next }
200         /No space left on device/   { print "   !!! disk full (expected)" 
201                                       next }
202                                     { print "   *** " $0 }
203     '
204 }
205
206 common_line_filter()
207 {
208     perl -ne 'if (/.*:(.*)/) {
209         if ( "$last_line" ne "$1" ) { print "$_"; $first_match=1; }
210         elsif ( $first_match==1 ) { print "*\n"; $first_match=0; }
211         $last_line="$1";
212     }
213     else {
214         print $_; $last_line=$_;
215     }'
216 }
217
218 _filter_xfs_io()
219 {
220     sed -e "s/[0-9/.]* [GMKiBbytes]*, [0-9]* ops\; [0-9/:. sec]* ([inf0-9/.]* [EPGMKiBbytes]*\/sec and [inf0-9/.]* ops\/sec)/XXX Bytes, X ops\; XX:XX:XX.X (XXX YYY\/sec and XXX ops\/sec)/"
221 }
222
223 _filter_xfs_io_unique()
224 {
225     common_line_filter | _filter_xfs_io
226 }
227
228 _filter_test_dir()
229 {
230         sed -e "s,$TEST_DEV,TEST_DEV,g" -e "s,$TEST_DIR,TEST_DIR,g"
231 }
232
233 _filter_scratch()
234 {
235         sed -e "s,$SCRATCH_DEV,SCRATCH_DEV,g" \
236             -e "s,$SCRATCH_MNT,SCRATCH_MNT,g" \
237             -e "/.use_space/d"
238 }
239
240 # Turn any device in the scratch pool into SCRATCH_DEV
241 _filter_scratch_pool()
242 {
243         FILTER_STRINGS=`echo $SCRATCH_DEV_POOL | sed -e 's/\s\+/\\\|/g'`
244         sed -e "s,$FILTER_STRINGS,SCRATCH_DEV,g"
245 }
246
247 _filter_spaces()
248 {
249        sed -e 's/ [ ]*/ /g'
250 }
251
252 _filter_quota()
253 {
254         # Long dev name might be split onto its own line; last
255         # seds remove that newline if present
256         _filter_scratch | _filter_test_dir | _filter_spaces | \
257         sed -e 'N;s/SCRATCH_DEV\n/SCRATCH_DEV/g' | \
258         sed -e 'N;s/TEST_DEV\n/TEST_DEV/g'
259 }
260
261 # Account for different "ln" failure messages
262 _filter_ln()
263 {
264         sed -e "s,\(creating symbolic link .*\) to .*: ,\1: ," \
265             -e "s,failed to create,creating,"
266 }
267
268 # If given an arg, filter *that* UUID string
269 # Otherwise look for something that looks like a generic UUID
270 _filter_uuid()
271 {
272         if [ ! -z $1 ]; then
273                 UUID=$1
274                 sed -e "s/\(uuid:\) $UUID/\1 <EXACTUUID>/i"
275         else
276                 sed -e "s/\(uuid:\) *[0-9a-f-][0-9a-f-]*/\1 <UUID>/i"
277         fi
278 }
279
280 # In mixed group the added disks may have zero used size
281 _filter_zero_size()
282 {
283         sed -e "s/0\.00/<SIZE>/g"
284 }
285
286 # Filter out sizes like 6.14MB etc
287 _filter_size()
288 {
289         sed -e "s/[0-9\.]\+\s\?[b|k|m|g|t][i]\?[b]\?/<SIZE>/ig"
290 }
291
292 # Convert string read from stdin like 128K to bytes and print it to stdout
293 _filter_size_to_bytes()
294 {
295         read size
296         suffix=${size:${#size}-1}
297         mul=1
298         case $suffix in
299                 k|K) mul=1024 ;;
300                 m|M) mul=$((1024*1024)) ;;
301                 g|G) mul=$((1024*1024*1024)) ;;
302                 t|T) mul=$((1024*1024*1024*1024)) ;;
303         esac
304         echo $((${size:0:${#size}-1}*$mul))
305 }
306
307 # Print trimmed bytes of fstrim
308 # Starting from util-linux v2.23 fstrim usees human readable sizes in
309 # verbose output
310 _filter_fstrim()
311 {
312         egrep -o "[0-9]+ bytes" | $AWK_PROG '{print $1}'
313 }
314
315 # Older mount output referred to "block device" when mounting RO devices
316 # It's gone in newer versions
317 _filter_ro_mount() {
318         sed -e "s/mount: block device/mount:/g" \
319             -e "s/mount: cannot mount block device/mount: cannot mount/g"
320 }
321
322 # make sure this script returns success
323 /bin/true