LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Closed Thread
  Search this Thread
Old 06-25-2010, 11:44 AM   #1
alburdet619
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 0
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"
 
Old 06-25-2010, 12:08 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Different results from vmstat and ps alburdet619 Linux - Newbie 11 06-28-2010 11:03 AM
vmstat question Robhogg Linux - General 3 04-10-2009 05:00 PM
vmstat lucifer_smiles Linux - Server 1 01-29-2008 06:09 PM
issue with VMSTAT Linux_Kidd Red Hat 1 12-30-2006 06:15 AM
vmstat UltraSoul Solaris / OpenSolaris 9 07-14-2006 10:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration