]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/cddl/zfs/tests/cache/cache.kshlib
MFV r331400: 8484 Implement aggregate sum and use for arc counters
[FreeBSD/FreeBSD.git] / tests / sys / cddl / zfs / tests / cache / cache.kshlib
1 # vim: filetype=sh
2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22
23 # $FreeBSD$
24
25 #
26 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
27 # Use is subject to license terms.
28 #
29 # ident "@(#)cache.kshlib       1.4     09/05/19 SMI"
30 #
31
32 . $STF_SUITE/include/libtest.kshlib
33
34 function cleanup
35 {
36         log_note "Final pool configurations:"
37         poolexists $TESTPOOL && log_must $ZPOOL status -v $TESTPOOL
38         poolexists $TESTPOOL2 && log_must $ZPOOL status -v $TESTPOOL2
39         destroy_pool $TESTPOOL
40         destroy_pool $TESTPOOL2
41 }
42
43 #
44 # Try zpool status/iostat for given pool
45 #
46 # $1 pool
47 #
48 function display_status
49 {
50         typeset pool=$1
51
52         typeset -i ret=0
53         $ZPOOL status -xv $pool > /dev/null 2>&1
54         ret=$?
55
56         $ZPOOL iostat > /dev/null 2>&1
57         ((ret |= $?))
58
59         typeset mntpnt=$(get_prop mountpoint $pool)
60         $DD if=/dev/random of=$mntpnt/testfile.${TESTCASE_ID} &
61         typeset pid=$!
62
63         $ZPOOL iostat -v 1 3 > /dev/null
64         ((ret |= $?))
65
66         kill -9 $pid
67
68         return $ret
69 }
70
71 #
72 # Verify the give cache device have correct type and status
73 #
74 # $1 pool name
75 # $2 device name
76 # $3 device status
77 # $4 device type
78 #
79 function verify_cache_device
80 {
81         typeset pool=$1
82         typeset device=$2
83         typeset status=$3
84         typeset type=$4
85
86         if [[ -z $pool || -z $device || -z $status ]]; then
87                 log_fail "Usage: verify_cache_device <pool> <device> " \
88                         "<status> [type]"
89         fi
90
91         # Zpool status returns on the device name sans the /dev, so
92         # if the device contains /dev/ remove it.
93         device=${device#"/dev/"}
94
95         if [[ $WRAPPER == *"smi"* ]]; then
96                 $ECHO $device | $EGREP "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
97                 if (( $? == 0 )); then
98                         device=${device}s2
99                 fi
100         fi
101
102         #
103         # Get all the cache devices and status table like below
104         #
105         # mirror:/disks/d ONLINE mirror:/disks/e ONLINE stripe:/disks/f ONLINE
106         #
107         set -A dev_stat_tab $($ZPOOL status -v $pool | $NAWK '
108                                 function parse_name(status)
109                                 {
110                                         if (status == "OFFLINE")
111                                                 return substr($7,6)
112                                         else if (status == "UNAVAIL")
113                                                 return substr($7,6)
114                                         else
115                                                 return $1
116                                 }
117
118                                 BEGIN {in_cache=0}
119                                 /\tcache/ {in_cache=1}
120                                 /\tlog/ || /\tspares/ || /^$/ {in_cache=0}
121
122                                 # Skip if not in a cache section
123                                 (in_cache==0) { next; }
124
125                                 /\t  (\/|[0-9a-zA-Z])/ {
126                                         print "stripe:" parse_name($2) " " $2;
127                                 }
128
129                                 /\t    (\/|[a-zA-Z])/ {
130                                         print "mirror:" parse_name($2) " " $2;
131                                 }
132
133                                 # When hotspare is replacing
134                                 /\t      (\/|[a-zA-Z])/ {
135                                         print "mirror:" parse_name($2) " " $2;
136                                 }
137         ')
138
139         typeset -i i=0
140         typeset find=0
141         while (( i < ${#dev_stat_tab[@]} )); do
142                 typeset dev=${dev_stat_tab[$i]}
143                 typeset stat=${dev_stat_tab[((i+1))]}
144
145                 case $dev in
146                         stripe:$device) 
147                                 if [[ "$type" == 'mirror' ]]; then
148                                         log_note "Unexpected type: mirror"
149                                         return 1
150                                 else
151                                         if [[ $stat != $status ]]; then
152                                                 log_note "Status($stat) " \
153                                                         "!= Expected stat($status)"
154                                                 return 1
155                                         fi
156                                         return 0
157                                 fi
158                                 ;;
159                         mirror:$device)
160                                 if [[ -z "$type" || $type == 'stripe' ]]; then
161                                         log_note "Unexpected type: stripe"
162                                         return 1
163                                 else
164                                         if [[ $stat != $status ]]; then
165                                                 log_note "Status($stat) " \
166                                                         "!= Expected stat($status)"
167                                                 return 1
168                                         fi
169                                         return 0
170                                 fi
171                                 ;;
172                 esac
173
174                 ((i += 2))
175         done
176
177         log_note "Can not find device: $device"
178
179         return 1
180 }