Different results from vmstat and ps
Hi all, I am trying to determine CPU usage on a radio controller running Linux. We have a script that another member of my work wrote using vmstat which I could not get any consistant results from. So, in an effort to understand what was going on I wrote a script using ps. Sadley after running several tests using both scripts, they do no match up at all, vmstat always gives me a much higher value. However, if I simply run the commands at any given time they do match up, therefore I believe that there is an error in one of the scripts. I am very new to linux and so I would very much appriciate a learned eye to look over these.
Here is the vmstat script:
#!/bin/bash
declare -i total
declare -i tries
tries=0
total=0
while [ $total -le 0 ]
do
if [ $tries -gt 6 ]; then
echo "You've tried entering a number $tries now. Maybe you should take a break and try again later."
exit
fi
if [ $tries -gt 3 ]; then
echo "Really? $tries attempts later and you're still not entering a number?"
fi
if [ $tries -gt 0 ]; then
if [ $tries -le 3 ]; then
echo "How about you enter a number this time?"
fi
fi
if [ $tries -le 0 ]; then
echo "How many seconds do you want the monitor to run for?"
fi
read total
tries="$(expr $tries + 1)"
done
vmstat -n 1 >> vmstat.log &
sleep 0.3
vmstat -n 1 >> vmstat.log &
sleep 0.3
vmstat -n 1 >> vmstat.log &
start="$(date +%s)"
elapsed=0
lastreport=0
report=0
cat /dev/null > vmstat.log
while [ $elapsed -lt $total ]
do
#date --rfc-3339=ns >> vmstat.log
#vmstat -n >> vmstat.log
end="$(date +%s)"
elapsed="$(expr $end - $start)"
report="$(expr $total - $elapsed)"
if [ $lastreport -ne $report ]; then
lastreport=$report
echo "$lastreport seconds remaining..."
fi
sleep 0.25
done
killall vmstat
echo "Finished"
========================================================================
And here is the ps script that I wrote:
#!/bin/bash
# Test script to log each running process and it's CPU utilization.
#empty the log file
cat /dev/null > psstats.log
# Read in user input for test duration.
echo "How long would you like to run the test in seconds?"
read time
#Variables for timing
start="$(date +%s)"
elapsed=0
oldelapsed=0
lastreport=0
report=0
#Loop to run for a specific number of seconds given by the user above.
while [ $elapsed -lt $time ]
do
end="$(date +%s)"
oldelapsed=$elapsed
elapsed="$(expr $end - $start)"
report="$(expr $time - $elapsed)"
#If time has not elapsed or if an entire second has passed perform the test.
if [ $elapsed -eq 0 ] || [ $oldelapsed -eq $(expr $elapsed - 1) ] && [ $report -gt 0 ]; then
echo $(expr $elapsed + 1) >> psstats.log
ps -eo pcpu,pmem,vsz,pid,user,args >> psstats.log &
sleep 0.3
echo "===============================" >> psstats.log
echo $(expr $elapsed + 1) >> psstats.log
ps -eo pcpu,pmem,vsz,pid,user,args >> psstats.log &
sleep 0.3
echo "===============================" >> psstats.log
echo $(expr $elapsed + 1) >> psstats.log
ps -eo pcpu,pmem,vsz,pid,user,args >> psstats.log &
sleep 0.3
echo "===============================" >> psstats.log
fi
#Print out remaining time per second.
if [ $lastreport -ne $report ]; then
lastreport=$report
if [ $lastreport -ge 10 ]; then
echo "$lastreport seconds remaining..."
else
echo "0$lastreport seconds remaining..."
fi
fi
done
echo "Finished"
|