trace_sched_wakeup.c 14 KB

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