trace_sysprof.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * trace stack traces
  3. *
  4. * Copyright (C) 2004-2008, Soeren Sandmann
  5. * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
  6. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  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 <asm/stacktrace.h>
  17. #include "trace.h"
  18. static struct trace_array *sysprof_trace;
  19. static int __read_mostly tracer_enabled;
  20. /*
  21. * 1 msec sample interval by default:
  22. */
  23. static unsigned long sample_period = 1000000;
  24. static const unsigned int sample_max_depth = 512;
  25. static DEFINE_MUTEX(sample_timer_lock);
  26. /*
  27. * Per CPU hrtimers that do the profiling:
  28. */
  29. static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
  30. struct stack_frame {
  31. const void __user *next_fp;
  32. unsigned long return_address;
  33. };
  34. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  35. {
  36. int ret;
  37. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  38. return 0;
  39. ret = 1;
  40. pagefault_disable();
  41. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  42. ret = 0;
  43. pagefault_enable();
  44. return ret;
  45. }
  46. struct backtrace_info {
  47. struct trace_array_cpu *data;
  48. struct trace_array *tr;
  49. int pos;
  50. };
  51. static void
  52. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  53. {
  54. /* Ignore warnings */
  55. }
  56. static void backtrace_warning(void *data, char *msg)
  57. {
  58. /* Ignore warnings */
  59. }
  60. static int backtrace_stack(void *data, char *name)
  61. {
  62. /* Don't bother with IRQ stacks for now */
  63. return -1;
  64. }
  65. static void backtrace_address(void *data, unsigned long addr, int reliable)
  66. {
  67. struct backtrace_info *info = data;
  68. if (info->pos < sample_max_depth && reliable) {
  69. __trace_special(info->tr, info->data, 1, addr, 0);
  70. info->pos++;
  71. }
  72. }
  73. const static struct stacktrace_ops backtrace_ops = {
  74. .warning = backtrace_warning,
  75. .warning_symbol = backtrace_warning_symbol,
  76. .stack = backtrace_stack,
  77. .address = backtrace_address,
  78. };
  79. static int
  80. trace_kernel(struct pt_regs *regs, struct trace_array *tr,
  81. struct trace_array_cpu *data)
  82. {
  83. struct backtrace_info info;
  84. unsigned long bp;
  85. char *stack;
  86. info.tr = tr;
  87. info.data = data;
  88. info.pos = 1;
  89. __trace_special(info.tr, info.data, 1, regs->ip, 0);
  90. stack = ((char *)regs + sizeof(struct pt_regs));
  91. #ifdef CONFIG_FRAME_POINTER
  92. bp = regs->bp;
  93. #else
  94. bp = 0;
  95. #endif
  96. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);
  97. return info.pos;
  98. }
  99. static void timer_notify(struct pt_regs *regs, int cpu)
  100. {
  101. struct trace_array_cpu *data;
  102. struct stack_frame frame;
  103. struct trace_array *tr;
  104. const void __user *fp;
  105. int is_user;
  106. int i;
  107. if (!regs)
  108. return;
  109. tr = sysprof_trace;
  110. data = tr->data[cpu];
  111. is_user = user_mode(regs);
  112. if (!current || current->pid == 0)
  113. return;
  114. if (is_user && current->state != TASK_RUNNING)
  115. return;
  116. __trace_special(tr, data, 0, 0, current->pid);
  117. if (!is_user)
  118. i = trace_kernel(regs, tr, data);
  119. else
  120. i = 0;
  121. /*
  122. * Trace user stack if we are not a kernel thread
  123. */
  124. if (current->mm && i < sample_max_depth) {
  125. regs = (struct pt_regs *)current->thread.sp0 - 1;
  126. fp = (void __user *)regs->bp;
  127. __trace_special(tr, data, 2, regs->ip, 0);
  128. while (i < sample_max_depth) {
  129. frame.next_fp = NULL;
  130. frame.return_address = 0;
  131. if (!copy_stack_frame(fp, &frame))
  132. break;
  133. if ((unsigned long)fp < regs->sp)
  134. break;
  135. __trace_special(tr, data, 2, frame.return_address,
  136. (unsigned long)fp);
  137. fp = frame.next_fp;
  138. i++;
  139. }
  140. }
  141. /*
  142. * Special trace entry if we overflow the max depth:
  143. */
  144. if (i == sample_max_depth)
  145. __trace_special(tr, data, -1, -1, -1);
  146. __trace_special(tr, data, 3, current->pid, i);
  147. }
  148. static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
  149. {
  150. /* trace here */
  151. timer_notify(get_irq_regs(), smp_processor_id());
  152. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  153. return HRTIMER_RESTART;
  154. }
  155. static void start_stack_timer(int cpu)
  156. {
  157. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  158. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  159. hrtimer->function = stack_trace_timer_fn;
  160. hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL);
  161. }
  162. static void start_stack_timers(void)
  163. {
  164. cpumask_t saved_mask = current->cpus_allowed;
  165. int cpu;
  166. for_each_online_cpu(cpu) {
  167. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  168. start_stack_timer(cpu);
  169. }
  170. set_cpus_allowed_ptr(current, &saved_mask);
  171. }
  172. static void stop_stack_timer(int cpu)
  173. {
  174. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  175. hrtimer_cancel(hrtimer);
  176. }
  177. static void stop_stack_timers(void)
  178. {
  179. int cpu;
  180. for_each_online_cpu(cpu)
  181. stop_stack_timer(cpu);
  182. }
  183. static void start_stack_trace(struct trace_array *tr)
  184. {
  185. mutex_lock(&sample_timer_lock);
  186. tracing_reset_online_cpus(tr);
  187. start_stack_timers();
  188. tracer_enabled = 1;
  189. mutex_unlock(&sample_timer_lock);
  190. }
  191. static void stop_stack_trace(struct trace_array *tr)
  192. {
  193. mutex_lock(&sample_timer_lock);
  194. stop_stack_timers();
  195. tracer_enabled = 0;
  196. mutex_unlock(&sample_timer_lock);
  197. }
  198. static int stack_trace_init(struct trace_array *tr)
  199. {
  200. sysprof_trace = tr;
  201. start_stack_trace(tr);
  202. return 0;
  203. }
  204. static void stack_trace_reset(struct trace_array *tr)
  205. {
  206. stop_stack_trace(tr);
  207. }
  208. static struct tracer stack_trace __read_mostly =
  209. {
  210. .name = "sysprof",
  211. .init = stack_trace_init,
  212. .reset = stack_trace_reset,
  213. #ifdef CONFIG_FTRACE_SELFTEST
  214. .selftest = trace_selftest_startup_sysprof,
  215. #endif
  216. };
  217. __init static int init_stack_trace(void)
  218. {
  219. return register_tracer(&stack_trace);
  220. }
  221. device_initcall(init_stack_trace);
  222. #define MAX_LONG_DIGITS 22
  223. static ssize_t
  224. sysprof_sample_read(struct file *filp, char __user *ubuf,
  225. size_t cnt, loff_t *ppos)
  226. {
  227. char buf[MAX_LONG_DIGITS];
  228. int r;
  229. r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
  230. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  231. }
  232. static ssize_t
  233. sysprof_sample_write(struct file *filp, const char __user *ubuf,
  234. size_t cnt, loff_t *ppos)
  235. {
  236. char buf[MAX_LONG_DIGITS];
  237. unsigned long val;
  238. if (cnt > MAX_LONG_DIGITS-1)
  239. cnt = MAX_LONG_DIGITS-1;
  240. if (copy_from_user(&buf, ubuf, cnt))
  241. return -EFAULT;
  242. buf[cnt] = 0;
  243. val = simple_strtoul(buf, NULL, 10);
  244. /*
  245. * Enforce a minimum sample period of 100 usecs:
  246. */
  247. if (val < 100)
  248. val = 100;
  249. mutex_lock(&sample_timer_lock);
  250. stop_stack_timers();
  251. sample_period = val * 1000;
  252. start_stack_timers();
  253. mutex_unlock(&sample_timer_lock);
  254. return cnt;
  255. }
  256. static struct file_operations sysprof_sample_fops = {
  257. .read = sysprof_sample_read,
  258. .write = sysprof_sample_write,
  259. };
  260. void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
  261. {
  262. struct dentry *entry;
  263. entry = debugfs_create_file("sysprof_sample_period", 0644,
  264. d_tracer, NULL, &sysprof_sample_fops);
  265. if (entry)
  266. return;
  267. pr_warning("Could not create debugfs 'dyn_ftrace_total_info' entry\n");
  268. }