trace_sched_wakeup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * trace task wakeup timings
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Based on code from 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/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/ftrace.h>
  18. #include <trace/events/sched.h>
  19. #include "trace.h"
  20. static struct trace_array *wakeup_trace;
  21. static int __read_mostly tracer_enabled;
  22. static struct task_struct *wakeup_task;
  23. static int wakeup_cpu;
  24. static int wakeup_current_cpu;
  25. static unsigned wakeup_prio = -1;
  26. static int wakeup_rt;
  27. static arch_spinlock_t wakeup_lock =
  28. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  29. static void wakeup_reset(struct trace_array *tr);
  30. static void __wakeup_reset(struct trace_array *tr);
  31. static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
  32. static void wakeup_graph_return(struct ftrace_graph_ret *trace);
  33. static int save_lat_flag;
  34. #define TRACE_DISPLAY_GRAPH 1
  35. static struct tracer_opt trace_opts[] = {
  36. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  37. /* display latency trace as call graph */
  38. { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
  39. #endif
  40. { } /* Empty entry */
  41. };
  42. static struct tracer_flags tracer_flags = {
  43. .val = 0,
  44. .opts = trace_opts,
  45. };
  46. #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
  47. #ifdef CONFIG_FUNCTION_TRACER
  48. /*
  49. * Prologue for the wakeup function tracers.
  50. *
  51. * Returns 1 if it is OK to continue, and preemption
  52. * is disabled and data->disabled is incremented.
  53. * 0 if the trace is to be ignored, and preemption
  54. * is not disabled and data->disabled is
  55. * kept the same.
  56. *
  57. * Note, this function is also used outside this ifdef but
  58. * inside the #ifdef of the function graph tracer below.
  59. * This is OK, since the function graph tracer is
  60. * dependent on the function tracer.
  61. */
  62. static int
  63. func_prolog_preempt_disable(struct trace_array *tr,
  64. struct trace_array_cpu **data,
  65. int *pc)
  66. {
  67. long disabled;
  68. int cpu;
  69. if (likely(!wakeup_task))
  70. return 0;
  71. *pc = preempt_count();
  72. preempt_disable_notrace();
  73. cpu = raw_smp_processor_id();
  74. if (cpu != wakeup_current_cpu)
  75. goto out_enable;
  76. *data = tr->data[cpu];
  77. disabled = atomic_inc_return(&(*data)->disabled);
  78. if (unlikely(disabled != 1))
  79. goto out;
  80. return 1;
  81. out:
  82. atomic_dec(&(*data)->disabled);
  83. out_enable:
  84. preempt_enable_notrace();
  85. return 0;
  86. }
  87. /*
  88. * wakeup uses its own tracer function to keep the overhead down:
  89. */
  90. static void
  91. wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
  92. {
  93. struct trace_array *tr = wakeup_trace;
  94. struct trace_array_cpu *data;
  95. unsigned long flags;
  96. int pc;
  97. if (!func_prolog_preempt_disable(tr, &data, &pc))
  98. return;
  99. local_irq_save(flags);
  100. trace_function(tr, ip, parent_ip, flags, pc);
  101. local_irq_restore(flags);
  102. atomic_dec(&data->disabled);
  103. preempt_enable_notrace();
  104. }
  105. static struct ftrace_ops trace_ops __read_mostly =
  106. {
  107. .func = wakeup_tracer_call,
  108. };
  109. #endif /* CONFIG_FUNCTION_TRACER */
  110. static int start_func_tracer(int graph)
  111. {
  112. int ret;
  113. if (!graph)
  114. ret = register_ftrace_function(&trace_ops);
  115. else
  116. ret = register_ftrace_graph(&wakeup_graph_return,
  117. &wakeup_graph_entry);
  118. if (!ret && tracing_is_enabled())
  119. tracer_enabled = 1;
  120. else
  121. tracer_enabled = 0;
  122. return ret;
  123. }
  124. static void stop_func_tracer(int graph)
  125. {
  126. tracer_enabled = 0;
  127. if (!graph)
  128. unregister_ftrace_function(&trace_ops);
  129. else
  130. unregister_ftrace_graph();
  131. }
  132. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  133. static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
  134. {
  135. if (!(bit & TRACE_DISPLAY_GRAPH))
  136. return -EINVAL;
  137. if (!(is_graph() ^ set))
  138. return 0;
  139. stop_func_tracer(!set);
  140. wakeup_reset(wakeup_trace);
  141. tracing_max_latency = 0;
  142. return start_func_tracer(set);
  143. }
  144. static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
  145. {
  146. struct trace_array *tr = wakeup_trace;
  147. struct trace_array_cpu *data;
  148. unsigned long flags;
  149. int pc, ret = 0;
  150. if (!func_prolog_preempt_disable(tr, &data, &pc))
  151. return 0;
  152. local_save_flags(flags);
  153. ret = __trace_graph_entry(tr, trace, flags, pc);
  154. atomic_dec(&data->disabled);
  155. preempt_enable_notrace();
  156. return ret;
  157. }
  158. static void wakeup_graph_return(struct ftrace_graph_ret *trace)
  159. {
  160. struct trace_array *tr = wakeup_trace;
  161. struct trace_array_cpu *data;
  162. unsigned long flags;
  163. int pc;
  164. if (!func_prolog_preempt_disable(tr, &data, &pc))
  165. return;
  166. local_save_flags(flags);
  167. __trace_graph_return(tr, trace, flags, pc);
  168. atomic_dec(&data->disabled);
  169. preempt_enable_notrace();
  170. return;
  171. }
  172. static void wakeup_trace_open(struct trace_iterator *iter)
  173. {
  174. if (is_graph())
  175. graph_trace_open(iter);
  176. }
  177. static void wakeup_trace_close(struct trace_iterator *iter)
  178. {
  179. if (iter->private)
  180. graph_trace_close(iter);
  181. }
  182. #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC)
  183. static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
  184. {
  185. /*
  186. * In graph mode call the graph tracer output function,
  187. * otherwise go with the TRACE_FN event handler
  188. */
  189. if (is_graph())
  190. return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
  191. return TRACE_TYPE_UNHANDLED;
  192. }
  193. static void wakeup_print_header(struct seq_file *s)
  194. {
  195. if (is_graph())
  196. print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
  197. else
  198. trace_default_header(s);
  199. }
  200. static void
  201. __trace_function(struct trace_array *tr,
  202. unsigned long ip, unsigned long parent_ip,
  203. unsigned long flags, int pc)
  204. {
  205. if (is_graph())
  206. trace_graph_function(tr, ip, parent_ip, flags, pc);
  207. else
  208. trace_function(tr, ip, parent_ip, flags, pc);
  209. }
  210. #else
  211. #define __trace_function trace_function
  212. static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
  213. {
  214. return -EINVAL;
  215. }
  216. static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
  217. {
  218. return -1;
  219. }
  220. static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
  221. {
  222. return TRACE_TYPE_UNHANDLED;
  223. }
  224. static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
  225. static void wakeup_print_header(struct seq_file *s) { }
  226. static void wakeup_trace_open(struct trace_iterator *iter) { }
  227. static void wakeup_trace_close(struct trace_iterator *iter) { }
  228. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  229. /*
  230. * Should this new latency be reported/recorded?
  231. */
  232. static int report_latency(cycle_t delta)
  233. {
  234. if (tracing_thresh) {
  235. if (delta < tracing_thresh)
  236. return 0;
  237. } else {
  238. if (delta <= tracing_max_latency)
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. static void
  244. probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
  245. {
  246. if (task != wakeup_task)
  247. return;
  248. wakeup_current_cpu = cpu;
  249. }
  250. static void notrace
  251. probe_wakeup_sched_switch(void *ignore,
  252. struct task_struct *prev, struct task_struct *next)
  253. {
  254. struct trace_array_cpu *data;
  255. cycle_t T0, T1, delta;
  256. unsigned long flags;
  257. long disabled;
  258. int cpu;
  259. int pc;
  260. tracing_record_cmdline(prev);
  261. if (unlikely(!tracer_enabled))
  262. return;
  263. /*
  264. * When we start a new trace, we set wakeup_task to NULL
  265. * and then set tracer_enabled = 1. We want to make sure
  266. * that another CPU does not see the tracer_enabled = 1
  267. * and the wakeup_task with an older task, that might
  268. * actually be the same as next.
  269. */
  270. smp_rmb();
  271. if (next != wakeup_task)
  272. return;
  273. pc = preempt_count();
  274. /* disable local data, not wakeup_cpu data */
  275. cpu = raw_smp_processor_id();
  276. disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
  277. if (likely(disabled != 1))
  278. goto out;
  279. local_irq_save(flags);
  280. arch_spin_lock(&wakeup_lock);
  281. /* We could race with grabbing wakeup_lock */
  282. if (unlikely(!tracer_enabled || next != wakeup_task))
  283. goto out_unlock;
  284. /* The task we are waiting for is waking up */
  285. data = wakeup_trace->data[wakeup_cpu];
  286. __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
  287. tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
  288. T0 = data->preempt_timestamp;
  289. T1 = ftrace_now(cpu);
  290. delta = T1-T0;
  291. if (!report_latency(delta))
  292. goto out_unlock;
  293. if (likely(!is_tracing_stopped())) {
  294. tracing_max_latency = delta;
  295. update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
  296. }
  297. out_unlock:
  298. __wakeup_reset(wakeup_trace);
  299. arch_spin_unlock(&wakeup_lock);
  300. local_irq_restore(flags);
  301. out:
  302. atomic_dec(&wakeup_trace->data[cpu]->disabled);
  303. }
  304. static void __wakeup_reset(struct trace_array *tr)
  305. {
  306. wakeup_cpu = -1;
  307. wakeup_prio = -1;
  308. if (wakeup_task)
  309. put_task_struct(wakeup_task);
  310. wakeup_task = NULL;
  311. }
  312. static void wakeup_reset(struct trace_array *tr)
  313. {
  314. unsigned long flags;
  315. tracing_reset_online_cpus(tr);
  316. local_irq_save(flags);
  317. arch_spin_lock(&wakeup_lock);
  318. __wakeup_reset(tr);
  319. arch_spin_unlock(&wakeup_lock);
  320. local_irq_restore(flags);
  321. }
  322. static void
  323. probe_wakeup(void *ignore, struct task_struct *p, int success)
  324. {
  325. struct trace_array_cpu *data;
  326. int cpu = smp_processor_id();
  327. unsigned long flags;
  328. long disabled;
  329. int pc;
  330. if (likely(!tracer_enabled))
  331. return;
  332. tracing_record_cmdline(p);
  333. tracing_record_cmdline(current);
  334. if ((wakeup_rt && !rt_task(p)) ||
  335. p->prio >= wakeup_prio ||
  336. p->prio >= current->prio)
  337. return;
  338. pc = preempt_count();
  339. disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
  340. if (unlikely(disabled != 1))
  341. goto out;
  342. /* interrupts should be off from try_to_wake_up */
  343. arch_spin_lock(&wakeup_lock);
  344. /* check for races. */
  345. if (!tracer_enabled || p->prio >= wakeup_prio)
  346. goto out_locked;
  347. /* reset the trace */
  348. __wakeup_reset(wakeup_trace);
  349. wakeup_cpu = task_cpu(p);
  350. wakeup_current_cpu = wakeup_cpu;
  351. wakeup_prio = p->prio;
  352. wakeup_task = p;
  353. get_task_struct(wakeup_task);
  354. local_save_flags(flags);
  355. data = wakeup_trace->data[wakeup_cpu];
  356. data->preempt_timestamp = ftrace_now(cpu);
  357. tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
  358. /*
  359. * We must be careful in using CALLER_ADDR2. But since wake_up
  360. * is not called by an assembly function (where as schedule is)
  361. * it should be safe to use it here.
  362. */
  363. __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
  364. out_locked:
  365. arch_spin_unlock(&wakeup_lock);
  366. out:
  367. atomic_dec(&wakeup_trace->data[cpu]->disabled);
  368. }
  369. static void start_wakeup_tracer(struct trace_array *tr)
  370. {
  371. int ret;
  372. ret = register_trace_sched_wakeup(probe_wakeup, NULL);
  373. if (ret) {
  374. pr_info("wakeup trace: Couldn't activate tracepoint"
  375. " probe to kernel_sched_wakeup\n");
  376. return;
  377. }
  378. ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
  379. if (ret) {
  380. pr_info("wakeup trace: Couldn't activate tracepoint"
  381. " probe to kernel_sched_wakeup_new\n");
  382. goto fail_deprobe;
  383. }
  384. ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
  385. if (ret) {
  386. pr_info("sched trace: Couldn't activate tracepoint"
  387. " probe to kernel_sched_switch\n");
  388. goto fail_deprobe_wake_new;
  389. }
  390. ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
  391. if (ret) {
  392. pr_info("wakeup trace: Couldn't activate tracepoint"
  393. " probe to kernel_sched_migrate_task\n");
  394. return;
  395. }
  396. wakeup_reset(tr);
  397. /*
  398. * Don't let the tracer_enabled = 1 show up before
  399. * the wakeup_task is reset. This may be overkill since
  400. * wakeup_reset does a spin_unlock after setting the
  401. * wakeup_task to NULL, but I want to be safe.
  402. * This is a slow path anyway.
  403. */
  404. smp_wmb();
  405. if (start_func_tracer(is_graph()))
  406. printk(KERN_ERR "failed to start wakeup tracer\n");
  407. return;
  408. fail_deprobe_wake_new:
  409. unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
  410. fail_deprobe:
  411. unregister_trace_sched_wakeup(probe_wakeup, NULL);
  412. }
  413. static void stop_wakeup_tracer(struct trace_array *tr)
  414. {
  415. tracer_enabled = 0;
  416. stop_func_tracer(is_graph());
  417. unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
  418. unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
  419. unregister_trace_sched_wakeup(probe_wakeup, NULL);
  420. unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
  421. }
  422. static int __wakeup_tracer_init(struct trace_array *tr)
  423. {
  424. save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
  425. trace_flags |= TRACE_ITER_LATENCY_FMT;
  426. tracing_max_latency = 0;
  427. wakeup_trace = tr;
  428. start_wakeup_tracer(tr);
  429. return 0;
  430. }
  431. static int wakeup_tracer_init(struct trace_array *tr)
  432. {
  433. wakeup_rt = 0;
  434. return __wakeup_tracer_init(tr);
  435. }
  436. static int wakeup_rt_tracer_init(struct trace_array *tr)
  437. {
  438. wakeup_rt = 1;
  439. return __wakeup_tracer_init(tr);
  440. }
  441. static void wakeup_tracer_reset(struct trace_array *tr)
  442. {
  443. stop_wakeup_tracer(tr);
  444. /* make sure we put back any tasks we are tracing */
  445. wakeup_reset(tr);
  446. if (!save_lat_flag)
  447. trace_flags &= ~TRACE_ITER_LATENCY_FMT;
  448. }
  449. static void wakeup_tracer_start(struct trace_array *tr)
  450. {
  451. wakeup_reset(tr);
  452. tracer_enabled = 1;
  453. }
  454. static void wakeup_tracer_stop(struct trace_array *tr)
  455. {
  456. tracer_enabled = 0;
  457. }
  458. static struct tracer wakeup_tracer __read_mostly =
  459. {
  460. .name = "wakeup",
  461. .init = wakeup_tracer_init,
  462. .reset = wakeup_tracer_reset,
  463. .start = wakeup_tracer_start,
  464. .stop = wakeup_tracer_stop,
  465. .print_max = 1,
  466. .print_header = wakeup_print_header,
  467. .print_line = wakeup_print_line,
  468. .flags = &tracer_flags,
  469. .set_flag = wakeup_set_flag,
  470. #ifdef CONFIG_FTRACE_SELFTEST
  471. .selftest = trace_selftest_startup_wakeup,
  472. #endif
  473. .open = wakeup_trace_open,
  474. .close = wakeup_trace_close,
  475. .use_max_tr = 1,
  476. };
  477. static struct tracer wakeup_rt_tracer __read_mostly =
  478. {
  479. .name = "wakeup_rt",
  480. .init = wakeup_rt_tracer_init,
  481. .reset = wakeup_tracer_reset,
  482. .start = wakeup_tracer_start,
  483. .stop = wakeup_tracer_stop,
  484. .wait_pipe = poll_wait_pipe,
  485. .print_max = 1,
  486. .print_header = wakeup_print_header,
  487. .print_line = wakeup_print_line,
  488. .flags = &tracer_flags,
  489. .set_flag = wakeup_set_flag,
  490. #ifdef CONFIG_FTRACE_SELFTEST
  491. .selftest = trace_selftest_startup_wakeup,
  492. #endif
  493. .open = wakeup_trace_open,
  494. .close = wakeup_trace_close,
  495. .use_max_tr = 1,
  496. };
  497. __init static int init_wakeup_tracer(void)
  498. {
  499. int ret;
  500. ret = register_tracer(&wakeup_tracer);
  501. if (ret)
  502. return ret;
  503. ret = register_tracer(&wakeup_rt_tracer);
  504. if (ret)
  505. return ret;
  506. return 0;
  507. }
  508. device_initcall(init_wakeup_tracer);