x86cpuspeed.c 493 B

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