How to count CPU Usage of multiprocess application in Linux How to count CPU Usage of multiprocess application in Linux apache apache

How to count CPU Usage of multiprocess application in Linux


I suggest keeping the data for each process separately.
When you have a new sample, each process may fall in one of three categories:
1. Existed both before and after - subtract old from new.
2. Exists now, but not before - just take the new values.
3. Existed before, but not now - ignore it. You're missing something here, because it may have used CPU during 90% of your sample period, but I hope you don't need perfect accuracy.

It makes you keep more data between samples, and requires using a more complicated data structure, but it should give reasonable results.


If you need accurate result or if life time of processes are short, then you must read time usage of a process when it is terminated.

There is atleast two ways:

1) Use wait4(2) or wait3(2) functions to wait process termination. Those functions will return utime and stime of the process.

2) Keep terminated processes in zombie state until reading of /prox/<pid>/stat.