LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-25-2005, 01:12 AM   #1
happy78
Member
 
Registered: Aug 2003
Posts: 50

Rep: Reputation: 15
find remaining percentage of cpu


Hi guys,

I'm trying to find out the remaining percentage of cpu. How do I do that?
I just want the cpu's status, and nothing else

I have tried vmstat, free. But that didn't give me the result I want.
 
Old 09-25-2005, 02:45 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Do you mean the CPU idle time? Try top, the line beginning with CPU, the entry named "id".
 
Old 09-25-2005, 04:10 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,493

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Can you elaborate on why vmstat doesn't give you the result you want ?
 
Old 09-26-2005, 03:50 PM   #4
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Thank you for responding.

vmstat just return this:

procs memory swap io system cpu
r b swpd free buff cache si so bi bo in cs us sy wa id
1 0 35000 1644948 411968 1127824 0 0 1 1 0 0 0 0 0 0


I know top can retrieve cpu's information, but it also returns other stuffs like:
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND

I just want to %CPU. Is there any combination of command to get the %CPU?
 
Old 09-26-2005, 04:39 PM   #5
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
when I do vmstat I get this output
Code:
shane@mainbox ~ $ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
  r          b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 1  0      0 208000  16904 128524    0    0     4     3 1104   899 10  5 85  0
the above code is kind of missallighned. I think idle is 85 or 85%

this code will get it
Code:
my_array=($(vmstat)
echo ${my_array[36]}
I am not sure why your cpu usage according to vmstat is 0

Last edited by shanenin; 09-26-2005 at 04:41 PM.
 
Old 09-26-2005, 05:03 PM   #6
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
I was wasting all kinds of time trying to put the output of top inot one big string then parsing the info. You can do it with the same technicqe I used above
Code:
my_array=($(top -n 1))
echo ${my_array[30]}
 
Old 09-26-2005, 05:45 PM   #7
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
I followed your suggestion and keep getting 0

procs memory swap io system cpu
r b swpd free buff cache si so bi bo in cs us sy wa id
0 0 35000 1833020 411376 1156680 0 0 1 1 0 0 0 0 0 0

at position 36, it is 0. It's correct, but I dont' know if that is the value of cpu's idel.
 
Old 09-26-2005, 05:49 PM   #8
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
did you try my same method(above) using top, that should work for you.
 
Old 09-26-2005, 05:54 PM   #9
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
thank you for a very quick reply.
I just followed the top example, but it returned this: irq
 
Old 09-26-2005, 06:01 PM   #10
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
look when I enter this command
Code:
mainbox shane # top -n 1

top - 17:55:29 up 10:00,  3 users,  load average: 0.24, 0.18, 0.09
Tasks:  61 total,   1 running,  60 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.3% us,  4.5% sy,  4.9% ni, 85.9% id,  0.1% wa,  0.2% hi,  0.1% si
Mem:    514760k total,   273636k used,   241124k free,    19896k buffers
Swap:   506036k total,        0k used,   506036k free,   133936k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
10091 shane     15   0 60972  32m  15m S  2.0  6.4   3:24.41 python
    1 root      16   0  1468  500  436 S  0.0  0.1   0:00.35 init
**** I cut some off****
Code:
mainbox shane # my_array=($(top -n 1))
mainbox shane # echo ${my_array[30]}
85.9%
If I count from left to right(spaces seperate my elements) starting with 0, the 85.9% is the 30th element. what does your top -n 1 look like?

Last edited by shanenin; 09-26-2005 at 06:02 PM.
 
Old 09-26-2005, 07:32 PM   #11
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Talking

yes, that's what I'm looking for...thank you very much for your help...
 
Old 09-26-2005, 08:02 PM   #12
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
your welcome :-)
 
  


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
where to find a cheap dual CPU? microsoft/linux General 10 10-18-2005 09:16 PM
Drake 10.1 find goes forever 100%cpu kristi Linux - General 4 02-08-2005 05:15 PM
where do i find via/s3g cl266 to my cpu lillen Linux - Hardware 0 01-24-2004 08:39 AM
how to find CPU and IO bound processes tuxrhl Linux - General 0 11-22-2003 01:11 AM
What is the percentage of each distro in use ? newlin Linux - General 1 08-03-2003 04:41 PM


All times are GMT -5. The time now is 05:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration