monotoniccpuinfo.c 617 B

123456789101112131415161718192021222324252627282930313233
  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. #include "osfreq.c"
  8. static double cpufrequency = 0;
  9. static void init(void)
  10. {
  11. cpufrequency = osfreq();
  12. }
  13. long long cpucycles_monotoniccpuinfo(void)
  14. {
  15. double result;
  16. struct timespec t;
  17. if (!cpufrequency) init();
  18. clock_gettime(CLOCK_MONOTONIC,&t);
  19. result = t.tv_nsec;
  20. result *= 0.000000001;
  21. result += (double) t.tv_sec;
  22. result *= cpufrequency;
  23. return result;
  24. }
  25. long long cpucycles_monotoniccpuinfo_persecond(void)
  26. {
  27. if (!cpufrequency) init();
  28. return cpufrequency;
  29. }