Re: How to find the CPU usage of a process

From: Ulrich Drepper
Date: Sat Feb 18 2006 - 13:53:09 EST


On 2/18/06, Bernd Eckenfels <be-news06@xxxxxxxxxxxx> 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.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/