trace_sched_wakeup.c 14 KB

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