Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Dramatically reduce the CPU usage of 'top' UNIX
I always use the venerable top program in the Terminal to see which program is hogging the CPU. Unfortunately, most of the time it is top itself. Even on a fast 1 GHz dual-processor G4, top can take 20% of the CPU time. On FreeBSD, this is often less than 0.1%. I thought Apple would fix this in subsequent releases of Mac OS X, but I was wrong. It is probably something deeply rooted in the Darwin memory management core that prevents top from being fast, lean and mean.

I am mostly interested in CPU hogs, not in memory usage. Also I always sort on CPU usage, the default behaviour on other OSes. So I made a little shell script to make top use 5 to 10 times fewer CPU cycles. It basically removes all the memory checks from top, so you cannot use it to see how much memory a program occupies, or how much RAM you have left. Use the plain old top for that. This is what I use:
/usr/bin/top -ocpu -R -F -s 2 -n30
It sorts on CPU, it doesn't 'traverse the memory object map,' (a huge time saver), it doesn't calculate library statistics, it only redraws every other second and shows only the first 30 programs. You can put this line in a script called top in your ~/bin/ directory, or make it into an alias. Don't forget to rehash to make it work!

[robg adds: I created it as an alias (in tcsh) by adding alias ttop 'top -ocpu -R -F -s 2 -n30' (ttop can be whatever you like, of course) to my .tcshrc file, so I can run both versions easily. Even on my Dual G5, top is a CPU hog -- it takes about 10% - 15% of the CPU; using these command line switches, it's down around 1%.]
    •    
  • Currently 3.40 / 5
  You rated: 5 / 5 (5 votes cast)
 
[43,891 views]  

Dramatically reduce the CPU usage of 'top' | 41 comments | Create New Account
Click here to return to the 'Dramatically reduce the CPU usage of 'top'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Dramatically reduce the CPU usage of 'top'
Authored by: Anidel on Feb 20, '04 11:00:32AM

If you have bash, simply edit your .bashrc or .bash_profile (for differences between them just type 'info bash' in your terminal) and add this line:

alias top='/usr/bin/top -ocpu -R -F -s 2 -n30'

---
Anidel



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: skimmilk on Feb 20, '04 11:28:39AM

Top is NOT a CPU hog. However, when it is polling your system for information, it "activates" and spends CPU power for a very short time and it reports itself since it is active. However, over the course of say 30 secs, the consumption is very small.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: lolajl on Feb 20, '04 04:39:25PM

I entered the tcshrc version into my .tcshrc. When I went into terminal and typed in ttop, this is what I got:

[code]tcsh: ttop: Command not found.[/code]

???



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: yellow on Feb 20, '04 08:58:36PM

Did you source your .tcshrc after adding this alias?

source ~/.tcshrc



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: yellow on Feb 20, '04 09:01:24PM

IMO, you should not alias already existing commands/binaries.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: outofcontrol on Feb 20, '04 10:19:08PM

If you have bash you do as above, but I prefer to put a different name as Rob suggested:

alias ttop='top -ocpu -R -F -s 2 -n30'

Keeping in the habit of using a slightly different name can save you some frustration when not on your own machine.

Don't forget to run:

source ~/.bashrc

afterwards to allow you to use your new alias.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: tpriest@mac.com on Feb 23, '04 06:39:25AM

Hello! Just use the -d and -s switches. Usind the -d -s5 switches will reduce the load to ~1%

tracker_oz



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: seven5 on Feb 20, '04 11:13:44AM

wow, great hint...



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: musox on Feb 20, '04 11:17:00AM

Perfect! My top % usage went from 22% to 3.3%. Thanks!

- MusOX

---
http://musox.com



[ Reply to This | # ]
Just use top -d
Authored by: hargreae on Feb 20, '04 11:21:03PM

If you just use top -d you'll also see top running at only 2-4% of the CPU.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: inertia186 on Feb 20, '04 11:19:59AM
My favorite top parameters came from the Jaguar man pages, but it looks like this exact example has been removed from the Panther version: top -s 5 -u 20

It means update every 5 seconds, sort by CPU utilization, show 20.

[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: jschank on Feb 20, '04 12:42:31PM

I thought I'd check Activity Monitor to see if it was as bad. Apparently, Activity Monitor performs better than the default top, but worse than the optimized call being discussed here. Perhaps folks would be interested in using AM instead of top?



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: ether on Feb 20, '04 02:20:13PM

I'd alwas thought, as a previous poster noted, that top overreports itself because it's active when it's looking for active processes. So does ActivityMonitor.

Interestingly, however, AM shows top above the 10% level, but top shows AM at about half that. Moreover, top and AM basically agree on their own and each other's usage.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: oscillik on Feb 20, '04 12:51:13PM

according to the iPulse documentation (an app that monitors system resources, cpu load, etc) a process that monitors itself is notoriously inaccurate

anyone else care to comment on that?

---
music is no longer considered an artform :(



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: mpiatek on Feb 20, '04 02:02:06PM

I'm the author of another system monitor application called XRG. In the latest version, I added the ability to control-click on the CPU graph and it will show you the 5 processes using the most CPU time. I have found that more often than not, XRG will display itself as using much more CPU time than it is really using (10-15% instead of 3-4% shown by top and Activity Monitor). I believe this is caused by the code module I use to get the process CPU usage statistics. Every time the menu is activated, it takes some time to grab CPU usages and that skews the results that I get for the XRG app itself. I haven't been able to come across a way to avoid this issue, aside from spending extra CPU time always by constantly keeping an updated list of process CPU usage.

Anyway, my two cents,
Mike Piatek-Jimenez

[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: huzzam on Feb 20, '04 04:24:51PM

...a process that monitors itself is notoriously inaccurate

Isn't that the basis of quantum mechanics? I guess that means quantum computing is here!



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: ChiefTypist on Feb 21, '04 01:58:19AM

I'm the author of iPulse and the quote mentioned in the parent post...

The problem is that top (and other monitoring applications) sample the CPU usage during the time that it is scheduled to consume CPU and, as such, it will always sample itself at a higher usage than normal and everything else at a slightly lower usage.

Any calls to host_processor_info(), task_info(), thread_info() are very likely take snapshots at a point in time where usage is artificially high.

If you want to get an idea of how much CPU usage an application like top, iPulse, or Activity Monitor is using, you should use another application to sample with.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: deleted_user18 on Feb 21, '04 04:34:23AM

But why is this different on Linux? Wheter I look at a Dual-Xeon or an old Pentium III server I always get a load for top of less than 2%.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: avarame on Feb 21, '04 05:23:30PM

I just ran two copies of top, and compared their monitoring of each other. One was the modified version mentioned here, the other was plain-jane /usr/bin/top. /usr/bin/top and 'ttop' agreed within a couple of percent on CPU usage by /usr/bin/top. The differences went both positive and negative (that is, there was no bias by either copy of top in either direction).

In any case, average top CPU usage has gone down from 15-20% to 2-5% on a 700Mhz G3! Thanks so much!!



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: Ezekiel on Feb 20, '04 02:00:04PM

Yeah I heard that too... if top shows top using 20% of the CPU, that's just not true. It just can't measure itself correctly. I mean, have any of you noticed any slowdowns at all of the rest of the system when top is running? Do 3D renders go slower? Filters in PS? I hardly think so.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: roncross@cox.net on Feb 20, '04 03:33:28PM

I am reading and understanding the following from this post. Top is not really a CPU hog, but that the value that is being reported is very inaccurate. If this is the case, then it would seem reasonable that the next logical step would be to modify top in such a way as to give itself an accurate reading.

I have also used top to monitor my processes and I never see a slow down in speed based on the reading that I get from top even though it usually reads above 10%.

thx
RLC

---
rlc



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: mpiatek on Feb 20, '04 03:50:29PM

I don't think top's reporting of it's own CPU usage is off by much. If you open Activity Monitor and check out the top process (after selecting All Processes in the drop-down), it reports a CPU % for top within a couple of percent from what top says itself is taking.

Mike



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: jakacmar on Feb 20, '04 06:07:03PM

Another way to check it, open two Terminal windows, run the "new" top in one and regular top in the other. The "new" top will tell you that the regular top is indeed taking A LOT of CPU usage (15-20% on my 800Mhz G4 iMac) compared to the "new" top (hasn't reached 4% once).



[ Reply to This | # ]
Load Avg using top command
Authored by: roncross@cox.net on Feb 20, '04 03:52:55PM

I believe that a better indicator of how hard the CPU is working is the load Avg in the top command. If the load average is less than 4, then you are probably not consuming a lot of the CPU resouces and programs do not have to wait to have access to the CPU. If the load average is between 4 and 10, then the system is overburden and programs may have to wait to gain access to the CPU. If the load average is above 10, then you are probably consuming excessive CPU resources and programs may be denied having access to the CPU.

For example on my computer right now. the Load averages are .50, .38, .29 I rarely see it ever go above 1 so this mean that are my programs have access to the CPU without having to fight over resources.

thx
RLC

---
rlc



[ Reply to This | # ]
Load Avg using top command
Authored by: adrianm on Feb 21, '04 03:12:02AM

That means that you don't use your Mac for anything :)



[ Reply to This | # ]
Load Avg using top command
Authored by: roncross@cox.net on Feb 21, '04 05:17:37PM

My loan average is now 1.54, 1.73, 1.94. It does change depending on what I am doing. My only point is that a lot of discussion is on top to determine %CPU usage when all I want to say is that load average is a better estimation of you %CPU usage. %CPU for top is pointless as most other people on this site have pointed out already.

thx
Ronald Crss

---
rlc



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: 47ronin on Feb 20, '04 03:56:27PM

Because plain "top" has to refresh as fast as it can, of course the CPU is hogged. If you use even top -s2 the cpu usage drops dramatically. Personally I just use top -us2 and that lowers the apparent cpu usage a lot.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: neill on Feb 20, '04 04:00:43PM

Sort of off this topic a bit . . . but since robg talked about aliasing it in his .tcshrc file it's sort of on topic.

Back in Jaguar you could put aliases in a file called aliases.mine that was stored in ~/library/init/tcsh.

I'm assuming that since the shell changed in Panther the .tcshrc file is the new alias location . . . does it go in the same place or someplace else? I've got a couple aliases that I've never recreated since I haven't gotten around to getting non-conflicting advice about where they need to go now.


same



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: derrickbass on Feb 20, '04 05:39:45PM

Actually no, top is not refreshing as fast as it can. From the top(1) man page: "The default delay between updates is 1 second." (You can easily verify this is true by just running top while looking at the clock.) Moreover, as has been noted, on virtually every other operating system top takes less than 1% (and usually around .1%) of the CPU. There is definitely something strange about Mac OS X and its top command.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: Mikey-San on Feb 22, '04 12:29:57AM

What's odd is that this command, in Mac OS X:

top -d

Consumes 3% or less of the CPU and updates just as frequently.

Ideas? Anyone?



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: Frederico on Feb 20, '04 04:53:06PM
For those who don't like playing in *NIX files:
    tell application "Terminal" to do script "/usr/bin/top -ocpu -R -F -s 2 -n30"
Save it as a compiled app for your Dock, or, better, a script in your ScriptMenu; even better, assign a keystroke to it.

[ Reply to This | # ]
Faster sampling?
Authored by: hysterion on Feb 20, '04 06:59:43PM
In Jaguar, top could sample faster than default -- e.g., 5 times per second with
% top -s .2
(Useful to figure out how what short-lived children a process may spawn.) Is there a way to do this in Panther? The same command now gives
$ top -s .2
top: Invalid argument: -s .2


[ Reply to This | # ]
Faster sampling?
Authored by: randalla on Feb 22, '04 04:23:31AM

top -d -s0

much fun :)



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: robleach on Feb 21, '04 01:31:08AM
robg says that he aliased top to ttop so he can use both commands easily. On the contrary, I think this is cumbersome. You have to memorize new names for commands. You can use the same name and still be able to run it without the alias just as easily. All you have to do is escape the command. You do this simply by putting a backslash in from of the command. Doing this causes aliases to be ignored. Observe:


[dantooine:~] robleach% which ls
ls:      aliased to ls -F
[dantooine:~] robleach% which \ls
/bin/ls
[dantooine:~] robleach% ls
Applications/                   Library/                        School/
...
[dantooine:~] robleach% \ls<BR>
Applications                    Library                         School
...

I only make other names for commands when I like to occassionally run the commands in different ways. For example, my .aliases file contains:


alias ls "ls -F"
alias l "ls -alF"
alias ld 'ls -Fa | grep "/"'

While I'm talking about aliases, here's another tip. Use \!* when you want to insert arguments supplied to an alias in a specific place. For example, I have this alias for my xemacs command:


alias xe 'xemacs \!* &'

This allows me to make a call like `xe myfile.txt` and the alias puts the "run in the background character: '&'" at the end of the command.

I suppose this is probably all stuff a lot of you already know. If so, give me a break. I've been couped up in my apartment too long with a sinus and ear infection. I'm going stir crazy. :o)

Rob

[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: Graff on Feb 21, '04 03:11:26AM

One big potential problem with doing this is what if a script or application tries to use the command without using its entire path. Yeah yeah I know you should always use the entire path of a command in a script or application but sometimes programmers take shortcuts.

Since you have modified the default command there is now a huge potential for disaster. The application will call your alias instead of the default command and will use your customizations in addition to any that it adds. Depending on the circumstances this could cause an innocent script which cleans up files to go amok and erase something unintended. It's probably unlikely but why take the risk.

When dealing with the command line it is almost always better to be as safe as possible. Your best bet is to bite the bullet and make an alias that is similar, but not the same as an existing command. Do something like repeat the first or last letter of the command and be consistent with it, that way you only need to remember that simple rule to get to all of your common alternate commands. Something like ttop is not that hard to remember especially if you also have other alternates like lls, mmv, or rrm.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: adrianm on Feb 21, '04 03:23:42AM

True, but generally, shell scripts should be writtin in the basic /bin/sh (bourne shell) as this is the only shell pretty much always going to be there and a lot less susceptible to modification from the global environment.

So if you but #!/bin/sh at the top of your script, you won't be picking up any aliases.

Also, aliases in your .bashrc, .zshrc (for bash/zsh) won't be available in scripts as they only apply to interactive shells.

.cshrc and csh/tcsh - well, if you use that shell, you deserve everything you get.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: FenrisUlf on Feb 22, '04 02:13:42AM

True, writing shell scripts, you should use /bin/sh

...
(not that you were trying to incite yet another shell war....)

However for an interactive shell, it doesn't matter which one you use.... some people prefer csh/tcsh.... If you don't program in the shell, there's nothing saying you can't use it. (since tcsh has just as much usefulness as any other shell in interactive mode.)

... if you use Windows, you deserve what you get.


---
---
Who are you that walk across the graves of giants at this late hour?



[ Reply to This | # ]
nice is nice
Authored by: ylee on Feb 22, '04 12:47:29AM
Why not just do this the true Unix way, and use
nice
? Like so:
$ nice -n 19 top -s 5 -c cpu
The above runs top at nice level 19 (the lowest priority), updates the screen every 5 seconds, and sorts by cpu usage (the default of sorting by pid is, like, bizarre.)

[ Reply to This | # ]
nice is nice
Authored by: Graff on Feb 23, '04 02:02:23AM

I think you meant something like this:

sudo nice -n 19 top -s 5 -o cpu

The -c option is not the sorting option, -o is.

The thing is that the nice level has very little to do with the improved performance of top. It's actually using less cpu because it is sampling less often (the "-s 5" option means sample every 5 seconds). If you remove the nice command you still use about the same amount of cpu:

top -s 5 -o cpu

On my machine both the nice version and the non-nice version used about 1.7% cpu, no matter what nice level I used when I tried it.

When I remove the "-s 5" option then top takes about 9%, no matter what nice level I use.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: macubergeek on Feb 23, '04 05:30:13AM

Nice hint! Very nice!



[ Reply to This | # ]
Alternative 'top' switch.
Authored by: IIR on Feb 23, '04 11:41:20AM

Conversely, 'top -d' which doesn't display extended memory statistics and uses less CPU time, is easier to remember.



[ Reply to This | # ]