perfstat_cpu_util interface

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

The perfstat_cpu_util interface includes the following fields:
Item Descriptor
cpu_id Holds CPU ID
entitlement Partition's entitlement
user_pct Percentage of utilization in user mode
kern_pct Percentage of utilization in kernel mode
idle_pct Percentage of utilization in idle mode
wait_pct Percentage of utilization in wait mode
physical_busy Physical CPU is busy
physical_consumed Total CPUs consumed by the partition
freq_pct Average frequency over the last interval in percentage
entitlement_pct Percentage of entitlement used
busy_pct Percentage of entitlement busy
idle_donated_pct Percentage of idle cycles donated
busy_donated_pct Percentage of busy cycles donated
idle_stolen_pct Percentage of idle cycles stolen
busy_stolen_pct Percentage of busy cycles stolen
float l_user_pct Percentage of utilization in user mode in terms of the logical processor ticks
float l_kern_pct Percentage of utilization in kernel mode in terms of the logical processor ticks
float l_idle_pct Percentage of utilization in idle mode in terms of the logical processor ticks
float l_wait_pct Percentage of utilization in wait mode in terms of the logical processor ticks
u_longlong_t delta_time Percentage of the delta time in milliseconds for which the utilization is evaluated

Both system utilization and per CPU utilization can be obtained by using theperfstat_cpu_util by mentioning the type field of the perfstat_rawdata_t data structure as UTIL_CPU_TOTAL or UTIL_CPU respectively. UTIL_CPU_TOTAL and UTIL_CPU are the macros, which can be referred in the definition of the perfstat_rawdata_t data structure.

The use of the perrfstat_cpu_util API for system-level utilization follows:
#include <libperfstat.h>
#define PERIOD 5
void main()
{
   perfstat_cpu_total_t *newt, *oldt;
   perfstat_cpu_util_t *util;
   perfstat_rawdata_t data;
   int rc;

   oldt = (perfstat_cpu_total_t*)malloc(sizeof(perfstat_cpu_total_t)*1);
   if(oldt==NULL){
      perror ("malloc");
      exit(-1);
   }

   newt = (perfstat_cpu_total_t*)malloc(sizeof(perfstat_cpu_total_t)*1);
   if(newt==NULL){
      perror ("malloc");
      exit(-1);
   }

   util = (perfstat_cpu_util_t*)malloc(sizeof(perfstat_cpu_util_t)*1);
   if(util==NULL){
      perror ("malloc");
      exit(-1);
   }

   rc = perfstat_cpu_total(NULL, oldt, sizeof(perfstat_cpu_total_t), 1);
    if(rc <= 0)
    {
    perror("Error in perfstat_cpu_total");
       exit(-1);
    }
   sleep(PERIOD);
     rc = perfstat_cpu_total(NULL, newt, sizeof(perfstat_cpu_total_t), 1);
     if(rc <= 0)
     {
      perror("Error in perfstat_cpu_total");
       exit(-1);
     }
    data.type = UTIL_CPU_TOTAL;
    data.curstat = newt; data.prevstat= oldt;
    data.sizeof_data = sizeof(perfstat_cpu_total_t);
    data.cur_elems = 1;
    data.prev_elems = 1;
    rc = perfstat_cpu_util(&data, util,sizeof(perfstat_cpu_util_t), 1);
 if(rc <= 0)
    {
      perror("Error in perfstat_cpu_util");
      exit(-1);
    }
   printf("=======Overall CPU Utilization Metrics=======\n");
   printf("Utilization Metrics for a period of %d seconds\n",PERIOD);
   printf("User Percentage =                %f\n",util->user_pct);
   printf("System Percentage =              %f\n",util->kern_pct);
   printf("Idle Percentage =                %f\n",util->idle_pct);
   printf("Wait Percentage =                %f\n",util->wait_pct);
   printf("Physical Busy =                  %f\n",util->physical_busy);
   printf("Physical Consumed =              %f\n",util->physical_consumed);
   printf("Freq Percentage =                %f\n",util->freq_pct);
   printf("Entitlement Used Percentage =    %f\n",util->entitlement_pct);
   printf("Entitlement Busy Percentage =    %f\n",util->busy_pct);
   printf("Idle Cycles Donated Percentage = %f\n",util->idle_donated_pct);
   printf("Busy Cycles Donated Percentage = %f\n",util->busy_donated_pct);
   printf("Idle Cycles Stolen Percentage =  %f\n",util->idle_stolen_pct);
   printf("Busy Cycles Stolen Percentage =  %f\n",util->busy_stolen_pct);
   printf("User percentage for logical cpu in ticks = %f\n",util->l_user_pct);
   printf("Sytem percentage for logical cpu in ticks= %f\n",util->l_kern_pct);
   printf("Idle percentage for logical cpu in ticks=  %f\n",util->l_idle_pct);
   printf("Wait percentage for logical cpu in ticks=  %f\n",util->l_wait_pct);
   printf("delta time in milliseconds =  %llu \n",util->delta_time);
   printf("=============================================\n");
}
The program produces the output similar to the following:
=======Overall CPU Utilization Metrics=======
Utilization Metrics for a period of 5 seconds
User Percentage =                0.050689
System Percentage =              0.262137
Idle Percentage =                99.687172
Wait Percentage =                0.000000
Physical Busy =                  0.003128
Physical Consumed =              0.008690
Freq Percentage =                99.935417
Entitlement Used Percentage =    0.869017
Entitlement Busy Percentage =    0.312826
Idle Cycles Donated Percentage = 0.000000
Busy Cycles Donated Percentage = 0.000000
Idle Cycles Stolen Percentage =  0.000000
Busy Cycles Stolen Percentage =  0.000000
User percentage for logical cpu in ticks = 0.000000
Sytem percentage for logical cpu in ticks= 0.082034
Idle percentage for logical cpu in ticks=  99.917969
Wait percentage for logical cpu in ticks=  0.000000
delta time in milliseconds =  4980
The example code to calculate system utilization per CPU, and CPU utilization, by using the perfstat_cpu_util interface follows:
#include <libperfstat.h>
#define PERIOD 5

void main()
{
  perfstat_rawdata_t data;
  perfstat_cpu_util_t *util;
  perfstat_cpu_t *newt,*oldt;
  perfstat_id_t id;
  int i,cpu_count,rc;

  /* Check how many perfstat_cpu_t structures are available */
  cpu_count = perfstat_cpu(NULL, NULL,sizeof(perfstat_cpu_t),0);

  /* check for error */
  if(cpu_count <= 0)
   {
     perror("Error in perfstat_cpu");
     exit(-1);
   }
    /* allocate enough memory */
   oldt = (perfstat_cpu_t *)calloc(cpu_count,sizeof(perfstat_cpu_t));
   if(oldt == NULL)
   {
     perror("Memory Allocation Error");
     exit(-1);
   }
   /* set name to first cpu */
   strcpy(id.name,FIRST_CPU);
   /* ask to get all the structures available in one call */
   rc = perfstat_cpu(&id, oldt, sizeof(perfstat_cpu_t), cpu_count);
    /* check for error */
   if(rc <=0)
   {
     perror("Error in perfstat_cpu");
      exit(-1);
    }
    data.type = UTIL_CPU;
    data.prevstat= oldt;
    data.sizeof_data = sizeof(perfstat_cpu_t);
    data.prev_elems = cpu_count;
    sleep(PERIOD);
     /* Check how many perfstat_cpu_t structures are available after a defined period */
   cpu_count = perfstat_cpu(NULL, NULL,sizeof(perfstat_cpu_t),0);

 /* Check for error */
    if(cpu_count <= 0)
    {
     perror("Error in perfstat_cpu");
      exit(-1);
    }

    data.cur_elems = cpu_count;
     if(data.prev_elems != data.cur_elems)
    {
     perror("The number of CPUs has become different for defined period");
     exit(-1);
    }
  /* allocate enough memory */
  newt = (perfstat_cpu_t *)calloc(cpu_count,sizeof(perfstat_cpu_t));
  util = (perfstat_cpu_util_t *)calloc(cpu_count,sizeof(perfstat_cpu_util_t));
  if(newt == NULL || util == NULL)
  {
   perror("Memory Allocation Error");
   exit(-1);
  }
  data.curstat = newt;
  rc = perfstat_cpu(&id, newt, sizeof(perfstat_cpu_t), cpu_count);
   if(rc <= 0)
   {
     perror("Error in perfstat_cpu");
     exit(-1);
   }
    /* Calculate CPU Utilization Metrics*/
   rc = perfstat_cpu_util(&data, util, sizeof(perfstat_cpu_util_t), cpu_count);
if(rc <= 0)
  {
    perror("Error in perfstat_cpu_util");
    exit(-1);
  }
printf("========= Per CPU Utilization Metrics =========\n");
  printf("Utilization Metrics for a period of %d seconds\n",PERIOD);
  printf("===============================================\n");
  for ( i = 0;i<cpu_count;i++)
{
   printf("Utilization metrics for CPU-ID =    %s\n",util[i].cpu_id);
   printf("User Percentage =                   %f\n",util[i].user_pct);
   printf("System Percentage =                 %f\n",util[i].kern_pct);
printf("Idle Percentage =                   %f\n",util[i].idle_pct);
   printf("Wait Percentage =                   %f\n",util[i].wait_pct);
   printf("Physical Busy =                     %f\n",util[i].physical_busy);
   printf("Physical Consumed =                 %f\n",util[i].physical_consumed);
   printf("Freq Percentage =                   %f\n",util[i].freq_pct);
   printf("Entitlement Used Percentage =       %f\n",util[i].entitlement_pct);
   printf("Entitlement Busy Percentage =       %f\n",util[i].busy_pct);
   printf("Idle Cycles Donated Percentage =    %f\n",util[i].idle_donated_pct);
   printf("Busy Cycles Donated Percentage =    %f\n",util[i].busy_donated_pct);
   printf("Idle Cycles Stolen Percentage =     %f\n",util[i].idle_stolen_pct);
   printf("Busy Cycles Stolen Percentage =     %f\n",util[i].busy_stolen_pct);
   printf("system percentage for logical cpu in ticks = %f\n",util[i].l_kern_pct);
   printf("idle percentage for logical cpu in ticks =  %f\n",util[i].l_idle_pct);
   printf("wait percentage for logical cpu in ticks =  %f\n",util[i].l_wait_pct);
   printf("delta time in milliseconds =  %llu \n",util[i].delta_time);
   printf("\n\n");
}
   printf("===========================================\n");
}
The program produces the output similar to the following:
========= Per CPU Utilization Metrics =========
Utilization Metrics for a period of 5 seconds
===============================================
Utilization metrics for CPU-ID =    cpu0
User Percentage =                   14.850358
System Percentage =                 63.440376
Idle Percentage =                   21.709267
Wait Percentage =                   0.000000
Physical Busy =                     0.003085
Physical Consumed =                 0.003941
Freq Percentage =                   99.975967
Entitlement Used Percentage =       0.394055
Entitlement Busy Percentage =       0.308508
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.000000
idle percentage for logical cpu in ticks =  100.000000
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  4999


Utilization metrics for CPU-ID =    cpu1
User Percentage =                   0.000000
System Percentage =                 4.720662
Idle Percentage =                   95.279335
Wait Percentage =                   0.000000
Physical Busy =                     0.000065
Physical Consumed =                 0.001371
Freq Percentage =                   99.938919
Entitlement Used Percentage =       0.137110
Entitlement Busy Percentage =       0.006472
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.000000
idle percentage for logical cpu in ticks =  100.000000
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  5000

Utilization metrics for CPU-ID =    cpu2
User Percentage =                   0.000000
System Percentage =                 5.848962
Idle Percentage =                   94.151039
Wait Percentage =                   0.000000
Physical Busy =                     0.000079
Physical Consumed =                 0.001348
Freq Percentage =                   99.900566
Entitlement Used Percentage =       0.134820
Entitlement Busy Percentage =       0.007886
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.000000
idle percentage for logical cpu in ticks =  100.000000
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  5000


Utilization metrics for CPU-ID =    cpu3
User Percentage =                   0.000000
System Percentage =                 4.644570
Idle Percentage =                   95.355431
Wait Percentage =                   0.000000
Physical Busy =                     0.000061
Physical Consumed =                 0.001312
Freq Percentage =                   99.925430
Entitlement Used Percentage =       0.131174
Entitlement Busy Percentage =       0.006092
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.000000
idle percentage for logical cpu in ticks =  100.000000
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  5000


Utilization metrics for CPU-ID =    cpu4
User Percentage =                   0.000000
System Percentage =                 55.325123
Idle Percentage =                   44.674877
Wait Percentage =                   0.000000
Physical Busy =                     0.000153
Physical Consumed =                 0.000276
Freq Percentage =                   99.927551
Entitlement Used Percentage =       0.027605
Entitlement Busy Percentage =       0.015273
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.000000
idle percentage for logical cpu in ticks =  100.000000
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  4999


Utilization metrics for CPU-ID =    cpu5
User Percentage =                   0.000000
System Percentage =                 1.854463
Idle Percentage =                   98.145538
Wait Percentage =                   0.000000
Physical Busy =                     0.000002
Physical Consumed =                 0.000113
Freq Percentage =                   99.612183
Entitlement Used Percentage =       0.011326
Entitlement Busy Percentage =       0.000210
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.255102
idle percentage for logical cpu in ticks =  99.744896
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  3913

Utilization metrics for CPU-ID =    cpu6
User Percentage =                   0.000000
System Percentage =                 1.776852
Idle Percentage =                   98.223145
Wait Percentage =                   0.000000
Physical Busy =                     0.000002
Physical Consumed =                 0.000115
Freq Percentage =                   99.475967
Entitlement Used Percentage =       0.011506
Entitlement Busy Percentage =       0.000204
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.255102
idle percentage for logical cpu in ticks =  99.744896
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  3912


Utilization metrics for CPU-ID =    cpu7
User Percentage =                   0.000000
System Percentage =                 2.138275
Idle Percentage =                   97.861725
Wait Percentage =                   0.000000
Physical Busy =                     0.000002
Physical Consumed =                 0.000112
Freq Percentage =                   99.593727
Entitlement Used Percentage =       0.011205
Entitlement Busy Percentage =       0.000240
Idle Cycles Donated Percentage =    0.000000
Busy Cycles Donated Percentage =    0.000000
Idle Cycles Stolen Percentage =     0.000000
Busy Cycles Stolen Percentage =     0.000000
system percentage for logical cpu in ticks = 0.255102
idle percentage for logical cpu in ticks =  99.744896
wait percentage for logical cpu in ticks =  0.000000
delta time in milliseconds =  3912