2008/12/10

C言語: ハードウェアパフォーマンスカウンタの情報を表示

CPUIDでハードウェアパフォーマンスカウンタの情報を表示するプログラム.
  1. #include   
  2. #include   
  3.   
  4. #define BIT(x, bit) (((x) >> (bit)) & 0x00000001)  
  5.   
  6. int main()  
  7. {  
  8.   unsigned int a, b, c, d;  
  9.   unsigned char eax_07_00, eax_15_08, eax_23_16, eax_31_24;  
  10.   unsigned char edx_04_00, edx_12_05;  
  11.   
  12.   asm volatile ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "a"(0x0a));  
  13.   
  14.   printf("CPUID(EAX=0AH): Architectural Performance Monitoring\n");  
  15.   
  16.   eax_07_00 = (unsigned char)(a & 0xff);  
  17.   eax_15_08 = (unsigned char)((a >> 8) & 0xff);  
  18.   eax_23_16 = (unsigned char)((a >> 16) & 0xff);  
  19.   eax_31_24 = (unsigned char)((a >> 24) & 0xff);  
  20.   printf(" Version ID of architectural PM: %d\n", eax_07_00);  
  21.   printf(" Number of general-purpose PMC per logical processor: %d\n", eax_15_08);  
  22.   printf(" Bit width of general-purpose PMC: %d\n", eax_23_16);  
  23.   printf(" Length of EBX bit vector: %d\n", eax_31_24);  
  24.   printf("\n");  
  25.    
  26.   printf(" Core cycle event not available: %d\n", BIT(b, 0));  
  27.   printf(" Instruction retired event not available: %d\n", BIT(b, 1));  
  28.   printf(" Reference cycles event not available: %d\n", BIT(b, 2));  
  29.   printf(" Last-level cache reference event not available: %d\n", BIT(b, 3));  
  30.   printf(" Last-level cache misses event not available: %d\n", BIT(b, 4));  
  31.   printf(" Branch instrunction retired event not available: %d\n", BIT(b, 5));  
  32.   printf(" Branch mispredict retired event not available: %d\n", BIT(b, 6));  
  33.   printf("\n");  
  34.   
  35.   if (eax_07_00 > 1) {  
  36.     edx_04_00 = (unsigned char)(d & 0x1f);  
  37.     edx_12_05 = (unsigned char)((d >> 5) & 0xff);  
  38.     printf(" Number of fixed-function PCs: %d\n", edx_04_00);  
  39.     printf(" Bit width of fixed-function PCs: %d\n", edx_12_05);  
  40.   }  
  41.   
  42.   return 0;  
  43. }  
  44.   

0 件のコメント: