LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2013, 06:20 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Rep: Reputation: 67
command to get CPU utilization over shorter interval than one minute?


I was wondering if there might exist a linux command that will tell me how busy my CPU is? I've gotten accustomed to typing 'w' which gives me a load average (not quite the same thing as CPU utilization but pretty good) or 'top' (which does actually calculate some kind of CPU % utilization).

Unfortunately, the 'w' command only shows load averages over 1, 5, and 15 minutes. I need something that looks at a shorter interval (e.g., 15 seconds). Can anyone tell me how I might get load average on the order of seconds rather than minutes?
 
Old 05-11-2013, 06:37 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
So, do you want CPU utilization or do you want load average ?.

If the former, "top" does the arithmetic nicely for you, and will accept sub-second parameters. Of course, the more you ask it to do, the more CPU it will eat itself.
If the latter, you get what the kernel exposes - which is a decaying average over the time periods you already noted. Some interesting maths involved if you dive into the code. Note that this is nothing like CPU utilization - we see threads here all the time from people with 60, 80, or greater.
 
Old 05-11-2013, 06:55 PM   #3
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
As I understand it, the load average is sort of a running average of how many processes are waiting in the process queue to be run. If you have more cores, I believe it is acceptable to have a higher load average. If your load average starts to get really high, it could be for a number of reasons: waiting on I/O, CPU too busy, etc.

Either load average or CPU utilization would be acceptable, I just need something that indicates how much of a trainwreck there is for processes trying to get executed. The reason I need this is because I'm writing a multi-threaded script and I'm trying to make it adaptive to the computing power available on the machine. If the computer is super busy, I want my script to sleep perhaps -- or not launch a new thread. If, on the other hand, the computer is not busy at all, I want to spawn a number of threads.

The problem with the 1 minute load average is that it seems to change only very slowly. The result is that my script totally halts while it waits for the 1m load average to slowly sink. I need something less sluggish / more responsive.
 
Old 05-11-2013, 07:00 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by sneakyimp View Post
The problem with the 1 minute load average is that it seems to change only very slowly. The result is that my script totally halts while it waits for the 1m load average to slowly sink. I need something less sluggish / more responsive.
What's wrong with just parsing the cpu column of "top -n 1"?
 
Old 05-11-2013, 07:22 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Top has some issues with the first iteration figures produced as noted in the manpage. Can be got around, or just parse /proc/stat regularly and do the math yourself if you don't want to launch an external process.
 
Old 05-11-2013, 07:31 PM   #6
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,001

Original Poster
Rep: Reputation: 67
Thanks for the suggestions. I'll be reading the man page for top in a bit here.

I have looked at /proc/stat and don't fully understand its contents. Can somone recommend an primer?

I think it's worth noting that I do need some kind of rolling average -- an instantaneous measurement is not particularly useful in determining how busy the computer is. I just need something quicker than one minute. maybe 10 seconds? 15 seconds?
 
Old 05-11-2013, 07:42 PM   #7
davidvnyc
LQ Newbie
 
Registered: May 2013
Posts: 6

Rep: Reputation: 0
How do I Find Out Linux CPU Utilization?
http://www.cyberciti.biz/tips/how-do...ilization.html

Several command tools and example usage.
 
Old 05-11-2013, 08:01 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
"man proc"
Seems you really want the runnable count - you can get that from /proc/stat too. Pick a number you're comfortable with (number of cores plus 50%, whatever), and keep the last 10 to maintain your own rolling average. Adjust numbers to suit.
Shouldn't be too hard.
 
Old 05-13-2013, 09:20 AM   #9
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,158
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
collectl.sourceforge.net maybe. It's what I use when the sar program (from sysstat) package isn't enough.

And let me take this minute to advise you that collectl is not a cure-all for what may be ailing your system.
In fact, you may not like it (most folks don't since it has NO GUI).
It has only 2 modes. (less room for error!)

So, if you're not inclined to kick some tires or look under some hood of the issue or are looking for an easy fix, collectl is not for you.

I'd install it, run it for 24h, stop collecting and examine yesterdays's output for signs.

Have fun!

Last edited by Habitual; 05-13-2013 at 11:37 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find all processes cpu utilization & the cpu processor in linux? vasanth.v999 Linux - Server 7 01-10-2012 09:43 AM
command line cpu utilization ninja master Linux - Software 1 04-24-2010 05:41 PM
Total CPU utilization returned by "top" command sanpnair Linux - Newbie 1 05-15-2009 01:19 PM
vmstat in 10 second interval in next start in next minute johnsonlim026 Linux - Newbie 2 08-23-2008 08:34 AM
Linux desktop GUI tool for CPU utilization and bandwidth utilization karimasif Linux - Software 4 03-03-2008 02:09 PM


All times are GMT -5. The time now is 12:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration