I try to make a program with C/C++, to behave like top command in Linux. I've done some research and already known how to count CPU Usage of a process. We can get the CPU Usage by calculating stime + utime from /proc/[PID]/stat in current time and after several seconds. Then calculate the stime + utime differences and divide the result with uptime differences to, then we get the CPU Usage percentage. It will be so easy on single process/multithread process.
The problem is in the case like httpd, where it works as multiprocess. When the webserver busy, httpd will fork child processes to serve bunch of requests. Then I count the number of total process, let's say 500. I want to calculate the CPU Usage of those processes, but summarize them so I only see 1 httpd CPU Usage. But if I do the algorithm like I've mentioned above, when the number of processes decrease into < 500 after several seconds, I get the negative values, since the calculation will be like this (for example, I choose random number, just to give you brief 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 the the example above, the delta of Stime + Utime will results in negative value, since the number of process decreasing, and give the lower value after few miliseconds. I just want to know, is there any other way to calculate such process behave like this? Thank you.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With