]> git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/tests/functional/trim/trim.kshlib
Add trim support to zpool wait
[mirror_zfs.git] / tests / zfs-tests / tests / functional / trim / trim.kshlib
1 #!/bin/ksh -p
2 #
3 # This file and its contents are supplied under the terms of the
4 # Common Development and Distribution License ("CDDL"), version 1.0.
5 # You may only use this file in accordance with the terms of version
6 # 1.0 of the CDDL.
7 #
8 # A full copy of the text of the CDDL should have accompanied this
9 # source. A copy of the CDDL is also available via the Internet at
10 # http://www.illumos.org/license/CDDL.
11 #
12
13 #
14 # Copyright (c) 2019 by Tim Chase. All rights reserved.
15 # Copyright (c) 2019 Lawrence Livermore National Security, LLC.
16 #
17
18 . $STF_SUITE/tests/functional/cli_root/zpool_trim/zpool_trim.kshlib
19
20 #
21 # Get the actual size on disk for the provided file.
22 #
23 function get_size_mb
24 {
25 typeset rval=$(du --block-size 1048576 -s "$1" | awk '{print $1}')
26 echo -n "$rval"
27 }
28
29 #
30 # Get the number of trim IOs issued for the pool (ind or agg).
31 #
32 function get_trim_io
33 {
34 typeset pool="${1-:$TESTPOOL}"
35 typeset type="${2-:ind}"
36 typeset rval
37
38 # Sum the ind or agg columns of the trim request size histogram.
39 case "$type" in
40 "ind")
41 rval=$(zpool iostat -pr $pool | awk \
42 '$1 ~ /[0-9].*/ { sum += $12 } END { print sum }')
43 echo -n "$rval"
44 ;;
45 "agg")
46 rval=$(zpool iostat -pr $pool | awk \
47 '$1 ~ /[0-9].*/ { sum += $13 } END { print sum }')
48 echo -n "$rval"
49 ;;
50 *)
51 log_fail "Type must be 'ind' or 'agg'"
52 ;;
53 esac
54 }
55
56 #
57 # Verify that trim IOs were send to devices in the pool.
58 #
59 function verify_trim_io
60 {
61 typeset pool="${1:-$TESTPOOL}"
62 typeset type="${2:-ind}"
63 typeset min_trim_ios=${3:-100}
64 typeset ios
65
66 ios=$(get_trim_io $pool $type)
67 if [[ $ios -ge $min_trim_ios ]]; then
68 log_note "Issued $ios $type trim IOs for pool $pool"
69 else
70 log_fail "Too few trim IOs issued $ios/$min_trim_ios"
71 fi
72 }
73
74 #
75 # Run N txgs which should be enough to trim the entire pool.
76 #
77 function wait_trim_io # pool type txgs
78 {
79 typeset pool="${1-:$TESTPOOL}"
80 typeset type="${2-:ind}"
81 typeset txgs=${3:-10}
82 typeset timeout=120
83 typeset stop_time=$(( $(date +%s) + $timeout ))
84
85 typeset -i i=0
86 while [[ $i -lt $txgs ]]; do
87 if [ "$(date +%s)" -ge $stop_time ]; then
88 log_fail "Exceeded trim time limit of ${timeout}s"
89 return
90 fi
91
92 zpool sync -f
93 ((i = i + 1))
94 done
95
96 typeset ios=$(get_trim_io $pool $type)
97 log_note "Waited for $txgs txgs, $ios $type TRIM IOs"
98 }
99
100 #
101 # Verify that file vdevs against a target value.
102 #
103 function verify_vdevs # op size vdevs
104 {
105 typeset tgt_op=$1
106 typeset tgt_size=$2
107 shift 2
108 typeset vdevs=$@
109
110 for vdev in $vdevs; do
111 typeset size=$(get_size_mb $vdev)
112 if test $size $tgt_op $tgt_size; then
113 log_note "Success $vdev is $size MB which is $tgt_op" \
114 "than $tgt_size MB"
115 else
116 log_fail "Failure $vdev is $size MB which is not" \
117 "$tgt_op than $tgt_size MB"
118 fi
119 done
120 }