amd64cpuinfo.c 584 B

123456789101112131415161718192021222324252627282930
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. long long cpucycles_amd64cpuinfo(void)
  4. {
  5. unsigned long long result;
  6. asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
  7. : "=a" (result) :: "%rdx");
  8. return result;
  9. }
  10. long long cpucycles_amd64cpuinfo_persecond(void)
  11. {
  12. FILE *f;
  13. double result;
  14. int s;
  15. f = fopen("/proc/cpuinfo","r");
  16. if (!f) return 0;
  17. for (;;) {
  18. s = fscanf(f,"cpu MHz : %lf",&result);
  19. if (s > 0) break;
  20. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  21. if (s < 0) { result = 0; break; }
  22. }
  23. fclose(f);
  24. return 1000000.0 * result;
  25. }