trace_sysprof.c 4.3 KB

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