tsc.c 899 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdbool.h>
  2. #include <errno.h>
  3. #include <linux/perf_event.h>
  4. #include "../../perf.h"
  5. #include <linux/types.h>
  6. #include "../../util/debug.h"
  7. #include "../../util/tsc.h"
  8. #include "tsc.h"
  9. int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
  10. struct perf_tsc_conversion *tc)
  11. {
  12. bool cap_user_time_zero;
  13. u32 seq;
  14. int i = 0;
  15. while (1) {
  16. seq = pc->lock;
  17. rmb();
  18. tc->time_mult = pc->time_mult;
  19. tc->time_shift = pc->time_shift;
  20. tc->time_zero = pc->time_zero;
  21. cap_user_time_zero = pc->cap_user_time_zero;
  22. rmb();
  23. if (pc->lock == seq && !(seq & 1))
  24. break;
  25. if (++i > 10000) {
  26. pr_debug("failed to get perf_event_mmap_page lock\n");
  27. return -EINVAL;
  28. }
  29. }
  30. if (!cap_user_time_zero)
  31. return -EOPNOTSUPP;
  32. return 0;
  33. }
  34. u64 rdtsc(void)
  35. {
  36. unsigned int low, high;
  37. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  38. return low | ((u64)high) << 32;
  39. }