trace_irqsoff.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. static DEFINE_PER_CPU(int, tracing_cpu);
  22. static DEFINE_SPINLOCK(max_trace_lock);
  23. enum {
  24. TRACER_IRQS_OFF = (1 << 1),
  25. TRACER_PREEMPT_OFF = (1 << 2),
  26. };
  27. static int trace_type __read_mostly;
  28. #ifdef CONFIG_PREEMPT_TRACER
  29. static inline int notrace
  30. preempt_trace(void)
  31. {
  32. return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
  33. }
  34. #else
  35. # define preempt_trace() (0)
  36. #endif
  37. #ifdef CONFIG_IRQSOFF_TRACER
  38. static inline int notrace
  39. irq_trace(void)
  40. {
  41. return ((trace_type & TRACER_IRQS_OFF) &&
  42. irqs_disabled());
  43. }
  44. #else
  45. # define irq_trace() (0)
  46. #endif
  47. /*
  48. * Sequence count - we record it when starting a measurement and
  49. * skip the latency if the sequence has changed - some other section
  50. * did a maximum and could disturb our measurement with serial console
  51. * printouts, etc. Truly coinciding maximum latencies should be rare
  52. * and what happens together happens separately as well, so this doesnt
  53. * decrease the validity of the maximum found:
  54. */
  55. static __cacheline_aligned_in_smp unsigned long max_sequence;
  56. #ifdef CONFIG_FTRACE
  57. /*
  58. * irqsoff uses its own tracer function to keep the overhead down:
  59. */
  60. static void notrace
  61. irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
  62. {
  63. struct trace_array *tr = irqsoff_trace;
  64. struct trace_array_cpu *data;
  65. unsigned long flags;
  66. long disabled;
  67. int cpu;
  68. /*
  69. * Does not matter if we preempt. We test the flags
  70. * afterward, to see if irqs are disabled or not.
  71. * If we preempt and get a false positive, the flags
  72. * test will fail.
  73. */
  74. cpu = raw_smp_processor_id();
  75. if (likely(!per_cpu(tracing_cpu, cpu)))
  76. return;
  77. local_save_flags(flags);
  78. /* slight chance to get a false positive on tracing_cpu */
  79. if (!irqs_disabled_flags(flags))
  80. return;
  81. data = tr->data[cpu];
  82. disabled = atomic_inc_return(&data->disabled);
  83. if (likely(disabled == 1))
  84. ftrace(tr, data, ip, parent_ip, flags);
  85. atomic_dec(&data->disabled);
  86. }
  87. static struct ftrace_ops trace_ops __read_mostly =
  88. {
  89. .func = irqsoff_tracer_call,
  90. };
  91. #endif /* CONFIG_FTRACE */
  92. /*
  93. * Should this new latency be reported/recorded?
  94. */
  95. static int notrace report_latency(cycle_t delta)
  96. {
  97. if (tracing_thresh) {
  98. if (delta < tracing_thresh)
  99. return 0;
  100. } else {
  101. if (delta <= tracing_max_latency)
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. static void notrace
  107. check_critical_timing(struct trace_array *tr,
  108. struct trace_array_cpu *data,
  109. unsigned long parent_ip,
  110. int cpu)
  111. {
  112. unsigned long latency, t0, t1;
  113. cycle_t T0, T1, delta;
  114. unsigned long flags;
  115. /*
  116. * usecs conversion is slow so we try to delay the conversion
  117. * as long as possible:
  118. */
  119. T0 = data->preempt_timestamp;
  120. T1 = now(cpu);
  121. delta = T1-T0;
  122. local_save_flags(flags);
  123. if (!report_latency(delta))
  124. goto out;
  125. spin_lock(&max_trace_lock);
  126. /* check if we are still the max latency */
  127. if (!report_latency(delta))
  128. goto out_unlock;
  129. ftrace(tr, data, CALLER_ADDR0, parent_ip, flags);
  130. latency = nsecs_to_usecs(delta);
  131. if (data->critical_sequence != max_sequence)
  132. goto out_unlock;
  133. tracing_max_latency = delta;
  134. t0 = nsecs_to_usecs(T0);
  135. t1 = nsecs_to_usecs(T1);
  136. data->critical_end = parent_ip;
  137. update_max_tr_single(tr, current, cpu);
  138. if (tracing_thresh)
  139. printk(KERN_INFO "(%16s-%-5d|#%d): %lu us critical section "
  140. "violates %lu us threshold.\n"
  141. " => started at timestamp %lu: ",
  142. current->comm, current->pid,
  143. raw_smp_processor_id(),
  144. latency, nsecs_to_usecs(tracing_thresh), t0);
  145. else
  146. printk(KERN_INFO "(%16s-%-5d|#%d):"
  147. " new %lu us maximum-latency "
  148. "critical section.\n => started at timestamp %lu: ",
  149. current->comm, current->pid,
  150. raw_smp_processor_id(),
  151. latency, t0);
  152. print_symbol(KERN_CONT "<%s>\n", data->critical_start);
  153. printk(KERN_CONT " => ended at timestamp %lu: ", t1);
  154. print_symbol(KERN_CONT "<%s>\n", data->critical_end);
  155. dump_stack();
  156. t1 = nsecs_to_usecs(now(cpu));
  157. printk(KERN_CONT " => dump-end timestamp %lu\n\n", t1);
  158. max_sequence++;
  159. out_unlock:
  160. spin_unlock(&max_trace_lock);
  161. out:
  162. data->critical_sequence = max_sequence;
  163. data->preempt_timestamp = now(cpu);
  164. tracing_reset(data);
  165. ftrace(tr, data, CALLER_ADDR0, parent_ip, flags);
  166. }
  167. static inline void notrace
  168. start_critical_timing(unsigned long ip, unsigned long parent_ip)
  169. {
  170. int cpu;
  171. struct trace_array *tr = irqsoff_trace;
  172. struct trace_array_cpu *data;
  173. unsigned long flags;
  174. if (likely(!tracer_enabled))
  175. return;
  176. if (__get_cpu_var(tracing_cpu))
  177. return;
  178. cpu = raw_smp_processor_id();
  179. data = tr->data[cpu];
  180. if (unlikely(!data) || unlikely(!data->trace) ||
  181. atomic_read(&data->disabled))
  182. return;
  183. atomic_inc(&data->disabled);
  184. data->critical_sequence = max_sequence;
  185. data->preempt_timestamp = now(cpu);
  186. data->critical_start = parent_ip ? : ip;
  187. tracing_reset(data);
  188. local_save_flags(flags);
  189. ftrace(tr, data, ip, parent_ip, flags);
  190. __get_cpu_var(tracing_cpu) = 1;
  191. atomic_dec(&data->disabled);
  192. }
  193. static inline void notrace
  194. stop_critical_timing(unsigned long ip, unsigned long parent_ip)
  195. {
  196. int cpu;
  197. struct trace_array *tr = irqsoff_trace;
  198. struct trace_array_cpu *data;
  199. unsigned long flags;
  200. /* Always clear the tracing cpu on stopping the trace */
  201. if (unlikely(__get_cpu_var(tracing_cpu)))
  202. __get_cpu_var(tracing_cpu) = 0;
  203. else
  204. return;
  205. if (!tracer_enabled)
  206. return;
  207. cpu = raw_smp_processor_id();
  208. data = tr->data[cpu];
  209. if (unlikely(!data) || unlikely(!data->trace) ||
  210. !data->critical_start || atomic_read(&data->disabled))
  211. return;
  212. atomic_inc(&data->disabled);
  213. local_save_flags(flags);
  214. ftrace(tr, data, ip, parent_ip, flags);
  215. check_critical_timing(tr, data, parent_ip ? : ip, cpu);
  216. data->critical_start = 0;
  217. atomic_dec(&data->disabled);
  218. }
  219. /* start and stop critical timings used to for stoppage (in idle) */
  220. void notrace start_critical_timings(void)
  221. {
  222. if (preempt_trace() || irq_trace())
  223. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  224. }
  225. void notrace stop_critical_timings(void)
  226. {
  227. if (preempt_trace() || irq_trace())
  228. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  229. }
  230. #ifdef CONFIG_IRQSOFF_TRACER
  231. #ifdef CONFIG_PROVE_LOCKING
  232. void notrace time_hardirqs_on(unsigned long a0, unsigned long a1)
  233. {
  234. if (!preempt_trace() && irq_trace())
  235. stop_critical_timing(a0, a1);
  236. }
  237. void notrace time_hardirqs_off(unsigned long a0, unsigned long a1)
  238. {
  239. if (!preempt_trace() && irq_trace())
  240. start_critical_timing(a0, a1);
  241. }
  242. #else /* !CONFIG_PROVE_LOCKING */
  243. /*
  244. * Stubs:
  245. */
  246. void early_boot_irqs_off(void)
  247. {
  248. }
  249. void early_boot_irqs_on(void)
  250. {
  251. }
  252. void trace_softirqs_on(unsigned long ip)
  253. {
  254. }
  255. void trace_softirqs_off(unsigned long ip)
  256. {
  257. }
  258. inline void print_irqtrace_events(struct task_struct *curr)
  259. {
  260. }
  261. /*
  262. * We are only interested in hardirq on/off events:
  263. */
  264. void notrace trace_hardirqs_on(void)
  265. {
  266. if (!preempt_trace() && irq_trace())
  267. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  268. }
  269. EXPORT_SYMBOL(trace_hardirqs_on);
  270. void notrace trace_hardirqs_off(void)
  271. {
  272. if (!preempt_trace() && irq_trace())
  273. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  274. }
  275. EXPORT_SYMBOL(trace_hardirqs_off);
  276. void notrace trace_hardirqs_on_caller(unsigned long caller_addr)
  277. {
  278. if (!preempt_trace() && irq_trace())
  279. stop_critical_timing(CALLER_ADDR0, caller_addr);
  280. }
  281. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  282. void notrace trace_hardirqs_off_caller(unsigned long caller_addr)
  283. {
  284. if (!preempt_trace() && irq_trace())
  285. start_critical_timing(CALLER_ADDR0, caller_addr);
  286. }
  287. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  288. #endif /* CONFIG_PROVE_LOCKING */
  289. #endif /* CONFIG_IRQSOFF_TRACER */
  290. #ifdef CONFIG_PREEMPT_TRACER
  291. void notrace trace_preempt_on(unsigned long a0, unsigned long a1)
  292. {
  293. stop_critical_timing(a0, a1);
  294. }
  295. void notrace trace_preempt_off(unsigned long a0, unsigned long a1)
  296. {
  297. start_critical_timing(a0, a1);
  298. }
  299. #endif /* CONFIG_PREEMPT_TRACER */
  300. static void start_irqsoff_tracer(struct trace_array *tr)
  301. {
  302. register_ftrace_function(&trace_ops);
  303. tracer_enabled = 1;
  304. }
  305. static void stop_irqsoff_tracer(struct trace_array *tr)
  306. {
  307. tracer_enabled = 0;
  308. unregister_ftrace_function(&trace_ops);
  309. }
  310. static void __irqsoff_tracer_init(struct trace_array *tr)
  311. {
  312. irqsoff_trace = tr;
  313. /* make sure that the tracer is visibel */
  314. smp_wmb();
  315. if (tr->ctrl)
  316. start_irqsoff_tracer(tr);
  317. }
  318. static void irqsoff_tracer_reset(struct trace_array *tr)
  319. {
  320. if (tr->ctrl)
  321. stop_irqsoff_tracer(tr);
  322. }
  323. static void irqsoff_tracer_ctrl_update(struct trace_array *tr)
  324. {
  325. if (tr->ctrl)
  326. start_irqsoff_tracer(tr);
  327. else
  328. stop_irqsoff_tracer(tr);
  329. }
  330. static void notrace irqsoff_tracer_open(struct trace_iterator *iter)
  331. {
  332. /* stop the trace while dumping */
  333. if (iter->tr->ctrl)
  334. stop_irqsoff_tracer(iter->tr);
  335. }
  336. static void notrace irqsoff_tracer_close(struct trace_iterator *iter)
  337. {
  338. if (iter->tr->ctrl)
  339. start_irqsoff_tracer(iter->tr);
  340. }
  341. #ifdef CONFIG_IRQSOFF_TRACER
  342. static void irqsoff_tracer_init(struct trace_array *tr)
  343. {
  344. trace_type = TRACER_IRQS_OFF;
  345. __irqsoff_tracer_init(tr);
  346. }
  347. static struct tracer irqsoff_tracer __read_mostly =
  348. {
  349. .name = "irqsoff",
  350. .init = irqsoff_tracer_init,
  351. .reset = irqsoff_tracer_reset,
  352. .open = irqsoff_tracer_open,
  353. .close = irqsoff_tracer_close,
  354. .ctrl_update = irqsoff_tracer_ctrl_update,
  355. .print_max = 1,
  356. #ifdef CONFIG_FTRACE_SELFTEST
  357. .selftest = trace_selftest_startup_irqsoff,
  358. #endif
  359. };
  360. # define register_irqsoff(trace) register_tracer(&trace)
  361. #else
  362. # define register_irqsoff(trace) do { } while (0)
  363. #endif
  364. #ifdef CONFIG_PREEMPT_TRACER
  365. static void preemptoff_tracer_init(struct trace_array *tr)
  366. {
  367. trace_type = TRACER_PREEMPT_OFF;
  368. __irqsoff_tracer_init(tr);
  369. }
  370. static struct tracer preemptoff_tracer __read_mostly =
  371. {
  372. .name = "preemptoff",
  373. .init = preemptoff_tracer_init,
  374. .reset = irqsoff_tracer_reset,
  375. .open = irqsoff_tracer_open,
  376. .close = irqsoff_tracer_close,
  377. .ctrl_update = irqsoff_tracer_ctrl_update,
  378. .print_max = 1,
  379. #ifdef CONFIG_FTRACE_SELFTEST
  380. .selftest = trace_selftest_startup_preemptoff,
  381. #endif
  382. };
  383. # define register_preemptoff(trace) register_tracer(&trace)
  384. #else
  385. # define register_preemptoff(trace) do { } while (0)
  386. #endif
  387. #if defined(CONFIG_IRQSOFF_TRACER) && \
  388. defined(CONFIG_PREEMPT_TRACER)
  389. static void preemptirqsoff_tracer_init(struct trace_array *tr)
  390. {
  391. trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
  392. __irqsoff_tracer_init(tr);
  393. }
  394. static struct tracer preemptirqsoff_tracer __read_mostly =
  395. {
  396. .name = "preemptirqsoff",
  397. .init = preemptirqsoff_tracer_init,
  398. .reset = irqsoff_tracer_reset,
  399. .open = irqsoff_tracer_open,
  400. .close = irqsoff_tracer_close,
  401. .ctrl_update = irqsoff_tracer_ctrl_update,
  402. .print_max = 1,
  403. #ifdef CONFIG_FTRACE_SELFTEST
  404. .selftest = trace_selftest_startup_preemptirqsoff,
  405. #endif
  406. };
  407. # define register_preemptirqsoff(trace) register_tracer(&trace)
  408. #else
  409. # define register_preemptirqsoff(trace) do { } while (0)
  410. #endif
  411. __init static int init_irqsoff_tracer(void)
  412. {
  413. register_irqsoff(irqsoff_tracer);
  414. register_preemptoff(preemptoff_tracer);
  415. register_preemptirqsoff(preemptirqsoff_tracer);
  416. return 0;
  417. }
  418. device_initcall(init_irqsoff_tracer);