How to count CPU usage of a multiprocessor application on Linux

I am trying to make a program with C / C ++ to behave like a top command in Linux. I did some research and already knew how to count CPU usage as a process. We can get CPU usage by calculating stime + utime from / proc / [PID] / stat at the current time and after a few seconds. Then calculate the stime + utime differences and split the result with the uptime differences, then we get the percentage of CPU usage. It will be so simple in a single process / multithreading process.

The problem is how httpd is where it works as a multiprocessor. When the web server is busy, httpd will process child processes to serve a bunch of requests. Then I count the number of the total process, say 500. I want to calculate the CPU usage of these processes, but summarize them, so I only see 1 using httpd CPU. But if I execute the algorithm, as I mentioned above, when the number of processes decreases to <500 after a few seconds, I get negative values, since the calculation will be like this (for example, I choose a random number, just to give you a short description):

Uptime: 155123, No of processes : 500, Stime + Utime total of 500 processes : 3887481923874
Uptime: 155545, No of processes : 390, Stime + Utime total of 390 processes : 2887123343874

If you look at the example above, the Stime + Utime delta will result in a negative value, as the number of processes decreases and gives a lower value after a few milliseconds. I just want to know if there is a way to calculate a process like this? Thank.

+5
source share
2 answers

.
, :
1. , - .
2. , - .
3. , - . - , 90% , , .

, .

+1

, , .

:

1) wait4(2) wait3(2) . utime stime .

2) /prox/<pid>/stat.

0

All Articles