trace_sysprof.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * Copyright (C) 2004, 2005, Soeren Sandmann
  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/irq.h>
  15. #include <linux/fs.h>
  16. #include "trace.h"
  17. static struct trace_array *sysprof_trace;
  18. static int __read_mostly tracer_enabled;
  19. /*
  20. * 10 msecs for now:
  21. */
  22. static const unsigned long sample_period = 1000000;
  23. static const unsigned int sample_max_depth = 512;
  24. /*
  25. * Per CPU hrtimers that do the profiling:
  26. */
  27. static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
  28. struct stack_frame {
  29. const void __user *next_fp;
  30. unsigned long return_address;
  31. };
  32. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  33. {
  34. int ret;
  35. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  36. return 0;
  37. ret = 1;
  38. pagefault_disable();
  39. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  40. ret = 0;
  41. pagefault_enable();
  42. return ret;
  43. }
  44. static void timer_notify(struct pt_regs *regs, int cpu)
  45. {
  46. struct trace_array_cpu *data;
  47. struct stack_frame frame;
  48. struct trace_array *tr;
  49. const void __user *fp;
  50. int is_user;
  51. int i;
  52. if (!regs)
  53. return;
  54. tr = sysprof_trace;
  55. data = tr->data[cpu];
  56. is_user = user_mode(regs);
  57. if (!current || current->pid == 0)
  58. return;
  59. if (is_user && current->state != TASK_RUNNING)
  60. return;
  61. if (!is_user) {
  62. /* kernel */
  63. ftrace(tr, data, current->pid, 1, 0);
  64. return;
  65. }
  66. trace_special(tr, data, 0, current->pid, regs->ip);
  67. fp = (void __user *)regs->bp;
  68. for (i = 0; i < sample_max_depth; i++) {
  69. frame.next_fp = 0;
  70. frame.return_address = 0;
  71. if (!copy_stack_frame(fp, &frame))
  72. break;
  73. if ((unsigned long)fp < regs->sp)
  74. break;
  75. trace_special(tr, data, 1, frame.return_address,
  76. (unsigned long)fp);
  77. fp = frame.next_fp;
  78. }
  79. trace_special(tr, data, 2, current->pid, i);
  80. /*
  81. * Special trace entry if we overflow the max depth:
  82. */
  83. if (i == sample_max_depth)
  84. trace_special(tr, data, -1, -1, -1);
  85. }
  86. static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
  87. {
  88. /* trace here */
  89. timer_notify(get_irq_regs(), smp_processor_id());
  90. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  91. return HRTIMER_RESTART;
  92. }
  93. static void start_stack_timer(int cpu)
  94. {
  95. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  96. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  97. hrtimer->function = stack_trace_timer_fn;
  98. hrtimer->cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  99. hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL);
  100. }
  101. static void start_stack_timers(void)
  102. {
  103. cpumask_t saved_mask = current->cpus_allowed;
  104. int cpu;
  105. for_each_online_cpu(cpu) {
  106. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  107. start_stack_timer(cpu);
  108. }
  109. set_cpus_allowed_ptr(current, &saved_mask);
  110. }
  111. static void stop_stack_timer(int cpu)
  112. {
  113. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  114. hrtimer_cancel(hrtimer);
  115. }
  116. static void stop_stack_timers(void)
  117. {
  118. int cpu;
  119. for_each_online_cpu(cpu)
  120. stop_stack_timer(cpu);
  121. }
  122. static notrace void stack_reset(struct trace_array *tr)
  123. {
  124. int cpu;
  125. tr->time_start = ftrace_now(tr->cpu);
  126. for_each_online_cpu(cpu)
  127. tracing_reset(tr->data[cpu]);
  128. }
  129. static notrace void start_stack_trace(struct trace_array *tr)
  130. {
  131. stack_reset(tr);
  132. start_stack_timers();
  133. tracer_enabled = 1;
  134. }
  135. static notrace void stop_stack_trace(struct trace_array *tr)
  136. {
  137. stop_stack_timers();
  138. tracer_enabled = 0;
  139. }
  140. static notrace void stack_trace_init(struct trace_array *tr)
  141. {
  142. sysprof_trace = tr;
  143. if (tr->ctrl)
  144. start_stack_trace(tr);
  145. }
  146. static notrace void stack_trace_reset(struct trace_array *tr)
  147. {
  148. if (tr->ctrl)
  149. stop_stack_trace(tr);
  150. }
  151. static void stack_trace_ctrl_update(struct trace_array *tr)
  152. {
  153. /* When starting a new trace, reset the buffers */
  154. if (tr->ctrl)
  155. start_stack_trace(tr);
  156. else
  157. stop_stack_trace(tr);
  158. }
  159. static struct tracer stack_trace __read_mostly =
  160. {
  161. .name = "sysprof",
  162. .init = stack_trace_init,
  163. .reset = stack_trace_reset,
  164. .ctrl_update = stack_trace_ctrl_update,
  165. #ifdef CONFIG_FTRACE_SELFTEST
  166. .selftest = trace_selftest_startup_sysprof,
  167. #endif
  168. };
  169. __init static int init_stack_trace(void)
  170. {
  171. return register_tracer(&stack_trace);
  172. }
  173. device_initcall(init_stack_trace);