Drop support for kernels < 4.4 from sched instrumentation
[lttng-modules.git] / include / instrumentation / events / sched.h
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: GPL-2.0-only */
f62b389e
MD
2#undef TRACE_SYSTEM
3#define TRACE_SYSTEM sched
4
3bc29f0a
MD
5#if !defined(LTTNG_TRACE_SCHED_H) || defined(TRACE_HEADER_MULTI_READ)
6#define LTTNG_TRACE_SCHED_H
f62b389e 7
3b4aafcb 8#include <lttng/tracepoint-event.h>
f62b389e 9#include <linux/sched.h>
7bbf55ea 10#include <linux/pid_namespace.h>
7c68b363 11#include <linux/binfmts.h>
5f4c791e 12#include <lttng/kernel-version.h>
090db00e 13#include <linux/sched/rt.h>
7bbf55ea
SL
14
15#define LTTNG_MAX_PID_NS_LEVEL 32
16
f62b389e
MD
17#ifndef _TRACE_SCHED_DEF_
18#define _TRACE_SCHED_DEF_
19
8e52fd71
MJ
20#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
21
22static inline long __trace_sched_switch_state(bool preempt,
23 unsigned int prev_state,
24 struct task_struct *p)
25{
26 unsigned int state;
27
28#ifdef CONFIG_SCHED_DEBUG
29 BUG_ON(p != current);
30#endif /* CONFIG_SCHED_DEBUG */
31
32 /*
33 * Preemption ignores task state, therefore preempted tasks are always
34 * RUNNING (we will not have dequeued if state != RUNNING).
35 */
36 if (preempt)
37 return TASK_REPORT_MAX;
38
39 /*
40 * task_state_index() uses fls() and returns a value from 0-8 range.
41 * Decrement it by 1 (except TASK_RUNNING state i.e 0) before using
42 * it for left shift operation to get the correct task->state
43 * mapping.
44 */
45 state = __task_state_index(prev_state, p->exit_state);
46
47 return state ? (1 << (state - 1)) : state;
48}
49
50#elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,15,0))
27e6eda7
GAPG
51
52static inline long __trace_sched_switch_state(bool preempt, struct task_struct *p)
53{
54 unsigned int state;
55
56#ifdef CONFIG_SCHED_DEBUG
57 BUG_ON(p != current);
58#endif /* CONFIG_SCHED_DEBUG */
59
60 /*
61 * Preemption ignores task state, therefore preempted tasks are always
62 * RUNNING (we will not have dequeued if state != RUNNING).
63 */
64 if (preempt)
65 return TASK_REPORT_MAX;
66
67 /*
68 * task_state_index() uses fls() and returns a value from 0-8 range.
69 * Decrement it by 1 (except TASK_RUNNING state i.e 0) before using
70 * it for left shift operation to get the correct task->state
71 * mapping.
72 */
73 state = task_state_index(p);
74
75 return state ? (1 << (state - 1)) : state;
76}
77
5f4c791e 78#elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,14,0))
27e6eda7
GAPG
79
80static inline long __trace_sched_switch_state(bool preempt, struct task_struct *p)
81{
82 unsigned int state;
83
84#ifdef CONFIG_SCHED_DEBUG
85 BUG_ON(p != current);
86#endif /* CONFIG_SCHED_DEBUG */
87
88 /*
89 * Preemption ignores task state, therefore preempted tasks are always
90 * RUNNING (we will not have dequeued if state != RUNNING).
91 */
92 if (preempt)
93 return TASK_REPORT_MAX;
94
95 /*
96 * __get_task_state() uses fls() and returns a value from 0-8 range.
97 * Decrement it by 1 (except TASK_RUNNING state i.e 0) before using
98 * it for left shift operation to get the correct task->state
99 * mapping.
100 */
101 state = __get_task_state(p);
102
103 return state ? (1 << (state - 1)) : state;
104}
105
dc818cc2 106#else
3ee729fe
MD
107
108static inline long __trace_sched_switch_state(bool preempt, struct task_struct *p)
109{
110#ifdef CONFIG_SCHED_DEBUG
111 BUG_ON(p != current);
112#endif /* CONFIG_SCHED_DEBUG */
113 /*
114 * Preemption ignores task state, therefore preempted tasks are always RUNNING
115 * (we will not have dequeued if state != RUNNING).
116 */
117 return preempt ? TASK_RUNNING | TASK_STATE_MAX : p->state;
118}
7c68b363
AG
119#endif
120
f62b389e
MD
121#endif /* _TRACE_SCHED_DEF_ */
122
e54b3828 123#ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
721caea4
GB
124/*
125 * Enumeration of the task state bitmask.
126 * Only bit flags are enumerated here, not composition of states.
127 */
128LTTNG_TRACEPOINT_ENUM(task_state,
129 TP_ENUM_VALUES(
130 ctf_enum_value("TASK_RUNNING", TASK_RUNNING)
131 ctf_enum_value("TASK_INTERRUPTIBLE", TASK_INTERRUPTIBLE)
132 ctf_enum_value("TASK_UNINTERRUPTIBLE", TASK_UNINTERRUPTIBLE)
133 ctf_enum_value("TASK_STOPPED", __TASK_STOPPED)
134 ctf_enum_value("TASK_TRACED", __TASK_TRACED)
135 ctf_enum_value("EXIT_DEAD", EXIT_DEAD)
136 ctf_enum_value("EXIT_ZOMBIE", EXIT_ZOMBIE)
721caea4 137 ctf_enum_value("TASK_PARKED", TASK_PARKED)
721caea4
GB
138 ctf_enum_value("TASK_DEAD", TASK_DEAD)
139 ctf_enum_value("TASK_WAKEKILL", TASK_WAKEKILL)
140 ctf_enum_value("TASK_WAKING", TASK_WAKING)
721caea4 141 ctf_enum_value("TASK_NOLOAD", TASK_NOLOAD)
721caea4 142
5f4c791e 143#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,8,0))
721caea4 144 ctf_enum_value("TASK_NEW", TASK_NEW)
5f4c791e 145#endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,8,0)) */
721caea4
GB
146
147 ctf_enum_value("TASK_STATE_MAX", TASK_STATE_MAX)
148 )
149)
e54b3828 150#endif /* CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM */
721caea4 151
f62b389e
MD
152/*
153 * Tracepoint for calling kthread_stop, performed to end a kthread:
154 */
3bc29f0a 155LTTNG_TRACEPOINT_EVENT(sched_kthread_stop,
f62b389e
MD
156
157 TP_PROTO(struct task_struct *t),
158
159 TP_ARGS(t),
160
f127e61e
MD
161 TP_FIELDS(
162 ctf_array_text(char, comm, t->comm, TASK_COMM_LEN)
163 ctf_integer(pid_t, tid, t->pid)
164 )
f62b389e
MD
165)
166
167/*
168 * Tracepoint for the return value of the kthread stopping:
169 */
3bc29f0a 170LTTNG_TRACEPOINT_EVENT(sched_kthread_stop_ret,
f62b389e
MD
171
172 TP_PROTO(int ret),
173
174 TP_ARGS(ret),
175
f127e61e
MD
176 TP_FIELDS(
177 ctf_integer(int, ret, ret)
178 )
f62b389e
MD
179)
180
181/*
182 * Tracepoint for waking up a task:
183 */
ffcf2393
MD
184LTTNG_TRACEPOINT_EVENT_CLASS(sched_wakeup_template,
185
186 TP_PROTO(struct task_struct *p),
187
188 TP_ARGS(p),
189
190 TP_FIELDS(
191 ctf_array_text(char, comm, p->comm, TASK_COMM_LEN)
192 ctf_integer(pid_t, tid, p->pid)
5aa835c0 193 ctf_integer(int, prio, p->prio - MAX_RT_PRIO)
ffcf2393
MD
194 ctf_integer(int, target_cpu, task_cpu(p))
195 )
196)
ffcf2393
MD
197
198/*
199 * Tracepoint called when waking a task; this tracepoint is guaranteed to be
200 * called from the waking context.
201 */
202LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_waking,
203 TP_PROTO(struct task_struct *p),
204 TP_ARGS(p))
205
206/*
207 * Tracepoint called when the task is actually woken; p->state == TASK_RUNNNG.
208 * It it not always called from the waking context.
209 */
210LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup,
211 TP_PROTO(struct task_struct *p),
212 TP_ARGS(p))
213
214/*
215 * Tracepoint for waking up a new task:
216 */
217LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup_new,
218 TP_PROTO(struct task_struct *p),
219 TP_ARGS(p))
220
f62b389e
MD
221/*
222 * Tracepoint for task switches, performed by the scheduler:
223 */
8e52fd71
MJ
224
225#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
3bc29f0a 226LTTNG_TRACEPOINT_EVENT(sched_switch,
f62b389e 227
3ee729fe 228 TP_PROTO(bool preempt,
8e52fd71 229 struct task_struct *prev,
9c5b8de3
MJ
230 struct task_struct *next,
231 unsigned int prev_state),
3ee729fe 232
9c5b8de3 233 TP_ARGS(preempt, prev, next, prev_state),
8e52fd71
MJ
234
235 TP_FIELDS(
236 ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
237 ctf_integer(pid_t, prev_tid, prev->pid)
238 ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
239#ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
240 ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(preempt, prev_state, prev))
ead65427 241#else
8e52fd71
MJ
242 ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev_state, prev))
243#endif
244 ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
245 ctf_integer(pid_t, next_tid, next->pid)
246 ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
247 )
248)
249
dc818cc2 250#else
8e52fd71
MJ
251
252LTTNG_TRACEPOINT_EVENT(sched_switch,
253
254 TP_PROTO(bool preempt,
255 struct task_struct *prev,
f62b389e
MD
256 struct task_struct *next),
257
8e52fd71 258 TP_ARGS(preempt, prev, next),
f62b389e 259
f127e61e
MD
260 TP_FIELDS(
261 ctf_array_text(char, prev_comm, prev->comm, TASK_COMM_LEN)
262 ctf_integer(pid_t, prev_tid, prev->pid)
263 ctf_integer(int, prev_prio, prev->prio - MAX_RT_PRIO)
e54b3828 264#ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
721caea4 265 ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(preempt, prev))
7c68b363 266#else
e54b3828
MJ
267 ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev))
268#endif
8e52fd71
MJ
269 ctf_array_text(char, next_comm, next->comm, TASK_COMM_LEN)
270 ctf_integer(pid_t, next_tid, next->pid)
271 ctf_integer(int, next_prio, next->prio - MAX_RT_PRIO)
272 )
273)
8e52fd71 274#endif
f62b389e
MD
275
276/*
277 * Tracepoint for a task being migrated:
278 */
3bc29f0a 279LTTNG_TRACEPOINT_EVENT(sched_migrate_task,
f62b389e
MD
280
281 TP_PROTO(struct task_struct *p, int dest_cpu),
282
283 TP_ARGS(p, dest_cpu),
284
f127e61e
MD
285 TP_FIELDS(
286 ctf_array_text(char, comm, p->comm, TASK_COMM_LEN)
287 ctf_integer(pid_t, tid, p->pid)
288 ctf_integer(int, prio, p->prio - MAX_RT_PRIO)
289 ctf_integer(int, orig_cpu, task_cpu(p))
290 ctf_integer(int, dest_cpu, dest_cpu)
291 )
f62b389e
MD
292)
293
3bc29f0a 294LTTNG_TRACEPOINT_EVENT_CLASS(sched_process_template,
f62b389e
MD
295
296 TP_PROTO(struct task_struct *p),
297
298 TP_ARGS(p),
299
f127e61e
MD
300 TP_FIELDS(
301 ctf_array_text(char, comm, p->comm, TASK_COMM_LEN)
302 ctf_integer(pid_t, tid, p->pid)
303 ctf_integer(int, prio, p->prio - MAX_RT_PRIO)
304 )
f62b389e
MD
305)
306
307/*
308 * Tracepoint for freeing a task:
309 */
3bc29f0a 310LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_process_free,
f62b389e
MD
311 TP_PROTO(struct task_struct *p),
312 TP_ARGS(p))
216b6baa 313
f62b389e
MD
314
315/*
316 * Tracepoint for a task exiting:
317 */
3bc29f0a 318LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_process_exit,
f62b389e
MD
319 TP_PROTO(struct task_struct *p),
320 TP_ARGS(p))
321
322/*
323 * Tracepoint for waiting on task to unschedule:
324 */
3bc29f0a 325LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_wait_task,
f62b389e
MD
326 TP_PROTO(struct task_struct *p),
327 TP_ARGS(p))
328
329/*
330 * Tracepoint for a waiting task:
331 */
3bc29f0a 332LTTNG_TRACEPOINT_EVENT(sched_process_wait,
f62b389e
MD
333
334 TP_PROTO(struct pid *pid),
335
336 TP_ARGS(pid),
337
f127e61e
MD
338 TP_FIELDS(
339 ctf_array_text(char, comm, current->comm, TASK_COMM_LEN)
340 ctf_integer(pid_t, tid, pid_nr(pid))
341 ctf_integer(int, prio, current->prio - MAX_RT_PRIO)
342 )
f62b389e
MD
343)
344
345/*
79b18ef7
MD
346 * Tracepoint for do_fork.
347 * Saving both TID and PID information, especially for the child, allows
348 * trace analyzers to distinguish between creation of a new process and
349 * creation of a new thread. Newly created processes will have child_tid
350 * == child_pid, while creation of a thread yields to child_tid !=
351 * child_pid.
f62b389e 352 */
7bbf55ea 353LTTNG_TRACEPOINT_EVENT_CODE(sched_process_fork,
f62b389e
MD
354
355 TP_PROTO(struct task_struct *parent, struct task_struct *child),
356
357 TP_ARGS(parent, child),
358
7bbf55ea
SL
359 TP_locvar(
360 pid_t vtids[LTTNG_MAX_PID_NS_LEVEL];
361 unsigned int ns_level;
362 ),
363
265822ae 364 TP_code_pre(
7bbf55ea
SL
365 if (child) {
366 struct pid *child_pid;
367 unsigned int i;
368
369 child_pid = task_pid(child);
370 tp_locvar->ns_level =
371 min_t(unsigned int, child_pid->level + 1,
372 LTTNG_MAX_PID_NS_LEVEL);
373 for (i = 0; i < tp_locvar->ns_level; i++)
374 tp_locvar->vtids[i] = child_pid->numbers[i].nr;
375 }
376 ),
377
f127e61e
MD
378 TP_FIELDS(
379 ctf_array_text(char, parent_comm, parent->comm, TASK_COMM_LEN)
380 ctf_integer(pid_t, parent_tid, parent->pid)
381 ctf_integer(pid_t, parent_pid, parent->tgid)
7bbf55ea
SL
382 ctf_integer(unsigned int, parent_ns_inum,
383 ({
384 unsigned int parent_ns_inum = 0;
385
386 if (parent) {
387 struct pid_namespace *pid_ns;
388
389 pid_ns = task_active_pid_ns(parent);
390 if (pid_ns)
391 parent_ns_inum =
93e5bcec 392 pid_ns->ns.inum;
7bbf55ea
SL
393 }
394 parent_ns_inum;
395 }))
f127e61e
MD
396 ctf_array_text(char, child_comm, child->comm, TASK_COMM_LEN)
397 ctf_integer(pid_t, child_tid, child->pid)
7bbf55ea 398 ctf_sequence(pid_t, vtids, tp_locvar->vtids, u8, tp_locvar->ns_level)
f127e61e 399 ctf_integer(pid_t, child_pid, child->tgid)
7bbf55ea
SL
400 ctf_integer(unsigned int, child_ns_inum,
401 ({
402 unsigned int child_ns_inum = 0;
403
404 if (child) {
405 struct pid_namespace *pid_ns;
406
407 pid_ns = task_active_pid_ns(child);
408 if (pid_ns)
409 child_ns_inum =
93e5bcec 410 pid_ns->ns.inum;
7bbf55ea
SL
411 }
412 child_ns_inum;
413 }))
265822ae
MD
414 ),
415
416 TP_code_post()
f62b389e
MD
417)
418
46142a81
PW
419/*
420 * Tracepoint for exec:
421 */
3bc29f0a 422LTTNG_TRACEPOINT_EVENT(sched_process_exec,
46142a81
PW
423
424 TP_PROTO(struct task_struct *p, pid_t old_pid,
425 struct linux_binprm *bprm),
426
427 TP_ARGS(p, old_pid, bprm),
428
f127e61e
MD
429 TP_FIELDS(
430 ctf_string(filename, bprm->filename)
431 ctf_integer(pid_t, tid, p->pid)
432 ctf_integer(pid_t, old_tid, old_pid)
433 )
46142a81
PW
434)
435
f62b389e
MD
436/*
437 * XXX the below sched_stat tracepoints only apply to SCHED_OTHER/BATCH/IDLE
438 * adding sched_stat support to SCHED_FIFO/RR would be welcome.
439 */
3bc29f0a 440LTTNG_TRACEPOINT_EVENT_CLASS(sched_stat_template,
f62b389e
MD
441
442 TP_PROTO(struct task_struct *tsk, u64 delay),
443
444 TP_ARGS(tsk, delay),
445
f127e61e
MD
446 TP_FIELDS(
447 ctf_array_text(char, comm, tsk->comm, TASK_COMM_LEN)
448 ctf_integer(pid_t, tid, tsk->pid)
449 ctf_integer(u64, delay, delay)
f62b389e 450 )
f62b389e
MD
451)
452
453
454/*
455 * Tracepoint for accounting wait time (time the task is runnable
456 * but not actually running due to scheduler contention).
457 */
3bc29f0a 458LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_wait,
f62b389e
MD
459 TP_PROTO(struct task_struct *tsk, u64 delay),
460 TP_ARGS(tsk, delay))
461
462/*
463 * Tracepoint for accounting sleep time (time the task is not runnable,
464 * including iowait, see below).
465 */
3bc29f0a 466LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_sleep,
f62b389e
MD
467 TP_PROTO(struct task_struct *tsk, u64 delay),
468 TP_ARGS(tsk, delay))
469
470/*
471 * Tracepoint for accounting iowait time (time the task is not runnable
472 * due to waiting on IO to complete).
473 */
3bc29f0a 474LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_iowait,
f62b389e
MD
475 TP_PROTO(struct task_struct *tsk, u64 delay),
476 TP_ARGS(tsk, delay))
477
7c68b363
AG
478/*
479 * Tracepoint for accounting blocked time (time the task is in uninterruptible).
480 */
3bc29f0a 481LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_blocked,
7c68b363
AG
482 TP_PROTO(struct task_struct *tsk, u64 delay),
483 TP_ARGS(tsk, delay))
7c68b363 484
f62b389e
MD
485/*
486 * Tracepoint for accounting runtime (time the task is executing
487 * on a CPU).
488 */
3bc29f0a 489LTTNG_TRACEPOINT_EVENT(sched_stat_runtime,
f62b389e
MD
490
491 TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime),
492
493 TP_ARGS(tsk, runtime, vruntime),
494
f127e61e
MD
495 TP_FIELDS(
496 ctf_array_text(char, comm, tsk->comm, TASK_COMM_LEN)
497 ctf_integer(pid_t, tid, tsk->pid)
498 ctf_integer(u64, runtime, runtime)
499 ctf_integer(u64, vruntime, vruntime)
f62b389e 500 )
f62b389e
MD
501)
502
5f4c791e 503#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,12,0) || \
46dded2d
MJ
504 LTTNG_RT_KERNEL_RANGE(4,9,27,18, 4,10,0,0) || \
505 LTTNG_RT_KERNEL_RANGE(4,11,5,1, 4,12,0,0))
673e9a03
MJ
506/*
507 * Tracepoint for showing priority inheritance modifying a tasks
508 * priority.
509 */
510LTTNG_TRACEPOINT_EVENT(sched_pi_setprio,
511
512 TP_PROTO(struct task_struct *tsk, struct task_struct *pi_task),
513
514 TP_ARGS(tsk, pi_task),
515
516 TP_FIELDS(
517 ctf_array_text(char, comm, tsk->comm, TASK_COMM_LEN)
518 ctf_integer(pid_t, tid, tsk->pid)
519 ctf_integer(int, oldprio, tsk->prio - MAX_RT_PRIO)
520 ctf_integer(int, newprio, pi_task ? pi_task->prio - MAX_RT_PRIO : tsk->prio - MAX_RT_PRIO)
521 )
522)
ead65427 523#else
f62b389e
MD
524/*
525 * Tracepoint for showing priority inheritance modifying a tasks
526 * priority.
527 */
3bc29f0a 528LTTNG_TRACEPOINT_EVENT(sched_pi_setprio,
f62b389e
MD
529
530 TP_PROTO(struct task_struct *tsk, int newprio),
531
532 TP_ARGS(tsk, newprio),
533
f127e61e
MD
534 TP_FIELDS(
535 ctf_array_text(char, comm, tsk->comm, TASK_COMM_LEN)
536 ctf_integer(pid_t, tid, tsk->pid)
537 ctf_integer(int, oldprio, tsk->prio - MAX_RT_PRIO)
538 ctf_integer(int, newprio, newprio - MAX_RT_PRIO)
539 )
f62b389e 540)
7c68b363 541#endif
f62b389e 542
3bc29f0a 543#endif /* LTTNG_TRACE_SCHED_H */
f62b389e
MD
544
545/* This part must be outside protection */
3b4aafcb 546#include <lttng/define_trace.h>
This page took 0.083834 seconds and 4 git commands to generate.