trace_clock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * tracing clocks
  3. *
  4. * Copyright (C) 2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Implements 3 trace clock variants, with differing scalability/precision
  7. * tradeoffs:
  8. *
  9. * - local: CPU-local trace clock
  10. * - medium: scalable global clock with some jitter
  11. * - global: globally monotonic, serialized clock
  12. *
  13. * Tracer plugins will chose a default from these clocks.
  14. */
  15. #include <linux/spinlock.h>
  16. #include <linux/irqflags.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/module.h>
  19. #include <linux/percpu.h>
  20. #include <linux/sched.h>
  21. #include <linux/sched/clock.h>
  22. #include <linux/ktime.h>
  23. #include <linux/trace_clock.h>
  24. /*
  25. * trace_clock_local(): the simplest and least coherent tracing clock.
  26. *
  27. * Useful for tracing that does not cross to other CPUs nor
  28. * does it go through idle events.
  29. */
  30. u64 notrace trace_clock_local(void)
  31. {
  32. u64 clock;
  33. /*
  34. * sched_clock() is an architecture implemented, fast, scalable,
  35. * lockless clock. It is not guaranteed to be coherent across
  36. * CPUs, nor across CPU idle events.
  37. */
  38. preempt_disable_notrace();
  39. clock = sched_clock();
  40. preempt_enable_notrace();
  41. return clock;
  42. }
  43. EXPORT_SYMBOL_GPL(trace_clock_local);
  44. /*
  45. * trace_clock(): 'between' trace clock. Not completely serialized,
  46. * but not completely incorrect when crossing CPUs either.
  47. *
  48. * This is based on cpu_clock(), which will allow at most ~1 jiffy of
  49. * jitter between CPUs. So it's a pretty scalable clock, but there
  50. * can be offsets in the trace data.
  51. */
  52. u64 notrace trace_clock(void)
  53. {
  54. return local_clock();
  55. }
  56. EXPORT_SYMBOL_GPL(trace_clock);
  57. /*
  58. * trace_jiffy_clock(): Simply use jiffies as a clock counter.
  59. * Note that this use of jiffies_64 is not completely safe on
  60. * 32-bit systems. But the window is tiny, and the effect if
  61. * we are affected is that we will have an obviously bogus
  62. * timestamp on a trace event - i.e. not life threatening.
  63. */
  64. u64 notrace trace_clock_jiffies(void)
  65. {
  66. return jiffies_64_to_clock_t(jiffies_64 - INITIAL_JIFFIES);
  67. }
  68. EXPORT_SYMBOL_GPL(trace_clock_jiffies);
  69. /*
  70. * trace_clock_global(): special globally coherent trace clock
  71. *
  72. * It has higher overhead than the other trace clocks but is still
  73. * an order of magnitude faster than GTOD derived hardware clocks.
  74. *
  75. * Used by plugins that need globally coherent timestamps.
  76. */
  77. /* keep prev_time and lock in the same cacheline. */
  78. static struct {
  79. u64 prev_time;
  80. arch_spinlock_t lock;
  81. } trace_clock_struct ____cacheline_aligned_in_smp =
  82. {
  83. .lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED,
  84. };
  85. u64 notrace trace_clock_global(void)
  86. {
  87. unsigned long flags;
  88. int this_cpu;
  89. u64 now, prev_time;
  90. local_irq_save(flags);
  91. this_cpu = raw_smp_processor_id();
  92. /*
  93. * The global clock "guarantees" that the events are ordered
  94. * between CPUs. But if two events on two different CPUS call
  95. * trace_clock_global at roughly the same time, it really does
  96. * not matter which one gets the earlier time. Just make sure
  97. * that the same CPU will always show a monotonic clock.
  98. *
  99. * Use a read memory barrier to get the latest written
  100. * time that was recorded.
  101. */
  102. smp_rmb();
  103. prev_time = READ_ONCE(trace_clock_struct.prev_time);
  104. now = sched_clock_cpu(this_cpu);
  105. /* Make sure that now is always greater than or equal to prev_time */
  106. if ((s64)(now - prev_time) < 0)
  107. now = prev_time;
  108. /*
  109. * If in an NMI context then dont risk lockups and simply return
  110. * the current time.
  111. */
  112. if (unlikely(in_nmi()))
  113. goto out;
  114. /* Tracing can cause strange recursion, always use a try lock */
  115. if (arch_spin_trylock(&trace_clock_struct.lock)) {
  116. /* Reread prev_time in case it was already updated */
  117. prev_time = READ_ONCE(trace_clock_struct.prev_time);
  118. if ((s64)(now - prev_time) < 0)
  119. now = prev_time;
  120. trace_clock_struct.prev_time = now;
  121. /* The unlock acts as the wmb for the above rmb */
  122. arch_spin_unlock(&trace_clock_struct.lock);
  123. }
  124. out:
  125. local_irq_restore(flags);
  126. return now;
  127. }
  128. EXPORT_SYMBOL_GPL(trace_clock_global);
  129. static atomic64_t trace_counter;
  130. /*
  131. * trace_clock_counter(): simply an atomic counter.
  132. * Use the trace_counter "counter" for cases where you do not care
  133. * about timings, but are interested in strict ordering.
  134. */
  135. u64 notrace trace_clock_counter(void)
  136. {
  137. return atomic64_add_return(1, &trace_counter);
  138. }