Monitoring CPU Process Usage

Duane Attaway dattaway at attaway.net
Sun Nov 10 18:15:02 CST 2002


On Sun, 10 Nov 2002, Scott Bowling wrote:

> I've got a CGI script that I'd like to determine how much CPU usage it uses
> over a period of time... let's say for a week.

cat /proc/<process id>/stat

We can get exactly what you need down to each 1/100th of a second right
from the /proc directory. And the best recepie for this is straight from
the command line, perfect for scripting.  The /proc/stat entry will serve
you these juicy morsels (from the proc manpage:)

       stat   kernel/system  statistics.   Varies  with architec-
              ture.  Common entries include:

              cpu  3357 0 4313 1362393
                     The number of jiffies (1/100ths of a second)
                     that  the  system  spent  in user mode, user
                     mode with low priority (nice), system  mode,
                     and  the  idle task, respectively.  The last
                     value should be 100 times the second entry in the
                     uptime pseudo-file.

Of course, stat will give you more than just those four numbers, but the
man page lists all the values given, in order. The two values you might be
interested in (with the scanf code:)

                     cutime %ld
                             The number of jiffies that this pro-
                             cess  and  its  children  have  been
                             scheduled in user mode.

                     cstime %ld
                             The number of jiffies that this pro-
                             cess  and  its  children  have  been
                             scheduled in kernel mode.




More information about the Kclug mailing list