2008/12/10

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

CPUIDでハードウェアパフォーマンスカウンタの情報を表示するプログラム.
#include 
#include 

#define BIT(x, bit) (((x) >> (bit)) & 0x00000001)

int main()
{
  unsigned int a, b, c, d;
  unsigned char eax_07_00, eax_15_08, eax_23_16, eax_31_24;
  unsigned char edx_04_00, edx_12_05;

  asm volatile ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "a"(0x0a));

  printf("CPUID(EAX=0AH): Architectural Performance Monitoring\n");

  eax_07_00 = (unsigned char)(a & 0xff);
  eax_15_08 = (unsigned char)((a >> 8) & 0xff);
  eax_23_16 = (unsigned char)((a >> 16) & 0xff);
  eax_31_24 = (unsigned char)((a >> 24) & 0xff);
  printf(" Version ID of architectural PM: %d\n", eax_07_00);
  printf(" Number of general-purpose PMC per logical processor: %d\n", eax_15_08);
  printf(" Bit width of general-purpose PMC: %d\n", eax_23_16);
  printf(" Length of EBX bit vector: %d\n", eax_31_24);
  printf("\n");
 
  printf(" Core cycle event not available: %d\n", BIT(b, 0));
  printf(" Instruction retired event not available: %d\n", BIT(b, 1));
  printf(" Reference cycles event not available: %d\n", BIT(b, 2));
  printf(" Last-level cache reference event not available: %d\n", BIT(b, 3));
  printf(" Last-level cache misses event not available: %d\n", BIT(b, 4));
  printf(" Branch instrunction retired event not available: %d\n", BIT(b, 5));
  printf(" Branch mispredict retired event not available: %d\n", BIT(b, 6));
  printf("\n");

  if (eax_07_00 > 1) {
    edx_04_00 = (unsigned char)(d & 0x1f);
    edx_12_05 = (unsigned char)((d >> 5) & 0xff);
    printf(" Number of fixed-function PCs: %d\n", edx_04_00);
    printf(" Bit width of fixed-function PCs: %d\n", edx_12_05);
  }

  return 0;
}

0 件のコメント: