1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17. $STF_SUITE/include/libtest.shlib
18. $STF_SUITE/tests/functional/trim/trim.kshlib
19. $STF_SUITE/tests/functional/trim/trim.cfg
20
21#
22# DESCRIPTION:
23# 	Verify trimming of L2ARC
24#
25# STRATEGY:
26#	1. Set 'l2arc_trim_ahead = 1' and `l2arc_write_size = 64MB`.
27#	2. Create a pool on file vdevs to trim.
28#	3. Verify the cache device was trimmed.
29#	4. Fill the pool with a file larger than the L2ARC vdev.
30#	5. Randomly read the previous written file long enough for the
31#		L2ARC vdev to be filled and overwritten 5 times.
32#	6. Verify trim IOs of the expected type were issued for the pool.
33#	7. Verify the allocated space on the cache device is less than
34#		its size.
35#
36
37verify_runnable "global"
38
39log_assert "Trim of L2ARC succeeds."
40
41function cleanup
42{
43	if poolexists $TESTPOOL; then
44		destroy_pool $TESTPOOL
45	fi
46
47	log_must rm -f $VDEVS
48	log_must set_tunable32 L2ARC_TRIM_AHEAD $l2arc_trimahead
49	log_must set_tunable32 L2ARC_WRITE_MAX $l2arc_writemax
50}
51log_onexit cleanup
52
53# The cache device $TRIM_VDEV2 has to be small enough, so that
54# dev->l2ad_hand loops around and dev->l2ad_first=0. Otherwise
55# l2arc_evict() exits before evicting/trimming.
56typeset l2arc_trimahead=$(get_tunable L2ARC_TRIM_AHEAD)
57typeset l2arc_writemax=$(get_tunable L2ARC_WRITE_MAX)
58log_must set_tunable32 L2ARC_TRIM_AHEAD 1
59log_must set_tunable32 L2ARC_WRITE_MAX $((64 * 1024 * 1024))
60VDEVS="$TRIM_VDEV1 $TRIM_VDEV2"
61log_must truncate -s $((MINVDEVSIZE)) $TRIM_VDEV2
62log_must truncate -s $((4 * MINVDEVSIZE)) $TRIM_VDEV1
63typeset VDEV_MIN_MB=$((MINVDEVSIZE * 0.30 / 1024 / 1024))
64
65log_must zpool create -f $TESTPOOL $TRIM_VDEV1 cache $TRIM_VDEV2
66verify_vdevs "-le" "$VDEV_MIN_MB" $TRIM_VDEV2
67
68typeset fill_mb=$(( floor(2 * MINVDEVSIZE) ))
69export DIRECTORY=/$TESTPOOL
70export NUMJOBS=1
71export FILE_SIZE=${fill_mb}
72export PERF_RANDSEED=1234
73export PERF_COMPPERCENT=66
74export PERF_COMPCHUNK=0
75export RUNTIME=30
76export BLOCKSIZE=128K
77export SYNC_TYPE=0
78export DIRECT=1
79
80# Write to the pool.
81log_must fio $FIO_SCRIPTS/mkfiles.fio
82
83# Read randomly from the pool to fill L2ARC.
84export RUNTIME=30
85log_must fio $FIO_SCRIPTS/random_reads.fio
86
87export RUNTIME=1
88typeset do_once=true
89while $do_once || [[ $l2_size1 -le $l2_size2 ]]; do
90	typeset l2_size1=$(get_arcstat l2_size)
91	log_must fio $FIO_SCRIPTS/random_reads.fio
92	typeset l2_size2=$(get_arcstat l2_size)
93	do_once=false
94done
95
96verify_trim_io $TESTPOOL "ind" 5 $TRIM_VDEV2
97
98typeset cache_size=$(zpool list -vp | grep $TRIM_VDEV2 | awk '{print $2}')
99typeset cache_alloc=$(zpool list -vp | grep $TRIM_VDEV2 | awk '{print $3}')
100
101log_must test $cache_alloc -lt $cache_size
102
103log_must zpool destroy $TESTPOOL
104log_must rm -f $VDEVS
105
106log_pass "Trim of L2ARC succeeds."
107