trace_sched_wakeup.c 14 KB

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