1
0

sgi.c 757 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <sys/sysctl.h>
  7. static double cpufrequency = 0;
  8. static void init(void)
  9. {
  10. FILE *f;
  11. f = popen("hinv -c processor | awk '{if ($3==\"MHZ\") print $2*1000000}'","r");
  12. if (!f) return;
  13. if (fscanf(f,"%lf",&cpufrequency) < 1) cpufrequency = 0;
  14. pclose(f);
  15. if (!cpufrequency) return;
  16. }
  17. long long cpucycles_sgi(void)
  18. {
  19. double result;
  20. struct timespec t;
  21. if (!cpufrequency) init();
  22. clock_gettime(CLOCK_SGI_CYCLE,&t);
  23. result = t.tv_nsec;
  24. result *= 0.000000001;
  25. result += (double) t.tv_sec;
  26. result *= cpufrequency;
  27. return result;
  28. }
  29. long long cpucycles_sgi_persecond(void)
  30. {
  31. if (!cpufrequency) init();
  32. return cpufrequency;
  33. }