xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/trim/trim.kshlib (revision 069e6b7e31ba5dcbc5441b98af272714d9a5455c)
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# Copyright 2019 Joyent, Inc.
17#
18
19. $STF_SUITE/tests/functional/cli_root/zpool_trim/zpool_trim.kshlib
20
21#
22# Get the actual size on disk for the provided file.
23#
24function get_size_mb
25{
26	typeset rval=$(du -s "$1" | awk '{print $1}')
27	rval=$((rval * 2048))
28	echo -n "$rval"
29}
30
31#
32# Get the number of trim IOs issued for the pool (ind or agg).
33#
34function get_trim_io
35{
36	typeset pool="${1-:$TESTPOOL}"
37	typeset type="${2-:ind}"
38	typeset rval
39
40	# Sum the ind or agg columns of the trim request size histogram.
41	case "$type" in
42	"ind")
43		rval=$(zpool iostat -pr $pool | awk \
44		    '$1 ~ /[0-9].*/ { sum += $12 } END { print sum }')
45		echo -n "$rval"
46		;;
47	"agg")
48		rval=$(zpool iostat -pr $pool | awk \
49		    '$1 ~ /[0-9].*/ { sum += $13 } END { print sum }')
50		echo -n "$rval"
51		;;
52	*)
53		log_fail "Type must be 'ind' or 'agg'"
54		;;
55	esac
56}
57
58#
59# Verify that trim IOs were send to devices in the pool.
60#
61function verify_trim_io
62{
63	typeset pool="${1:-$TESTPOOL}"
64	typeset type="${2:-ind}"
65	typeset min_trim_ios=${3:-100}
66	typeset ios
67
68	ios=$(get_trim_io $pool $type)
69
70	if [[ $ios -ge $min_trim_ios ]]; then
71		log_note "Issued $ios $type trim IOs for pool $pool"
72	else
73		log_fail "Too few trim IOs issued $ios/$min_trim_ios"
74	fi
75}
76
77#
78# Run N txgs which should be enough to trim the entire pool.
79#
80function wait_trim_io # pool type txgs
81{
82	typeset pool="${1-:$TESTPOOL}"
83	typeset type="${2-:ind}"
84	typeset txgs=${3:-10}
85	typeset timeout=120
86	typeset stop_time=$(( $(date +%s) + $timeout ))
87
88	typeset -i i=0
89	while [[ $i -lt $txgs ]]; do
90		if [ "$(date +%s)" -ge $stop_time ]; then
91			log_fail "Exceeded trim time limit of ${timeout}s"
92			return
93		fi
94
95		zpool sync -f
96		((i = i + 1))
97	done
98
99	typeset ios=$(get_trim_io $pool $type)
100	log_note "Waited for $txgs txgs, $ios $type TRIM IOs"
101}
102
103#
104# Verify that file vdevs against a target value.
105#
106function verify_vdevs # op size vdevs
107{
108	typeset tgt_op=$1
109	typeset tgt_size=$2
110	shift 2
111	typeset vdevs=$@
112
113	for vdev in $vdevs; do
114		typeset size=$(get_size_mb $vdev)
115		if test $size $tgt_op $tgt_size; then
116			log_note "Success $vdev is $size MB which is $tgt_op" \
117			    "than $tgt_size MB"
118		else
119			log_fail "Failure $vdev is $size MB which is not" \
120			    "$tgt_op than $tgt_size MB"
121		fi
122	done
123}
124
125#
126# Wait for up to 120 seconds for trimming of the listed vdevs to complete.
127#
128function wait_trim # pool vdevs
129{
130	typeset stop_time=$(( $(date +%s) + 120 ))
131	typeset pool="$1"
132	shift
133	typeset vdevs=$@
134	typeset complete
135
136	while [[ $complete -eq 0 ]]; do
137		complete=1
138
139		for vdev in $vdevs; do
140			if [[ "$(trim_progress $pool $vdev)" -lt "100" ]]; then
141				complete=0
142				break
143			else
144				log_must eval "trim_prog_line $pool $vdev | \
145				    grep complete"
146			fi
147		done
148
149		if [ "$(date +%s)" -ge $stop_time ]; then
150			log_fail "Exceeded trim time limit of 120s"
151		fi
152
153		sleep 0.5
154	done
155
156	log_note "Pool completed trim successfully."
157}
158