summaryrefslogtreecommitdiff
path: root/usr/src/test/zfs-tests/tests/functional/trim/trim.kshlib
blob: 92687c3ebab188fb6842eccd2f6d53e3ae463138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/ksh -p
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright (c) 2019 by Tim Chase. All rights reserved.
# Copyright (c) 2019 Lawrence Livermore National Security, LLC.
# Copyright 2019 Joyent, Inc.
#

. $STF_SUITE/tests/functional/cli_root/zpool_trim/zpool_trim.kshlib

#
# Get the actual size on disk for the provided file.
#
function get_size_mb
{
	typeset rval=$(du -s "$1" | awk '{print $1}')
	rval=$((rval * 2048))
	echo -n "$rval"
}

#
# Get the number of trim IOs issued for the pool (ind or agg).
#
function get_trim_io
{
	typeset pool="${1-:$TESTPOOL}"
	typeset type="${2-:ind}"
	typeset rval

	# Sum the ind or agg columns of the trim request size histogram.
	case "$type" in
	"ind")
		rval=$(zpool iostat -pr $pool | awk \
		    '$1 ~ /[0-9].*/ { sum += $12 } END { print sum }')
		echo -n "$rval"
		;;
	"agg")
		rval=$(zpool iostat -pr $pool | awk \
		    '$1 ~ /[0-9].*/ { sum += $13 } END { print sum }')
		echo -n "$rval"
		;;
	*)
		log_fail "Type must be 'ind' or 'agg'"
		;;
	esac
}

#
# Verify that trim IOs were send to devices in the pool.
#
function verify_trim_io
{
	typeset pool="${1:-$TESTPOOL}"
	typeset type="${2:-ind}"
	typeset min_trim_ios=${3:-100}
	typeset ios

	ios=$(get_trim_io $pool $type)

	if [[ $ios -ge $min_trim_ios ]]; then
		log_note "Issued $ios $type trim IOs for pool $pool"
	else
		log_fail "Too few trim IOs issued $ios/$min_trim_ios"
	fi
}

#
# Run N txgs which should be enough to trim the entire pool.
#
function wait_trim_io # pool type txgs
{
	typeset pool="${1-:$TESTPOOL}"
	typeset type="${2-:ind}"
	typeset txgs=${3:-10}
	typeset timeout=120
	typeset stop_time=$(( $(date +%s) + $timeout ))

	typeset -i i=0
	while [[ $i -lt $txgs ]]; do
		if [ "$(date +%s)" -ge $stop_time ]; then
			log_fail "Exceeded trim time limit of ${timeout}s"
			return
		fi

		zpool sync -f
		((i = i + 1))
	done

	typeset ios=$(get_trim_io $pool $type)
	log_note "Waited for $txgs txgs, $ios $type TRIM IOs"
}

#
# Verify that file vdevs against a target value.
#
function verify_vdevs # op size vdevs
{
	typeset tgt_op=$1
	typeset tgt_size=$2
	shift 2
	typeset vdevs=$@

	for vdev in $vdevs; do
		typeset size=$(get_size_mb $vdev)
		if test $size $tgt_op $tgt_size; then
			log_note "Success $vdev is $size MB which is $tgt_op" \
			    "than $tgt_size MB"
		else
			log_fail "Failure $vdev is $size MB which is not" \
			    "$tgt_op than $tgt_size MB"
		fi
	done
}

#
# Wait for up to 120 seconds for trimming of the listed vdevs to complete.
#
function wait_trim # pool vdevs
{
	typeset stop_time=$(( $(date +%s) + 120 ))
	typeset pool="$1"
	shift
	typeset vdevs=$@
	typeset complete

	while [[ $complete -eq 0 ]]; do
		complete=1

		for vdev in $vdevs; do
			if [[ "$(trim_progress $pool $vdev)" -lt "100" ]]; then
				complete=0
				break
			else
				log_must eval "trim_prog_line $pool $vdev | \
				    grep complete"
			fi
		done

		if [ "$(date +%s)" -ge $stop_time ]; then
			log_fail "Exceeded trim time limit of 120s"
		fi

		sleep 0.5
	done

	log_note "Pool completed trim successfully."
}