amd64cpuspeed.c 552 B

12345678910111213141516171819202122232425
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/param.h>
  4. #include <sys/sysctl.h>
  5. long long cpucycles_amd64cpuspeed(void)
  6. {
  7. unsigned long long result;
  8. asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
  9. : "=a" (result) :: "%rdx");
  10. return result;
  11. }
  12. long long cpucycles_amd64cpuspeed_persecond(void)
  13. {
  14. int oid[2];
  15. int val;
  16. size_t size;
  17. oid[0] = CTL_HW;
  18. oid[1] = HW_CPUSPEED;
  19. size = sizeof val;
  20. if (sysctl(oid,2,&val,&size,0,0) == -1) return 0;
  21. if (size != sizeof val) return 0;
  22. return val * 1000000LL;
  23. }