trace_sysprof.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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->cb_mode = HRTIMER_CB_IRQSAFE_PERCPU;
  161. hrtimer_start(hrtimer, ns_to_ktime(sample_period), HRTIMER_MODE_REL);
  162. }
  163. static void start_stack_timers(void)
  164. {
  165. cpumask_t saved_mask = current->cpus_allowed;
  166. int cpu;
  167. for_each_online_cpu(cpu) {
  168. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  169. start_stack_timer(cpu);
  170. }
  171. set_cpus_allowed_ptr(current, &saved_mask);
  172. }
  173. static void stop_stack_timer(int cpu)
  174. {
  175. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  176. hrtimer_cancel(hrtimer);
  177. }
  178. static void stop_stack_timers(void)
  179. {
  180. int cpu;
  181. for_each_online_cpu(cpu)
  182. stop_stack_timer(cpu);
  183. }
  184. static void stack_reset(struct trace_array *tr)
  185. {
  186. int cpu;
  187. tr->time_start = ftrace_now(tr->cpu);
  188. for_each_online_cpu(cpu)
  189. tracing_reset(tr, cpu);
  190. }
  191. static void start_stack_trace(struct trace_array *tr)
  192. {
  193. mutex_lock(&sample_timer_lock);
  194. stack_reset(tr);
  195. start_stack_timers();
  196. tracer_enabled = 1;
  197. mutex_unlock(&sample_timer_lock);
  198. }
  199. static void stop_stack_trace(struct trace_array *tr)
  200. {
  201. mutex_lock(&sample_timer_lock);
  202. stop_stack_timers();
  203. tracer_enabled = 0;
  204. mutex_unlock(&sample_timer_lock);
  205. }
  206. static void stack_trace_init(struct trace_array *tr)
  207. {
  208. sysprof_trace = tr;
  209. if (tr->ctrl)
  210. start_stack_trace(tr);
  211. }
  212. static void stack_trace_reset(struct trace_array *tr)
  213. {
  214. if (tr->ctrl)
  215. stop_stack_trace(tr);
  216. }
  217. static void stack_trace_ctrl_update(struct trace_array *tr)
  218. {
  219. /* When starting a new trace, reset the buffers */
  220. if (tr->ctrl)
  221. start_stack_trace(tr);
  222. else
  223. stop_stack_trace(tr);
  224. }
  225. static struct tracer stack_trace __read_mostly =
  226. {
  227. .name = "sysprof",
  228. .init = stack_trace_init,
  229. .reset = stack_trace_reset,
  230. .ctrl_update = stack_trace_ctrl_update,
  231. #ifdef CONFIG_FTRACE_SELFTEST
  232. .selftest = trace_selftest_startup_sysprof,
  233. #endif
  234. };
  235. __init static int init_stack_trace(void)
  236. {
  237. return register_tracer(&stack_trace);
  238. }
  239. device_initcall(init_stack_trace);
  240. #define MAX_LONG_DIGITS 22
  241. static ssize_t
  242. sysprof_sample_read(struct file *filp, char __user *ubuf,
  243. size_t cnt, loff_t *ppos)
  244. {
  245. char buf[MAX_LONG_DIGITS];
  246. int r;
  247. r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
  248. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  249. }
  250. static ssize_t
  251. sysprof_sample_write(struct file *filp, const char __user *ubuf,
  252. size_t cnt, loff_t *ppos)
  253. {
  254. char buf[MAX_LONG_DIGITS];
  255. unsigned long val;
  256. if (cnt > MAX_LONG_DIGITS-1)
  257. cnt = MAX_LONG_DIGITS-1;
  258. if (copy_from_user(&buf, ubuf, cnt))
  259. return -EFAULT;
  260. buf[cnt] = 0;
  261. val = simple_strtoul(buf, NULL, 10);
  262. /*
  263. * Enforce a minimum sample period of 100 usecs:
  264. */
  265. if (val < 100)
  266. val = 100;
  267. mutex_lock(&sample_timer_lock);
  268. stop_stack_timers();
  269. sample_period = val * 1000;
  270. start_stack_timers();
  271. mutex_unlock(&sample_timer_lock);
  272. return cnt;
  273. }
  274. static struct file_operations sysprof_sample_fops = {
  275. .read = sysprof_sample_read,
  276. .write = sysprof_sample_write,
  277. };
  278. void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
  279. {
  280. struct dentry *entry;
  281. entry = debugfs_create_file("sysprof_sample_period", 0644,
  282. d_tracer, NULL, &sysprof_sample_fops);
  283. if (entry)
  284. return;
  285. pr_warning("Could not create debugfs 'dyn_ftrace_total_info' entry\n");
  286. }