The Haiku/BeOS Tip Server
Tips and tricks for Haiku/BeOS users

Calculating CPU usage

Contributed by: Scot Hacker
This tip is valid for: Both BeOS and Haiku

Pulse is interesting to watch and decent for getting ballpark guesstimates of processor usage, but it isn’t very helpful when you need an accurate reading.

Much better is the command-line utility “top” which will show you resource consumption for individual threads. If you want more detail than is provided by the version of top bundled with BeOS, download an updated top replacement from BeWare. By default, the updated version of top will update its report once per second, which may be too rapid to study properly. If you want a better view, use:

top -d 5

to have top update itself every five seconds.

The problem with top is that it isn’t always easy to figure out exactly how much CPU a given application is consuming, because many apps do their work in worker threads that may be difficult to identify. Fortunately, you can launch an app through the “time” utility. BeOS will calculate the total time the app was running and report the total number of seconds of processor time consumed in system space and in user space. From that, you can calculate the percentage of available CPU consumed by that app.

For example, let’s say you want to see whether CL-Amp or SoundPlay requires more horsepower to play the same MP3 file. Close SoundPlay, then type something like this:

time /path/to/SoundPlay/SoundPlay /path/to/Song.mp3"

As soon as SoundPlay finishes playing the song, close it and you’ll get a report like this:

real    1m42.708s
user    0m9.427s
sys     0m1.769s

To calculate CPU usage, use this formula:

(user + sys)*100 / real = percentage of available CPU time

Note that these figure will be slightly off because launching the app will consume extra CPU time, but it gives you a pretty good idea of what’s going on.

Thanks to Marco Nelissen for pointing out the time utility.

Posted in Miscellaneous

(comments are closed).