2
votes

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().

2
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
So you implemented your own rusage, but for CPU usage only? - user405725
@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
@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. - user405725
@VladLazarenko According to this answer, reading directly from /proc is fairly conventional. - rkyser

2 Answers

1
votes

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.

0
votes

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