Off-by-one problem for psd_activeprocs in pstat_getdynamic on 11iv1 ? (425 Views)
Reply
Trusted Contributor
Earl_Crowder
Posts: 76
Registered: ‎07-17-2001
Message 1 of 5 (425 Views)
Accepted Solution

Off-by-one problem for psd_activeprocs in pstat_getdynamic on 11iv1 ?

psd_activeprocs seems to be off-by-one on 11iv1, always returning 1 less than the actual number of active processes.  I can't find a patch that addresses this either.

 

 

#include <stdlib.h>
#include <sys/param.h>
#include <sys/pstat.h>

int main ( void )
{

  struct pst_status * psa = NULL;
  struct pst_status * prc = NULL;
  struct pst_dynamic  psd;
  long                nproc = 0;
  long                thsum = 0;
  long                i,j;

  if ( pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1 )
    (void)perror("pstat_getdynamic failed");

  // Get the number of active processes from pst_dynamic
  // nproc  = psd.psd_activeprocs;
  nproc  = psd.psd_numprocsallocd;
  printf("\nNum Procs Allocd = %d, Active procs = %d , psd_maxprocs = %d\n", psd.psd_numprocsallocd, psd.psd_activeprocs, psd.psd_maxprocs);
  psa    = (struct pst_status *)malloc(nproc * sizeof(struct pst_status));

  // Read the info about the active processes into the array 'psa'
  if ( pstat_getproc(psa, sizeof(struct pst_status), nproc, 0) == -1 )
    (void)perror("pstat_getproc failed");

  (void)printf("\n\n------------------------------------------------------------------------------");
  (void)printf("\n %5s | %5s | %5s |%7s| %5s | %s", "INDEX", "PID", "UID", "Threads", "RSS", "Command");
  (void)printf("\n------------------------------------------------------------------------------");

  // Report the process info as required
  prc = (struct pst_status *)psa;
  for (i=0,j=0; i < nproc; i++,j++)
  {
    if (prc->pst_pid == 0 && prc->pst_uid == 0 && prc->pst_nlwps == 0 )
        break;
    (void)printf("\n %5ld | ", i);
    (void)printf("%5ld | ", prc->pst_pid);
    (void)printf("%5ld | ", prc->pst_uid);
    (void)printf("%5ld | ", prc->pst_nlwps);
    (void)printf("%5ld | ", prc->pst_rssize);
    (void)printf("%s ", prc->pst_cmd);
    thsum += prc->pst_nlwps;
    ++prc;
  }

  (void)printf("\n\n*** %ld processes reported by pstat, %ld threads running, %ld procs scanned\n\n",  psd.psd_activeprocs, thsum, j);
  (void)free(psa);
  (void)exit(0);
}

 

cc +DD64 -D_PSTAT64 -z -Ae -D_XOPEN_SOURCE_EXTENDED -D_XOPEN_SOURCE -lnm -lxnet -g -o g3 g3.c

 

Output on 11iv1: 

 *** 119 processes reported by pstat, 255 threads running, 120 procs scanned


 

Output on 11iv3:

*** 266 processes reported by pstat, 904 threads running, 266 procs scanned

 

 

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 5 (420 Views)

Re: Off-by-one problem for psd_activeprocs in pstat_getdynamic on 11iv1 ?

Hi:

 

Interestingly, in 11.23, PHKL_35181 fixes incorrect values for 'psd_activeprocs' among others when calling 'pstat_getdynamic(2)'.

 

Since 11.11 is no longer actively patched, you may be stuck, though you could contact the Response Center and ask about your options.

 

Regards!

 

...JRF...

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 3 of 5 (401 Views)

Re: Off-by-one problem for psd_activeprocs in pstat_getdynamic on 11.11?

>psd_activeprocs seems to be off-by-one on 11.11, always returning 1 less than the actual number of active processes.

 

Hmm, I thought I saw it match once??

 

> if (prc->pst_pid == 0 && prc->pst_uid == 0 && prc->pst_nlwps == 0 )

 

This logic isn't needed.  You should simply use the result of:

   count = pstat_getproc(psa, sizeof(struct pst_status), nproc, 0)

 

>PHKL_35181 fixes incorrect values for 'psd_activeprocs' among others when calling 'pstat_getdynamic(2)'.

 

This mentions vPars and deactivating a CPU, which I don't have.

Trusted Contributor
Earl_Crowder
Posts: 76
Registered: ‎07-17-2001
Message 4 of 5 (391 Views)

Re: Off-by-one problem for psd_activeprocs in pstat_getdynamic on 11.11?

Thank you both.

 

Interestingly, it's off by one on 11.0, v1, and v2.   v3 returns the correct value.

 

I modified the code to allocate (psd.psd_activeprocs+10) structures, and used the count returned by pstat_getproc, which works across all 4 versions. 

 

Earl

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 5 of 5 (381 Views)

Re: Off-by-one problem for psd_activeprocs in pstat_getdynamic on 11.11?

[ Edited ]

>Interestingly, it's off by one on 11.00,  11.11, and 11.23.   11.31 returns the correct value.

 

I guess I didn't try 11.23, I'll do that later.

(Yes it fails the same way.)

>I modified the code to allocate (psd.psd_activeprocs+10) structures, and used the count returned by pstat_getproc

 

If you look at the man page examples, you can do multiple requests with smaller chunks.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.