trace_sched_switch.c 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * trace context switch
 *
 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
 *
 */
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/debugfs.h>
#include <linux/kallsyms.h>
#include <linux/uaccess.h>
#include <linux/ftrace.h>
M
Mathieu Desnoyers 已提交
13
#include <trace/sched.h>
14 15 16 17 18

#include "trace.h"

static struct trace_array	*ctx_trace;
static int __read_mostly	tracer_enabled;
M
Mathieu Desnoyers 已提交
19
static atomic_t			sched_ref;
20

I
Ingo Molnar 已提交
21
static void
M
Mathieu Desnoyers 已提交
22
probe_sched_switch(struct rq *__rq, struct task_struct *prev,
M
Mathieu Desnoyers 已提交
23
			struct task_struct *next)
24 25 26 27 28 29
{
	struct trace_array_cpu *data;
	unsigned long flags;
	long disabled;
	int cpu;

M
Mathieu Desnoyers 已提交
30 31 32
	if (!atomic_read(&sched_ref))
		return;

33 34 35
	tracing_record_cmdline(prev);
	tracing_record_cmdline(next);

36 37 38
	if (!tracer_enabled)
		return;

39
	local_irq_save(flags);
40
	cpu = raw_smp_processor_id();
M
Mathieu Desnoyers 已提交
41
	data = ctx_trace->data[cpu];
42 43
	disabled = atomic_inc_return(&data->disabled);

I
Ingo Molnar 已提交
44
	if (likely(disabled == 1))
M
Mathieu Desnoyers 已提交
45
		tracing_sched_switch_trace(ctx_trace, data, prev, next, flags);
46 47

	atomic_dec(&data->disabled);
48
	local_irq_restore(flags);
49 50
}

51
static void
M
Mathieu Desnoyers 已提交
52
probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee)
53 54 55 56 57 58
{
	struct trace_array_cpu *data;
	unsigned long flags;
	long disabled;
	int cpu;

M
Mathieu Desnoyers 已提交
59
	if (!likely(tracer_enabled))
60 61
		return;

M
Mathieu Desnoyers 已提交
62
	tracing_record_cmdline(current);
I
Ingo Molnar 已提交
63

64 65
	local_irq_save(flags);
	cpu = raw_smp_processor_id();
M
Mathieu Desnoyers 已提交
66
	data = ctx_trace->data[cpu];
67 68
	disabled = atomic_inc_return(&data->disabled);

I
Ingo Molnar 已提交
69
	if (likely(disabled == 1))
M
Mathieu Desnoyers 已提交
70 71
		tracing_sched_wakeup_trace(ctx_trace, data, wakee, current,
			flags);
72 73 74 75 76

	atomic_dec(&data->disabled);
	local_irq_restore(flags);
}

I
Ingo Molnar 已提交
77
static void sched_switch_reset(struct trace_array *tr)
78 79 80
{
	int cpu;

I
Ingo Molnar 已提交
81
	tr->time_start = ftrace_now(tr->cpu);
82 83

	for_each_online_cpu(cpu)
84
		tracing_reset(tr, cpu);
85 86
}

M
Mathieu Desnoyers 已提交
87 88 89 90
static int tracing_sched_register(void)
{
	int ret;

M
Mathieu Desnoyers 已提交
91
	ret = register_trace_sched_wakeup(probe_sched_wakeup);
M
Mathieu Desnoyers 已提交
92
	if (ret) {
M
Mathieu Desnoyers 已提交
93
		pr_info("wakeup trace: Couldn't activate tracepoint"
M
Mathieu Desnoyers 已提交
94 95 96 97
			" probe to kernel_sched_wakeup\n");
		return ret;
	}

M
Mathieu Desnoyers 已提交
98
	ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
M
Mathieu Desnoyers 已提交
99
	if (ret) {
M
Mathieu Desnoyers 已提交
100
		pr_info("wakeup trace: Couldn't activate tracepoint"
M
Mathieu Desnoyers 已提交
101 102 103 104
			" probe to kernel_sched_wakeup_new\n");
		goto fail_deprobe;
	}

M
Mathieu Desnoyers 已提交
105
	ret = register_trace_sched_switch(probe_sched_switch);
M
Mathieu Desnoyers 已提交
106
	if (ret) {
M
Mathieu Desnoyers 已提交
107
		pr_info("sched trace: Couldn't activate tracepoint"
M
Mathieu Desnoyers 已提交
108 109 110 111 112 113
			" probe to kernel_sched_schedule\n");
		goto fail_deprobe_wake_new;
	}

	return ret;
fail_deprobe_wake_new:
M
Mathieu Desnoyers 已提交
114
	unregister_trace_sched_wakeup_new(probe_sched_wakeup);
M
Mathieu Desnoyers 已提交
115
fail_deprobe:
M
Mathieu Desnoyers 已提交
116
	unregister_trace_sched_wakeup(probe_sched_wakeup);
M
Mathieu Desnoyers 已提交
117 118 119 120 121
	return ret;
}

static void tracing_sched_unregister(void)
{
M
Mathieu Desnoyers 已提交
122 123 124
	unregister_trace_sched_switch(probe_sched_switch);
	unregister_trace_sched_wakeup_new(probe_sched_wakeup);
	unregister_trace_sched_wakeup(probe_sched_wakeup);
M
Mathieu Desnoyers 已提交
125 126
}

I
Ingo Molnar 已提交
127
static void tracing_start_sched_switch(void)
M
Mathieu Desnoyers 已提交
128 129 130 131 132 133 134 135
{
	long ref;

	ref = atomic_inc_return(&sched_ref);
	if (ref == 1)
		tracing_sched_register();
}

I
Ingo Molnar 已提交
136
static void tracing_stop_sched_switch(void)
M
Mathieu Desnoyers 已提交
137 138 139 140 141 142 143 144
{
	long ref;

	ref = atomic_dec_and_test(&sched_ref);
	if (ref)
		tracing_sched_unregister();
}

145 146 147 148 149 150 151 152 153 154
void tracing_start_cmdline_record(void)
{
	tracing_start_sched_switch();
}

void tracing_stop_cmdline_record(void)
{
	tracing_stop_sched_switch();
}

I
Ingo Molnar 已提交
155
static void start_sched_trace(struct trace_array *tr)
156 157
{
	sched_switch_reset(tr);
158
	tracing_start_cmdline_record();
159
	tracer_enabled = 1;
160 161
}

I
Ingo Molnar 已提交
162
static void stop_sched_trace(struct trace_array *tr)
163 164
{
	tracer_enabled = 0;
165
	tracing_stop_cmdline_record();
166 167
}

I
Ingo Molnar 已提交
168
static void sched_switch_trace_init(struct trace_array *tr)
169 170 171 172 173 174 175
{
	ctx_trace = tr;

	if (tr->ctrl)
		start_sched_trace(tr);
}

I
Ingo Molnar 已提交
176
static void sched_switch_trace_reset(struct trace_array *tr)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
{
	if (tr->ctrl)
		stop_sched_trace(tr);
}

static void sched_switch_trace_ctrl_update(struct trace_array *tr)
{
	/* When starting a new trace, reset the buffers */
	if (tr->ctrl)
		start_sched_trace(tr);
	else
		stop_sched_trace(tr);
}

static struct tracer sched_switch_trace __read_mostly =
{
	.name		= "sched_switch",
	.init		= sched_switch_trace_init,
	.reset		= sched_switch_trace_reset,
	.ctrl_update	= sched_switch_trace_ctrl_update,
S
Steven Rostedt 已提交
197 198 199
#ifdef CONFIG_FTRACE_SELFTEST
	.selftest    = trace_selftest_startup_sched_switch,
#endif
200 201 202 203
};

__init static int init_sched_switch_trace(void)
{
M
Mathieu Desnoyers 已提交
204 205 206 207 208 209 210 211
	int ret = 0;

	if (atomic_read(&sched_ref))
		ret = tracing_sched_register();
	if (ret) {
		pr_info("error registering scheduler trace\n");
		return ret;
	}
212 213 214
	return register_tracer(&sched_switch_trace);
}
device_initcall(init_sched_switch_trace);