perfstat_process_util interface

The perfstat_process_util interface returns a set of structures of type perfstat_process_t, which is defined in the libperfstat.h file.

The following is an example of code that uses the perfstat_process_util API:
#include <libperfstat.h>
#include <stdio.h>
#include <stdlib.h>
#define PERIOD 5

void main()
{
  perfstat_process_t *cur, *prev;
  perfstat_rawdata_t buf;
  perfstat_process_t *proc_util;
  perfstat_id_t id;
  int cur_proc_count,prev_proc_count;
  int i,rc;
  prev_proc_count = perfstat_process(NULL, NULL,sizeof(perfstat_process_t),0);
  if(prev_proc_count <= 0)
  {
    perror("Error in perfstat_process");
    exit(-1) ;
  }
  prev = (perfstat_process_t *)calloc(prev_proc_count,sizeof(perfstat_process_t));
  if(prev == NULL)
  {
    perror("Memory Allocation Error");
    exit(-1) ;
  }
  strcpy(id.name,"");
  rc = perfstat_process(&id,prev,sizeof(perfstat_process_t),prev_proc_count);
  if(rc <= 0)
  {
    perror("Error in perfstat_process"); 
    exit(-1) ; 
  }
  sleep(PERIOD); 

  cur_proc_count = perfstat_process(NULL, NULL,sizeof(perfstat_process_t),0);
  if(cur_proc_count <= 0)
  {
    perror("Error in perfstat_process");
    exit(-1) ;
  }
  cur = (perfstat_process_t *)calloc(cur_proc_count,sizeof(perfstat_process_t));
  proc_util = (perfstat_process_t *)calloc(cur_proc_count,sizeof(perfstat_process_t));
  if(cur == NULL || proc_util == NULL)
  {
    perror("Memory Allocation Error");
    exit(-1) ;
  }
  rc = perfstat_process(&id,cur,sizeof(perfstat_process_t),cur_proc_count); 
  if(rc < 0) 
  { 
    perror("Error in perfstat_process"); 
    exit(-1) ; 
  }

  bzero(&buf, sizeof(perfstat_rawdata_t)); 
  buf.type = UTIL_PROCESS;
  buf.curstat = cur;
  buf.prevstat = prev;
  buf.sizeof_data = sizeof(perfstat_process_t);
  buf.cur_elems = cur_proc_count;
  buf.prev_elems = prev_proc_count;


/* Calculate Process Utilization */
  rc = perfstat_process_util(&buf,proc_util,sizeof(perfstat_process_t),cur_proc_count); 
  if(rc <= 0) 
  { 
    perror("Error in perfstat_process_util"); 
    exit(-1); 
  }

  printf("\n =======Process Related Utilization Metrics =======\n");
  for(i=0 ; i<cur_proc_count ;i++)
  {
    printf("Process ID =                %lld\n",proc_util[i].pid);
    printf("User Mode CPU time =        %lf \n",proc_util[i].ucpu_time);
    printf("System Mode CPU time =      %lf \n",proc_util[i].scpu_time);
    printf("Bytes Written to Disk =     %lld\n",proc_util[i].inBytes);
    printf("Bytes Read from Disk =      %lld\n",proc_util[i].outBytes);
    printf("In Operations from Disk =   %lld\n",proc_util[i].inOps);
    printf("Out Operations from Disk =  %lld\n",proc_util[i].outOps);
    printf("=====================================\n");
    printf("\n\n");
}
}
The program displays an output that is similar to the following example output:
=======Process Related Utilization Metrics =======
Process ID =                0
User Mode CPU time =        0.000000
System Mode CPU time =      0.013752
Bytes Written to Disk =     0
Bytes Read from Disk =      0
In Operations from Disk =   0
Out Operations from Disk =  0
=====================================


Process ID =                1
User Mode CPU time =        0.000000
System Mode CPU time =      0.000000
Bytes Written to Disk =     0
Bytes Read from Disk =      0
In Operations from Disk =   0
Out Operations from Disk =  0
=====================================


Process ID =                196614
User Mode CPU time =        0.000000
System Mode CPU time =      0.000000
Bytes Written to Disk =     0
Bytes Read from Disk =      0
In Operations from Disk =   0
Out Operations from Disk =  0
=====================================


Process ID =                262152
User Mode CPU time =        0.000000
System Mode CPU time =      0.000000
Bytes Written to Disk =     0
Bytes Read from Disk =      0
In Operations from Disk =   0
Out Operations from Disk =  0
=====================================