Operating System - HP-UX
1833707 Members
2319 Online
110063 Solutions
New Discussion

I'd like to know the Idle percentage using an C API.

 
SOLVED
Go to solution
alessandro_37
Advisor

I'd like to know the Idle percentage using an C API.

Dear all, I am tryning to have (with an C API) the current IDLE of the process.

I have an HP-UX 11.0 system with only one Processor (#model 9000/800/L1000-44 )

I'd like to know the Idle percentage using an C API.

LOAD USER NICE SYS IDLE BLOCK SWAIT INTR SSYS
0.03 1.0% 0.0% 0.4% 98.6% 0.0% 0.0% 0.0% 0.0%

I am using this program.

#include
#include
#include
#include
#include
#define MAX_PROCS 128

struct pst_processor pstat_buffer[MAX_PROCS];
struct pst_dynamic pstat_dyn;

int rc;

main()
{
int i, total,tot[PST_MAX_CPUSTATES];
rc=pstat_getprocessor(pstat_buffer,sizeof(struct pst_processor),
MAX_PROCS, 0);
if ( rc == -1 )
perror("pstat_getprocessor"), exit(1);

for (i=0; i {
printf("cpu[%d] 1 min avg runq=%f, cputime=%d\n", i,

pstat_buffer[i].psp_avg_1_min,
pstat_buffer[i].psp_cpu_time[0]);
}
/* Now take a look at cpu time/state totals */

rc=pstat_getdynamic(&pstat_dyn, sizeof(struct pst_dynamic),
1,0);
if ( rc == -1 )
perror("pstat_getdynamic"), exit(1);
printf("Number of cpus: %d\n", pstat_dyn.psd_proc_cnt);

for (i=0;i {
total+=pstat_dyn.psd_cpu_time[i]; /* overall total */

tot[i]+=pstat_dyn.psd_cpu_time[i]; /* total per state */

}

printf("Total CPU time = %d, average per cpu= %d\n", total,
total/pstat_dyn.psd_proc_cnt);
}

Results:

---------------------------------------------------------------
cpu[0] 1 min avg runq=0.030652, cputime=6034482
cpu[1] 1 min avg runq=0.000000, cputime=0
cpu[2] 1 min avg runq=0.000000, cputime=0
cpu[3] 1 min avg runq=0.000000, cputime=0
...
...
cpu[127] 1 min avg runq=0.000000, cputime=0
Number of cpus: 1

Total CPU time = 679720937, average per cpu= 679720937
-----------------------------------------------------------------
My questions is: how can transform 679720937 to have Idle=98.6% ?



#top
LOAD USER NICE SYS IDLE BLOCK SWAIT INTR SSYS
0.03 1.0% 0.0% 0.4% 98.6% 0.0% 0.0% 0.0% 0.0%


Thanks in Advance
Alex

11 REPLIES 11
James Lynch
Valued Contributor

Re: I'd like to know the Idle percentage using an C API.

Alex,

First off you don't need to get info on 128 CPUs when you only have one. You should call pstat_getdynamic() first, then use the pstat_dyn.psd_proc_cnt field as the elemcount for the call to pstat_getprocessor(). You can also use that in place of MAX_PROCS in your for loop.

There are only 5 real CPUSTATES to be concerned with:

CPU_IDLE 0 /* CPU idle state */
CPU_USER 1 /* CPU user state */
CPU_KERNEL 2 /* CPU kernel state */
CPU_WAIT 3 /* CPU wait state */
CPU_SXBRK 4 /* CPU sxbrk state */


To get the idle percentage, take tot[CPU_IDLE] and divide it by total and then multiply by 100:

idle=tot[CPU_IDLE]/total*100.0;

-JL
Wild turkey surprise? I love wild turkey surprise!
alessandro_37
Advisor

Re: I'd like to know the Idle percentage using an C API.

James, thanks for your reply,

so (if i have well understand) MAX_PROCS is the max number of cpu's installed in my system and not the maximum numbers of the processes in the system.

2)
Idle:
idle=tot[CPU_IDLE]/total*100.0;

with my examples which is tot[CPU_IDLE]?
idle=tot[CPU_IDLE]/ 679720937*100.0


3) How cai i get the iformation about the percentage of:
CPU_USER 1 /* CPU user state */
CPU_KERNEL 2 /* CPU kernel state */
CPU_WAIT 3 /* CPU wait state */
CPU_SXBRK 4 /* CPU sxbrk state */

Thanks
Alex




James Lynch
Valued Contributor

Re: I'd like to know the Idle percentage using an C API.

Alex,

1)
MAX_PROCS is the maximun number of processors that can be installed in a given HP-UX system. It is not the number of cpu's installed in my system.

2)
Idle:
tot[0] is the same as tot[CPU_IDLE]. CPU_IDLE is a #define in the header file.
I don't know what that actual value is because you did not print out any of the values for tot[i]. My guess is that it should be 98.6% of 679720937. That would make it 670204844.
idle=tot[0]/ 679720937*100.0


3) To get iformation about the percentage of:
CPU_USER 1 /* CPU user state */
user=tot[1] / 679720937*100.0

CPU_KERNEL 2 /* CPU kernel state */
kernel=tot[2] 679720937*100.0

CPU_WAIT 3 /* CPU wait state */
wait=tot[3] 679720937*100.0

CPU_SXBRK 4 /* CPU sxbrk state */
sxbrk=tot[4] 679720937*100.0

Regards,
JL
Wild turkey surprise? I love wild turkey surprise!
rick jones
Honored Contributor

Re: I'd like to know the Idle percentage using an C API.

If you want to try to validate whatyou get via other means, you might consider a trial version of glance (I think we still do that?) and/or extracting some of the code from netlib.c of netperf, which uses different pstat() data on HP-UX to retrieve the number of idle cycles for each CPU in the system and from that determine the CPU utilization.

http://www.netperf.org/
there is no rest for the wicked yet the virtuous have no pillows
alessandro_37
Advisor

Re: I'd like to know the Idle percentage using an C API.

Thanks James and ric,
but I have not corrispondence.
Considering that:
CPU_IDLE 0 /* CPU idle state */
CPU_USER 1 /* CPU user state */
CPU_KERNEL 2 /* CPU kernel state */
CPU_WAIT 3 /* CPU wait state */
CPU_SXBRK 4 /* CPU sxbrk state */

and that the actual idle on my system is:98.4%
#TOP:
LOAD USER NICE SYS IDLE BLOCK SWAIT INTR SSYS
0.16 0.0% 0.6% 1.0% 98.4% 0.0% 0.0% 0.0% 0.0%


I have extracted:
tot[i]+=pstat_dyn.psd_cpu_time[i]; /* total per state
printf("\n Rate: %d: Value: %f",i,(float)((tot[i]*100)/total));

CPU[0]: Value: 6159393 (6159393/695712580*100.0 = (0.88)
CPU[1]: Value: 6810230: (6810230/695712580*100.0 = (0.97)
CPU[2]: Value: 4240213: (4240213/695712580*100.0 = (0.61)
CPU[3]: Value: 663992273: (663992273/695712580*100.0 = (95.4)
CPU[4]: Value: 14510471: (14510471/695712580*100.0 =2.08)


Total CPU time = 695712580, average per cpu= 695712580

These values not corrisond to the correct idle of the cpu...
Alessandro
James Lynch
Valued Contributor

Re: I'd like to know the Idle percentage using an C API.

Alex,

You need to make a slight addjustment in your calculation if you want to match the idle value that the "top" command displays.
"Top" calculates the idle time as a combination of CPU_IDLE + CPU_WAIT + CPU_SXBRK. Basically, anything that is not CPU_USER and CPU_KERNEL is considered to be idle time.

If you add up your claculated percentages of CPU[0], CPU[3] and CPU[4] you should get the same idle value as the top command, like so, 0.88 + 95.4 + 2.08 = 98.36%
98.36% rounds up to 98.4%

Sorry for the confusion.

JL
Wild turkey surprise? I love wild turkey surprise!
alessandro_37
Advisor

Re: I'd like to know the Idle percentage using an C API.

James,
there is a mistake...
I have executed one very expencive job now to test the correct idle rate.
Please see below...

Idle is only 0.9% (from Top command),but cpu[0]+cpu[3]+cpu[4] is 98.41%.

LOAD USER NICE SYS IDLE BLOCK SWAIT INTR SSYS
1.01 39.8% 0.0% 59.4% 0.9% 0.0% 0.0% 0.0% 0.0%

cpu[0] 1 min avg runq=1.093308, cputime=6180458
Number of cpus: 1

cpu 0: Value: 6180458 (0,88)
cpu 1: Value: 6820988 (0.97)
cpu 2: Value: 4260551 (0,61)
cpu 3: Value: 665064833 (95.43)
cpu 4: Value: 14540073 (2.086)


Total CPU time = 696866903, average per cpu= 696866903

Rate: 98.41%
It is not possible, my Idle (from top command) is 0.9%
James Lynch
Valued Contributor
Solution

Re: I'd like to know the Idle percentage using an C API.

Alex,

Sorry for the delay. I have given you some bad information before and I wanted to make sure that I got it right before I replied.

1) I was wrong in my previous posting when I stated "There are only 5 real CPUSTATES to be concerned with:

CPU_IDLE 0 /* CPU idle state */
CPU_USER 1 /* CPU user state */
CPU_KERNEL 2 /* CPU kernel state */
CPU_WAIT 3 /* CPU wait state */
CPU_SXBRK 4 /* CPU sxbrk state */"

There are actually 9 states and they are defined in /usr/include/sys/dk.h. The correct states are
#define CP_USER 0 /* user mode of USER process */
#define CP_NICE 1 /* user mode of USER process at nice priority */
#define CP_SYS 2 /* kernel mode of USER process */
#define CP_IDLE 3 /* IDLE mode */
#define CP_WAIT 4
#define CP_BLOCK 5 /* time blocked on a spinlock */
#define CP_SWAIT 6 /* time blocked on the kernel semaphore */
#define CP_INTR 7 /* INTERRUPT mode */
#define CP_SSYS 8 /* kernel mode of KERNEL process */

So the IDLE value would be found in pstat_dyn.psd_cpu_time[3] and NOT pstat_dyn.psd_cpu_time[0] like I stated before.

2) I was incorrect when I said:
"Top" calculates the idle time as a combination of CPU_IDLE + CPU_WAIT + CPU_SXBRK.

The value that TOP displays as IDLE is the same derived from pstat_dyn.psd_cpu_time[3].

3) If you want to be able to display the IDLE value like TOP does, then you are going to have to do some more work. When you retrieve the values in the pstat_dyn.psd_cpu_time[] array from pstat_getdynamic(), you are actually getting the cumulative vaules since the last system boot. In order to calculate near instant IDLE percentage you will have to make at least one more call to pstat_getdynamic() and then calulate the delta value since the previous call. See the code below.

Here is a modified version of your program. It will calculate the IDLE percentage rate every 5 seconds.

#include
#include
#include
#include
#include
#define MAX_PROCS 128
#define VALID_CPUSTATES 9

struct pst_processor pstat_buffer[MAX_PROCS];
struct pst_dynamic pstat_dyn;

int rc;
int i, j, total_prev, total_cur, total_delta;
int tot_prev[VALID_CPUSTATES], tot_cur[VALID_CPUSTATES], tot_delta[VALID_CPUSTATES];

main()
{

rc=pstat_getdynamic(&pstat_dyn, sizeof(struct pst_dynamic), 1,0);
if ( rc == -1 )
perror("pstat_getdynamic"), exit(1);
printf("Number of cpus: %d\n", pstat_dyn.psd_proc_cnt);

rc=pstat_getprocessor(pstat_buffer,sizeof(struct pst_processor),
pstat_dyn.psd_proc_cnt, 0);
if ( rc == -1 )
perror("pstat_getprocessor"), exit(1);

for (i=0; i {
printf("cpu[%d] 1 min avg runq=%f, cputime=%d\n", i,
pstat_buffer[i].psp_avg_1_min,
pstat_buffer[i].psp_cpu_time[0]);
}

/* Now take a look at cpu time/state totals, get inital values */
for (i=0;i {
total_prev+=pstat_dyn.psd_cpu_time[i]; /* overall total */
tot_prev[i]=pstat_dyn.psd_cpu_time[i]; /* total per state */
printf("\n Initial: %d: Value: %d",i, tot_prev[i]);
}
while(1) {
printf("\nSleeping 5 seconds\n");
sleep(5);
/* Get current values and calculate the delta*/
rc=pstat_getdynamic(&pstat_dyn,sizeof(struct pst_dynamic), 1,0);
if ( rc == -1 )
perror("pstat_getdynamic"), exit(1);

for (i=0;i {
total_cur+=pstat_dyn.psd_cpu_time[i];
tot_cur[i]=pstat_dyn.psd_cpu_time[i];
tot_delta[i] = tot_cur[i] - tot_prev[i];
tot_prev[i] = tot_cur[i]; /* Reset prev value */
}
total_delta = total_cur - total_prev;
total_prev = total_cur; /* reset prev value */
total_cur=0;

for (i=0;i {
printf("\n Rate[%d] %f",i,(float)((tot_delta[i]*100.00)/total_delta));
}
printf("\n IDLE:%f",(float)((tot_delta[3]*100.00)/total_delta));
}
}
Wild turkey surprise? I love wild turkey surprise!
alessandro_37
Advisor

Re: I'd like to know the Idle percentage using an C API.

Yeah!!! (sorry for delay )
thanks a lot James, now it is running fine.

Greetings
Alessandro
Rajesh K_1
New Member

Re: I'd like to know the Idle percentage using an C API.

Are the CPU values returned here in seconds.
rick jones
Honored Contributor

Re: I'd like to know the Idle percentage using an C API.

FWIW, I don't fully "trust" the different CPU state stuff and in netperf I use the idlecycles statistic.
there is no rest for the wicked yet the virtuous have no pillows