Skip to contentSkip to conversation

How to find the CPU usage of a process

DISCUSSION Irfan Habib 3 participants 5 messages Feb 18, 2006

lore
Share & tools
lore
#0 lore raw

Hi,

I'm a research student, working on self-organizing grids.
I wanted to ask how can I find the cpu usage of a process, as opposed to runtime, with cpu usage I mean actually how many time slices were awarded to a specific process, like the runtime of job may be 4 s, but this also includes time it was suspended by some interrupt, or had to wait for the scheduler etc..

Thanks in advance

Irfan Habib

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

#1 lore raw
Quoted 4 lines · Irfan Habib

I wanted to ask how can I find the cpu usage of a process, as opposed to runtime, with cpu usage I mean actually how many time slices were awarded to a specific process

It is accounted in seconds in usermode and kernelmode (io processing), not in slicess.

You can use the result wof wait3(2) if you are the parent:

/usr/bin/time -v sleep 3
Command being timed: "sleep 3"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.00 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 0 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 175 Voluntary context switches: 2
Involuntary context switches: 0
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0

You could maybe also use BSD Job Accounting.

At runtime, the /proc interface is for you. See libproc for the data you can query. /usr/include/proc/readroc.h:

    utime,          // stat            user-mode CPU time accumulated by proces 
    stime,          // stat            kernel-mode CPU time accumulated by process

Gruss
Bernd

#2 lore raw
Quoted 2 lines · Bernd Eckenfels

You can use the result wof wait3(2) if you are the parent:
[...]

That's after the fact. Programs which want to get the information while running can use the CPU clocks:

  struct timespec ts;
  if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) != 0)
    fatal("cannot get CPU time");
  /* result is in TS */

It's also possible to get a thread's CPU consumption. Use CLOCK_THREAD_CPUTIME_ID in that case.

All this works as it should ever since Roland's clock patches landed, in early 2.6 kernels. Before that the time returned did not discount the time the process wasn't scheduled.

#3 lore raw
Quoted 7 lines · Ulrich Drepper

That's after the fact. Programs which want to get the information while running can use the CPU clocks:

struct timespec ts;
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) != 0)
fatal("cannot get CPU time");
/* result is in TS */

But thats only for yourself, right?

Gruss
Bernd

#4 lore raw
Quoted 1 line · Bernd Eckenfels

But thats only for yourself, right?

In that abbreviated form, yes.

To get an arbitrary process' time use

  clockid_t cl;
  clock_getcpuclockid(pid, &cl);
  struct timespec ts;
  clock_gettime(cl, &ts);

This is all documented in POSIX.