2006-02-18 17:42:12

by Irfan Habib

[permalink] [raw]
Subject: How to find the CPU usage of a process

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


2006-02-18 18:44:50

by be-news06

[permalink] [raw]
Subject: Re: How to find the CPU usage of a process

Irfan Habib <[email protected]> wrote:
> 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

2006-02-18 18:55:17

by Ulrich Drepper

[permalink] [raw]
Subject: Re: How to find the CPU usage of a process

On 2/18/06, Bernd Eckenfels <[email protected]> wrote:
> 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.

2006-02-18 19:27:33

by be-news06

[permalink] [raw]
Subject: Re: How to find the CPU usage of a process

Ulrich Drepper <[email protected]> wrote:
> 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

2006-02-18 20:15:06

by Ulrich Drepper

[permalink] [raw]
Subject: Re: How to find the CPU usage of a process

On 2/18/06, Bernd Eckenfels <[email protected]> wrote:
> 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.