sched.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #undef TRACE_SYSTEM
  3. #define TRACE_SYSTEM sched
  4. #if !defined(_TRACE_SCHED_H) || defined(TRACE_HEADER_MULTI_READ)
  5. #define _TRACE_SCHED_H
  6. #include <linux/sched/numa_balancing.h>
  7. #include <linux/tracepoint.h>
  8. #include <linux/binfmts.h>
  9. /*
  10. * Tracepoint for calling kthread_stop, performed to end a kthread:
  11. */
  12. TRACE_EVENT(sched_kthread_stop,
  13. TP_PROTO(struct task_struct *t),
  14. TP_ARGS(t),
  15. TP_STRUCT__entry(
  16. __array( char, comm, TASK_COMM_LEN )
  17. __field( pid_t, pid )
  18. ),
  19. TP_fast_assign(
  20. memcpy(__entry->comm, t->comm, TASK_COMM_LEN);
  21. __entry->pid = t->pid;
  22. ),
  23. TP_printk("comm=%s pid=%d", __entry->comm, __entry->pid)
  24. );
  25. /*
  26. * Tracepoint for the return value of the kthread stopping:
  27. */
  28. TRACE_EVENT(sched_kthread_stop_ret,
  29. TP_PROTO(int ret),
  30. TP_ARGS(ret),
  31. TP_STRUCT__entry(
  32. __field( int, ret )
  33. ),
  34. TP_fast_assign(
  35. __entry->ret = ret;
  36. ),
  37. TP_printk("ret=%d", __entry->ret)
  38. );
  39. /*
  40. * Tracepoint for waking up a task:
  41. */
  42. DECLARE_EVENT_CLASS(sched_wakeup_template,
  43. TP_PROTO(struct task_struct *p),
  44. TP_ARGS(__perf_task(p)),
  45. TP_STRUCT__entry(
  46. __array( char, comm, TASK_COMM_LEN )
  47. __field( pid_t, pid )
  48. __field( int, prio )
  49. __field( int, success )
  50. __field( int, target_cpu )
  51. ),
  52. TP_fast_assign(
  53. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  54. __entry->pid = p->pid;
  55. __entry->prio = p->prio; /* XXX SCHED_DEADLINE */
  56. __entry->success = 1; /* rudiment, kill when possible */
  57. __entry->target_cpu = task_cpu(p);
  58. ),
  59. TP_printk("comm=%s pid=%d prio=%d target_cpu=%03d",
  60. __entry->comm, __entry->pid, __entry->prio,
  61. __entry->target_cpu)
  62. );
  63. /*
  64. * Tracepoint called when waking a task; this tracepoint is guaranteed to be
  65. * called from the waking context.
  66. */
  67. DEFINE_EVENT(sched_wakeup_template, sched_waking,
  68. TP_PROTO(struct task_struct *p),
  69. TP_ARGS(p));
  70. /*
  71. * Tracepoint called when the task is actually woken; p->state == TASK_RUNNNG.
  72. * It it not always called from the waking context.
  73. */
  74. DEFINE_EVENT(sched_wakeup_template, sched_wakeup,
  75. TP_PROTO(struct task_struct *p),
  76. TP_ARGS(p));
  77. /*
  78. * Tracepoint for waking up a new task:
  79. */
  80. DEFINE_EVENT(sched_wakeup_template, sched_wakeup_new,
  81. TP_PROTO(struct task_struct *p),
  82. TP_ARGS(p));
  83. #ifdef CREATE_TRACE_POINTS
  84. static inline long __trace_sched_switch_state(bool preempt, struct task_struct *p)
  85. {
  86. unsigned int state;
  87. #ifdef CONFIG_SCHED_DEBUG
  88. BUG_ON(p != current);
  89. #endif /* CONFIG_SCHED_DEBUG */
  90. /*
  91. * Preemption ignores task state, therefore preempted tasks are always
  92. * RUNNING (we will not have dequeued if state != RUNNING).
  93. */
  94. if (preempt)
  95. return TASK_REPORT_MAX;
  96. /*
  97. * task_state_index() uses fls() and returns a value from 0-8 range.
  98. * Decrement it by 1 (except TASK_RUNNING state i.e 0) before using
  99. * it for left shift operation to get the correct task->state
  100. * mapping.
  101. */
  102. state = task_state_index(p);
  103. return state ? (1 << (state - 1)) : state;
  104. }
  105. #endif /* CREATE_TRACE_POINTS */
  106. /*
  107. * Tracepoint for task switches, performed by the scheduler:
  108. */
  109. TRACE_EVENT(sched_switch,
  110. TP_PROTO(bool preempt,
  111. struct task_struct *prev,
  112. struct task_struct *next),
  113. TP_ARGS(preempt, prev, next),
  114. TP_STRUCT__entry(
  115. __array( char, prev_comm, TASK_COMM_LEN )
  116. __field( pid_t, prev_pid )
  117. __field( int, prev_prio )
  118. __field( long, prev_state )
  119. __array( char, next_comm, TASK_COMM_LEN )
  120. __field( pid_t, next_pid )
  121. __field( int, next_prio )
  122. ),
  123. TP_fast_assign(
  124. memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
  125. __entry->prev_pid = prev->pid;
  126. __entry->prev_prio = prev->prio;
  127. __entry->prev_state = __trace_sched_switch_state(preempt, prev);
  128. memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
  129. __entry->next_pid = next->pid;
  130. __entry->next_prio = next->prio;
  131. /* XXX SCHED_DEADLINE */
  132. ),
  133. TP_printk("prev_comm=%s prev_pid=%d prev_prio=%d prev_state=%s%s ==> next_comm=%s next_pid=%d next_prio=%d",
  134. __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
  135. (__entry->prev_state & (TASK_REPORT_MAX - 1)) ?
  136. __print_flags(__entry->prev_state & (TASK_REPORT_MAX - 1), "|",
  137. { TASK_INTERRUPTIBLE, "S" },
  138. { TASK_UNINTERRUPTIBLE, "D" },
  139. { __TASK_STOPPED, "T" },
  140. { __TASK_TRACED, "t" },
  141. { EXIT_DEAD, "X" },
  142. { EXIT_ZOMBIE, "Z" },
  143. { TASK_PARKED, "P" },
  144. { TASK_DEAD, "I" }) :
  145. "R",
  146. __entry->prev_state & TASK_REPORT_MAX ? "+" : "",
  147. __entry->next_comm, __entry->next_pid, __entry->next_prio)
  148. );
  149. /*
  150. * Tracepoint for a task being migrated:
  151. */
  152. TRACE_EVENT(sched_migrate_task,
  153. TP_PROTO(struct task_struct *p, int dest_cpu),
  154. TP_ARGS(p, dest_cpu),
  155. TP_STRUCT__entry(
  156. __array( char, comm, TASK_COMM_LEN )
  157. __field( pid_t, pid )
  158. __field( int, prio )
  159. __field( int, orig_cpu )
  160. __field( int, dest_cpu )
  161. ),
  162. TP_fast_assign(
  163. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  164. __entry->pid = p->pid;
  165. __entry->prio = p->prio; /* XXX SCHED_DEADLINE */
  166. __entry->orig_cpu = task_cpu(p);
  167. __entry->dest_cpu = dest_cpu;
  168. ),
  169. TP_printk("comm=%s pid=%d prio=%d orig_cpu=%d dest_cpu=%d",
  170. __entry->comm, __entry->pid, __entry->prio,
  171. __entry->orig_cpu, __entry->dest_cpu)
  172. );
  173. DECLARE_EVENT_CLASS(sched_process_template,
  174. TP_PROTO(struct task_struct *p),
  175. TP_ARGS(p),
  176. TP_STRUCT__entry(
  177. __array( char, comm, TASK_COMM_LEN )
  178. __field( pid_t, pid )
  179. __field( int, prio )
  180. ),
  181. TP_fast_assign(
  182. memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
  183. __entry->pid = p->pid;
  184. __entry->prio = p->prio; /* XXX SCHED_DEADLINE */
  185. ),
  186. TP_printk("comm=%s pid=%d prio=%d",
  187. __entry->comm, __entry->pid, __entry->prio)
  188. );
  189. /*
  190. * Tracepoint for freeing a task:
  191. */
  192. DEFINE_EVENT(sched_process_template, sched_process_free,
  193. TP_PROTO(struct task_struct *p),
  194. TP_ARGS(p));
  195. /*
  196. * Tracepoint for a task exiting:
  197. */
  198. DEFINE_EVENT(sched_process_template, sched_process_exit,
  199. TP_PROTO(struct task_struct *p),
  200. TP_ARGS(p));
  201. /*
  202. * Tracepoint for waiting on task to unschedule:
  203. */
  204. DEFINE_EVENT(sched_process_template, sched_wait_task,
  205. TP_PROTO(struct task_struct *p),
  206. TP_ARGS(p));
  207. /*
  208. * Tracepoint for a waiting task:
  209. */
  210. TRACE_EVENT(sched_process_wait,
  211. TP_PROTO(struct pid *pid),
  212. TP_ARGS(pid),
  213. TP_STRUCT__entry(
  214. __array( char, comm, TASK_COMM_LEN )
  215. __field( pid_t, pid )
  216. __field( int, prio )
  217. ),
  218. TP_fast_assign(
  219. memcpy(__entry->comm, current->comm, TASK_COMM_LEN);
  220. __entry->pid = pid_nr(pid);
  221. __entry->prio = current->prio; /* XXX SCHED_DEADLINE */
  222. ),
  223. TP_printk("comm=%s pid=%d prio=%d",
  224. __entry->comm, __entry->pid, __entry->prio)
  225. );
  226. /*
  227. * Tracepoint for do_fork:
  228. */
  229. TRACE_EVENT(sched_process_fork,
  230. TP_PROTO(struct task_struct *parent, struct task_struct *child),
  231. TP_ARGS(parent, child),
  232. TP_STRUCT__entry(
  233. __array( char, parent_comm, TASK_COMM_LEN )
  234. __field( pid_t, parent_pid )
  235. __array( char, child_comm, TASK_COMM_LEN )
  236. __field( pid_t, child_pid )
  237. ),
  238. TP_fast_assign(
  239. memcpy(__entry->parent_comm, parent->comm, TASK_COMM_LEN);
  240. __entry->parent_pid = parent->pid;
  241. memcpy(__entry->child_comm, child->comm, TASK_COMM_LEN);
  242. __entry->child_pid = child->pid;
  243. ),
  244. TP_printk("comm=%s pid=%d child_comm=%s child_pid=%d",
  245. __entry->parent_comm, __entry->parent_pid,
  246. __entry->child_comm, __entry->child_pid)
  247. );
  248. /*
  249. * Tracepoint for exec:
  250. */
  251. TRACE_EVENT(sched_process_exec,
  252. TP_PROTO(struct task_struct *p, pid_t old_pid,
  253. struct linux_binprm *bprm),
  254. TP_ARGS(p, old_pid, bprm),
  255. TP_STRUCT__entry(
  256. __string( filename, bprm->filename )
  257. __field( pid_t, pid )
  258. __field( pid_t, old_pid )
  259. ),
  260. TP_fast_assign(
  261. __assign_str(filename, bprm->filename);
  262. __entry->pid = p->pid;
  263. __entry->old_pid = old_pid;
  264. ),
  265. TP_printk("filename=%s pid=%d old_pid=%d", __get_str(filename),
  266. __entry->pid, __entry->old_pid)
  267. );
  268. /*
  269. * XXX the below sched_stat tracepoints only apply to SCHED_OTHER/BATCH/IDLE
  270. * adding sched_stat support to SCHED_FIFO/RR would be welcome.
  271. */
  272. DECLARE_EVENT_CLASS(sched_stat_template,
  273. TP_PROTO(struct task_struct *tsk, u64 delay),
  274. TP_ARGS(__perf_task(tsk), __perf_count(delay)),
  275. TP_STRUCT__entry(
  276. __array( char, comm, TASK_COMM_LEN )
  277. __field( pid_t, pid )
  278. __field( u64, delay )
  279. ),
  280. TP_fast_assign(
  281. memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN);
  282. __entry->pid = tsk->pid;
  283. __entry->delay = delay;
  284. ),
  285. TP_printk("comm=%s pid=%d delay=%Lu [ns]",
  286. __entry->comm, __entry->pid,
  287. (unsigned long long)__entry->delay)
  288. );
  289. /*
  290. * Tracepoint for accounting wait time (time the task is runnable
  291. * but not actually running due to scheduler contention).
  292. */
  293. DEFINE_EVENT(sched_stat_template, sched_stat_wait,
  294. TP_PROTO(struct task_struct *tsk, u64 delay),
  295. TP_ARGS(tsk, delay));
  296. /*
  297. * Tracepoint for accounting sleep time (time the task is not runnable,
  298. * including iowait, see below).
  299. */
  300. DEFINE_EVENT(sched_stat_template, sched_stat_sleep,
  301. TP_PROTO(struct task_struct *tsk, u64 delay),
  302. TP_ARGS(tsk, delay));
  303. /*
  304. * Tracepoint for accounting iowait time (time the task is not runnable
  305. * due to waiting on IO to complete).
  306. */
  307. DEFINE_EVENT(sched_stat_template, sched_stat_iowait,
  308. TP_PROTO(struct task_struct *tsk, u64 delay),
  309. TP_ARGS(tsk, delay));
  310. /*
  311. * Tracepoint for accounting blocked time (time the task is in uninterruptible).
  312. */
  313. DEFINE_EVENT(sched_stat_template, sched_stat_blocked,
  314. TP_PROTO(struct task_struct *tsk, u64 delay),
  315. TP_ARGS(tsk, delay));
  316. /*
  317. * Tracepoint for accounting runtime (time the task is executing
  318. * on a CPU).
  319. */
  320. DECLARE_EVENT_CLASS(sched_stat_runtime,
  321. TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime),
  322. TP_ARGS(tsk, __perf_count(runtime), vruntime),
  323. TP_STRUCT__entry(
  324. __array( char, comm, TASK_COMM_LEN )
  325. __field( pid_t, pid )
  326. __field( u64, runtime )
  327. __field( u64, vruntime )
  328. ),
  329. TP_fast_assign(
  330. memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN);
  331. __entry->pid = tsk->pid;
  332. __entry->runtime = runtime;
  333. __entry->vruntime = vruntime;
  334. ),
  335. TP_printk("comm=%s pid=%d runtime=%Lu [ns] vruntime=%Lu [ns]",
  336. __entry->comm, __entry->pid,
  337. (unsigned long long)__entry->runtime,
  338. (unsigned long long)__entry->vruntime)
  339. );
  340. DEFINE_EVENT(sched_stat_runtime, sched_stat_runtime,
  341. TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime),
  342. TP_ARGS(tsk, runtime, vruntime));
  343. /*
  344. * Tracepoint for showing priority inheritance modifying a tasks
  345. * priority.
  346. */
  347. TRACE_EVENT(sched_pi_setprio,
  348. TP_PROTO(struct task_struct *tsk, struct task_struct *pi_task),
  349. TP_ARGS(tsk, pi_task),
  350. TP_STRUCT__entry(
  351. __array( char, comm, TASK_COMM_LEN )
  352. __field( pid_t, pid )
  353. __field( int, oldprio )
  354. __field( int, newprio )
  355. ),
  356. TP_fast_assign(
  357. memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN);
  358. __entry->pid = tsk->pid;
  359. __entry->oldprio = tsk->prio;
  360. __entry->newprio = pi_task ?
  361. min(tsk->normal_prio, pi_task->prio) :
  362. tsk->normal_prio;
  363. /* XXX SCHED_DEADLINE bits missing */
  364. ),
  365. TP_printk("comm=%s pid=%d oldprio=%d newprio=%d",
  366. __entry->comm, __entry->pid,
  367. __entry->oldprio, __entry->newprio)
  368. );
  369. #ifdef CONFIG_DETECT_HUNG_TASK
  370. TRACE_EVENT(sched_process_hang,
  371. TP_PROTO(struct task_struct *tsk),
  372. TP_ARGS(tsk),
  373. TP_STRUCT__entry(
  374. __array( char, comm, TASK_COMM_LEN )
  375. __field( pid_t, pid )
  376. ),
  377. TP_fast_assign(
  378. memcpy(__entry->comm, tsk->comm, TASK_COMM_LEN);
  379. __entry->pid = tsk->pid;
  380. ),
  381. TP_printk("comm=%s pid=%d", __entry->comm, __entry->pid)
  382. );
  383. #endif /* CONFIG_DETECT_HUNG_TASK */
  384. DECLARE_EVENT_CLASS(sched_move_task_template,
  385. TP_PROTO(struct task_struct *tsk, int src_cpu, int dst_cpu),
  386. TP_ARGS(tsk, src_cpu, dst_cpu),
  387. TP_STRUCT__entry(
  388. __field( pid_t, pid )
  389. __field( pid_t, tgid )
  390. __field( pid_t, ngid )
  391. __field( int, src_cpu )
  392. __field( int, src_nid )
  393. __field( int, dst_cpu )
  394. __field( int, dst_nid )
  395. ),
  396. TP_fast_assign(
  397. __entry->pid = task_pid_nr(tsk);
  398. __entry->tgid = task_tgid_nr(tsk);
  399. __entry->ngid = task_numa_group_id(tsk);
  400. __entry->src_cpu = src_cpu;
  401. __entry->src_nid = cpu_to_node(src_cpu);
  402. __entry->dst_cpu = dst_cpu;
  403. __entry->dst_nid = cpu_to_node(dst_cpu);
  404. ),
  405. TP_printk("pid=%d tgid=%d ngid=%d src_cpu=%d src_nid=%d dst_cpu=%d dst_nid=%d",
  406. __entry->pid, __entry->tgid, __entry->ngid,
  407. __entry->src_cpu, __entry->src_nid,
  408. __entry->dst_cpu, __entry->dst_nid)
  409. );
  410. /*
  411. * Tracks migration of tasks from one runqueue to another. Can be used to
  412. * detect if automatic NUMA balancing is bouncing between nodes
  413. */
  414. DEFINE_EVENT(sched_move_task_template, sched_move_numa,
  415. TP_PROTO(struct task_struct *tsk, int src_cpu, int dst_cpu),
  416. TP_ARGS(tsk, src_cpu, dst_cpu)
  417. );
  418. DEFINE_EVENT(sched_move_task_template, sched_stick_numa,
  419. TP_PROTO(struct task_struct *tsk, int src_cpu, int dst_cpu),
  420. TP_ARGS(tsk, src_cpu, dst_cpu)
  421. );
  422. TRACE_EVENT(sched_swap_numa,
  423. TP_PROTO(struct task_struct *src_tsk, int src_cpu,
  424. struct task_struct *dst_tsk, int dst_cpu),
  425. TP_ARGS(src_tsk, src_cpu, dst_tsk, dst_cpu),
  426. TP_STRUCT__entry(
  427. __field( pid_t, src_pid )
  428. __field( pid_t, src_tgid )
  429. __field( pid_t, src_ngid )
  430. __field( int, src_cpu )
  431. __field( int, src_nid )
  432. __field( pid_t, dst_pid )
  433. __field( pid_t, dst_tgid )
  434. __field( pid_t, dst_ngid )
  435. __field( int, dst_cpu )
  436. __field( int, dst_nid )
  437. ),
  438. TP_fast_assign(
  439. __entry->src_pid = task_pid_nr(src_tsk);
  440. __entry->src_tgid = task_tgid_nr(src_tsk);
  441. __entry->src_ngid = task_numa_group_id(src_tsk);
  442. __entry->src_cpu = src_cpu;
  443. __entry->src_nid = cpu_to_node(src_cpu);
  444. __entry->dst_pid = task_pid_nr(dst_tsk);
  445. __entry->dst_tgid = task_tgid_nr(dst_tsk);
  446. __entry->dst_ngid = task_numa_group_id(dst_tsk);
  447. __entry->dst_cpu = dst_cpu;
  448. __entry->dst_nid = cpu_to_node(dst_cpu);
  449. ),
  450. TP_printk("src_pid=%d src_tgid=%d src_ngid=%d src_cpu=%d src_nid=%d dst_pid=%d dst_tgid=%d dst_ngid=%d dst_cpu=%d dst_nid=%d",
  451. __entry->src_pid, __entry->src_tgid, __entry->src_ngid,
  452. __entry->src_cpu, __entry->src_nid,
  453. __entry->dst_pid, __entry->dst_tgid, __entry->dst_ngid,
  454. __entry->dst_cpu, __entry->dst_nid)
  455. );
  456. /*
  457. * Tracepoint for waking a polling cpu without an IPI.
  458. */
  459. TRACE_EVENT(sched_wake_idle_without_ipi,
  460. TP_PROTO(int cpu),
  461. TP_ARGS(cpu),
  462. TP_STRUCT__entry(
  463. __field( int, cpu )
  464. ),
  465. TP_fast_assign(
  466. __entry->cpu = cpu;
  467. ),
  468. TP_printk("cpu=%d", __entry->cpu)
  469. );
  470. #endif /* _TRACE_SCHED_H */
  471. /* This part must be outside protection */
  472. #include <trace/define_trace.h>