trace_sysprof.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * 1 msec sample interval by default:
  21. */
  22. static unsigned long sample_period = 1000000;
  23. static const unsigned int sample_max_depth = 512;
  24. static DEFINE_MUTEX(sample_timer_lock);
  25. /*
  26. * Per CPU hrtimers that do the profiling:
  27. */
  28. static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
  29. struct stack_frame {
  30. const void __user *next_fp;
  31. unsigned long return_address;
  32. };
  33. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  34. {
  35. int ret;
  36. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  37. return 0;
  38. ret = 1;
  39. pagefault_disable();
  40. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  41. ret = 0;
  42. pagefault_enable();
  43. return ret;
  44. }
  45. static void timer_notify(struct pt_regs *regs, int cpu)
  46. {
  47. struct trace_array_cpu *data;
  48. struct stack_frame frame;
  49. struct trace_array *tr;
  50. const void __user *fp;
  51. int is_user;
  52. int i;
  53. if (!regs)
  54. return;
  55. tr = sysprof_trace;
  56. data = tr->data[cpu];
  57. is_user = user_mode(regs);
  58. if (!current || current->pid == 0)
  59. return;
  60. if (is_user && current->state != TASK_RUNNING)
  61. return;
  62. if (!is_user) {
  63. /* kernel */
  64. ftrace(tr, data, current->pid, 1, 0);
  65. return;
  66. }
  67. trace_special(tr, data, 0, current->pid, regs->ip);
  68. fp = (void __user *)regs->bp;
  69. for (i = 0; i < sample_max_depth; i++) {
  70. frame.next_fp = 0;
  71. frame.return_address = 0;
  72. if (!copy_stack_frame(fp, &frame))
  73. break;
  74. if ((unsigned long)fp < regs->sp)
  75. break;
  76. trace_special(tr, data, 1, frame.return_address,
  77. (unsigned long)fp);
  78. fp = frame.next_fp;
  79. }
  80. trace_special(tr, data, 2, current->pid, i);
  81. /*
  82. * Special trace entry if we overflow the max depth:
  83. */
  84. if (i == sample_max_depth)
  85. trace_special(tr, data, -1, -1, -1);
  86. }
  87. static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
  88. {
  89. /* trace here */
  90. timer_notify(get_irq_regs(), smp_processor_id());
  91. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  92. return HRTIMER_RESTART;
  93. }
  94. static void start_stack_timer(int cpu)
  95. {
  96. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  97. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  98. hrtimer->function = stack_trace_timer_fn;
  99. hrtimer->cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  100. hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL);
  101. }
  102. static void start_stack_timers(void)
  103. {
  104. cpumask_t saved_mask = current->cpus_allowed;
  105. int cpu;
  106. for_each_online_cpu(cpu) {
  107. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  108. start_stack_timer(cpu);
  109. }
  110. set_cpus_allowed_ptr(current, &saved_mask);
  111. }
  112. static void stop_stack_timer(int cpu)
  113. {
  114. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  115. hrtimer_cancel(hrtimer);
  116. }
  117. static void stop_stack_timers(void)
  118. {
  119. int cpu;
  120. for_each_online_cpu(cpu)
  121. stop_stack_timer(cpu);
  122. }
  123. static void stack_reset(struct trace_array *tr)
  124. {
  125. int cpu;
  126. tr->time_start = ftrace_now(tr->cpu);
  127. for_each_online_cpu(cpu)
  128. tracing_reset(tr->data[cpu]);
  129. }
  130. static void start_stack_trace(struct trace_array *tr)
  131. {
  132. mutex_lock(&sample_timer_lock);
  133. stack_reset(tr);
  134. start_stack_timers();
  135. tracer_enabled = 1;
  136. mutex_unlock(&sample_timer_lock);
  137. }
  138. static void stop_stack_trace(struct trace_array *tr)
  139. {
  140. mutex_lock(&sample_timer_lock);
  141. stop_stack_timers();
  142. tracer_enabled = 0;
  143. mutex_unlock(&sample_timer_lock);
  144. }
  145. static void stack_trace_init(struct trace_array *tr)
  146. {
  147. sysprof_trace = tr;
  148. if (tr->ctrl)
  149. start_stack_trace(tr);
  150. }
  151. static void stack_trace_reset(struct trace_array *tr)
  152. {
  153. if (tr->ctrl)
  154. stop_stack_trace(tr);
  155. }
  156. static void stack_trace_ctrl_update(struct trace_array *tr)
  157. {
  158. /* When starting a new trace, reset the buffers */
  159. if (tr->ctrl)
  160. start_stack_trace(tr);
  161. else
  162. stop_stack_trace(tr);
  163. }
  164. static struct tracer stack_trace __read_mostly =
  165. {
  166. .name = "sysprof",
  167. .init = stack_trace_init,
  168. .reset = stack_trace_reset,
  169. .ctrl_update = stack_trace_ctrl_update,
  170. #ifdef CONFIG_FTRACE_SELFTEST
  171. .selftest = trace_selftest_startup_sysprof,
  172. #endif
  173. };
  174. __init static int init_stack_trace(void)
  175. {
  176. return register_tracer(&stack_trace);
  177. }
  178. device_initcall(init_stack_trace);
  179. #define MAX_LONG_DIGITS 22
  180. static ssize_t
  181. sysprof_sample_read(struct file *filp, char __user *ubuf,
  182. size_t cnt, loff_t *ppos)
  183. {
  184. char buf[MAX_LONG_DIGITS];
  185. int r;
  186. r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
  187. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  188. }
  189. static ssize_t
  190. sysprof_sample_write(struct file *filp, const char __user *ubuf,
  191. size_t cnt, loff_t *ppos)
  192. {
  193. char buf[MAX_LONG_DIGITS];
  194. unsigned long val;
  195. if (cnt > MAX_LONG_DIGITS-1)
  196. cnt = MAX_LONG_DIGITS-1;
  197. if (copy_from_user(&buf, ubuf, cnt))
  198. return -EFAULT;
  199. buf[cnt] = 0;
  200. val = simple_strtoul(buf, NULL, 10);
  201. /*
  202. * Enforce a minimum sample period of 100 usecs:
  203. */
  204. if (val < 100)
  205. val = 100;
  206. mutex_lock(&sample_timer_lock);
  207. stop_stack_timers();
  208. sample_period = val * 1000;
  209. start_stack_timers();
  210. mutex_unlock(&sample_timer_lock);
  211. return cnt;
  212. }
  213. static struct file_operations sysprof_sample_fops = {
  214. .read = sysprof_sample_read,
  215. .write = sysprof_sample_write,
  216. };
  217. void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
  218. {
  219. struct dentry *entry;
  220. entry = debugfs_create_file("sysprof_sample_period", 0644,
  221. d_tracer, NULL, &sysprof_sample_fops);
  222. if (entry)
  223. return;
  224. pr_warning("Could not create debugfs 'dyn_ftrace_total_info' entry\n");
  225. }