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