Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

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

SOLVED
Go to Solution
Trusted Contributor Trusted Contributor

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

 

 

4 REPLIES
Acclaimed Contributor Acclaimed Contributor

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 Acclaimed Contributor

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 Trusted Contributor

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 Acclaimed Contributor

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.