-   Programming (
-   -   How to record a process's CPU usage and dump it out?? (

neyshule 10-28-2011 02:08 PM

How to record a process's CPU usage and dump it out??
Hi all:
I want to monitor the CPU usage of a process which running for one hour, I know top can show all the stuff on screen but I want to record the usage in a file so that it can be used later, is there any tools that can achieves this??

deloquencia 10-28-2011 02:39 PM

Please check the manual page, but I already checked, cause it sounded interesting - and, yes it's possible with top.

top -b -n <x> -p <p>, where x is the iteration value and p is the PID(s) to monitor.

You'll have at each iteration step tops default output, you can configure the output with .toprc to suit your needs or you can go along with grep, sed or (g)awk to post-process the output.

For example...

top -b -n <x> -p <pid> >> pid-monitoring
grep <pid> pid-monitoring | awk -F" " '{ print $9 }' >> pid.monitoring.csv

In default top's configuration the 9th col is the CPU usage. Please check with your config to get the right col for (g)awk. So you get a file for further post-processing with a spreadsheed application for example, guess thats what you're about to doing - charting CPU usage, do you?

Hope that helps you... :)

PS: I like the idea of monitoring processes and generating charts, it'll get on my scratch list for future spare-time coding projects :)

syg00 10-28-2011 07:15 PM

Have a look at collectl - it produces good stats per process. Mark has posted several how-to's, with luck he'll drop in on this thread.

All times are GMT -5. The time now is 05:02 PM.