tsc.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
  9. struct perf_tsc_conversion *tc)
  10. {
  11. bool cap_user_time_zero;
  12. u32 seq;
  13. int i = 0;
  14. while (1) {
  15. seq = pc->lock;
  16. rmb();
  17. tc->time_mult = pc->time_mult;
  18. tc->time_shift = pc->time_shift;
  19. tc->time_zero = pc->time_zero;
  20. cap_user_time_zero = pc->cap_user_time_zero;
  21. rmb();
  22. if (pc->lock == seq && !(seq & 1))
  23. break;
  24. if (++i > 10000) {
  25. pr_debug("failed to get perf_event_mmap_page lock\n");
  26. return -EINVAL;
  27. }
  28. }
  29. if (!cap_user_time_zero)
  30. return -EOPNOTSUPP;
  31. return 0;
  32. }
  33. u64 rdtsc(void)
  34. {
  35. unsigned int low, high;
  36. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  37. return low | ((u64)high) << 32;
  38. }
  39. int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc,
  40. struct perf_tool *tool,
  41. perf_event__handler_t process,
  42. struct machine *machine)
  43. {
  44. union perf_event event = {
  45. .time_conv = {
  46. .header = {
  47. .type = PERF_RECORD_TIME_CONV,
  48. .size = sizeof(struct time_conv_event),
  49. },
  50. },
  51. };
  52. struct perf_tsc_conversion tc;
  53. int err;
  54. if (!pc)
  55. return 0;
  56. err = perf_read_tsc_conversion(pc, &tc);
  57. if (err == -EOPNOTSUPP)
  58. return 0;
  59. if (err)
  60. return err;
  61. pr_debug2("Synthesizing TSC conversion information\n");
  62. event.time_conv.time_mult = tc.time_mult;
  63. event.time_conv.time_shift = tc.time_shift;
  64. event.time_conv.time_zero = tc.time_zero;
  65. return process(tool, &event, NULL, machine);
  66. }