common/rc: Add _require_{chown,chmod}()
[xfstests-dev.git] / tests / generic / 563
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2019 Red Hat, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 563
6 #
7 # This test verifies that cgroup aware writeback properly accounts I/Os in
8 # various scenarios. We perform reads/writes from different combinations of
9 # cgroups and verify that pages are accounted against the group that brought
10 # them into cache.
11 #
12
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 here=`pwd`
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         cd /
25         rm -f $tmp.*
26
27         echo $$ > $cgdir/cgroup.procs
28         rmdir $cgdir/$seq-cg* > /dev/null 2>&1
29         umount $SCRATCH_MNT > /dev/null 2>&1
30         _destroy_loop_device $LOOP_DEV > /dev/null 2>&1
31 }
32
33 # get standard environment, filters and checks
34 . ./common/rc
35 . ./common/filter
36 . ./common/cgroup2
37
38 # remove previous $seqres.full before test
39 rm -f $seqres.full
40
41 # real QA test starts here
42
43 # Modify as appropriate.
44 _supported_fs generic
45 _require_scratch_nocheck
46 _require_cgroup2 io
47 _require_loop
48
49 # cgroup v2 writeback is only support on block devices so far
50 _require_block_device $SCRATCH_DEV
51
52 cgdir=$CGROUP2_PATH
53 iosize=$((1024 * 1024 * 8))
54
55 # Check cgroup read/write charges against expected values. Allow for some
56 # tolerance as different filesystems seem to account slightly differently.
57 check_cg()
58 {
59         cgroot=$1
60         cgname=$(basename $cgroot)
61         expectedread=$2
62         expectedwrite=$3
63         rbytes=0
64         wbytes=0
65
66         iobytes=`cat $cgroot/io.stat | grep $smajor:$sminor`
67         if [ $? == 0 ]; then
68                 rbytes=`echo $iobytes | awk '{ print $2 }' | \
69                         awk -F = '{ print $2 }'`
70                 wbytes=`echo $iobytes | awk '{ print $3 }' | \
71                         awk -F = '{ print $2 }'`
72         fi
73
74         _within_tolerance "read" $rbytes $expectedread 5% -v
75         _within_tolerance "write" $wbytes $expectedwrite 5% -v
76 }
77
78 # Move current process to another cgroup.
79 switch_cg()
80 {
81         mkdir -p $1
82         echo $$ > $1/cgroup.procs
83 }
84
85 # Reset cgroup state for a new test.
86 reset()
87 {
88         echo $$ > $cgdir/cgroup.procs
89         rmdir $cgdir/$seq-cg* > /dev/null 2>&1
90         $XFS_IO_PROG -fc "pwrite 0 $iosize" $SCRATCH_MNT/file \
91                 >> $seqres.full 2>&1
92         umount $SCRATCH_MNT || _fail "umount failed"
93         _mount $LOOP_DEV $SCRATCH_MNT || _fail "mount failed"
94         stat $SCRATCH_MNT/file > /dev/null
95 }
96
97 # cgroup I/O accounting doesn't work on partitions. Use a loop device to rule
98 # that out.
99 LOOP_DEV=$(_create_loop_device $SCRATCH_DEV)
100 smajor=$((0x`stat -L -c %t $LOOP_DEV`))
101 sminor=$((0x`stat -L -c %T $LOOP_DEV`))
102
103 _mkfs_dev $LOOP_DEV >> $seqres.full 2>&1
104 _mount $LOOP_DEV $SCRATCH_MNT || _fail "mount failed"
105
106 drop_io_cgroup=
107 grep -q -w io $cgdir/cgroup.subtree_control || drop_io_cgroup=1
108
109 echo "+io" > $cgdir/cgroup.subtree_control || _fail "subtree control"
110
111 # Read and write from a single group.
112 echo "read/write"
113 reset
114 switch_cg $cgdir/$seq-cg
115 $XFS_IO_PROG -c "pread 0 $iosize" -c "pwrite 0 $iosize" -c fsync \
116         $SCRATCH_MNT/file >> $seqres.full 2>&1
117 switch_cg $cgdir
118 $XFS_IO_PROG -c fsync $SCRATCH_MNT/file
119 check_cg $cgdir/$seq-cg $iosize $iosize
120
121 # Write from one cgroup then read and write from a second. Writes are charged to
122 # the first group and nothing to the second.
123 echo "write -> read/write"
124 reset
125 switch_cg $cgdir/$seq-cg
126 $XFS_IO_PROG -c "pwrite 0 $iosize" $SCRATCH_MNT/file >> $seqres.full 2>&1
127 switch_cg $cgdir/$seq-cg-2
128 $XFS_IO_PROG -c "pread 0 $iosize" -c "pwrite 0 $iosize" $SCRATCH_MNT/file \
129         >> $seqres.full 2>&1
130 switch_cg $cgdir
131 $XFS_IO_PROG -c fsync $SCRATCH_MNT/file
132 check_cg $cgdir/$seq-cg 0 $iosize
133 check_cg $cgdir/$seq-cg-2 0 0
134
135 # Read from one cgroup, read & write from a second. Both reads and writes are
136 # charged to the first group and nothing to the second.
137 echo "read -> read/write"
138 reset
139 switch_cg $cgdir/$seq-cg
140 $XFS_IO_PROG -c "pread 0 $iosize" $SCRATCH_MNT/file >> $seqres.full 2>&1
141 switch_cg $cgdir/$seq-cg-2
142 $XFS_IO_PROG -c "pread 0 $iosize" -c "pwrite 0 $iosize" $SCRATCH_MNT/file \
143         >> $seqres.full 2>&1
144 switch_cg $cgdir
145 $XFS_IO_PROG -c fsync $SCRATCH_MNT/file
146 check_cg $cgdir/$seq-cg $iosize $iosize
147 check_cg $cgdir/$seq-cg-2 0 0
148
149 if [ "$drop_io_cgroup" = 1 ]; then
150         echo "-io" > $cgdir/cgroup.subtree_control || _fail "subtree control"
151 fi
152
153 # success, all done
154 status=0
155 exit