summaryrefslogtreecommitdiff
path: root/usr/src/cmd/th_tools/th_script.sh
blob: 91e1aa6079d7654af2cdd1431ce2bdfc0a4be6e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#pragma ident	"%Z%%M%	%I%	%E% SMI"

#
# usage: force_state_change <path> <target state>
#
force_state_change()
{
	[[ $2 != "online" && $2 != "offline" ]] && exit 1
	th_manage $1 getstate | read path state busy
	[[ $? != 0 ]] && exit 1
	[[ "$state" = "$2" ]] && return 0
	th_manage $1 $2
	[[ $? != 0 ]] && exit 1
	th_manage $1 getstate | read path state busy
	[[ $? != 0 ]] && exit 1
	[[ "$state" != "$2" ]] && exit 1
	return 0
}


script_pid=0
trap ' terminate $script_pid ' 1 2 3 15
terminate()
{
	[[ $1 -gt 0 ]] && kill $1 > /dev/null 2>&1
	exit 1
}

#
# usage: control_workload <path> <pid>
# The following function is called (as a background task) prior to taking a
# driver instance offline and immediately after it is brought online. If the
# th_define process which created this script did not specify a script with the
# -e option then the default action is to run in the background this script
# which will continuously offline and online the instance until the injected
# error is detected by the driver or until the errdef is aborted.
#
control_workload()
{
	fixup_script 1
	if [ $? == 0 ]; then
		return
	fi

	#
	# Default workload - continuously offline and online the driver instance
	# while injecting errors
	#

	if [[ $2 -gt 0 ]]; then
		kill $2 > /dev/null 2>&1
	fi
	if [ $# -lt 2 ]; then
		echo syntax: $0 path pid
	elif [ $DRIVER_UNCONFIGURE = 1 ]; then
		: no unconfigure action required ;
	elif [ $DRIVER_CONFIGURE = 1 ]; then
		while [ 1 ]; do
			sleep 2
			force_state_change $1 offline
			force_state_change $1 online
		done &
		script_pid=$!
	fi
}

#
# usage: prepare_for_errdef <path> <driver> <instance> <do_unconfigure>
#
prepare_for_errdef()
{
	export DRIVER_PATH=$1
	export DRIVER_NAME=$2
	export DRIVER_INSTANCE=$3
	export DRIVER_UNCONFIGURE=1
	export DRIVER_CONFIGURE=0
	control_workload $1 $script_pid
	script_pid=0

	th_manage $2 $3 get_handles >/dev/null 2>&1
	[[ $? != 0 ]] && exit 1
	force_state_change $1 offline
	force_state_change $1 online

	export DRIVER_UNCONFIGURE=0
	export DRIVER_CONFIGURE=1
	[[ $4 == 1 ]] &&
		control_workload $1 $script_pid
}

# usage: monitor_edef <driver> <instance> <nsteps>
monitor_edef()
{
	let aborted=0
	trap ' (( aborted += 1 )) ' 16
	sleep 2	# Wait for the errdef to be added
	th_manage $1 $2 start
	[[ $? != 0 ]] && exit 1

	let s=0
	let x=$3
	set -A stats 0 0 1 0 0 0 0 ""

	#
	# Loop for x reports unless the error is reported or the access fail
	# count goes to zero.
	#
	while (( (x -= 1) >= 0 ))
	do
		(( aborted > 0 )) && break
		read line
		[ -z "$line" ] && break
		set -A stats $(echo "$line" |
		    /usr/bin/awk -F: '{for (i = 1; i <= NF; i++) print $i}')
		[ "${stats[6]}" -ne "0" ] && break	# Fault was reported
		#
		# If fail count is zero - increment a loop counter 3 times
		# before aborting this errdef.
		#
		[ "${stats[3]}" = "0" ] && (( (s += 1) > 3 )) && break
	done
	th_manage $1 $2 clear_errdefs			# Clear errors.
	[[ $? != 0 ]] && exit 1
	echo "${stats[@]}"
}

#
# Install, activate and monitor some error definitions
# usage: run_subtest <driver> <instance> < errdefs
#
run_subtest()
{
	let edefid=0
	drv=$1
	inst=$2
	if [ $devpath = "NULL" ]
	then
		path=$(th_manage $1 $2 getpath)
	else
		path=$devpath
	fi
	while read line
	do
		set -- $(echo "$line" | \
		    /usr/bin/awk '{for (i = 1; i <= NF; i++) print $i}')
		w=${line##*"-w "}
		let a=${w%%" "*}
		let b=${w##*" "}
		let x='a / b'
		(( a % b > 0 )) && (( x += 1 ))
		prepare_for_errdef $path $drv $inst 1
		set -A status $(th_define $* 2>./elog | \
		    monitor_edef $drv $inst $x)
		if [ "${status[2]}" -gt 0 ]; then
			res="test not triggered"
		elif [ "${status[1]}" -eq 0 ]; then
			res="success (error undetected)"
		elif [ "${status[1]}" -gt 0 ]; then
			if [ "${status[6]}" -eq 16 ]; then
				res="failure (no service impact reported)"
			else
				res="success (error reported)"
			fi
		else
			res=
		fi
		echo "Subtest $edefid: Result: \"$res\""
		echo $line
		if [ -n "${status[7]}" ]; then
			let i=6
			let l=${#status[@]}
			echo "	Fail Msg  :\t\c"
			while (( (i += 1) <= l ))
			do
				echo "${status[$i]} \c"
			done
			echo ""
		fi
		echo "\tFail Time :\t${status[0]}\tMsg Time  :\t${status[1]}"
		echo "\tAcc count :\t${status[2]}\tFail count:\t${status[3]}"
		echo "\tAccess Chk:\t${status[4]}\tEmsg count:\t${status[5]}"
		if [ "${status[6]}" -eq 0 ]; then
			echo "\tSeverity:\tSERVICE UNAFFECTED"
		elif [ "${status[6]}" -eq -16 ]; then
			echo "\tSeverity:\tSERVICE DEGRADED"
		elif [ "${status[6]}" -eq -32 ]; then
			echo "\tSeverity:\tSERVICE LOST"
		fi
		((edefid += 1))
	done

	fixup_script 0
	prepare_for_errdef $path $drv $inst 0
}