PERFORMANCE-MONITORING(转)

Performance-Monitoring 是Intel提供的可以监测统计CPU内部所产生事件的一组方法。在Intel的手册上介绍了两类CPU事件监测方法:architectural performance monitoring 和 non-architectural performance monitoring。Architectural performance monitoring与平台(CPU系列)无关但所能监测的事件少;non-architectural performance monitoring与平台密切相关,能监测大量事件。我仅关注architectural performance monitoring。
Architectural performance monitoring介绍
CPU 通过两个寄存器来完成事件监测工作:事件选择寄存器IA32_PERFEVTSELx ( programming performance event select registers)和计数器IA32_PMCx (performance monitoring counter)。在计数前,设置事件选择寄存器并将计数器清零;计数结束时,读取计数器。
IA32_PERFEVTSELx与 IA32_PMCx都是成对使用,共同完成计数工作。IA32_PMCx寄存器对应于从0xc1开始的一段连续地址,IA32_PERFEVTSELx寄 存器对应于从0x186开始的一段连续地址。每种CPU的寄存器位数和可以使用的寄存器对数可能不一样,但可以通过CUPID.0AH:EAX指令来获取 这些元信息:
Bits 07 - 00: Version ID of architectural performance monitoring, If >0, architectural performance monitoring capability is supported.
Bits 15- 08: Number of general-purpose performance monitoring counter per logical processor
Bits 23 - 16: Bit width of general-purpose, performance monitoring counter
Bits 31 - 24: Length of EBX bit vector to enumerate architectural performance monitoring events
事件选择寄存器IA32_PERFEVTSELx的配置
  未命名
Event select field (bits 0 through 7):事件选择码区填写需要监测的事件码,这些事件码都是事先定义好的,可以在Intel的手册中查找。
Unit mask (UMASK) field (bits 8 through 15):掩码区填写与事件选择码对应掩码,掩码与事件码共同使用来确定要监测的事件,掩码与事件码一样是事先定义好的,可在Intel手册上查找。
USR (user mode) flag (bit 16):标识是否统计CPU处于用户态(CPU处于特权级别为:1、2、3)下发生的事件。可以与下面的OS位配合使用。
OS (operating system mode) flag (bit 17):标识是否统计CPU处于系统态(CPU处于特权级别为0)下发生的事件。可以与上面的USR位配合使用。
EN (Enable Counters) Flag (bit 22):计数允许位。注意:在写计数器IA32_PMCx之前,必须清除计数允许位(设为0)。
Counter mask (CMASK) field (bits 24 through 31):计算器掩码,如它不为零,但事件发生是,只有它小于计数器的值,计数器计数才会增加1。
计数示例
下面代码是统计事件DTLB_MISSES.ANY,其事件码为0x08,掩码为0x01。
//寄存器地址码
#define IA32_PMC0 0xc1
#define IA32_PERFEVTSEL0 0x186
//事件码及其掩码
#define EVENT 0x08
#define MASK 0x01
Int nEventMask, nCount;
Int nEventRegisterHigh,nEventRegisterLow;
nEventMask = IA32_PERFEVTSEL0;
nCount = IA32_PMC0;
nEventRegisterHigh=nEventRegisterLow=0;
//设置事件码及掩码
nEventRegisterLow |= EVENT;
nEventRegisterLow |= MASK<<8;
//设置用户态系统态标识位
nEventRegisterLow |= 1<<16;
nEventRegisterLow |= 1<<17;
//清楚计数允许位
nEventRegisterLow &= ~(1<<22);
//设置事件选择寄存器
wrmsr(nEventMask, -(u32)( nEventRegisterLowl), -1);
//计数器清零
wrmsr(nCount, -(u32)( 0), -1);
//设置计数允许位
nEventRegisterLow |= 1<<22;
wrmsr(nEventMask, -(u32)( nEventRegisterLowl), -1);
。。。。//计数中
//读起计数结果
rdmsr(nCount, nEventRegisterLow, nEventRegisterHigh);
Non-architectural performance monitoring介绍
(还不太了解这方面的知识!)
http://rf-lai.spaces.live.com/blog/cns!8A6F01FE4875D04A!412.entry

转载于:https://www.cnblogs.com/parrynee/archive/2010/01/21/1653157.html

猜你喜欢

转载自blog.csdn.net/weixin_33730836/article/details/93447493