-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathnic_smp_affinity_set.sh
472 lines (417 loc) · 10.1 KB
/
nic_smp_affinity_set.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/bin/bash
# Author:"Wang Chunsheng"<[email protected]>
# Date: 2013.05.09
# usage example : ./$0 -a -o/-q
#
# Some documents can help you to understand smp_affinity or rps/rfs,
# please visit: http://blog.chunshengster.me/2013/05/smp_irq_affinity.html
# TODO:add numa node support
#set -x
#for grep nic card pattern, you can change this one to meet your system environment!
NIC_PATTERN='em\|eth\|p1'
RX_PATTERN='rx-'
LS='/bin/ls'
CAT='/bin/cat'
GREP='/bin/grep'
PS='/bin/ps'
WC='/usr/bin/wc'
#TODO:add support to ubuntu system stop a service
#this time only support Redhat system
SERVICE='/sbin/service'
CHKCONFIG='/sbin/chkconfig'
DISABLE_IRQBALANCE='NO'
ONLY_SHOW='NO'
QUIET='NO'
LOG_MSG='NO'
SYSNET_PATH='/sys/class/net'
QUEUE_PATH='queues'
RPS_ENTRIES_FILE="/proc/sys/net/core/rps_sock_flow_entries"
RPS_ENTRIES_NUM=32768
RPS_FLOW_CNT=4096
CPU_IDS=''
CPU_COUNT=0
ALL_USED_INTERFACE=''
SMP_AFFINITY_INF=''
RPS_RFS_INF=''
RPS_RFS_MASK=''
function parser_args() {
local all_run=0
while test -n "$1"; do
case "$1" in
-o|--only_show)
DISABLE_IRQBALANCE='NO'
ONLY_SHOW='YES'
shift
;;
-q|--quiet)
QUIET='YES'
shift
;;
-a|--all)
all_run=1
shift
;;
-b|--both)
both=1
shift
;;
-d|--disable_irqbalance)
DISABLE_IRQBALANCE='YES'
shift
;;
-n|--nic)
ALL_USED_INTERFACE=$2
shift 2
;;
-h|--help)
usage
return 1
;;
*)
echo "Unknown argument: $1"
usage
return 1
;;
esac
done
if [ $all_run -eq 0 ] && [ -z $ALL_USED_INTERFACE ]; then
usage
return 1
fi
###
# suck !!!!
if [ "$ONLY_SHOW" == 'YES' ] && [ "$QUIET" == 'YES' ]; then
echo '$ONLY_SHOW:'$ONLY_SHOW
echo '$QUIET:'$QUIET
echo "[ERROR] You can set either -o or -q, but can not both !"
return 1
else
return 0
fi
return 1
}
function debug_echo() {
[ "$QUIET" == 'NO' ] && echo "$*"
[ "$LOG_MSG" == 'YES' ] && logger "$*"
}
function version_compare () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
function check_interface_inuse() {
if [ ${#@} -lt 1 ]; then
return 1
fi
if [ -f "$SYSNET_PATH/$1/operstate" ]; then
local updown=$($CAT "$SYSNET_PATH/$1/operstate")
if [ $? -eq 0 ]; then
if [ "$updown" == "up" ];then
return 0
else
return 1
#return "$updown" == "up" ? 0 : 1
fi
fi
fi
return 1
}
function get_all_inused_interfaces() {
local allInterFaces=$($LS "$SYSNET_PATH" | $GREP $NIC_PATTERN)
#local len_t=${#allInterFaces}
local len_t=$(echo $allInterFaces | $WC -w)
if [ $len_t -lt 1 ];then
return 1
fi
for inf_t in $allInterFaces; do
#echo $inf_t
check_interface_inuse "$inf_t"
if [ $? -eq 0 ]; then
#echo $inf_t
ALL_USED_INTERFACE=$ALL_USED_INTERFACE" "$inf_t
fi
done
#echo $ALL_USED_INTERFACE
return 0
}
# get all nic card that support smp_affinity or rfs/rfs ,if no one ,return error
function get_smp_affinity_nics() {
if [ -z $1 ];then
return 1
fi
local nic_t_queue_count=$($LS "$SYSNET_PATH/$1/$QUEUE_PATH" | $GREP -c $RX_PATTERN)
if [ $nic_t_queue_count -gt 1 ]; then
SMP_AFFINITY_INF=$SMP_AFFINITY_INF" "$1
else
RPS_RFS_INF=$RPS_RFS_INF" "$1
fi
local count_t=$(echo $SMP_AFFINITY_INF$RPS_RFS_INF|$WC -w)
if [ $count_t -gt 0 ]; then
return 0
fi
return 1
}
# check for irq_balance running
# if $DISABLE_IRQBALANCE == 'YES' then disable irqbalance service
function check_irqbalance_on() {
IRQBALANCE_ON=$($PS ax | $GREP -v "grep" | $GREP -q "irqbalance"; echo $?)
if [ "$IRQBALANCE_ON" == "0" ] ; then
echo " WARNING: irq_balance is running and will"
echo " likely override this script's affinitization."
echo " Please stop the irq_balance service and/or execute"
echo " 'killall irqbalance'"
if [ "$ONLY_SHOW" == 'YES' ];then
return 0
fi
if [ "$DISABLE_IRQBALANCE" == 'YES' ]; then
$SERVICE irqbalance stop
if [ $? -eq 0 ]; then
return 0
fi
fi
else
return 0
fi
return 1
}
function get_cpuinfo() {
CPU_IDS=$($CAT "/proc/cpuinfo" | $GREP processor | $GREP -v grep | cut -d: -f 2)
CPU_COUNT=$(echo $CPU_IDS | $WC -w)
if [ $CPU_COUNT -lt 1 ];then
return 1
fi
return 0
}
#
function get_rpfs_affinity_mask() {
local t=$(($CPU_COUNT/4))
if [ $t -lt 1 ]; then
return 1
fi
for i in $(seq $t); do
RPS_RFS_MASK=$RPS_RFS_MASK'f'
done
#local t1=$(($CPU_COUNT-$t))
#if [ $t1 -gt 0 ]; then
# for i in $(seq $t1); do
# RPS_RFS_MASK="0"$RPS_RFS_MASK
# done
#fi
local ok_t=$(echo $RPS_RFS_MASK | $GREP -q 'f'; echo $?)
return $ok_t
}
function do_rpfs_enable() {
nic_dev=$1
debug_echo "dealing with interface :"$nic_dev
local rps_cpus_file_t="$SYSNET_PATH/$nic_dev/$QUEUE_PATH/rx-0/rps_cpus"
debug_echo "dealing with rps_cpus_file :"$rps_cpus_file_t
if [ -f $rps_cpus_file_t ]; then
local rps_cpus=$($CAT $rps_cpus_file_t | tr -d '0')
if [ -z "$rps_cpus" ]; then
local rps_cpus=$($CAT $rps_cpus_file_t)
fi
#compare current rps_cus to $RPS_RFS_MASK
if [ "$rps_cpus" != "$RPS_RFS_MASK" ]; then
if [ "$ONLY_SHOW" == 'NO' ]; then
echo $RPS_RFS_MASK > $rps_cpus_file_t
local rps_cpus_tt=$($CAT $rps_cpus_file_t | tr -d '0')
if [ "$rps_cpus_tt" != "$RPS_RFS_MASK" ]; then
echo " [ERROR]:setting $rps_cpus_file_t to $RPS_RFS_MASK error !"
return 1
fi
fi
debug_echo " origin value :"$rps_cpus
debug_echo " will be set to :"$RPS_RFS_MASK
else
debug_echo " already been set to:"$RPS_RFS_MASK
fi
else
return 1
fi
local rps_flow_cnt_file_t="$SYSNET_PATH/$nic_dev/$QUEUE_PATH/rx-0/rps_flow_cnt"
debug_echo "dealing with rps_flow_cnt_file :"$rps_flow_cnt_file_t
if [ -f $rps_flow_cnt_file_t ]; then
local rps_flow_cnt=$($CAT $rps_flow_cnt_file_t)
#compare current rps_flow_cnt to $RPS_FLOW_CNT
if [ "$rps_flow_cnt" != "$RPS_FLOW_CNT" ];then
# echo $RPS_FLOW_CNT
if [ "$ONLY_SHOW" == 'NO' ];then
echo $RPS_FLOW_CNT > $rps_flow_cnt_file_t
local rps_flow_cnt_tt=$($CAT $rps_flow_cnt_file_t)
if [ $rps_flow_cnt_tt != $RPS_FLOW_CNT ]; then
echo " [ERROR]:setting $rps_flow_cnt_file_t to $RPS_FLOW_CNT error !"
return 1
fi
fi
debug_echo " origin value :" $rps_flow_cnt
debug_echo " will be set to :"$RPS_FLOW_CNT
else
debug_echo " already been set to:"$RPS_FLOW_CNT
fi
else
return 1
fi
return 0
}
function do_rps_entries_enable() {
debug_echo "dealing with rps_entries :"$RPS_ENTRIES_FILE
local rps_entries=$($CAT $RPS_ENTRIES_FILE)
if [ $rps_entries -eq $RPS_ENTRIES_NUM ]; then
return 0
else
if [ "$ONLY_SHOW" == 'NO' ];then
echo $RPS_ENTRIES_NUM > $RPS_ENTRIES_FILE
local rps_entries_t=$($CAT $RPS_ENTRIES_FILE)
if [ $rps_entries -eq $RPS_ENTRIES_NUM ]; then
return 0
else
echo " [ERROR]:setting $RPS_ENTRIES_FILE to $RPS_ENTRIES_NUM error !"
return 1
fi
else
debug_echo " origin value :"$rps_entries
debug_echo " will be set to :"$RPS_ENTRIES_NUM
fi
fi
}
function do_smpaffinity_enable() {
nic_dev=$1
softIrqs=$($CAT "/proc/interrupts" | $GREP $nic_dev"-" | tr -s " " ":" | cut -d: -f 2)
softIrqCount=$(echo $softIrqs | $WC -w)
#first=$(echo $softIrqs|cut -d" " -f 1)
if [ $CPU_COUNT -lt $softIrqCount ]; then
debug_echo "Notice, CPU_COUNT:" $CPU_COUNT " is little than nic softIrqCount :" $softIrqCount
fi
debug_echo "$nic_dev nic softIrq list: "$softIrqs
debug_echo "Total cpu list: "$CPU_IDS
#debug_echo "First softIqr: "$first
tmpC=$CPU_COUNT
for s in $softIrqs; do
tmpC=$(($tmpC - 1))
if [ $tmpC -lt 0 ]; then
tmpC=$(($CPU_COUNT-1))
fi
debug_echo "irq no. : "$s;
local irq_t="/proc/irq/$s/smp_affinity_list"
if [ -f $irq_t ]; then
orig=$(cat $irq_t);
debug_echo " original set :"$orig
cpuid=$tmpC
if [ "$orig" == "$cpuid" ]; then
debug_echo " already set to :"$cpuid
else
debug_echo " will modified to: "$cpuid
if [ "$ONLY_SHOW" == 'NO' ]; then
echo $cpuid > $irq_t;
cid=$(cat "$irq_t");
if [ "$cpuid" == "$cid" ];then
debug_echo " done"
else
debug_echo " Error setting,please check manually!!"
debug_echo " check path:$irq_t"
return 1
fi
fi
fi
fi
done
return 0
}
function usage() {
echo "This Script will set smp_affinity or rps/rps automatically"
echo " via modify '/sys/class/net/' files and/or '/proc/' sysfs"
echo "USAGE:$0 -a -o/-q"
echo " -a/--all: Check all in_used interfaces"
echo " -b/--both: Both set rps/rqs and smp irq affinity"
echo " -n/--nic: Only check/set the specified nic interface"
echo " -d/--disable_irqbalance: Disable irq_balance service automatically"
echo " -o/--only_show: Only show what will happen"
echo " -q/--quiet: Nothing will display if no error happen"
exit 1
}
############################MAIN################################
#if [ "$#" -lt 2 ]; then
# usage
# exit
#fi
#if [ "$1" == "-nic" ];then
# nic_dev=$2
#else
# usage
#fi
#echo "$*"
if [ ${#@} -lt 1 ]; then
usage
fi
parser_args $*
if [ $? -gt 0 ]; then
exit
fi
check_irqbalance_on
if [ $? -gt 0 ]; then
exit
fi
if [ $(echo $ALL_USED_INTERFACE | $WC -w) -lt 1 ]; then
get_all_inused_interfaces
if [ $? -gt 0 ]; then
exit
fi
fi
get_cpuinfo
if [ $? -gt 0 ]; then
exit
fi
debug_echo "All used interface :" $ALL_USED_INTERFACE
for inf in $ALL_USED_INTERFACE; do
get_smp_affinity_nics $inf
done
debug_echo ' $SMP_AFFINITY_INF:'$SMP_AFFINITY_INF
debug_echo ' $RPS_RFS_INF:'$RPS_RFS_INF
if [ $(echo $SMP_AFFINITY_INF | $WC -w) -gt 0 ]; then
for inf_t in $SMP_AFFINITY_INF; do
do_smpaffinity_enable $inf_t
if [ $? -gt 0 ]; then
exit 1
fi
done
fi
if [ $(echo $RPS_RFS_INF | $WC -w) -gt 0 ]; then
get_rpfs_affinity_mask
if [ $? -gt 0 ]; then
exit 1
else
do_rps_entries_enable
for inf_t in $RPS_RFS_INF; do
do_rpfs_enable $inf_t
if [ $? -gt 0 ]; then
exit 1
fi
done
fi
fi
exit 1