perfstat_cpu_util 接口

perfstat_cpu_util 接口返回类型为 perfstat_cpu_util_t的一组结构,该结构在 libperfstat.h 文件中定义

perfstat_cpu_util 接口包含以下字段:
描述信息
CPU ID 保留 CPU 标识
授权 分区的权利
用户百分比 用户方式下的利用率百分比
内核百分比 内核方式下的利用率百分比
空闲百分比 空闲方式下的利用率百分比
等待百分比 处于等待方式的利用率百分比
物理繁忙 物理 CPU 正忙
物理耗用时间 分区使用的总 CPU 数
自由百分比 上一个时间间隔内的平均频率 (以百分比计)
授权百分比 已使用权利的百分比
总线百分比 权利繁忙百分比
闲置捐赠比例 捐赠的空闲周期百分比
总线 (busy_donated_pct) 捐赠的忙碌周期百分比
闲置被盗率 失窃的空闲周期所占的百分比
总线 (busy_stolen_pct) 窃用的忙碌周期所占的百分比
浮点 l_user_pct 以逻辑处理器记号表示的用户方式的利用率百分比
浮点 l_kern_pct 内核方式下的利用率相对于逻辑处理器刻度的百分比
浮点 l_idle_pct 以空闲方式使用逻辑处理器记号的百分比
浮点 l_wait_pct 以逻辑处理器刻度标记表示的处于等待方式的利用率百分比
u_longlong_t 时间差 对利用率求值的增量时间百分比 (以毫秒计)

可以通过使用perfstat_cpu_util ,将 perfstat_rawdata_t 数据结构的类型字段分别指定为 UTIL_CPU_TOTALUTIL_CPU 来获取系统利用率和每个 CPU 利用率。 UTIL_CPU_TOTALUTIL_CPU 是可在 perfstat_rawdata_t 数据结构定义中引用的宏。

perrfstat_cpu_util API 用于系统级别的利用率如下所示:
#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");
}
程序生成类似于以下内容的输出:
=======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
使用 perfstat_cpu_util 接口计算每个 CPU 的系统利用率和 CPU 利用率的示例代码如下所示:
#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");
}
程序生成类似于以下内容的输出:
========= 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