perfstat_cpu_rset 接口

perfstat_cpu_rset 接口返回在 libperfstat.h 文件中定义的一组类型为 perfstat_cpu_t的结构。

perfstat_cpu_t 结构中的所选字段包括:

描述信息
名称 逻辑处理器名称 (cpu0, cpu1等)
用户 在用户方式下花费的时钟信号数
sys 在系统 (内核) 方式下花费的时钟信号数
空闲 在无 I/O 暂挂的情况下空闲的时钟信号数
WAIT 由于 I/O 暂挂而处于空闲状态的时钟信号数
syscall 已执行的系统调用数

还会返回其他几个与调页空间相关的度量值 (例如叉数,读取数,写入数和执行数)。 有关其他与调页空间相关的度量的完整列表,请参阅 libperfstat.h 头文件中的 perfstat_cpu_t 部分。

以下代码显示了如何从全局环境使用 perfstat_cpu_rset 的示例:
#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;
}
程序显示类似于以下示例输出的输出:
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
以下代码显示了如何从 WPAR 环境中使用 perfstat_cpu_rset 的示例:
#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;
}