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