Drop support for kernels < 4.4 from sched instrumentation
[lttng-modules.git] / include / instrumentation / events / sched.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #undef TRACE_SYSTEM
3 #define TRACE_SYSTEM sched
4
5 #if !defined(LTTNG_TRACE_SCHED_H) || defined(TRACE_HEADER_MULTI_READ)
6 #define LTTNG_TRACE_SCHED_H
7
8 #include <lttng/tracepoint-event.h>
9 #include <linux/sched.h>
10 #include <linux/pid_namespace.h>
11 #include <linux/binfmts.h>
12 #include <lttng/kernel-version.h>
13 #include <linux/sched/rt.h>
14
15 #define LTTNG_MAX_PID_NS_LEVEL 32
16
17 #ifndef _TRACE_SCHED_DEF_
18 #define _TRACE_SCHED_DEF_
19
20 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
21
22 static 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))
51
52 static 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
78 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,14,0))
79
80 static 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
106 #else
107
108 static 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 }
119 #endif
120
121 #endif /* _TRACE_SCHED_DEF_ */
122
123 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
124 /*
125 * Enumeration of the task state bitmask.
126 * Only bit flags are enumerated here, not composition of states.
127 */
128 LTTNG_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)
137 ctf_enum_value("TASK_PARKED", TASK_PARKED)
138 ctf_enum_value("TASK_DEAD", TASK_DEAD)
139 ctf_enum_value("TASK_WAKEKILL", TASK_WAKEKILL)
140 ctf_enum_value("TASK_WAKING", TASK_WAKING)
141 ctf_enum_value("TASK_NOLOAD", TASK_NOLOAD)
142
143 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,8,0))
144 ctf_enum_value("TASK_NEW", TASK_NEW)
145 #endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,8,0)) */
146
147 ctf_enum_value("TASK_STATE_MAX", TASK_STATE_MAX)
148 )
149 )
150 #endif /* CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM */
151
152 /*
153 * Tracepoint for calling kthread_stop, performed to end a kthread:
154 */
155 LTTNG_TRACEPOINT_EVENT(sched_kthread_stop,
156
157 TP_PROTO(struct task_struct *t),
158
159 TP_ARGS(t),
160
161 TP_FIELDS(
162 ctf_array_text(char, comm, t->comm, TASK_COMM_LEN)
163 ctf_integer(pid_t, tid, t->pid)
164 )
165 )
166
167 /*
168 * Tracepoint for the return value of the kthread stopping:
169 */
170 LTTNG_TRACEPOINT_EVENT(sched_kthread_stop_ret,
171
172 TP_PROTO(int ret),
173
174 TP_ARGS(ret),
175
176 TP_FIELDS(
177 ctf_integer(int, ret, ret)
178 )
179 )
180
181 /*
182 * Tracepoint for waking up a task:
183 */
184 LTTNG_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)
193 ctf_integer(int, prio, p->prio - MAX_RT_PRIO)
194 ctf_integer(int, target_cpu, task_cpu(p))
195 )
196 )
197
198 /*
199 * Tracepoint called when waking a task; this tracepoint is guaranteed to be
200 * called from the waking context.
201 */
202 LTTNG_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 */
210 LTTNG_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 */
217 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup_new,
218 TP_PROTO(struct task_struct *p),
219 TP_ARGS(p))
220
221 /*
222 * Tracepoint for task switches, performed by the scheduler:
223 */
224
225 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
226 LTTNG_TRACEPOINT_EVENT(sched_switch,
227
228 TP_PROTO(bool preempt,
229 struct task_struct *prev,
230 struct task_struct *next,
231 unsigned int prev_state),
232
233 TP_ARGS(preempt, prev, next, prev_state),
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))
241 #else
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
250 #else
251
252 LTTNG_TRACEPOINT_EVENT(sched_switch,
253
254 TP_PROTO(bool preempt,
255 struct task_struct *prev,
256 struct task_struct *next),
257
258 TP_ARGS(preempt, prev, next),
259
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)
264 #ifdef CONFIG_LTTNG_EXPERIMENTAL_BITWISE_ENUM
265 ctf_enum(task_state, long, prev_state, __trace_sched_switch_state(preempt, prev))
266 #else
267 ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev))
268 #endif
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 )
274 #endif
275
276 /*
277 * Tracepoint for a task being migrated:
278 */
279 LTTNG_TRACEPOINT_EVENT(sched_migrate_task,
280
281 TP_PROTO(struct task_struct *p, int dest_cpu),
282
283 TP_ARGS(p, dest_cpu),
284
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 )
292 )
293
294 LTTNG_TRACEPOINT_EVENT_CLASS(sched_process_template,
295
296 TP_PROTO(struct task_struct *p),
297
298 TP_ARGS(p),
299
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 )
305 )
306
307 /*
308 * Tracepoint for freeing a task:
309 */
310 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_process_free,
311 TP_PROTO(struct task_struct *p),
312 TP_ARGS(p))
313
314
315 /*
316 * Tracepoint for a task exiting:
317 */
318 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_process_exit,
319 TP_PROTO(struct task_struct *p),
320 TP_ARGS(p))
321
322 /*
323 * Tracepoint for waiting on task to unschedule:
324 */
325 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_wait_task,
326 TP_PROTO(struct task_struct *p),
327 TP_ARGS(p))
328
329 /*
330 * Tracepoint for a waiting task:
331 */
332 LTTNG_TRACEPOINT_EVENT(sched_process_wait,
333
334 TP_PROTO(struct pid *pid),
335
336 TP_ARGS(pid),
337
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 )
343 )
344
345 /*
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.
352 */
353 LTTNG_TRACEPOINT_EVENT_CODE(sched_process_fork,
354
355 TP_PROTO(struct task_struct *parent, struct task_struct *child),
356
357 TP_ARGS(parent, child),
358
359 TP_locvar(
360 pid_t vtids[LTTNG_MAX_PID_NS_LEVEL];
361 unsigned int ns_level;
362 ),
363
364 TP_code_pre(
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
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)
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 =
392 pid_ns->ns.inum;
393 }
394 parent_ns_inum;
395 }))
396 ctf_array_text(char, child_comm, child->comm, TASK_COMM_LEN)
397 ctf_integer(pid_t, child_tid, child->pid)
398 ctf_sequence(pid_t, vtids, tp_locvar->vtids, u8, tp_locvar->ns_level)
399 ctf_integer(pid_t, child_pid, child->tgid)
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 =
410 pid_ns->ns.inum;
411 }
412 child_ns_inum;
413 }))
414 ),
415
416 TP_code_post()
417 )
418
419 /*
420 * Tracepoint for exec:
421 */
422 LTTNG_TRACEPOINT_EVENT(sched_process_exec,
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
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 )
434 )
435
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 */
440 LTTNG_TRACEPOINT_EVENT_CLASS(sched_stat_template,
441
442 TP_PROTO(struct task_struct *tsk, u64 delay),
443
444 TP_ARGS(tsk, delay),
445
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)
450 )
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 */
458 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_wait,
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 */
466 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_sleep,
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 */
474 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_iowait,
475 TP_PROTO(struct task_struct *tsk, u64 delay),
476 TP_ARGS(tsk, delay))
477
478 /*
479 * Tracepoint for accounting blocked time (time the task is in uninterruptible).
480 */
481 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_blocked,
482 TP_PROTO(struct task_struct *tsk, u64 delay),
483 TP_ARGS(tsk, delay))
484
485 /*
486 * Tracepoint for accounting runtime (time the task is executing
487 * on a CPU).
488 */
489 LTTNG_TRACEPOINT_EVENT(sched_stat_runtime,
490
491 TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime),
492
493 TP_ARGS(tsk, runtime, vruntime),
494
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)
500 )
501 )
502
503 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,12,0) || \
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))
506 /*
507 * Tracepoint for showing priority inheritance modifying a tasks
508 * priority.
509 */
510 LTTNG_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 )
523 #else
524 /*
525 * Tracepoint for showing priority inheritance modifying a tasks
526 * priority.
527 */
528 LTTNG_TRACEPOINT_EVENT(sched_pi_setprio,
529
530 TP_PROTO(struct task_struct *tsk, int newprio),
531
532 TP_ARGS(tsk, newprio),
533
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 )
540 )
541 #endif
542
543 #endif /* LTTNG_TRACE_SCHED_H */
544
545 /* This part must be outside protection */
546 #include <lttng/define_trace.h>
This page took 0.040194 seconds and 4 git commands to generate.