2

Background I've written a tool to capture CPU usage on a per/thread basis. The output of the tools is a binary file, that I can pump into my parsing utility that I wrote. And the output of the parsing utility is a CSV file that I can import into Excel to chart pretty graphs of process/thread CPU usage.

This CPU usage capture tool is running on an embedded ARM platform running a Linux kernel based on 2.6.35.3. That being said, I was concerned about making the tool light weight. I didn't want it to store directly to a CSV file, in order to minimize the processing time and the file size of the captured data.

Question The tool works, but I'm wondering if I took the long way around the problem? Is there already a tool out there that does this (or something like it)?

You're probably wondering why I care if I already made a tool that works. Well, it's not as light weight as I'd like. It's taking up about 10% of CPU usage. As a benchmark, top only takes up about 1% (max).

Update I've decided to continue using my tool for now. At least until a better solution becomes available. I was able to shave off a couple percentage points by using open() instead of fopen() on /proc/stat. I'm also using read() instead of fgets().

rkyser
  • 3,241
  • 1
  • 19
  • 28
  • Unfortunately, the `top` and `ps` I have available present limited information and have few options. I assume this is because they are part of busybox package. – rkyser Mar 27 '12 at 14:45
  • So you implemented your own rusage, but for CPU usage only? –  Mar 28 '12 at 17:56
  • @VladLazarenko I've never used `getrusage()` before, but it looks like you wouldn't be able to get CPU usage for ALL threads on the system. – rkyser Mar 28 '12 at 18:23
  • @rkyser: in Linux, rusage gives out information for the calling thread. You can essentially get the same thing from procfs if you have one, if you mean ALL threads like system-wide. –  Mar 28 '12 at 18:41
  • @VladLazarenko According to this [answer](http://stackoverflow.com/a/939855/651848), reading directly from /proc is fairly conventional. – rkyser Mar 28 '12 at 18:43
  • @rkyser: Doesn't matter. Re-implementing this stats gathering yourself is definitely time not well spent. Whatever you do is not my business. Just letting you know there is such a thing. CPU usage is not enough, you have to know context switches, page faults and other stuff if you want to improve things - it let's you know where to start. –  Mar 28 '12 at 18:46
  • @VladLazarenko: Sorry if I wasn't clear. It's not a replacement for `top`, but a supplement. We are trying to gather the necessary data for displaying graphs similar to [this QNX tool](http://www.qnx.com/developers/docs/6.4.1/ide_en/user_guide/sysprof.html#High_CPU_usage). Our older systems were QNX based, but we are now switching to using more open source tools. Also, we are using `busybox top` which shows a rather limited set of information. In addition to these tools, we'll be using LTTng, OProfile, Valgrind, and others yet to be determined. – rkyser Mar 28 '12 at 19:05

2 Answers2

1

IBM has a tool called nmon which does the same(for AIX & Linux): According to IBM's documentation, it takes ~2% CPU. You may want to look at that.

Comparing nmon with your tool could give you a fair idea about your program's performance and how you may improve your csv capture.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • I've cross-compiled nmon and can run it in interactive mode, but for some reason it exits right away in capture mode. This is the command I'm running: `arm-nmon -F /tmp/nmon.cap -s1 -c600` – rkyser Mar 27 '12 at 15:14
0

This might be a bit of a steep learning curve, but you might want look into SystemTap: http://sourceware.org/systemtap/

Wil Cooley
  • 900
  • 6
  • 18
  • I thought SystemTap was more of a tracing toolkit -- like LTTng. Does this capture the necessary data for showing user & sys CPU usage per thread? – rkyser Mar 28 '12 at 18:07