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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
#include <linux/module.h>
#include <linux/kernel.h> /* for sscanf, prink, etc. */
#include <linux/uaccess.h> /* for copy_from_user */
#include <linux/percpu.h>
#include <linux/spinlock.h>
#include <linux/smp.h>
#include <linux/ctype.h>
#include <linux/time.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <litmus/litmus.h>
#include <litmus/rt_domain.h>
#include <litmus/sched_plugin.h>
#include <litmus/litmus_proc.h>
#include <litmus/edf_common.h>
/* forward declaration so the below macro works */
static struct sched_plugin edf_hsb_plugin __cacheline_aligned_in_smp;
#define is_active_plugin (litmus == &edf_hsb_plugin)
struct hrt_server {
int cpu;
/* lt_t is nanoseconds (as of now, anyway...) */
lt_t wcet;
lt_t budget;
lt_t period;
rt_domain_t domain;
/* lock for setting wcet and period */
rwlock_t param_lock;
};
DEFINE_PER_CPU_SHARED_ALIGNED(struct hrt_server, hrt_servers);
/*
* Set the WCET and period for an HRT server on a specific CPU.
* Times are given in milliseconds.
* There is a small race condition in checking we're not the active plugin
* before setting parameters, but we are not worried about that so much.
*/
int set_hrt_params(unsigned long long cpu, unsigned long long wcet,
unsigned long long period)
{
struct hrt_server *hrt_server;
int rv = 0;
/* do some sanity checking first */
if (wcet <= 0) {
printk(KERN_WARNING "Invalid WCET in %s\n", __func__);
rv = -EINVAL;
goto out;
}
if (period < wcet) {
printk(KERN_WARNING "Invalid period in %s\n", __func__);
rv = -EINVAL;
goto out;
}
if (cpu < 0 || cpu >= nr_cpu_ids) {
printk(KERN_WARNING "Invalid CPU in %s\n", __func__);
rv = -EINVAL;
goto out;
}
if (is_active_plugin) {
printk(KERN_WARNING "Can't set params on active plugin.\n");
rv = -EPERM;
goto out;
}
hrt_server = &per_cpu(hrt_servers, cpu);
write_lock_irq(&hrt_server->param_lock);
hrt_server->wcet = wcet * NSEC_PER_MSEC;
hrt_server->period = period * NSEC_PER_MSEC;
/*
* TODO probably need a function to reset hrtimers and stuff here
*/
write_unlock_irq(&hrt_server->param_lock);
out:
return rv;
}
/*
* Do all hrt_servers have a non-zero WCET and period? Return 0 if so.
*/
static int check_hrt_servers_initialized(void)
{
struct hrt_server *hrt_server;
int cpu, all_init = 1;
for_each_online_cpu(cpu) {
hrt_server = &per_cpu(hrt_servers, cpu);
read_lock_irq(&hrt_server->param_lock);
/* if any parameter is zero, all_init will become zero */
all_init = all_init && hrt_server->wcet &&
hrt_server->period;
read_unlock_irq(&hrt_server->param_lock);
}
/* inverted because zero is success */
return !all_init;
}
void reset_hrt_servers(void)
{
int cpu;
struct hrt_server *hrt_server;
for_each_online_cpu(cpu) {
hrt_server = &per_cpu(hrt_servers, cpu);
rwlock_init(&hrt_server->param_lock);
hrt_server->cpu = cpu;
hrt_server->wcet = hrt_server->period = hrt_server->budget = 0;
/* TODO correctly */
edf_domain_init(&hrt_server->domain, NULL, NULL);
}
}
static long edf_hsb_deactivate_plugin(void)
{
reset_hrt_servers();
return 0;
}
/*
* There is a small race condition in checking that all the server parameters
* are valid, then having one of them change before setting the active plugin,
* but oh well.
*/
static long edf_hsb_activate_plugin(void)
{
long rv = 0;
if (check_hrt_servers_initialized())
rv = -EINVAL;
return rv;
}
/************************************************************
* Proc stuff
************************************************************/
static struct proc_dir_entry *edf_hsb_proc_dir = NULL, *hrt_server_proc = NULL;
static int hrt_proc_read(char* page, char **start, off_t off, int count,
int *eof, void *data)
{
int cpu, wcet, period, len = 0;
struct hrt_server *hrt_server;
for_each_online_cpu(cpu) {
hrt_server = &per_cpu(hrt_servers, cpu);
read_lock_irq(&hrt_server->param_lock);
wcet = hrt_server->wcet / NSEC_PER_MSEC;
period = hrt_server->period / NSEC_PER_MSEC;
read_unlock_irq(&hrt_server->param_lock);
len += snprintf(page + len, PAGE_SIZE - len,
"%3d %8d %8d\n", cpu, wcet, period);
}
*eof = 1;
return len;
}
/* macro to see if we are in the buffer's range and not at the null byte */
#define buf_in_range(buf, pos, max) (buf <= pos && pos < (buf + max) && *pos)
#define find_newline(buf, pos, max) \
do { \
while (buf_in_range(buf, pos, max) && \
*pos != '\n') \
++pos; \
} while (0)
static int hrt_proc_write(struct file *file, const char __user *input,
unsigned long count, void *data)
{
#define HRT_PROC_BUF 512
char buffer[HRT_PROC_BUF];
unsigned long long cpu, wcet, period;
char *pos, *newline, *space_check;
int nums_converted, chars_seen, ret;
if (count >= HRT_PROC_BUF){
printk(KERN_WARNING "proc buffer possibly too small in %s.\n",
__func__);
return -ENOSPC;
}
memset(buffer, 0, HRT_PROC_BUF);
/* input is definitely < HRT_PROC_BUF (see above check) */
if (copy_from_user(buffer, input, count))
return -EFAULT;
buffer[HRT_PROC_BUF-1] = '\0'; /* just making sure :) */
pos = buffer;
while (buf_in_range(buffer, pos, HRT_PROC_BUF)) {
newline = pos;
find_newline(buffer, newline, HRT_PROC_BUF);
if (buf_in_range(buffer, newline, HRT_PROC_BUF)) {
/* if there was a newline character */
*newline = '\0';
}
nums_converted = sscanf(pos, "%llu %llu %llu%n", &cpu, &wcet,
&period, &chars_seen);
if (nums_converted != 3) {
printk(KERN_WARNING "Didn't see 3 integers for HRT "
"server config: %s\n", pos);
goto loop_end;
}
space_check = pos + chars_seen;
if (space_check != newline) {
/*
* if the newline was not right after the numbers
* converted, ensure extra characters are just space
*/
for (; *space_check; space_check++) {
if (!isspace(*space_check)) {
printk(KERN_WARNING "Extra characters "
"in line: %s\n", pos);
goto loop_end;
}
}
}
/* finally, set the params */
ret = set_hrt_params(cpu, wcet, period);
if (ret) goto loop_end; /* currently, this does nothing */
loop_end:
pos = newline + 1; /* consider next line */
}
return count;
}
/* actual plugin object */
/* static struct sched_plugin edf_hsb_plugin = { */
static struct sched_plugin edf_hsb_plugin __cacheline_aligned_in_smp = {
.plugin_name = "EDF-HSB",
/* need not implement all these methods (there are dummy methods) */
/* setup */
.activate_plugin = edf_hsb_activate_plugin,
.deactivate_plugin = edf_hsb_deactivate_plugin,
#if 0
/* scheduler invocation */
.tick =,
.schedule =,
.finish_switch =,
/* syscall backend */
.complete_job =,
.release_at =,
/* task state changes */
.admit_task =,
.task_new =,
.task_wake_up =,
.task_block =,
.task_exit =,
#endif
};
#define HRT_SERVER_PROC_NAME "hrt_server"
static void exit_proc(void)
{
if (hrt_server_proc) {
remove_proc_entry(HRT_SERVER_PROC_NAME, edf_hsb_proc_dir);
hrt_server_proc = NULL;
}
if (edf_hsb_proc_dir) {
remove_plugin_proc_dir(&edf_hsb_plugin);
edf_hsb_proc_dir = NULL;
}
}
static int __init init_edf_hsb(void)
{
int rv;
rv = register_sched_plugin(&edf_hsb_plugin);
if (rv) {
printk(KERN_ERR "Could not register plugin %s.\n",
edf_hsb_plugin.plugin_name);
goto out;
}
rv = make_plugin_proc_dir(&edf_hsb_plugin, &edf_hsb_proc_dir);
if (rv) {
printk(KERN_ERR "Could not create %s procfs dir.\n",
edf_hsb_plugin.plugin_name);
goto out;
}
hrt_server_proc = create_proc_entry(HRT_SERVER_PROC_NAME,
0644, edf_hsb_proc_dir);
if (!hrt_server_proc) {
printk(KERN_ERR "Could not create proc entry: %s.\n",
HRT_SERVER_PROC_NAME);
rv = -ENOENT;
goto out_clean_proc;
}
hrt_server_proc->read_proc = hrt_proc_read;
hrt_server_proc->write_proc = hrt_proc_write;
reset_hrt_servers();
goto out;
out_clean_proc:
exit_proc();
out:
return rv;
}
static void exit_edf_hsb(void)
{
exit_proc();
}
module_init(init_edf_hsb);
module_exit(exit_edf_hsb);
|