Operating System - HP-UX
1833159 Members
3017 Online
110051 Solutions
New Discussion

calculating busy% for a process in C/C++

 
SOLVED
Go to solution
Tony Young
Occasional Advisor

calculating busy% for a process in C/C++

Here's the short version of my question: How do I calculate cpu busy % for a process over a given interval programmatically?

Now here's the long version:
On most platforms (AIX/Solaris/IRIX/UnixWare/Linux), determining the time a process spent in the cpu over an interval, and dividing it by that interval is sufficient to determine how busy that process was, and is also quite easy. This is also what I tried to do on HP, using pstat_getproc() to get the pst_status structure, and using the pst_cptickstotal field. By determining the number of ticks per second I could determine the cputime over an interval by subtracting the previous pst_cptickstotal value from the current one.
The problem with that is that the values obtained didn't match the total cpu busy values obtained from pstat_dynamic() (pst_dynamic.pst_mp_cpu_time) when the system was under load. I used two methods to load the system. The first was a simple shell script with an inifite loop that just did 'echo here'. The second was using the following C code:
#include
#include
int main(argc, argv)
int argc;
char * argv[];
{ int i; double x; for (i=0;i < atol(argv[1]); i++) x = sin(3.1); }
When under load, the total cpu rates would go up to 100%, but the cpu% for the process would only be around 10-20%.
I looked at top's output and saw that it produces 2 cpu busy fields %CPU and %WCPU (weighted cpu). The %CPU values matched the per process values that I was producing, but the %WCPU values could be matched to the total system cpu rates.
Can anyone explain what this weighted cpu value is? and why there are 2 cpu% values?
The man page for top showed me nothing to explain weighted cpu. So I went to the source. I looked at the source for top 3.5b8 and saw it's algorithm for calculating weighted cpu - still no documentation, but at least the algorithm is there. I built this version of top just to make sure the values it produced matched up with HP's top, only to find that they don't. When the system was loaded, my top was showing around half of what HP's top was showing for %WCPU - a dead end.
Another problem I face is that the only algorithm I've seen so far (ie top's) is based on the pst_pctcpu field in the pst_status structure, which is a busy rate based on process start time. This is no good to me as I need to be able to determine it for a given interval.
I had previously (back in the early days of the project >1yr ago) looked at using the pst_time,pst_utime, and pst_stime fields of the pst_status struct, but their resolution is not fine enough - I need at least microsecond resolution, they have second resolution.

Does anyone know the correct method (or at least the method employed by HP's version of top) for correctly determining a process's cpu busy%. Can anyone explain what weighted cpu percent is and why there are two types of cpu percent values? Can anyone explain some of the various pst_status fields? difference between pst_cpticks and pst_cptickstotal? what actually is pst_pctcpu? Is there any useful documentation around that explains these things in detail?

Any help that anyone can provide is appreciated.
If anyone would like more detail - test source, output listings, etc - just ask.

Thanks.
Tony...
9 REPLIES 9
Gregory Fruth
Esteemed Contributor

Re: calculating busy% for a process in C/C++

Maybe the times(2) function will do what you want. It's
POSIX so it ought to be portable. AFAIK pstat_* is HP-UX
specific. clock(3) may also help, and it's even ANSI.

The units of times() and clock are CLK_TCK and
CLOCKS_PER_SEC, which ought to give you
sub-second granularity.

HTH
Tony Young
Occasional Advisor

Re: calculating busy% for a process in C/C++

My apologies - I should've been clearer is my description. I need to be able to work this out for any/all processes on the system. That is, I need to find out the information based on a PID.
The 2 functions you mentioned only provide information for the current process.
Thankyou, however, for your quick response.
Tony Young
Occasional Advisor

Re: calculating busy% for a process in C/C++

My apologies also about not providing points - I attempted to but kept getting 404 Not Found responses when submitting the points form. I would've given 3.
Laurent Paumier
Trusted Contributor

Re: calculating busy% for a process in C/C++

Maybe you could use pst_cptickstotal (total ticks for life of process), or pst_usercycles, pst_systemcycles and pst_interruptcycles (user mode, system mode and interrupt cycle counts). Never used these, but if they work, the struct has microsecond resolution...
Tony Young
Occasional Advisor

Re: calculating busy% for a process in C/C++

As mentioned I'm already using pst_cptickstotal and the values provided are not correct. I haven't yet looks at the others - I'll see what they give me.
Tony Young
Occasional Advisor

Re: calculating busy% for a process in C/C++

I remember not using those cycles fields before as I couldn't work out how to determine the number of cycles per second. However, in other tasks I found I could extract the iticks_per_tick field from kmem, use that to determine the cpu speed, and can now translate these values based on that.
My initial tests so far show that these fields give me a correct indication of cpubusy. So thanks very much Laurent!
Now, does anyone know a better way of determining the cpu speed programmatically to translate these cycle values?
Tony Young
Occasional Advisor

Re: calculating busy% for a process in C/C++

Is anyone able to explain why these cycle fields provide a correct indication of busy%, whereas the tickstotal and time fields do not?
Laurent Paumier
Trusted Contributor
Solution

Re: calculating busy% for a process in C/C++

I just browsed /usr/include/sys/pstat.h, found a field in the pst_processor struct that looks like what you need : psp_iticksperclktick (interval timer counts (CR16) per clock tick).
Tony Young
Occasional Advisor

Re: calculating busy% for a process in C/C++

Thanks Laurent, I'd just found that field too - I thought there may have been a cleaner way yet.
Is someone able to explain the differences in values obtained between the various fields?