aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/sched_task_trace.c
blob: b7ea6d4e6e573468e11e7c80288390e8aa4356cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * sched_task_trace.c -- record scheduling events to a byte stream
 */

#define NO_TASK_TRACE_DECLS

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/percpu.h>

#include <litmus/ftdev.h>
#include <litmus/litmus.h>

#include <litmus/sched_trace.h>
#include <litmus/feather_trace.h>
#include <litmus/ftdev.h>

#define FT_TASK_TRACE_MAJOR	253
#define NO_EVENTS		4096 /* this is a buffer of 12 4k pages per CPU */

#define now() litmus_clock()

struct local_buffer {
	struct st_event_record record[NO_EVENTS];
	char   flag[NO_EVENTS];
	struct ft_buffer ftbuf;
};

DEFINE_PER_CPU(struct local_buffer, st_event_buffer);

static struct ftdev st_dev;

static int st_dev_can_open(struct ftdev *dev, unsigned int cpu)
{
	return cpu_online(cpu) ? 0 : -ENODEV;
}

static int __init init_sched_task_trace(void)
{
	struct local_buffer* buf;
	int i, ok = 0;
	ftdev_init(&st_dev, THIS_MODULE);
	for (i = 0; i < NR_CPUS; i++) {
		buf = &per_cpu(st_event_buffer, i);
		ok += init_ft_buffer(&buf->ftbuf, NO_EVENTS,
				     sizeof(struct st_event_record),
				     buf->flag,
				     buf->record);
		st_dev.minor[i].buf = &buf->ftbuf;
	}
	if (ok == NR_CPUS) {
		st_dev.minor_cnt = NR_CPUS;
		st_dev.can_open = st_dev_can_open;
		return register_ftdev(&st_dev, "sched_trace", FT_TASK_TRACE_MAJOR);
	} else {
		return -EINVAL;
	}
}

module_init(init_sched_task_trace);


static inline struct st_event_record* get_record(u8 type, struct task_struct* t)
{
	struct st_event_record* rec;
	struct local_buffer* buf;

	buf = &get_cpu_var(st_event_buffer);
	if (ft_buffer_start_write(&buf->ftbuf, (void**) &rec)) {
		rec->hdr.type = type;
		rec->hdr.cpu  = smp_processor_id();
		rec->hdr.pid  = t ? t->pid : 0;
		rec->hdr.job  = t ? t->rt_param.job_params.job_no : 0;
	} else {
		put_cpu_var(st_event_buffer);
	}
	/* rec will be NULL if it failed */
	return rec;
}

static inline void put_record(struct st_event_record* rec)
{
	struct local_buffer* buf;
	buf = &__get_cpu_var(st_event_buffer);
	ft_buffer_finish_write(&buf->ftbuf, rec);
	put_cpu_var(st_event_buffer);
}

feather_callback void do_sched_trace_task_name(unsigned long id, unsigned long _task)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec = get_record(ST_NAME, t);
	int i;
	if (rec) {
		for (i = 0; i < min(TASK_COMM_LEN, ST_NAME_LEN); i++)
			rec->data.name.cmd[i] = t->comm[i];
		put_record(rec);
	}
}

feather_callback void do_sched_trace_task_param(unsigned long id, unsigned long _task)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec = get_record(ST_PARAM, t);
	if (rec) {
		rec->data.param.wcet      = get_exec_cost(t);
		rec->data.param.period    = get_rt_period(t);
		rec->data.param.phase     = get_rt_phase(t);
		rec->data.param.partition = get_partition(t);
		put_record(rec);
	}
}

feather_callback void do_sched_trace_task_release(unsigned long id, unsigned long _task)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec = get_record(ST_RELEASE, t);
	if (rec) {
		rec->data.release.release  = get_release(t);
		rec->data.release.deadline = get_deadline(t);
		put_record(rec);
	}
}

/* skipped: st_assigned_data, we don't use it atm */

feather_callback void do_sched_trace_task_switch_to(unsigned long id,
						    unsigned long _task)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec;
	if (is_realtime(t)) {
		rec = get_record(ST_SWITCH_TO, t);
		if (rec) {
			rec->data.switch_to.when      = now();
			rec->data.switch_to.exec_time = get_exec_time(t);
			put_record(rec);
		}
	}
}

feather_callback void do_sched_trace_task_switch_away(unsigned long id,
						      unsigned long _task)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec;
	if (is_realtime(t)) {
		rec = get_record(ST_SWITCH_AWAY, t);
		if (rec) {
			rec->data.switch_away.when      = now();
			rec->data.switch_away.exec_time = get_exec_time(t);
			put_record(rec);
		}
	}
}

feather_callback void do_sched_trace_task_completion(unsigned long id,
						     unsigned long _task,
						     unsigned long forced)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec = get_record(ST_COMPLETION, t);
	if (rec) {
		rec->data.completion.when   = now();
		rec->data.completion.forced = forced;
		put_record(rec);
	}
}

feather_callback void do_sched_trace_task_block(unsigned long id,
						unsigned long _task)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec = get_record(ST_BLOCK, t);
	if (rec) {
		rec->data.block.when      = now();
		put_record(rec);
	}
}

feather_callback void do_sched_trace_task_resume(unsigned long id,
						 unsigned long _task)
{
	struct task_struct *t = (struct task_struct*) _task;
	struct st_event_record* rec = get_record(ST_RESUME, t);
	if (rec) {
		rec->data.resume.when      = now();
		put_record(rec);
	}
}

feather_callback void do_sched_trace_sys_release(unsigned long id,
						 unsigned long _start)
{
	lt_t *start = (lt_t*) _start;
	struct st_event_record* rec = get_record(ST_SYS_RELEASE, NULL);
	if (rec) {
		rec->data.sys_release.when    = now();
		rec->data.sys_release.release = *start;
		put_record(rec);
	}
}