trace_sched_wakeup.c 14 KB

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