Add namespace contexts
[deliverable/lttng-modules.git] / instrumentation / events / lttng-module / sched.h
1 /* SPDX-License-Identifier: GPL-2.0 */
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 <probes/lttng-tracepoint-event.h>
9 #include <linux/sched.h>
10 #include <linux/pid_namespace.h>
11 #include <linux/binfmts.h>
12 #include <linux/version.h>
13 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
14 #include <linux/sched/rt.h>
15 #endif
16 #include <wrapper/namespace.h>
17
18 #define LTTNG_MAX_PID_NS_LEVEL 32
19
20 #ifndef _TRACE_SCHED_DEF_
21 #define _TRACE_SCHED_DEF_
22
23 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0))
24
25 static 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))
38
39 static 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))
58
59 static 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))
75
76 static 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)
85 state = TASK_RUNNING | TASK_STATE_MAX;
86 #endif
87
88 return state;
89 }
90
91 #else
92
93 static 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;
103 #endif
104
105 return state;
106 }
107
108 #endif
109
110 #endif /* _TRACE_SCHED_DEF_ */
111
112 /*
113 * Tracepoint for calling kthread_stop, performed to end a kthread:
114 */
115 LTTNG_TRACEPOINT_EVENT(sched_kthread_stop,
116
117 TP_PROTO(struct task_struct *t),
118
119 TP_ARGS(t),
120
121 TP_FIELDS(
122 ctf_array_text(char, comm, t->comm, TASK_COMM_LEN)
123 ctf_integer(pid_t, tid, t->pid)
124 )
125 )
126
127 /*
128 * Tracepoint for the return value of the kthread stopping:
129 */
130 LTTNG_TRACEPOINT_EVENT(sched_kthread_stop_ret,
131
132 TP_PROTO(int ret),
133
134 TP_ARGS(ret),
135
136 TP_FIELDS(
137 ctf_integer(int, ret, ret)
138 )
139 )
140
141 /*
142 * Tracepoint for waking up a task:
143 */
144 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0) || \
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))
152 LTTNG_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)
161 ctf_integer(int, prio, p->prio - MAX_RT_PRIO)
162 ctf_integer(int, target_cpu, task_cpu(p))
163 )
164 )
165 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)) */
166 LTTNG_TRACEPOINT_EVENT_CLASS(sched_wakeup_template,
167
168 TP_PROTO(struct task_struct *p, int success),
169
170 TP_ARGS(p, success),
171
172 TP_FIELDS(
173 ctf_array_text(char, comm, p->comm, TASK_COMM_LEN)
174 ctf_integer(pid_t, tid, p->pid)
175 ctf_integer(int, prio, p->prio - MAX_RT_PRIO)
176 ctf_integer(int, success, success)
177 ctf_integer(int, target_cpu, task_cpu(p))
178 )
179 )
180 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)) */
181
182 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0) || \
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))
190
191 /*
192 * Tracepoint called when waking a task; this tracepoint is guaranteed to be
193 * called from the waking context.
194 */
195 LTTNG_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 */
203 LTTNG_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 */
210 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup_new,
211 TP_PROTO(struct task_struct *p),
212 TP_ARGS(p))
213
214 #else
215
216 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup,
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 */
223 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_wakeup_template, sched_wakeup_new,
224 TP_PROTO(struct task_struct *p, int success),
225 TP_ARGS(p, success))
226
227 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)) */
228
229 /*
230 * Tracepoint for task switches, performed by the scheduler:
231 */
232 LTTNG_TRACEPOINT_EVENT(sched_switch,
233
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),
240 #else
241 TP_PROTO(struct task_struct *prev,
242 struct task_struct *next),
243
244 TP_ARGS(prev, next),
245 #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)) */
246
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)
251 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0))
252 ctf_integer(long, prev_state, __trace_sched_switch_state(preempt, prev))
253 #else
254 ctf_integer(long, prev_state, __trace_sched_switch_state(prev))
255 #endif
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 )
260 )
261
262 /*
263 * Tracepoint for a task being migrated:
264 */
265 LTTNG_TRACEPOINT_EVENT(sched_migrate_task,
266
267 TP_PROTO(struct task_struct *p, int dest_cpu),
268
269 TP_ARGS(p, dest_cpu),
270
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 )
278 )
279
280 LTTNG_TRACEPOINT_EVENT_CLASS(sched_process_template,
281
282 TP_PROTO(struct task_struct *p),
283
284 TP_ARGS(p),
285
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 )
291 )
292
293 /*
294 * Tracepoint for freeing a task:
295 */
296 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_process_free,
297 TP_PROTO(struct task_struct *p),
298 TP_ARGS(p))
299
300
301 /*
302 * Tracepoint for a task exiting:
303 */
304 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_process_exit,
305 TP_PROTO(struct task_struct *p),
306 TP_ARGS(p))
307
308 /*
309 * Tracepoint for waiting on task to unschedule:
310 */
311 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_process_template, sched_wait_task,
312 TP_PROTO(struct task_struct *p),
313 TP_ARGS(p))
314
315 /*
316 * Tracepoint for a waiting task:
317 */
318 LTTNG_TRACEPOINT_EVENT(sched_process_wait,
319
320 TP_PROTO(struct pid *pid),
321
322 TP_ARGS(pid),
323
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 )
329 )
330
331 /*
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.
338 */
339 LTTNG_TRACEPOINT_EVENT_CODE(sched_process_fork,
340
341 TP_PROTO(struct task_struct *parent, struct task_struct *child),
342
343 TP_ARGS(parent, child),
344
345 TP_locvar(
346 pid_t vtids[LTTNG_MAX_PID_NS_LEVEL];
347 unsigned int ns_level;
348 ),
349
350 TP_code_pre(
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
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)
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 =
379 pid_ns->lttng_ns_inum;
380 }
381 parent_ns_inum;
382 }))
383 #endif
384 ctf_array_text(char, child_comm, child->comm, TASK_COMM_LEN)
385 ctf_integer(pid_t, child_tid, child->pid)
386 ctf_sequence(pid_t, vtids, tp_locvar->vtids, u8, tp_locvar->ns_level)
387 ctf_integer(pid_t, child_pid, child->tgid)
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 =
399 pid_ns->lttng_ns_inum;
400 }
401 child_ns_inum;
402 }))
403 #endif
404 ),
405
406 TP_code_post()
407 )
408
409 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
410 /*
411 * Tracepoint for exec:
412 */
413 LTTNG_TRACEPOINT_EVENT(sched_process_exec,
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
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 )
425 )
426 #endif
427
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 */
432 LTTNG_TRACEPOINT_EVENT_CLASS(sched_stat_template,
433
434 TP_PROTO(struct task_struct *tsk, u64 delay),
435
436 TP_ARGS(tsk, delay),
437
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)
442 )
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 */
450 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_wait,
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 */
458 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_sleep,
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 */
466 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_iowait,
467 TP_PROTO(struct task_struct *tsk, u64 delay),
468 TP_ARGS(tsk, delay))
469
470 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0))
471 /*
472 * Tracepoint for accounting blocked time (time the task is in uninterruptible).
473 */
474 LTTNG_TRACEPOINT_EVENT_INSTANCE(sched_stat_template, sched_stat_blocked,
475 TP_PROTO(struct task_struct *tsk, u64 delay),
476 TP_ARGS(tsk, delay))
477 #endif
478
479 /*
480 * Tracepoint for accounting runtime (time the task is executing
481 * on a CPU).
482 */
483 LTTNG_TRACEPOINT_EVENT(sched_stat_runtime,
484
485 TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime),
486
487 TP_ARGS(tsk, runtime, vruntime),
488
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)
494 )
495 )
496
497 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0) || \
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))
500 /*
501 * Tracepoint for showing priority inheritance modifying a tasks
502 * priority.
503 */
504 LTTNG_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 )
517 #else
518 /*
519 * Tracepoint for showing priority inheritance modifying a tasks
520 * priority.
521 */
522 LTTNG_TRACEPOINT_EVENT(sched_pi_setprio,
523
524 TP_PROTO(struct task_struct *tsk, int newprio),
525
526 TP_ARGS(tsk, newprio),
527
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 )
534 )
535 #endif
536
537 #endif /* LTTNG_TRACE_SCHED_H */
538
539 /* This part must be outside protection */
540 #include <probes/define_trace.h>
This page took 0.042753 seconds and 5 git commands to generate.