Skip to content

Commit 0a16b60

Browse files
Mathieu DesnoyersIngo Molnar
authored andcommitted
tracing, sched: LTTng instrumentation - scheduler
Instrument the scheduler activity (sched_switch, migration, wakeups, wait for a task, signal delivery) and process/thread creation/destruction (fork, exit, kthread stop). Actually, kthread creation is not instrumented in this patch because it is architecture dependent. It allows to connect tracers such as ftrace which detects scheduling latencies, good/bad scheduler decisions. Tools like LTTng can export this scheduler information along with instrumentation of the rest of the kernel activity to perform post-mortem analysis on the scheduler activity. About the performance impact of tracepoints (which is comparable to markers), even without immediate values optimizations, tests done by Hideo Aoki on ia64 show no regression. His test case was using hackbench on a kernel where scheduler instrumentation (about 5 events in code scheduler code) was added. See the "Tracepoints" patch header for performance result detail. Changelog : - Change instrumentation location and parameter to match ftrace instrumentation, previously done with kernel markers. [ mingo@elte.hu: conflict resolutions ] Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> Acked-by: 'Peter Zijlstra' <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 4a08975 commit 0a16b60

6 files changed

Lines changed: 71 additions & 12 deletions

File tree

include/trace/sched.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef _TRACE_SCHED_H
2+
#define _TRACE_SCHED_H
3+
4+
#include <linux/sched.h>
5+
#include <linux/tracepoint.h>
6+
7+
DEFINE_TRACE(sched_kthread_stop,
8+
TPPROTO(struct task_struct *t),
9+
TPARGS(t));
10+
DEFINE_TRACE(sched_kthread_stop_ret,
11+
TPPROTO(int ret),
12+
TPARGS(ret));
13+
DEFINE_TRACE(sched_wait_task,
14+
TPPROTO(struct rq *rq, struct task_struct *p),
15+
TPARGS(rq, p));
16+
DEFINE_TRACE(sched_wakeup,
17+
TPPROTO(struct rq *rq, struct task_struct *p),
18+
TPARGS(rq, p));
19+
DEFINE_TRACE(sched_wakeup_new,
20+
TPPROTO(struct rq *rq, struct task_struct *p),
21+
TPARGS(rq, p));
22+
DEFINE_TRACE(sched_switch,
23+
TPPROTO(struct rq *rq, struct task_struct *prev,
24+
struct task_struct *next),
25+
TPARGS(rq, prev, next));
26+
DEFINE_TRACE(sched_migrate_task,
27+
TPPROTO(struct rq *rq, struct task_struct *p, int dest_cpu),
28+
TPARGS(rq, p, dest_cpu));
29+
DEFINE_TRACE(sched_process_free,
30+
TPPROTO(struct task_struct *p),
31+
TPARGS(p));
32+
DEFINE_TRACE(sched_process_exit,
33+
TPPROTO(struct task_struct *p),
34+
TPARGS(p));
35+
DEFINE_TRACE(sched_process_wait,
36+
TPPROTO(struct pid *pid),
37+
TPARGS(pid));
38+
DEFINE_TRACE(sched_process_fork,
39+
TPPROTO(struct task_struct *parent, struct task_struct *child),
40+
TPARGS(parent, child));
41+
DEFINE_TRACE(sched_signal_send,
42+
TPPROTO(int sig, struct task_struct *p),
43+
TPARGS(sig, p));
44+
45+
#endif

kernel/exit.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <linux/blkdev.h>
4848
#include <linux/task_io_accounting_ops.h>
4949
#include <linux/tracehook.h>
50+
#include <trace/sched.h>
5051

5152
#include <asm/uaccess.h>
5253
#include <asm/unistd.h>
@@ -149,7 +150,10 @@ static void __exit_signal(struct task_struct *tsk)
149150

150151
static void delayed_put_task_struct(struct rcu_head *rhp)
151152
{
152-
put_task_struct(container_of(rhp, struct task_struct, rcu));
153+
struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
154+
155+
trace_sched_process_free(tsk);
156+
put_task_struct(tsk);
153157
}
154158

155159

@@ -1074,6 +1078,8 @@ NORET_TYPE void do_exit(long code)
10741078

10751079
if (group_dead)
10761080
acct_process();
1081+
trace_sched_process_exit(tsk);
1082+
10771083
exit_sem(tsk);
10781084
exit_files(tsk);
10791085
exit_fs(tsk);
@@ -1675,6 +1681,8 @@ static long do_wait(enum pid_type type, struct pid *pid, int options,
16751681
struct task_struct *tsk;
16761682
int retval;
16771683

1684+
trace_sched_process_wait(pid);
1685+
16781686
add_wait_queue(&current->signal->wait_chldexit,&wait);
16791687
repeat:
16801688
/*

kernel/fork.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <linux/tty.h>
5959
#include <linux/proc_fs.h>
6060
#include <linux/blkdev.h>
61+
#include <trace/sched.h>
6162

6263
#include <asm/pgtable.h>
6364
#include <asm/pgalloc.h>
@@ -1364,6 +1365,8 @@ long do_fork(unsigned long clone_flags,
13641365
if (!IS_ERR(p)) {
13651366
struct completion vfork;
13661367

1368+
trace_sched_process_fork(current, p);
1369+
13671370
nr = task_pid_vnr(p);
13681371

13691372
if (clone_flags & CLONE_PARENT_SETTID)

kernel/kthread.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/file.h>
1414
#include <linux/module.h>
1515
#include <linux/mutex.h>
16+
#include <trace/sched.h>
1617

1718
#define KTHREAD_NICE_LEVEL (-5)
1819

@@ -206,6 +207,8 @@ int kthread_stop(struct task_struct *k)
206207
/* It could exit after stop_info.k set, but before wake_up_process. */
207208
get_task_struct(k);
208209

210+
trace_sched_kthread_stop(k);
211+
209212
/* Must init completion *before* thread sees kthread_stop_info.k */
210213
init_completion(&kthread_stop_info.done);
211214
smp_wmb();
@@ -221,6 +224,8 @@ int kthread_stop(struct task_struct *k)
221224
ret = kthread_stop_info.err;
222225
mutex_unlock(&kthread_stop_lock);
223226

227+
trace_sched_kthread_stop_ret(ret);
228+
224229
return ret;
225230
}
226231
EXPORT_SYMBOL(kthread_stop);

kernel/sched.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include <linux/debugfs.h>
7272
#include <linux/ctype.h>
7373
#include <linux/ftrace.h>
74+
#include <trace/sched.h>
7475

7576
#include <asm/tlb.h>
7677
#include <asm/irq_regs.h>
@@ -1936,6 +1937,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
19361937
* just go back and repeat.
19371938
*/
19381939
rq = task_rq_lock(p, &flags);
1940+
trace_sched_wait_task(rq, p);
19391941
running = task_running(rq, p);
19401942
on_rq = p->se.on_rq;
19411943
ncsw = 0;
@@ -2297,9 +2299,7 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
22972299
success = 1;
22982300

22992301
out_running:
2300-
trace_mark(kernel_sched_wakeup,
2301-
"pid %d state %ld ## rq %p task %p rq->curr %p",
2302-
p->pid, p->state, rq, p, rq->curr);
2302+
trace_sched_wakeup(rq, p);
23032303
check_preempt_curr(rq, p, sync);
23042304

23052305
p->state = TASK_RUNNING;
@@ -2432,9 +2432,7 @@ void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
24322432
p->sched_class->task_new(rq, p);
24332433
inc_nr_running(rq);
24342434
}
2435-
trace_mark(kernel_sched_wakeup_new,
2436-
"pid %d state %ld ## rq %p task %p rq->curr %p",
2437-
p->pid, p->state, rq, p, rq->curr);
2435+
trace_sched_wakeup_new(rq, p);
24382436
check_preempt_curr(rq, p, 0);
24392437
#ifdef CONFIG_SMP
24402438
if (p->sched_class->task_wake_up)
@@ -2607,11 +2605,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
26072605
struct mm_struct *mm, *oldmm;
26082606

26092607
prepare_task_switch(rq, prev, next);
2610-
trace_mark(kernel_sched_schedule,
2611-
"prev_pid %d next_pid %d prev_state %ld "
2612-
"## rq %p prev %p next %p",
2613-
prev->pid, next->pid, prev->state,
2614-
rq, prev, next);
2608+
trace_sched_switch(rq, prev, next);
26152609
mm = next->mm;
26162610
oldmm = prev->active_mm;
26172611
/*
@@ -2851,6 +2845,7 @@ static void sched_migrate_task(struct task_struct *p, int dest_cpu)
28512845
|| unlikely(!cpu_active(dest_cpu)))
28522846
goto out;
28532847

2848+
trace_sched_migrate_task(rq, p, dest_cpu);
28542849
/* force the process onto the specified CPU */
28552850
if (migrate_task(p, dest_cpu, &req)) {
28562851
/* Need to wait for migration thread (might exit: take ref). */

kernel/signal.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/freezer.h>
2828
#include <linux/pid_namespace.h>
2929
#include <linux/nsproxy.h>
30+
#include <trace/sched.h>
3031

3132
#include <asm/param.h>
3233
#include <asm/uaccess.h>
@@ -803,6 +804,8 @@ static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
803804
struct sigpending *pending;
804805
struct sigqueue *q;
805806

807+
trace_sched_signal_send(sig, t);
808+
806809
assert_spin_locked(&t->sighand->siglock);
807810
if (!prepare_signal(sig, t))
808811
return 0;

0 commit comments

Comments
 (0)