LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Different results from vmstat and ps (https://www.linuxquestions.org/questions/linux-newbie-8/different-results-from-vmstat-and-ps-816113/)

alburdet619 06-24-2010 07:19 AM

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"

alburdet619 06-25-2010 11:43 AM

Reposting on General...

pixellany 06-25-2010 12:07 PM

Quote:

Originally Posted by alburdet619 (Post 4014898)
Reposting on General...

wrong answer!!--double-posting not allowed

alburdet619 06-25-2010 03:12 PM

My bad, just trying to get an answer for work and hadn't gotten a response in over a day... oh well i'll just be patient. Sorry to ruffle feathers.

halvy 06-26-2010 05:46 AM

Had any luck?

What have you tried so far?

The only suggestion that comes to mind is the two programs are just that.. two different programs, with possibly different nice settings, etc.. which if not completely aligned.. will give different answers.

Sorry I cannot think of anything better.

Lettuce know.. :)

syg00 06-26-2010 07:16 AM

I really don't undestand what you're trying to achieve.
If you want to know what the (entire) system has done over a period, probe /proc/stat at the beginning and end of the period. A small amount of basic maths will sort the numbers out.

Or try collectl - it maintains process data, but may be overkill for you.

alburdet619 06-26-2010 10:11 AM

Haven't had much time to work on this, our product went beta in the last few days so testing has been more urgent. Like I said i'm very green on Linux so i'm not sure what the nice settings are, i'm guessing command line options? As far as I have been able to test it seems like it's definatly a problem in the code of one of them. As I said earlier running the same command line functions with the same options as in the programs gives very consistant results. However the scripts run simultaniously gives very differing results... I don't know what to think other than i'm not giving the correct command line options to ps, however I have tested most of thos from the man file and haven't found any that give me more processes than I get with the -e option.

alburdet619 06-26-2010 10:20 AM

Quote:

Originally Posted by syg00 (Post 4015567)
I really don't undestand what you're trying to achieve.

The system i'm testing is a Voice over IP radio controler, and by making radio calls using the system and monitoring the CPU usage i'm hoping to get a projection for maximum simultanious radio calls. Memory is not an issue, these calls take very little, however CPU gets eaten up the more calls that are currently on the system.

Therefore, my company had the above script using vmstat to take a sample very 1/3 of a second for however many seconds the user desires. However, upon running this the results were very erratic and inconsistant.

So, I am attempting to root cause this problem by writing my own script using ps to see what processess cause this erratice CPU usage.

syg00 06-26-2010 07:04 PM

Running multiple processes to scan /proc/ (after all that's what both vmstat and ps actully do) is always going to be dodgy. Despite your attempts to separate them by a third of a second, their dispatch after that is at the whim of the scheduler. They won't be at exactly 0.3 apart - and they'll be separate processes competing with everything else.
Go get collectl.

alburdet619 06-28-2010 08:32 AM

Quote:

Originally Posted by syg00 (Post 4015942)
Go get collectl.

I'm gonna try this if I get time, it sounds like a much better program than either vmstat or ps. Thanks for the suggestion, i'll let you know how it works out.

alburdet619 06-28-2010 11:02 AM

Okay, so the big problem is that we are running Wind River which is not a very popular linux platform. I don't know, and doubt, that collectl would work on Wind River and we can't even compile it because we don't have a compiler that works with Wind River. Any suggestions/help with this?

alburdet619 06-28-2010 11:03 AM

Nevermind, another guy at work got it to work! So I will play with that thanks everyone!


All times are GMT -5. The time now is 07:40 PM.