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

Record CPU usage for a given process over time UNIX
For an upcoming project involving virtualization applications, I wanted an easy way to record the CPU usage for a given program over time.

I'm aware of the Instruments program included in the Xcode Developer Tools, but I was looking for something simple, that I could run without a lot of overhead, and that would just record data for further analysis in Excel.

After a bit of digging with Google, I didn't find anything that quite worked (a friend tells me that mrtg and Cacti should handle this; I haven't tried them yet), so I created a relatively simple bash script to get the job done - special thanks to Keith Bradnam for assistance in making it more interactive.

The program reports on a chosen program (process ID, actually) at a specified interval, then saves a few items (timestamp, process ID, CPU usage, and command name) to a text file, in this format:
20:20:39 29916  15.7 firefox-bin
20:20:41 29916   4.0 firefox-bin
20:20:43 29916  48.6 firefox-bin
The program does not create pretty graphs, or do any sort of analysis -- that part is up to you! However, with the data, you can do some interesting things. For instance, the graph above (click it for a much larger version) shows Firefox's CPU usage while browsing and playing a few Flash games on my MacBook Pro for ten minutes or so.

Read on for the details...



Here's the code I created:
#!/bin/bash
# Usage: cputrack [PID#] [filename]

filepath=/Users/your_user/Desktop   # modify as desired
interval=20                         # reports per minute
timelimit=6000                      # how long to run, in seconds

mydate=`date "+%H:%M:%S"`           # the timestamp
freq=$((60/$interval))              # for sleep function

while [ "$SECONDS" -le "$timelimit" ] ; do
  ps -p$1 -opid -opcpu -ocomm -c | grep $1 | sed "s/^/$mydate /" >> $filepath/$2.txt
  sleep $freq
  mydate=`date "+%H:%M:%S"`
done
(As I expect I'll be modifying this script over time, I've also posted it on gist.github; that's where the newest version will always reside.)

To use, copy and paste the code into a pure text editor, and change the variables to your liking. Then save it somewhere on your path, make it executable (chmod a+x cputrack), and then call it with both the process ID you wish to track and the base name (not including the extension) of the file you'd like it to create: cputrack 123 cpu_someapp.

If you have suggestions for improvements, please post them -- I know there's a lot more that can be done for error checking, taking user input, etc., but I wanted something simple and fast, and this works for my needs.
    •    
  • Currently 2.84 / 5
  You rated: 2 / 5 (19 votes cast)
 
[14,008 views]  

Record CPU usage for a given process over time | 12 comments | Create New Account
Click here to return to the 'Record CPU usage for a given process over time' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Record CPU usage for a given process over time
Authored by: mario_grgic on Oct 29, '09 08:28:20AM

Doesn't BigTop (included with developer tools) show nice graphs for CPU, memory, disk usage etc, and I believe it also allows you to save the graphs.

It already calculates basic stats, but I don't know if that is enough for you.



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: robg on Oct 29, '09 09:40:03AM

I hadn't run Big Top before; it's interesting. I need to see if it will run in a "collect only" fashion and let me get to the data -- I don't really want the overhead of real time graphing and a whole GUI.

Thanks for the pointer!

-rob.



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: mario_grgic on Oct 29, '09 04:40:54PM

It sounds like you really want Instruments/Activity Monitor Instrument

this allows you to record same things that activity monitor shows, and save it, or export as comma separated values list that you can open directly in Excel or other tools.



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: robg on Oct 30, '09 04:10:04AM

When I was testing Instruments, part of my problem with it was the amount of CPU time that it took -- I was seeing ranges from 8% up to 30% for sampling while doing the other stuff.

That seems excessive, especially when I'm trying to give as much CPU as possible to the app I'm measuring.

Probably there are ways to reduce this -- I need to read the Help file for Instruments, it seems (as I can't even see how to export at first glance).

thanks;
-rob.



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: lowbatteries on Oct 29, '09 09:37:41AM

Big Top does only keep graphs of a single process, so that may not be ideal. However, the inspector in Activity Monitor will show the "CPU Time", so if you're comparing two applications running for the same real amount of time, the application with more CPU time obviously uses more CPU cycles.



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: robg on Oct 29, '09 09:56:32AM

I was looking for a way to record the usage, so that I could compare several different programs that may be doing the same task, and see how CPU usage compares.

The problem with all the real time monitors is that you have to keep one eye on them, and you still won't have a historical record when you're done. If there's a way to do that in Activity Monitor, I haven't stumbled onto it yet.

-rob.



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: tattooedpierre on Oct 30, '09 02:13:34AM

Out of interest, how did you graph it once you had collected the data?



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: robg on Oct 30, '09 04:07:35AM

I just imported it into Excel and graphed the time and CPU usage columns, then played with the chart options to make it look acceptable.

-rob.



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: elevensix on Oct 30, '09 04:02:02AM

I was working on a similar script to monitor disk usage and folder sizes on several servers. As in this script I was passing the results to a text file, from du and df, but then I also run a gnuplot script once a week that generates the graphs and uploads them to a company wiki. You can use gnuplot to generate graphs and save them automatically in PNG, GIF, PDF, EPS, etc...



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: klax88 on Oct 31, '09 03:00:47PM

I'm guessing that Hardware Monitor is too intrusive?



[ Reply to This | # ]
Record CPU usage for a given process over time
Authored by: teacy123 on Nov 01, '09 10:15:24PM

Have you looked at dtrace? That's the technology / command-line tool which is underlying Instruments. It can record everything in your system (Instruments is just a glimpse at the more commonly used options) with hardly any overhead. It originally came from Sun and their Solaris OS. Type "man dtrace" for instructions and a URL to an introduction website.



[ Reply to This | # ]
Using SYRUPY to record CPU (and memory) usage for a given process over time
Authored by: jeetsukumaran on Nov 05, '09 05:26:21AM
I've written a utility to do exactly this: http://jeetworks.org/programs/syrupy Basic example usage:
$ syrupy.py program.sh -o1 --opt2=foo -opt3="foo foo" /path/to/foo foo
  PID DATE        TIME     ELAPSED  CPU   MEM    RSS  VSIZE
14634 2008-10-10  20:45:25   00:00  0.0   0.0   2996   6680
14634 2008-10-10  20:45:26   00:01  105   0.2   7804  12592
14634 2008-10-10  20:45:27   00:02  103   0.2   8996  13776
14634 2008-10-10  20:45:28   00:03  103   0.2  10468  15348
14634 2008-10-10  20:45:29   00:04  103   0.3  11412  16396
14634 2008-10-10  20:45:30   00:05  104   0.3  12492  17444
...
You can supply it the command to be invoked, as above, or alternatively specify the PID of an already-running process to track. -- jeet

[ Reply to This | # ]