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