trace_clock.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/hardirq.h>
  17. #include <linux/module.h>
  18. #include <linux/percpu.h>
  19. #include <linux/sched.h>
  20. #include <linux/ktime.h>
  21. /*
  22. * trace_clock_local(): the simplest and least coherent tracing clock.
  23. *
  24. * Useful for tracing that does not cross to other CPUs nor
  25. * does it go through idle events.
  26. */
  27. u64 notrace trace_clock_local(void)
  28. {
  29. unsigned long flags;
  30. u64 clock;
  31. /*
  32. * sched_clock() is an architecture implemented, fast, scalable,
  33. * lockless clock. It is not guaranteed to be coherent across
  34. * CPUs, nor across CPU idle events.
  35. */
  36. raw_local_irq_save(flags);
  37. clock = sched_clock();
  38. raw_local_irq_restore(flags);
  39. return clock;
  40. }
  41. /*
  42. * trace_clock(): 'inbetween' trace clock. Not completely serialized,
  43. * but not completely incorrect when crossing CPUs either.
  44. *
  45. * This is based on cpu_clock(), which will allow at most ~1 jiffy of
  46. * jitter between CPUs. So it's a pretty scalable clock, but there
  47. * can be offsets in the trace data.
  48. */
  49. u64 notrace trace_clock(void)
  50. {
  51. return cpu_clock(raw_smp_processor_id());
  52. }
  53. /*
  54. * trace_clock_global(): special globally coherent trace clock
  55. *
  56. * It has higher overhead than the other trace clocks but is still
  57. * an order of magnitude faster than GTOD derived hardware clocks.
  58. *
  59. * Used by plugins that need globally coherent timestamps.
  60. */
  61. static u64 prev_trace_clock_time;
  62. static raw_spinlock_t trace_clock_lock ____cacheline_aligned_in_smp =
  63. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  64. u64 notrace trace_clock_global(void)
  65. {
  66. unsigned long flags;
  67. int this_cpu;
  68. u64 now;
  69. raw_local_irq_save(flags);
  70. this_cpu = raw_smp_processor_id();
  71. now = cpu_clock(this_cpu);
  72. /*
  73. * If in an NMI context then dont risk lockups and return the
  74. * cpu_clock() time:
  75. */
  76. if (unlikely(in_nmi()))
  77. goto out;
  78. __raw_spin_lock(&trace_clock_lock);
  79. /*
  80. * TODO: if this happens often then maybe we should reset
  81. * my_scd->clock to prev_trace_clock_time+1, to make sure
  82. * we start ticking with the local clock from now on?
  83. */
  84. if ((s64)(now - prev_trace_clock_time) < 0)
  85. now = prev_trace_clock_time + 1;
  86. prev_trace_clock_time = now;
  87. __raw_spin_unlock(&trace_clock_lock);
  88. out:
  89. raw_local_irq_restore(flags);
  90. return now;
  91. }