trace_irqsoff.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * trace irqs off criticall timings
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * From code in the latency_tracer, that is:
  8. *
  9. * Copyright (C) 2004-2006 Ingo Molnar
  10. * Copyright (C) 2004 William Lee Irwin III
  11. */
  12. #include <linux/kallsyms.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/module.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/fs.h>
  18. #include "trace.h"
  19. static struct trace_array *irqsoff_trace __read_mostly;
  20. static int tracer_enabled __read_mostly;
  21. /*
  22. * Sequence count - we record it when starting a measurement and
  23. * skip the latency if the sequence has changed - some other section
  24. * did a maximum and could disturb our measurement with serial console
  25. * printouts, etc. Truly coinciding maximum latencies should be rare
  26. * and what happens together happens separately as well, so this doesnt
  27. * decrease the validity of the maximum found:
  28. */
  29. static __cacheline_aligned_in_smp unsigned long max_sequence;
  30. #ifdef CONFIG_FTRACE
  31. /*
  32. * irqsoff uses its own tracer function to keep the overhead down:
  33. */
  34. static void notrace
  35. irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
  36. {
  37. struct trace_array *tr = irqsoff_trace;
  38. struct trace_array_cpu *data;
  39. unsigned long flags;
  40. long disabled;
  41. int cpu;
  42. if (likely(!tracer_enabled))
  43. return;
  44. local_save_flags(flags);
  45. if (!irqs_disabled_flags(flags))
  46. return;
  47. cpu = raw_smp_processor_id();
  48. data = tr->data[cpu];
  49. disabled = atomic_inc_return(&data->disabled);
  50. if (likely(disabled == 1))
  51. ftrace(tr, data, ip, parent_ip, flags);
  52. atomic_dec(&data->disabled);
  53. }
  54. static struct ftrace_ops trace_ops __read_mostly =
  55. {
  56. .func = irqsoff_tracer_call,
  57. };
  58. #endif /* CONFIG_FTRACE */
  59. /*
  60. * Should this new latency be reported/recorded?
  61. */
  62. static int notrace report_latency(cycle_t delta)
  63. {
  64. if (tracing_thresh) {
  65. if (delta < tracing_thresh)
  66. return 0;
  67. } else {
  68. if (delta <= tracing_max_latency)
  69. return 0;
  70. }
  71. return 1;
  72. }
  73. static void notrace
  74. check_critical_timing(struct trace_array *tr,
  75. struct trace_array_cpu *data,
  76. unsigned long parent_ip,
  77. int cpu)
  78. {
  79. unsigned long latency, t0, t1;
  80. cycle_t T0, T1, T2, delta;
  81. unsigned long flags;
  82. /*
  83. * usecs conversion is slow so we try to delay the conversion
  84. * as long as possible:
  85. */
  86. T0 = data->preempt_timestamp;
  87. T1 = now(cpu);
  88. delta = T1-T0;
  89. local_save_flags(flags);
  90. if (!report_latency(delta))
  91. goto out;
  92. ftrace(tr, data, CALLER_ADDR0, parent_ip, flags);
  93. /*
  94. * Update the timestamp, because the trace entry above
  95. * might change it (it can only get larger so the latency
  96. * is fair to be reported):
  97. */
  98. T2 = now(cpu);
  99. delta = T2-T0;
  100. latency = nsecs_to_usecs(delta);
  101. if (data->critical_sequence != max_sequence)
  102. goto out;
  103. tracing_max_latency = delta;
  104. t0 = nsecs_to_usecs(T0);
  105. t1 = nsecs_to_usecs(T1);
  106. data->critical_end = parent_ip;
  107. update_max_tr_single(tr, current, cpu);
  108. if (tracing_thresh)
  109. printk(KERN_INFO "(%16s-%-5d|#%d): %lu us critical section "
  110. "violates %lu us threshold.\n"
  111. " => started at timestamp %lu: ",
  112. current->comm, current->pid,
  113. raw_smp_processor_id(),
  114. latency, nsecs_to_usecs(tracing_thresh), t0);
  115. else
  116. printk(KERN_INFO "(%16s-%-5d|#%d):"
  117. " new %lu us maximum-latency "
  118. "critical section.\n => started at timestamp %lu: ",
  119. current->comm, current->pid,
  120. raw_smp_processor_id(),
  121. latency, t0);
  122. print_symbol(KERN_CONT "<%s>\n", data->critical_start);
  123. printk(KERN_CONT " => ended at timestamp %lu: ", t1);
  124. print_symbol(KERN_CONT "<%s>\n", data->critical_end);
  125. dump_stack();
  126. t1 = nsecs_to_usecs(now(cpu));
  127. printk(KERN_CONT " => dump-end timestamp %lu\n\n", t1);
  128. max_sequence++;
  129. out:
  130. data->critical_sequence = max_sequence;
  131. data->preempt_timestamp = now(cpu);
  132. tracing_reset(data);
  133. ftrace(tr, data, CALLER_ADDR0, parent_ip, flags);
  134. }
  135. static inline void notrace
  136. start_critical_timing(unsigned long ip, unsigned long parent_ip)
  137. {
  138. int cpu;
  139. struct trace_array *tr = irqsoff_trace;
  140. struct trace_array_cpu *data;
  141. unsigned long flags;
  142. if (likely(!tracer_enabled))
  143. return;
  144. cpu = raw_smp_processor_id();
  145. data = tr->data[cpu];
  146. if (unlikely(!data) || unlikely(!data->trace) ||
  147. data->critical_start || atomic_read(&data->disabled))
  148. return;
  149. atomic_inc(&data->disabled);
  150. data->critical_sequence = max_sequence;
  151. data->preempt_timestamp = now(cpu);
  152. data->critical_start = parent_ip;
  153. tracing_reset(data);
  154. local_save_flags(flags);
  155. ftrace(tr, data, ip, parent_ip, flags);
  156. atomic_dec(&data->disabled);
  157. }
  158. static inline void notrace
  159. stop_critical_timing(unsigned long ip, unsigned long parent_ip)
  160. {
  161. int cpu;
  162. struct trace_array *tr = irqsoff_trace;
  163. struct trace_array_cpu *data;
  164. unsigned long flags;
  165. if (likely(!tracer_enabled))
  166. return;
  167. cpu = raw_smp_processor_id();
  168. data = tr->data[cpu];
  169. if (unlikely(!data) || unlikely(!data->trace) ||
  170. !data->critical_start || atomic_read(&data->disabled))
  171. return;
  172. atomic_inc(&data->disabled);
  173. local_save_flags(flags);
  174. ftrace(tr, data, ip, parent_ip, flags);
  175. check_critical_timing(tr, data, parent_ip, cpu);
  176. data->critical_start = 0;
  177. atomic_dec(&data->disabled);
  178. }
  179. void notrace start_critical_timings(void)
  180. {
  181. unsigned long flags;
  182. local_save_flags(flags);
  183. if (irqs_disabled_flags(flags))
  184. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  185. }
  186. void notrace stop_critical_timings(void)
  187. {
  188. unsigned long flags;
  189. local_save_flags(flags);
  190. if (irqs_disabled_flags(flags))
  191. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  192. }
  193. #ifdef CONFIG_PROVE_LOCKING
  194. void notrace time_hardirqs_on(unsigned long a0, unsigned long a1)
  195. {
  196. unsigned long flags;
  197. local_save_flags(flags);
  198. if (irqs_disabled_flags(flags))
  199. stop_critical_timing(a0, a1);
  200. }
  201. void notrace time_hardirqs_off(unsigned long a0, unsigned long a1)
  202. {
  203. unsigned long flags;
  204. local_save_flags(flags);
  205. if (irqs_disabled_flags(flags))
  206. start_critical_timing(a0, a1);
  207. }
  208. #else /* !CONFIG_PROVE_LOCKING */
  209. /*
  210. * Stubs:
  211. */
  212. void early_boot_irqs_off(void)
  213. {
  214. }
  215. void early_boot_irqs_on(void)
  216. {
  217. }
  218. void trace_softirqs_on(unsigned long ip)
  219. {
  220. }
  221. void trace_softirqs_off(unsigned long ip)
  222. {
  223. }
  224. inline void print_irqtrace_events(struct task_struct *curr)
  225. {
  226. }
  227. /*
  228. * We are only interested in hardirq on/off events:
  229. */
  230. void notrace trace_hardirqs_on(void)
  231. {
  232. unsigned long flags;
  233. local_save_flags(flags);
  234. if (irqs_disabled_flags(flags))
  235. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  236. }
  237. EXPORT_SYMBOL(trace_hardirqs_on);
  238. void notrace trace_hardirqs_off(void)
  239. {
  240. unsigned long flags;
  241. local_save_flags(flags);
  242. if (irqs_disabled_flags(flags))
  243. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  244. }
  245. EXPORT_SYMBOL(trace_hardirqs_off);
  246. void notrace trace_hardirqs_on_caller(unsigned long caller_addr)
  247. {
  248. unsigned long flags;
  249. local_save_flags(flags);
  250. if (irqs_disabled_flags(flags))
  251. stop_critical_timing(CALLER_ADDR0, caller_addr);
  252. }
  253. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  254. void notrace trace_hardirqs_off_caller(unsigned long caller_addr)
  255. {
  256. unsigned long flags;
  257. local_save_flags(flags);
  258. if (irqs_disabled_flags(flags))
  259. start_critical_timing(CALLER_ADDR0, caller_addr);
  260. }
  261. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  262. #endif /* CONFIG_PROVE_LOCKING */
  263. static void start_irqsoff_tracer(struct trace_array *tr)
  264. {
  265. tracer_enabled = 1;
  266. register_ftrace_function(&trace_ops);
  267. }
  268. static void stop_irqsoff_tracer(struct trace_array *tr)
  269. {
  270. unregister_ftrace_function(&trace_ops);
  271. tracer_enabled = 0;
  272. }
  273. static void irqsoff_tracer_init(struct trace_array *tr)
  274. {
  275. irqsoff_trace = tr;
  276. /* make sure that the tracer is visibel */
  277. smp_wmb();
  278. if (tr->ctrl)
  279. start_irqsoff_tracer(tr);
  280. }
  281. static void irqsoff_tracer_reset(struct trace_array *tr)
  282. {
  283. if (tr->ctrl)
  284. stop_irqsoff_tracer(tr);
  285. }
  286. static void irqsoff_tracer_ctrl_update(struct trace_array *tr)
  287. {
  288. if (tr->ctrl)
  289. start_irqsoff_tracer(tr);
  290. else
  291. stop_irqsoff_tracer(tr);
  292. }
  293. static void notrace irqsoff_tracer_open(struct trace_iterator *iter)
  294. {
  295. /* stop the trace while dumping */
  296. if (iter->tr->ctrl)
  297. stop_irqsoff_tracer(iter->tr);
  298. }
  299. static void notrace irqsoff_tracer_close(struct trace_iterator *iter)
  300. {
  301. if (iter->tr->ctrl)
  302. start_irqsoff_tracer(iter->tr);
  303. }
  304. static struct tracer irqsoff_tracer __read_mostly =
  305. {
  306. .name = "irqsoff",
  307. .init = irqsoff_tracer_init,
  308. .reset = irqsoff_tracer_reset,
  309. .open = irqsoff_tracer_open,
  310. .close = irqsoff_tracer_close,
  311. .ctrl_update = irqsoff_tracer_ctrl_update,
  312. .print_max = 1,
  313. };
  314. __init static int init_irqsoff_tracer(void)
  315. {
  316. register_tracer(&irqsoff_tracer);
  317. return 0;
  318. }
  319. device_initcall(init_irqsoff_tracer);