Operating System - HP-UX
1831635 Members
1714 Online
110027 Solutions
New Discussion

Re: Use of library pstat.h

 
SOLVED
Go to solution
Jupiracy
Occasional Advisor

Use of library pstat.h

I would like to know as I do to discover the % of CPU IDLE using functions of the library pstat.h
5 REPLIES 5
Scott Van Kalken
Esteemed Contributor

Re: Use of library pstat.h

if you have a look at /usr/include/pstat.h you've got psp_idlecycles, but I think that's the closest you'll get.

You may be able to tell from the pst_status structure. There's a thing called pst_cpu and another called pst_pctcpu

int32_t pst_cpu; /* processor utilization for scheduling */


float pst_pctcpu; /* %cpu for this process during p_time */


although these may only be for per process information.

Hope this helps

Scott.

Kirby Collins
Advisor

Re: Use of library pstat.h

you can get this data from the psp_cputime array returned by pstat_getprocessor.

Here's a simple example (I hacked this up from one of my existing tools, so pardon any sloppy coding):

/*
* Description:
*
* program to collect cpu state data during
* execution of a command
*
* Usage:
*
* prstats [-l] [-i int]
*
*/
#define MAXCPU 128
#define MAXSTATES 5
char *cpstates[9] = {"USER","NICE","SYS","IDLE","WAIT","BLOCK","SWAIT","INTR","SSYS"};

#include
#include
#include

#include
#include

main(argc,argv)
int argc;
char * argv[];
{
int i,j,n;
pid_t child;
char * ProgName;
int wstat;

struct pst_dynamic psd,psdinit;
struct pst_processor psptot,pspinit[MAXCPU],psp[MAXCPU];
long cpucycles[PST_MAX_CPUSTATES],cycles[PST_MAX_CPUSTATES],totcycles;

if (argc < 2) {
fprintf(stderr, "usage: stats \n");
exit(1);
}
/* get initial per processor data */
if(pstat_getprocessor(&pspinit[0], sizeof(pspinit[0]), (size_t)MAXCPU, 0) <0) {
perror("pstat_getprocessor");
exit(-1);
}

ProgName = argv[0];

if ((child = fork()) == -1) {
fprintf(stderr, "%s: unable to fork child: %s\n",
ProgName, strerror(errno));
exit(1);
}

if (child == 0) {
/*
* Child: execute command.
*/
if (execvp(argv[1], &argv[1]) == -1) {
fprintf(stderr, "%s: unable to execv %s: %s\n",
ProgName, argv[1], strerror(errno));
exit(1);
}
}
else {
while ((waitpid(child, &wstat, 0) == -1) && (errno == EINTR))
;
/*collect final system stats*/
if(pstat_getprocessor(&psp[0], sizeof(psp[0]), (size_t)MAXCPU, 0) <0) {
perror("pstat_getprocessor");
exit(-1);
}
if(pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) <0) {
perror("pstat_getdynamic");
exit(-1);
}
printf("cpu ");
for(i=0;i printf("\n");
for(i=0;i for(n=0;n printf("%3d ",n);
totcycles = 0;
for(i=0;i cycles[i] = psp[n].psp_cpu_time[i] - pspinit[n].psp_cpu_time[i];
cpucycles[i] += cycles[i];
totcycles += cycles[i];
}
for(i=0;i printf("%5.1f ",100.0*(float)cycles[i]/(float)totcycles);
}
printf("\n");
}

printf("tot cpu");
for(i=0;i totcycles += cpucycles[i];
}
for(i=0;i printf("%5.1f ",100.0*(float)cpucycles[i]/(float)totcycles);
}
printf("\n");
}

exit(0);
}

kiara [182] cc prstats.c
kiara [183] a.out sleep 1
cpu USER NICE SYS IDLE WAIT
0 0.0 0.0 0.0 100.0 0.0
1 0.0 0.0 0.0 100.0 0.0
2 0.0 0.0 0.0 100.0 0.0
3 0.0 0.0 0.0 100.0 0.0
4 0.0 0.0 0.0 100.0 0.0
5 0.0 0.0 0.0 100.0 0.0
6 0.0 0.0 0.0 100.0 0.0
7 0.0 0.0 0.0 100.0 0.0
8 0.0 0.0 0.0 100.0 0.0
9 0.0 0.0 0.0 100.0 0.0
10 0.0 0.0 0.0 100.0 0.0
11 1.0 0.0 0.0 99.0 0.0
12 1.0 0.0 0.0 98.0 1.0
13 0.0 0.0 0.0 100.0 0.0
14 0.0 0.0 0.0 100.0 0.0
15 0.0 0.0 0.0 100.0 0.0
tot cpu 0.1 0.0 0.0 93.9 0.1




Kirby Collins
Advisor
Solution

Re: Use of library pstat.h

well, let's try this again. You may have already noticed the (ahem, rather embarassing) bug where I neglected to reset totcycles to zero before using it to accumulate the cpucycles for all the cpus.

I'll try adding the code as an attachment - perhaps it will be more readable that way.

Oh, and this code carries no warranty of any kind...$-).
Jupiracy
Occasional Advisor

Re: Use of library pstat.h

Thank you Collins, only more a question. As I do to know the order of PST_MAX_CPUSTATES for the function pstat_getprocessor. I didn't find a documentation for this structure. Like you knows that the first item is for USER, the second for NICE.......
Kirby Collins
Advisor

Re: Use of library pstat.h

Unfortunately, I don't believe that the pstat data structures are documented, beyond the comments in the header files. In general I think these are internal interfaces, and we would expect customers to use the higher level tools like sar and glance.