Monitor one task's CPU usage via top

Nov 16, '04 11:57:00AM

Contributed by: robg

Yesterday, I was trying to figure out the best way to keep an eye on the CPU usage of one process (the Finder in this case). There are obviously a number of ways to do this -- using Activity Monitor was the first one that came to mind, but it's got a pretty large window and I wanted to work on other stuff while watching the Finder's CPU usage.

So then I went to the Terminal and spent a bit of time looking at top's options. top will let you run a given number of samples with the -l option, and if you specify 0 as the sample count, you'll get an infinite number of samples. Combine this with grep to isolate the Finder from top's output, and you get the following command and output, updated every second.

robg$ top -l 0 | grep Finder
  406 Finder       0.0%  0:19.09   1   134   209  7.45M  37.4M  25.0M   267M 
  406 Finder       0.8%  0:19.10   2   139   212  7.46M+ 38.9M+ 25.1M+  268M+
  406 Finder       7.2%  0:19.19   2   145   212  7.48M+ 38.9M+ 25.2M+  268M+
  406 Finder       9.2%  0:19.30   2   142   213  7.48M  39.6M+ 25.7M+  269M+
  406 Finder       6.6%  0:19.38   2   142   211  7.47M- 39.0M- 25.2M-  268M-
^C
So there's one relatively trivial solution -- I then resized the Terminal window to a very small size, and just let that scroll by for a while as I worked.

I still wasn't quite happy, though, as this still took up a fair bit of screen real estate, and I had to be sure to make a new Terminal window when I wanted to use Terminal itself. The image at left is more like what I was after ... read the rest of the hint to see how it was created after some detective work by myself and Kirk McElhearn...

I thought that GeekTool (a previous Pick of the Week selection) might be a perfect answer to this problem, as it can output Terminal commands to floating desktop windows. Ideally, I'd just be able to tell GeekTool to run top -l 1 | grep 'Finder', set that to auto-update every second, and be done with. Unforunately, top -l 1 will always return a 0.0% utilization on the one sample it runs; it's only the second and subsequent samples that return the actual utilization. So I needed a way to ignore the first reply, and focus only on the second.

There were some other issues as well. I didn't want nor need the extraneous memory usage information; I was just interested in CPU usage. And the top command by itself is very CPU intensive, so you'll be loading your CPU if you run the command every second or two. So Kirk and I spent a bit of time this morning trying to put this all together to come up with the ultimate one-line GeekTool single-process monitoring tool. After some trial and error, here's what we wound up with to generate the image you saw above:

top -FR -l2 | grep Finder | grep -v 0.0% | cut -c 7-25
Here's how each piece works: There are, of course, many improvements that could be made -- you don't have to use the Finder as the process (you could even search on process numbers if you wished); you don't have to use a display window in GeekTool (then you could actually place the one line on your menubar, for instance); and there very well may be a more efficient way to do this (anyone?). But this works for me, and helped me keep an eye on the Finder to see what specific tasks were taking the most CPU time.

Comments (16)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041116055933912