forked from rake74/bash_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_ahead_kb-for-path.sh
285 lines (242 loc) · 8.48 KB
/
read_ahead_kb-for-path.sh
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
# debugging made useful/easy
PS4='Line ${LINENO}: '
# set -ex
myName="$(basename $0)"
user_id=$(id -u)
function usage () {
cat << EOF
Sets read_ahead_kb for all block devices used behind path.
Usage: $myName [-s SIZE|-r] -p PATH
or: $myName -f SETTINGS-FILE
Options:
-p - path to search for block devices behind
REQUIRED
-s - size to set for device via read_ahead_kb
-r - report the current read_ahead_kb setting and blockdev
setting for each block device behind PATH
DEFAULT if -s unset
-h - Do not print header in 'report' output
-f - specify setting file
if set, -s and -p options ignored as they're in the file
File format:
[directory] [read_ahead_kb]
Lines that start with # are ignored, all whitespace treated
equaly.
Example:
$myName -s 16384 -p /mnt/iscsi/10_vod_db_main
Notes:
Without -s SIZE or -r, this will default to -r.
Requires:
blockdev command, root privs., column (for output)
EOF
echo
exit
}
# set default
report=0
function is_integer () {
case ${1#[-+]} in
''|*[!0-9]* ) return 1 ;;
* ) return 0 ;;
esac
}
function echo_err () { echo -e "$myName: $@" 1>&2 ; }
function echo_err_exit () { echo_err "$@" ; exit 1 ; }
# this IDs all block devices behind path - multipath and lvm make for fun
function get_block_devs_for_dir () {
whichDir="$1"
mainDev=$(readlink -f $(df -P $whichDir|tail -n1| cut -d\ -f1) | sed 's,^.*/,,g')
realDevs=$(lsblk -i | \
grep -e '^[a-zA-Z]' -e "$mainDev " -e "($mainDev)" | \
grep -B1 -e "$mainDev " -e "($mainDev)" | \
grep -e '^[a-zA-Z]' | \
cut -d\ -f1)
echo $mainDev $realDevs | tr ' ' '\n' | sort -u | tr '\n' ' ' | sed 's/\ \+$//g'
}
# get read_ahead_kb file contents/setting
function get_ra_kb_val () {
local dev="$1"
if [ "x$dev" = 'x' ] ; then
echo_err "$FUNCNAME - required block device argument missing"
return 1
fi
if [ ! -e /sys/block/$dev ] ; then
echo_err "$FUNCNAME - block device '/sys/block/$dev' does not exist"
return 1
fi
cat /sys/block/$dev/queue/read_ahead_kb
}
# get blockdev read_ahead value
function get_ra_block_val () {
local dev="$1"
if [ "x$dev" = 'x' ] ; then
echo_err "$FUNCNAME - required device argument missing"
return 1
fi
if [ ! -e /dev/$dev ] ; then
echo_err "$FUNCNAME - device '/dev/$dev' does not exist"
return 1
fi
blockdev --getra /dev/$dev
}
function show_ra_kb_blocks () {
local dev="$1"
local dir="$2"
if [ "x$dev" = 'x' ] ; then
echo_err "$FUNCNAME - required device argument missing"
return 1
fi
read_ahead_kb=$(get_ra_kb_val $dev)
err=$?
[ $err -ne 0 ] && \
echo_err "$FUNCNAME call to function 'get_ra_kb_val' returned error '$err', with the message (if any):\n$read_ahead_kb"
if [ $user_id -eq 0 ] ; then
read_ahead_block=$(get_ra_block_val $dev)
err=$?
[ $err -ne 0 ] && \
echo_err "$FUNCNAME call to function 'get_ra_block_val' returned error '$err', with the message (if any):\n$read_ahead_kb"
else
read_ahead_block='(non-root:n/a)'
fi
if [ "x$dir" != 'x' ] ; then
echo "$dir $dev $read_ahead_kb $read_ahead_block"
else
echo "$dev read_ahead_kb:$read_ahead_kb block_ra:$read_ahead_block"
fi
}
function show_all_read_ahead_kb_block () {
local show_all_dir="$1"
(
[ "x$show_all_dir" != 'x' ] && [ $noHeader -eq 0 ] && \
echo 'dir dev read_ahead_kb blockdev_getra'
for show_all_device in ${block_devices[@]}; do
show_ra_kb_blocks "$show_all_device" "$show_all_dir"
done
) | column -t
}
# identify block to kb ratio
# blockdev accepts blocks as arguments, file is managed and read in kb
# note: this does NO input validation, that should be handled before this
function get_block_kb_ratio () {
local get_block_kb_ratio_dev="$1"
local read_ahead_kb=$(get_ra_kb_val $get_block_kb_ratio_dev)
err=$?
[ $err -ne 0 ] && \
echo_err "$FUNCNAME call to function 'get_ra_kb_val' returned error '$err', with the message (if any):\n$read_ahead_kb"
read_ahead_block=$(get_ra_block_val $get_block_kb_ratio_dev)
err=$?
[ $err -ne 0 ] && \
echo_err "$FUNCNAME call to function 'get_ra_block_val' returned error '$err', with the message (if any):\n$read_ahead_kb"
echo "$read_ahead_block/$read_ahead_kb"|bc
}
# sets read_ahead_kb intelligently using blockdev, which uses blocks, not kb
function set_read_ahead_kb_val () {
local set_read_ahead_kb_val_dev="$1"
local set_read_ahead_kb_val_size="$2"
[ "x$set_read_ahead_kb_val_dev" = 'x' ] && \
echo_err "$FUNCNAME usage: $FUNCNAME [dev] [size]" && return 1
[ "x$set_read_ahead_kb_val_size" = 'x' ] && \
echo_err "$FUNCNAME usage: $FUNCNAME [dev] [size]" && return 1
[ ! -e /sys/block/$set_read_ahead_kb_val_dev ] && \
echo_err "$FUNCNAME: block device $set_read_ahead_kb_val_dev does not exist" && return 1
! is_integer $set_read_ahead_kb_val_size && \
echo_err "$FUNCNAME: size '$set_read_ahead_kb_val_size' is not an integer" && return 1
local block_kb_ratio=$(get_block_kb_ratio $set_read_ahead_kb_val_dev)
local set_new_read_ahead_block="$(echo "$block_kb_ratio*$set_read_ahead_kb_val_size"|bc)"
blockdev --setra $set_new_read_ahead_block /dev/$set_read_ahead_kb_val_dev
}
function parse_validate_settings_file () {
# this is only used when SETTING params, so if not root, go away
[ $user_id -ne 0 ] &&
echo_err_exit "non-root: this functionality requires root"
# set an array off of what's found in the file
tmp_path_array=( $(
grep -v -e '^#' -e '^$' $settings_file | \
sed \
-e 's/[\t ]\+/ /g' \
-e 's/^\ //g' \
-e 's/\ $//g' \
-e 's/\ /:/g' | \
cut -d: -f1-2
) )
[ ${#tmp_path_array[@]} -eq 0 ] && \
echo_err_exit "no data found in '$settings_file"
# now validate each path:size pair found and build real array
for path_size_val in ${tmp_path_array[@]} ; do
[ ! -d ${path_size_val/:*} ] && \
echo_err "specified path '${path_size_val/:*}' does not exist" && \
continue
! is_integer ${path_size_val/*:} && \
echo_err "read_ahead_kb size is not an int: '${path_size_val/*:}'" && \
continue
path_array[${#path_array[@]}]=$path_size_val
done
[ ${#path_array[@]} -eq 0 ] && \
echo_err_exit "no valid paths:sizes in '$settings_file'"
}
#
# process arguments
#
while [ $# -gt 0 ] ; do
ARG="$1" ; shift
case $ARG in
-p ) mount_path="$1" ; shift
[ ! -d $mount_path ] && \
echo_err_exit "specified path '$mount_path' does not exist"
;;
-s ) ra_kb_size="$1" ; shift
is_integer $ra_kb_size || \
echo_err_exit "read_ahead_kb size is not an int: '$ra_kb_size'"
;;
-r ) report=1 ;;
-h ) noHeader=1;;
-f ) settings_file="$1" ; shift
[ ! -r $settings_file ] && \
echo_err_exit "settings file '$settings_file' does not exist or is not readable"
;;
'-?' ) usage ;;
-h ) usage ;;
--help ) usage ;;
* ) echo_err_exit "unkown arg: $ARG" ;;
esac
done
#
# Usage is very different when called with a settings file, parse input sanely
#
[ "x$noHeader" = 'x' ] && noHeader=0
if [ "x$settings_file" != 'x' ] ; then
parse_validate_settings_file
else
# regular input validation here
# if not root, explain limited functionality
[ $user_id -ne 0 ] && \
echo '(non-root:blockdev binary unavailable, output/options limited)' 1>&2
# ensure valid params provided
[ "x$mount_path" = 'x' ] && echo_err_exit "-p [path] required"
[ "x$ra_kb_size" = 'x' ] && report=1
# mount_path and size validation done in argument parsing
path_array=( $mount_path:$ra_kb_size )
fi
#
# main work loop
#
for path_size_val in ${path_array[@]} ; do
# get devices, do sanity check
block_devices=( $(get_block_devs_for_dir ${path_size_val/:*}) )
[ ${#block_devices[@]} -eq 0 ] && \
echo_err "failed to identify block devices used by $mount_path." && \
continue
# report requested? 'ere ya go!
[ $report -eq 1 ] && show_all_read_ahead_kb_block ${path_size_val/:*}
# this would only happen if NOT using settings file and size not set
[ "x${path_size_val/*:}" = 'x' ] && exit
# if not root, quit now
[ $user_id -ne 0 ] && echo_err_exit 'non-root: blockdev requires root'
for main_loop_dev in ${block_devices[@]} ; do
set_read_ahead_kb_val $main_loop_dev ${path_size_val/*:}
done
# report requested? 'ere ya go again!
[ $report -eq 1 ] && show_all_read_ahead_kb_block ${path_size_val/:*} | \
sed 's/$/ post-set-verification/g'
done