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#
23function 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#
32function get_trim_io
33{
34	typeset pool="${1-:$TESTPOOL}"
35	typeset type="${2-:ind}"
36	typeset vdev="${3}"
37	typeset rval
38
39	# Sum the ind or agg columns of the trim request size histogram.
40	case "$type" in
41	"ind")
42		rval=$(zpool iostat -pr $pool $vdev | awk \
43		    '$1 ~ /[0-9].*/ { sum += $12 } END { print sum }')
44		echo -n "$rval"
45		;;
46	"agg")
47		rval=$(zpool iostat -pr $pool $vdev | awk \
48		    '$1 ~ /[0-9].*/ { sum += $13 } END { print sum }')
49		echo -n "$rval"
50		;;
51	*)
52		log_fail "Type must be 'ind' or 'agg'"
53		;;
54	esac
55}
56
57#
58# Verify that trim IOs were send to devices in the pool.
59#
60function verify_trim_io
61{
62	typeset pool="${1:-$TESTPOOL}"
63	typeset type="${2:-ind}"
64	typeset min_trim_ios=${3:-100}
65	typeset vdev="${4}"
66	typeset ios
67
68	ios=$(get_trim_io $pool $type $vdev)
69	if [[ $ios -ge $min_trim_ios ]]; then
70		log_note "Issued $ios $type trim IOs for pool $pool"
71	else
72		log_fail "Too few trim IOs issued $ios/$min_trim_ios"
73	fi
74}
75
76#
77# Run N txgs which should be enough to trim the entire pool.
78#
79function wait_trim_io # pool type txgs
80{
81	typeset pool="${1-:$TESTPOOL}"
82	typeset type="${2-:ind}"
83	typeset txgs=${3:-10}
84	typeset timeout=120
85	typeset stop_time=$(( $(date +%s) + $timeout ))
86
87	typeset -i i=0
88	while [[ $i -lt $txgs ]]; do
89		if [ "$(date +%s)" -ge $stop_time ]; then
90			log_fail "Exceeded trim time limit of ${timeout}s"
91			return
92		fi
93
94		zpool sync -f
95		((i = i + 1))
96	done
97
98	typeset ios=$(get_trim_io $pool $type)
99	log_note "Waited for $txgs txgs, $ios $type TRIM IOs"
100}
101
102#
103# Verify that file vdevs against a target value.
104#
105function verify_vdevs # op size vdevs
106{
107	typeset tgt_op=$1
108	typeset tgt_size=$2
109	shift 2
110	typeset vdevs=$@
111
112	for vdev in $vdevs; do
113		typeset size=$(get_size_mb $vdev)
114		if test $size $tgt_op $tgt_size; then
115			log_note "Success $vdev is $size MB which is $tgt_op" \
116			    "than $tgt_size MB"
117		else
118			log_fail "Failure $vdev is $size MB which is not" \
119			    "$tgt_op than $tgt_size MB"
120		fi
121	done
122}
123