Support custom build directories and move includes
[zfs.git] / scripts / zpios.sh
1 #!/bin/bash
2 #
3 # Wrapper script for easily running zpios based tests
4 #
5
6 basedir="$(dirname $0)"
7
8 SCRIPT_COMMON=common.sh
9 if [ -f "${basedir}/${SCRIPT_COMMON}" ]; then
10 . "${basedir}/${SCRIPT_COMMON}"
11 else
12 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
13 fi
14
15 PROG=zpios.sh
16 DATE=`date +%Y%m%d-%H%M%S`
17 if [ "${ZPIOS_MODULES}" ]; then
18         MODULES=(${ZPIOS_MODULES[*]})
19 else
20         MODULES=(zpios)
21 fi
22
23 usage() {
24 cat << EOF
25 USAGE:
26 $0 [hvp] <-c config> <-t test>
27
28 DESCRIPTION:
29         Helper script for easy zpios benchmarking.
30
31 OPTIONS:
32         -h      Show this message
33         -v      Verbose
34         -f      Force everything
35         -p      Enable profiling
36         -c      Zpool configuration
37         -t      Zpios test
38         -o      Additional zpios options
39         -l      Additional zpool options
40         -s      Additional zfs options
41
42 EOF
43 }
44
45 print_header() {
46         echo --------------------- ZPIOS RESULTS ----------------------------
47         echo -n "Date: "; date
48         echo -n "Kernel: "; uname -r
49         dmesg | grep "Loaded Solaris Porting Layer" | tail -n1
50         dmesg | grep "Loaded ZFS Filesystem" | tail -n1
51         echo
52 }
53
54 print_spl_info() {
55         echo --------------------- SPL Tunings ------------------------------
56         ${SYSCTL} -A | grep spl
57
58         if [ -d /sys/module/spl/parameters ]; then
59                 grep [0-9] /sys/module/spl/parameters/*
60         else
61                 grep [0-9] /sys/module/spl/*
62         fi
63
64         echo
65 }
66
67 print_zfs_info() {
68         echo --------------------- ZFS Tunings ------------------------------
69         ${SYSCTL} -A | grep zfs
70
71         if [ -d /sys/module/zfs/parameters ]; then
72                 grep [0-9] /sys/module/zfs/parameters/*
73         else
74                 grep [0-9] /sys/module/zfs/*
75         fi
76
77         echo
78 }
79
80 print_stats() {
81         echo ---------------------- Statistics -------------------------------
82         ${SYSCTL} -A | grep spl | grep stack_max
83
84         if [ -d /proc/spl/kstat/ ]; then
85                 if [ -f /proc/spl/kstat/zfs/arcstats ]; then
86                         echo "* ARC"
87                         cat /proc/spl/kstat/zfs/arcstats
88                         echo
89                 fi
90
91                 if [ -f /proc/spl/kstat/zfs/vdev_cache_stats ]; then
92                         echo "* VDEV Cache"
93                         cat /proc/spl/kstat/zfs/vdev_cache_stats
94                         echo
95                 fi
96         fi
97
98         if [ -f /proc/spl/kmem/slab ]; then
99                 echo "* SPL SLAB"
100                 cat /proc/spl/kmem/slab
101                 echo
102         fi
103
104         echo
105 }
106
107 check_test() {
108
109         if [ ! -f ${ZPIOS_TEST} ]; then
110                 local NAME=`basename ${ZPIOS_TEST} .sh`
111                 ERROR="Unknown test '${NAME}', available tests are:\n"
112
113                 for TST in `ls ${ZPIOSDIR}/ | grep ".sh"`; do
114                         local NAME=`basename ${TST} .sh`
115                         ERROR="${ERROR}${NAME}\n"
116                 done
117
118                 return 1
119         fi
120
121         return 0
122 }
123
124 zpios_profile_config() {
125 cat > ${PROFILE_DIR}/zpios-config.sh << EOF
126 #
127 # Zpios Profiling Configuration
128 #
129
130 PROFILE_DIR=/tmp/zpios/${ZPOOL_CONFIG}+${ZPIOS_TEST_ARG}+${DATE}
131 PROFILE_PRE=${ZPIOSPROFILEDIR}/zpios-profile-pre.sh
132 PROFILE_POST=${ZPIOSPROFILEDIR}/zpios-profile-post.sh
133 PROFILE_USER=${ZPIOSPROFILEDIR}/zpios-profile.sh
134 PROFILE_PIDS=${ZPIOSPROFILEDIR}/zpios-profile-pids.sh
135 PROFILE_DISK=${ZPIOSPROFILEDIR}/zpios-profile-disk.sh
136 PROFILE_ARC_PROC=/proc/spl/kstat/zfs/arcstats
137 PROFILE_VDEV_CACHE_PROC=/proc/spl/kstat/zfs/vdev_cache_stats
138
139 OPROFILE_KERNEL="/boot/vmlinux-`uname -r`"
140 OPROFILE_KERNEL_DIR="/lib/modules/`uname -r`/kernel/"
141 OPROFILE_SPL_DIR=${SPLBUILD}/module/
142 OPROFILE_ZFS_DIR=${MODDIR}
143
144 EOF
145 }
146
147 zpios_profile_start() {
148         PROFILE_DIR=/tmp/zpios/${ZPOOL_CONFIG}+${ZPIOS_TEST_ARG}+${DATE}
149
150         mkdir -p ${PROFILE_DIR}
151         zpios_profile_config
152         . ${PROFILE_DIR}/zpios-config.sh
153
154         ZPIOS_OPTIONS="${ZPIOS_OPTIONS} --log=${PROFILE_DIR}"
155         ZPIOS_OPTIONS="${ZPIOS_OPTIONS} --prerun=${PROFILE_PRE}"
156         ZPIOS_OPTIONS="${ZPIOS_OPTIONS} --postrun=${PROFILE_POST}"
157
158         /usr/bin/opcontrol --init
159         /usr/bin/opcontrol --setup --vmlinux=${OPROFILE_KERNEL}
160 }
161
162 zpios_profile_stop() {
163         /usr/bin/opcontrol --shutdown
164         /usr/bin/opcontrol --deinit
165 }
166
167 PROFILE=
168 ZPOOL_CONFIG=zpool-config.sh
169 ZPIOS_TEST=zpios-test.sh
170 ZPOOL_NAME=zpios
171 ZPIOS_OPTIONS=
172 ZPOOL_OPTIONS=""
173 ZFS_OPTIONS=""
174
175 while getopts 'hvfpc:t:o:l:s:' OPTION; do
176         case $OPTION in
177         h)
178                 usage
179                 exit 1
180                 ;;
181         v)
182                 VERBOSE=1
183                 VERBOSE_FLAG="-v"
184                 ;;
185         f)
186                 FORCE=1
187                 FORCE_FLAG="-f"
188                 ;;
189         p)
190                 PROFILE=1
191                 ;;
192         c)
193                 ZPOOL_CONFIG=${OPTARG}
194                 ;;
195         t)
196                 ZPIOS_TEST_ARG=${OPTARG}
197                 ZPIOS_TEST=${ZPIOSDIR}/${OPTARG}.sh
198                 ;;
199         o)
200                 ZPIOS_OPTIONS=${OPTARG}
201                 ;;
202         l)      # Passed through to zpool-create.sh 
203                 ZPOOL_OPTIONS=${OPTARG}
204                 ;;
205         s)      # Passed through to zpool-create.sh
206                 ZFS_OPTIONS=${OPTARG}
207                 ;;
208         ?)
209                 usage
210                 exit
211                 ;;
212         esac
213 done
214
215 if [ $(id -u) != 0 ]; then
216         die "Must run as root"
217 fi
218
219 # Validate and source your test config
220 check_test || die "${ERROR}"
221 . ${ZPIOS_TEST}
222
223 # Pull in the zpios test module is not loaded.  If this fails it is
224 # likely because the full module stack was not yet loaded with zfs.sh
225 if check_modules; then
226         if ! load_modules; then
227                 die "Run 'zfs.sh' to ensure the full module stack is loaded"
228         fi
229 fi
230
231 # Wait for device creation
232 while [ ! -c /dev/zpios ]; do
233         sleep 1
234 done
235
236 if [ ${VERBOSE} ]; then
237         print_header
238         print_spl_info
239         print_zfs_info
240 fi
241
242 # Create the zpool configuration
243 ${ZPOOL_CREATE_SH} ${VERBOSE_FLAG} ${FORCE_FLAG} \
244         -p ${ZPOOL_NAME} -c ${ZPOOL_CONFIG} \
245         -l "${ZPOOL_OPTIONS}" -s "${ZFS_OPTIONS}" || exit 1
246
247 if [ ${PROFILE} ]; then
248         zpios_profile_start
249 fi
250
251 zpios_start
252 zpios_stop
253
254 if [ ${PROFILE} ]; then
255         zpios_profile_stop
256 fi
257
258 if [ ${VERBOSE} ]; then
259         print_stats
260 fi
261
262 # Destroy the zpool configuration
263 ${ZPOOL_CREATE_SH} ${VERBOSE_FLAG} ${FORCE_FLAG} \
264         -p ${ZPOOL_NAME} -c ${ZPOOL_CONFIG} -d || exit 1
265
266 # Unload the test module stack and wait for device removal
267 unload_modules
268 while [ -c /dev/zpios ]; do
269         sleep 1
270 done
271
272 exit 0