w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Calculate average IO wait time for linux bash script

You can find any cpu usage info in /proc/stat, so all you have to do is record the info before your command and after your command. I write a sample.

You can test it just like: ./avg_iowait.sh "dd if=/dev/zero of=tmp bs=1M count=2000"

You also can use the date data in the sample to calculate the cpu usage avg during the execute time. I

======================================================================= avg_iowait.sh

CMD="$1"

LOG_START="start.txt"
LOG_END="end.txt"

cat /proc/stat | grep "cpu " > $LOG_START

echo "exec : $CMD"
$CMD

cat /proc/stat | grep "cpu " > $LOG_END

cat $LOG_START
cat $LOG_END

USR1=`awk -F " " '{print $2}' $LOG_START`
NICE1=`awk -F " " '{print $3}' $LOG_START`
SYS1=`awk -F " " '{print $4}' $LOG_START`
IDLE1=`awk -F " " '{print $5}' $LOG_START`
IOWAIT1=`awk -F " " '{print $6}' $LOG_START`
IRQ1=`awk -F " " '{print $7}' $LOG_START`
SOFRIRQ1=`awk -F " " '{print $8}' $LOG_START`
STEAL1=`awk -F " " '{print $9}' $LOG_START`
GUEST1=`awk -F " " '{print $10}' $LOG_START`

USR2=`awk -F " " '{print $2}' $LOG_END`
NICE2=`awk -F " " '{print $3}' $LOG_END`
SYS2=`awk -F " " '{print $4}' $LOG_END`
IDLE2=`awk -F " " '{print $5}' $LOG_END`
IOWAIT2=`awk -F " " '{print $6}' $LOG_END`
IRQ2=`awk -F " " '{print $7}' $LOG_END`
SOFRIRQ2=`awk -F " " '{print $8}' $LOG_END`
STEAL2=`awk -F " " '{print $9}' $LOG_END`
GUEST2=`awk -F " " '{print $10}' $LOG_END`

SUM1=`expr $USR1 + $NICE1 + $SYS1 + $IDLE1 + $IOWAIT1 + $IRQ1 + $SOFRIRQ1 +
$STEAL1 + $GUEST1`

SUM2=`expr $USR2 + $NICE2 + $SYS2 + $IDLE2 + $IOWAIT2 + $IRQ2 + $SOFRIRQ2 +
$STEAL2 + $GUEST2`

IOWAIT_PERCENT=`expr ( $IOWAIT2 - $IOWAIT1 ) * 100 / ( $SUM2 - $SUM1 )`
echo "IOWAIT : $IOWAIT_PERCENT%"




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.