trace_sysprof.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * trace stack traces
  3. *
  4. * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. */
  8. #include <linux/kallsyms.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/hrtimer.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include "trace.h"
  16. static struct trace_array *ctx_trace;
  17. static int __read_mostly tracer_enabled;
  18. static const unsigned long sample_period = 1000000;
  19. /*
  20. * Per CPU hrtimers that do the profiling:
  21. */
  22. static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
  23. static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
  24. {
  25. /* trace here */
  26. panic_timeout++;
  27. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  28. return HRTIMER_RESTART;
  29. }
  30. static void start_stack_timer(int cpu)
  31. {
  32. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  33. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  34. hrtimer->function = stack_trace_timer_fn;
  35. hrtimer->cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  36. hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL);
  37. }
  38. static void start_stack_timers(void)
  39. {
  40. cpumask_t saved_mask = current->cpus_allowed;
  41. int cpu;
  42. for_each_online_cpu(cpu) {
  43. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  44. start_stack_timer(cpu);
  45. printk("started timer on cpu%d\n", cpu);
  46. }
  47. set_cpus_allowed_ptr(current, &saved_mask);
  48. }
  49. static void stop_stack_timer(int cpu)
  50. {
  51. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  52. hrtimer_cancel(hrtimer);
  53. printk("cancelled timer on cpu%d\n", cpu);
  54. }
  55. static void stop_stack_timers(void)
  56. {
  57. int cpu;
  58. for_each_online_cpu(cpu)
  59. stop_stack_timer(cpu);
  60. }
  61. static notrace void stack_reset(struct trace_array *tr)
  62. {
  63. int cpu;
  64. tr->time_start = ftrace_now(tr->cpu);
  65. for_each_online_cpu(cpu)
  66. tracing_reset(tr->data[cpu]);
  67. }
  68. static notrace void start_stack_trace(struct trace_array *tr)
  69. {
  70. stack_reset(tr);
  71. start_stack_timers();
  72. tracer_enabled = 1;
  73. }
  74. static notrace void stop_stack_trace(struct trace_array *tr)
  75. {
  76. stop_stack_timers();
  77. tracer_enabled = 0;
  78. }
  79. static notrace void stack_trace_init(struct trace_array *tr)
  80. {
  81. ctx_trace = tr;
  82. if (tr->ctrl)
  83. start_stack_trace(tr);
  84. }
  85. static notrace void stack_trace_reset(struct trace_array *tr)
  86. {
  87. if (tr->ctrl)
  88. stop_stack_trace(tr);
  89. }
  90. static void stack_trace_ctrl_update(struct trace_array *tr)
  91. {
  92. /* When starting a new trace, reset the buffers */
  93. if (tr->ctrl)
  94. start_stack_trace(tr);
  95. else
  96. stop_stack_trace(tr);
  97. }
  98. static struct tracer stack_trace __read_mostly =
  99. {
  100. .name = "sysprof",
  101. .init = stack_trace_init,
  102. .reset = stack_trace_reset,
  103. .ctrl_update = stack_trace_ctrl_update,
  104. #ifdef CONFIG_FTRACE_SELFTEST
  105. .selftest = trace_selftest_startup_stack,
  106. #endif
  107. };
  108. __init static int init_stack_trace(void)
  109. {
  110. return register_tracer(&stack_trace);
  111. }
  112. device_initcall(init_stack_trace);