LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 02-07-2012, 08:17 AM   #1
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

Rep: Reputation: 55
top command - is there an easier way to do this


I'm rebuilding a part of my conky and have noticed that the built in top command doesn't actually return total cpu usage, but only on a per processor basis, so I'm trying to extract the information that I want using the top command.

I want the top 4 processes, only by name and processor usage, and this is what I have:

Code:
top -b -n 1 | grep -Ev top | head -11 | tail -4 | sed 's/^.* [A-Z] *\([0-9]*\).*\.[0-9]\{2\} \(.*\)/\2 \1 /g'
Now, it does work and provides exactly what I want, but I'm sure there must be a more efficient way of doing it. Any ideas?

Note - the grep part is to exclude the top command itself from the returned results. I know that using top in interactive mode I could get the information from top without all the parsing, but I can't find a way to do it directly without interactive mode.

NB. I'm including conky formatting codes in the substitution which I've stripped out for the purpose of this post.

Last edited by Roken; 02-07-2012 at 08:23 AM.
 
Old 02-07-2012, 09:06 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
top -b -n1 | awk '/^ *[0-9]/ && NR<12 && !/top/{print $12" "$9}'
will print lines like "process name" + space + "cpu usage"

(edit)
Mistake in cpu field number, also noticed that output of top is not sorted
Code:
top -b -n1 | sort -r +9 | awk '/^ *[0-9]/ && !/top/ && NR<12 {print $12" "$10}'

Last edited by Cedrik; 02-07-2012 at 11:09 AM.
 
Old 02-07-2012, 11:27 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
AFAIK, top simply reads from the /proc filesystem, and you can probably find all of the information you want there, without resorting to attempting to parse the output of top. The format of the data in /proc pseudo-files is more likely to remain constant, than will the format of the output of top.

--- rod.
 
Old 02-07-2012, 01:35 PM   #4
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300

Original Poster
Blog Entries: 1

Rep: Reputation: 55
@ Cedrik,

Thanks for the suggestion. awk is something I still need to study, but I did a test and the time for your suggestion to execute is practically identical to the time for mine. Incidentally, your sort command failed with "+9 file not found". I've never used sort so not sure what it does. I simply did the timed tests without the sort.

@theNbomr,

Interesting. I had a look through /proc, and whilst I can find the processes and the names I'm not sure where exactly the cpu usage comes from. Nevertheless, as best as I can see I would have to walk the /proc FS to establish the same information, which would be essentially recreating what top does anyway (probably much less efficiently). It does, however, explain why top is so slow to return the information (0.5 seconds here).
 
Old 02-07-2012, 03:08 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Did you look in /proc/stat?

--- rod.
 
Old 02-07-2012, 03:32 PM   #6
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300

Original Poster
Blog Entries: 1

Rep: Reputation: 55
Code:
$ cat /proc/stat 
cpu  205294 0 33197 561761 19056 42 298 0 0 0
cpu0 49477 0 8304 143543 4715 0 11 0 0 0
cpu1 52399 0 7905 141442 4305 0 10 0 0 0
cpu2 50793 0 7835 143555 3654 2 42 0 0 0
cpu3 52623 0 9152 133218 6381 39 233 0 0 0
intr 5769603 723254 2 0 0 0 0 0 1 1 0 0 0 4 0 0 0 0 81011 368720 0 209 0 500407 1364 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 6996296
btime 1328648224
processes 60017
procs_running 4
procs_blocked 0
softirq 3752772 0 1384899 40 450 25793 0 81172 257002 186861 1816555
I have no idea what to do with that :/
 
Old 02-07-2012, 03:46 PM   #7
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Roken View Post
@ Cedrik,

Thanks for the suggestion. awk is something I still need to study, but I did a test and the time for your suggestion to execute is practically identical to the time for mine. Incidentally, your sort command failed with "+9 file not found". I've never used sort so not sure what it does. I simply did the timed tests without the sort.
I think top command needs a few seconds to compute CPU usage
For the sort command, maybe sort -r -k9 would work...

Also I just see top output more carefully and cpu field number is 9, not 10 (awk should print $9 instead of $10)
 
Old 02-07-2012, 04:38 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Some time perusing manpages would be well spent methinks. Anything (well, most) in /proc is documented in "man proc". Also note the warning in "man top" - under BUGS.
Top output is user controllable - including sort key and column placement. This can make it difficult aligning suggestions to a particular setup.

Personally I've always found awk best for this, but I don't know if conky would like all that shelling out to run separate commands.
 
Old 02-07-2012, 05:37 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by Roken View Post
I have no idea what to do with that :/
To find out:
Code:
man proc
. Here's what you should see:
Code:
       /proc/stat
              kernel/system statistics.  Varies with architecture.  Common entries include:

              cpu  3357 0 4313 1362393
                     The amount of time, measured in units of USER_HZ (1/100ths of a second  on  most  architectures,
                     use  sysconf(_SC_CLK_TCK)  to  obtain the right value), that the system spent in user mode, user
                     mode with low priority (nice), system mode, and the idle task,  respectively.   The  last  value
                     should be USER_HZ times the second entry in the uptime pseudo-file.

                     In  Linux 2.6 this line includes three additional columns: iowait - time waiting for I/O to com‐
                     plete (since 2.5.41); irq - time servicing interrupts (since 2.6.0-test4); softirq -  time  ser‐
                     vicing softirqs (since 2.6.0-test4).

                     Since  Linux  2.6.11, there is an eighth column, steal - stolen time, which is the time spent in
                     other operating systems when running in a virtualized environment

                     Since Linux 2.6.24, there is a ninth column, guest, which is the time spent  running  a  virtual
                     CPU for guest operating systems under the control of the Linux kernel.

              page 5741 1808
                     The number of pages the system paged in and the number that were paged out (from disk).

              swap 1 0
                     The number of swap pages that have been brought in and out.

              intr 1462898
                     This  line  shows counts of interrupts serviced since boot time, for each of the possible system
                     interrupts.  The first column is the total of all interrupts serviced; each subsequent column is
                     the total for a particular interrupt.

              disk_io: (2,0):(31,30,5764,1,2) (3,0):...
                     (major,disk_idx):(noinfo, read_io_ops, blks_read, write_io_ops, blks_written)
                     (Linux 2.4 only)

              ctxt 115315
                     The number of context switches that the system underwent.

              btime 769041601
                     boot time, in seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

              processes 86031
                     Number of forks since boot.

              procs_running 6
                     Number of processes in runnable state.  (Linux 2.5.45 onwards.)

              procs_blocked 2
                     Number of processes blocked waiting for I/O to complete.  (Linux 2.5.45 onwards.)
 
Old 02-07-2012, 06:19 PM   #10
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300

Original Poster
Blog Entries: 1

Rep: Reputation: 55
Ok - perhaps my "I don't know" was misleading. Whilst I can see the value of /proc/stat for identifying overall CPU usage, I can't see a way to separate the usage out to identify an individual task's CPU usage.
 
Old 02-07-2012, 07:18 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Oh, I thought you wanted the total CPU usage.
--- rod.
 
Old 02-07-2012, 07:23 PM   #12
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300

Original Poster
Blog Entries: 1

Rep: Reputation: 55
No. Using conky's built in top function only provides the top process on a "per processor" basis rather than overall CPU usage. For example, I run a server that uses approx 25 - 39% CPU, but conky's built in function only ever reports 8% - 9%. The server is multi threaded, and the conky function only reports usage for one core, rather than total percentage of all cores. Top reports it correctly, hence my desire to use the system top rather than the conky top.

EDIT: I should add that this is probably another one of my computer obsessive behavioural traits (more over at the Arch forums - https://bbs.archlinux.org/viewtopic.php?id=129022) and I use the top readout in conjunction with temperature readouts to make sure that everything is kinda OK. Joys of a self-built system that really needs re-building - personal paranoia.

Last edited by Roken; 02-07-2012 at 07:30 PM.
 
Old 02-08-2012, 10:06 AM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, now (I think) I understand. Sorry for contributing to the confusion.

If you know the PID of the process, it seems that /proc/[pid]/stat provides the aggregate CPU time statistics. In fact, in reading the proc man page, it isn't clear how the individual processor statistics are acquired.

--- rod.
 
Old 02-08-2012, 05:06 PM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
All the data is in there - getting it and doing the math is a pain. Top is easiest, but as I said, I don't know if I'd want to use it on a constant basis shelled out from something like conky.
 
Old 02-08-2012, 06:42 PM   #15
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300

Original Poster
Blog Entries: 1

Rep: Reputation: 55
The problem I have is, using bash (which is how I get the information for conky using top) I can't imagine that I could write a script that would be faster than top when it would have no choice but to walk the top filesystem. If you know a way to speed it up, I'd be happy to be corrected. As it stands, I have conky updating each second (apart from one or two items that I've extended deliberatley, such as my google calendar appointments script which I only run every 30 seconds). I can afford a half second each cycle, though it does on occasion freeze conky longer, usually if I'm doing something else more processor intensive.

I could drop the conky output to each two seconds for example, or 5 seconds, which for purpose I guess would be OK, but then my Computer OCD would kick in again that I'm not getting the update often enough.

Until someone can come up with a faster solution than top (which is the bottleneck it seems, not the grep, not the sed, and not awk) I'm probably stuck with either conky's limited functionality or the .5 second delay.
 
  


Reply



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
LXer: Windows is Easier, Just Like Stabbing Your Own Eyeballs is Easier LXer Syndicated Linux News 1 03-06-2010 03:17 AM
LXer: Can You Top This? 15 Practical Linux Top Command Examples LXer Syndicated Linux News 0 01-09-2010 09:42 PM
Finding the top CPU-using processes? "top" command does not help... catalytical Linux - Server 1 04-06-2009 04:50 PM
top command Volcano Programming 18 10-15-2008 10:02 PM
Top command mtnpaul Linux - Software 2 04-17-2008 04:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 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
Open Source Consulting | Domain Registration