trace_sched_wakeup.c 15 KB

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