Sib
Sib

Reputation: 33

Calculate average IO wait time for linux bash script

I need some help calculating the average IO wait time for a bash script.

The script I will be running is very simple. Sort 2 files and then join. Because I will be testing on huge files, I need to know how much IO bottleneck I have in order to make a few decisions(which files to join ++ ).

I have found many different answers to get IO wait time in general, but no way to calculate the average time for a program.

If the above is solved, I would also like a cpu average run time for the above, but the IO is more important.

One solution I had was to run iostat until the program terminates, and collect the iowait times and average afterwards. Not sure if this is a good idea or not.

Upvotes: 0

Views: 1658

Answers (1)

godlered
godlered

Reputation: 21

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%"

Upvotes: 2

Related Questions