perfstat_cpu_rset interface

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

Selected fields from the perfstat_cpu_t structure include:

Item Descriptor
name Logical processor name (cpu0, cpu1, and so on)
user Number of clock ticks spent in user mode
sys Number of clock ticks spent in system (kernel) mode
idle Number of clock ticks spent idle with no I/O pending
wait Number of clock ticks spent idle with I/O pending
syscall Number of system call executed

Several other paging-space-related metrics (such as number of forks, reads, writes, and execs) are also returned. For a complete list of other paging-space-related metrics, see the perfstat_cpu_t section in the libperfstat.h header file.

The following code shows an example of how perfstat_cpu_rset is used from the global environment:
#include <stdio.h>
#include <stdlib.h>
#include <libperfstat.h>
int main(){
   int i, retcode, rsetcpus;
   perfstat_id_wpar_t wparid;
   perfstat_cpu_t *statp;
   wparid.spec = WPARNAME;
   strcpy(wparid.u.wparname,NULL);
 
   /* give the wparname "wpar1" as the identifier */
   strcpy(wparid.u.wparname, "test");
   
   /* check how many perfstat_cpu_t structures are available */
   rsetcpus =  perfstat_cpu_rset(&wparid, NULL, sizeof(perfstat_cpu_t), 0);
   
   if (rsetcpus < 0 ){
       perror("perfstat_cpu_rset");
       exit(-1);
   }

   /*allocate memory for perfstat_cpu_t structures */
   statp = (perfstat_cpu_t *)calloc(rsetcpus , sizeof(perfstat_cpu_t)); 

   if(!statp){
      perror("calloc");
   }

   /*call the API and get the values */

   retcode = perfstat_cpu_rset(&wparid, statp,sizeof(perfstat_cpu_t), rsetcpus);
   
   if(retcode < 0){
      perror("perfstat_cpu_rset");
   }

   for(i=0;i<retcode;i++){
	printf("Logical processor name=%s\n",statp[i].name);
	printf("Raw number of clock ticks spent in user mode=%lld\n",statp[i].user);
	printf("Raw number of clock ticks spent in system mode=%lld\n",statp[i].sys);
	printf("Raw number of clock ticks spent in idle mode=%lld\n",statp[i].idle);
	printf("Raw number of clock ticks spent in wait mode=%lld\n",statp[i].wait);		
   }
   return 0;
}
The program displays an output that is similar to the following example output:
Logical processor name=cpu0
Raw number of clock ticks spent in user mode=2050
Raw number of clock ticks spent in system mode=22381
Raw number of clock ticks spent in idle mode=6863114
Raw number of clock ticks spent in wait mode=3002
Logical processor name=cpu1
Raw number of clock ticks spent in user mode=10
Raw number of clock ticks spent in system mode=651
Raw number of clock ticks spent in idle mode=6876627
Raw number of clock ticks spent in wait mode=42
Logical processor name=cpu2
Raw number of clock ticks spent in user mode=0
Raw number of clock ticks spent in system mode=610
Raw number of clock ticks spent in idle mode=6876712
Raw number of clock ticks spent in wait mode=0
Logical processor name=cpu3
Raw number of clock ticks spent in user mode=0
Raw number of clock ticks spent in system mode=710
Raw number of clock ticks spent in idle mode=6876612
Raw number of clock ticks spent in wait mode=0
Logical processor name=cpu4
Raw number of clock ticks spent in user mode=243
Raw number of clock ticks spent in system mode=1659
Raw number of clock ticks spent in idle mode=6875427
Raw number of clock ticks spent in wait mode=62
Logical processor name=cpu5
Raw number of clock ticks spent in user mode=0
Raw number of clock ticks spent in system mode=207327
Raw number of clock ticks spent in idle mode=6848952
Raw number of clock ticks spent in wait mode=0
Logical processor name=cpu6
Raw number of clock ticks spent in user mode=0
Raw number of clock ticks spent in system mode=207904
Raw number of clock ticks spent in idle mode=6849969
Raw number of clock ticks spent in wait mode=0
Logical processor name=cpu7
Raw number of clock ticks spent in user mode=0
Raw number of clock ticks spent in system mode=207375
Raw number of clock ticks spent in idle mode=6848209
Raw number of clock ticks spent in wait mode=0
The following code shows an example of how perfstat_cpu_rset is used from the WPAR environment:
#include <stdio.h>
#include <stdlib.h>
#include <libperfstat.h>
int main(){
   int i, retcode, rsetcpus;
   perfstat_id_wpar_t wparid;
   perfstat_cpu_t *statp;
      
   /* check how many perfstat_cpu_t structures are available */
   rsetcpus =  perfstat_cpu_rset(NULL, NULL, sizeof(perfstat_cpu_t), 0);
   
   if (rsetcpus < 0 ){
       perror("perfstat_cpu_rset");
       exit(-1);
   }

   /*allocate memory for perfstat_cpu_t structures */
   statp = (perfstat_cpu_t *)calloc(rsetcpus , sizeof(perfstat_cpu_t)); 

   if(!statp){
      perror("calloc");
   }

   /*call the API and get the values */

   retcode = perfstat_cpu_rset(NULL, statp,sizeof(perfstat_cpu_t), rsetcpus);
   
   if(retcode < 0){
      perror("perfstat_cpu_rset");
   }

   for(i=0;i<retcode;i++){
	printf("Logical processor name=%s\n",statp[i].name);
	printf("Raw number of clock ticks spent in user mode=%lld\n",statp[i].user);
	printf("Raw number of clock ticks spent in system mode=%lld\n",statp[i].sys);
	printf("Raw number of clock ticks spent in idle mode=%lld\n",statp[i].idle);
	printf("Raw number of clock ticks spent in wait mode=%lld\n",statp[i].wait);		
   }
   return 0;
}