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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
/*
* litmus/sched_edf_fm.c
*
* Implementation of the EDF-fm scheduling algorithm.
*/
#include <linux/percpu.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <litmus/litmus.h>
#include <litmus/jobs.h>
#include <litmus/sched_plugin.h>
#include <litmus/edf_common.h>
typedef struct {
rt_domain_t domain;
int cpu;
struct task_struct* scheduled; /* only RT tasks */
/* domain lock */
#define slock domain.ready_lock
} edffm_domain_t;
DEFINE_PER_CPU(edffm_domain_t, edffm_domains);
#define local_edffm (&__get_cpu_var(edffm_domains))
#define remote_edf(cpu) (&per_cpu(edffm_domains, cpu).domain)
#define remote_edffm(cpu) (&per_cpu(edffm_domains, cpu))
#define task_edf(task) remote_edf(get_partition(task))
#define task_edffm(task) remote_edffm(get_partition(task))
#define edffm_params(t) (t->rt_param.task_params.semi_part.fm)
/* Is the task a migratory task? */
#define is_migrat_task(task) (edffm_params(task).nr_cpus)
/* t is on the wrong CPU (it should be requeued properly) */
#define wrong_cpu(t) is_migrat_task((t)) && task_cpu((t)) != get_partition((t))
/* Get next CPU */
#define migrat_next_cpu(t) \
((tsk_rt(t)->task_params.cpu == edffm_params(t).cpus[0]) ? \
edffm_params(t).cpus[1] : \
edffm_params(t).cpus[0])
/* Get current cpu */
#define migrat_cur_cpu(t) \
((tsk_rt(t)->task_params.cpu == edffm_params(t).cpus[0]) ? \
edffm_params(t).cpus[0] : \
edffm_params(t).cpus[1])
/* Manipulate share for current cpu */
#define cur_cpu_fract_num(t) \
((tsk_rt(t)->task_params.cpu == edffm_params(t).cpus[0]) ? \
edffm_params(t).fraction[0][0] : \
edffm_params(t).fraction[0][1])
#define cur_cpu_fract_den(t) \
((tsk_rt(t)->task_params.cpu == edffm_params(t).cpus[0]) ? \
edffm_params(t).fraction[1][0] : \
edffm_params(t).fraction[1][1])
/* Get job number for current cpu */
#define cur_cpu_job_no(t) \
((tsk_rt(t)->task_params.cpu == edffm_params(t).cpus[0]) ? \
tsk_rt(t)->semi_part.cpu_job_no[0] : \
tsk_rt(t)->semi_part.cpu_job_no[1])
/* What is the current cpu position in the array? */
#define edffm_cpu_pos(cpu,t) \
((cpu == edffm_params(t).cpus[0]) ? \
0 : 1)
/*
* EDF-fm: migratory tasks have higher prio than fixed, EDF in both classes.
* (Both first and second may be NULL).
*/
int edffm_higher_prio(struct task_struct* first, struct task_struct* second)
{
if ((first && edffm_params(first).nr_cpus) ||
(second && edffm_params(second).nr_cpus)) {
if ((first && edffm_params(first).nr_cpus) &&
(second && edffm_params(second).nr_cpus))
/* both are migrating */
return edf_higher_prio(first, second);
if (first && edffm_params(first).nr_cpus)
/* first is migrating */
return 1;
else
/* second is migrating */
return 0;
}
/* both are fixed or not real time */
return edf_higher_prio(first, second);
}
/* need_to_preempt - check whether the task t needs to be preempted
* call only with irqs disabled and with ready_lock acquired
*/
int edffm_preemption_needed(rt_domain_t* rt, struct task_struct *t)
{
/* we need the read lock for edf_ready_queue */
/* no need to preempt if there is nothing pending */
if (!__jobs_pending(rt))
return 0;
/* we need to reschedule if t doesn't exist */
if (!t)
return 1;
/* make sure to get non-rt stuff out of the way */
return !is_realtime(t) || edffm_higher_prio(__next_ready(rt), t);
}
/* we assume the lock is being held */
static void preempt(edffm_domain_t *edffm)
{
preempt_if_preemptable(edffm->scheduled, edffm->cpu);
}
static void edffm_release_jobs(rt_domain_t* rt, struct bheap* tasks)
{
unsigned long flags;
edffm_domain_t *edffm = container_of(rt, edffm_domain_t, domain);
raw_spin_lock_irqsave(&edffm->slock, flags);
__merge_ready(rt, tasks);
if (edffm_preemption_needed(rt, edffm->scheduled))
preempt(edffm);
raw_spin_unlock_irqrestore(&edffm->slock, flags);
}
/* EDF-fm uses the "release_master" field to force the next release for
* the task 'task' to happen on a remote CPU. The remote cpu for task is
* previously set up during job_completion() taking into consideration
* whether a task is a migratory task or not.
*/
static inline void
edffm_add_release_remote(struct task_struct *task)
{
unsigned long flags;
rt_domain_t *rt = task_edf(task);
raw_spin_lock_irqsave(&rt->tobe_lock, flags);
/* "modify" destination cpu */
rt->release_master = get_partition(task);
TRACE_TASK(task, "Add remote release: smp_proc_id = %d, cpu = %d, remote = %d\n",
smp_processor_id(), task_cpu(task), rt->release_master);
/* trigger future release */
__add_release(rt, task);
/* reset proper release_master and unlock */
rt->release_master = NO_CPU;
raw_spin_unlock_irqrestore(&rt->tobe_lock, flags);
}
/* perform double ready_queue locking in an orderwise fashion
* this is called with: interrupt disabled and rq->lock held (from
* schedule())
*/
static noinline void double_domain_lock(edffm_domain_t *dom1, edffm_domain_t *dom2)
{
if (dom1 == dom2) {
/* fake */
raw_spin_lock(&dom1->slock);
} else {
if (dom1 < dom2) {
raw_spin_lock(&dom1->slock);
raw_spin_lock(&dom2->slock);
TRACE("acquired %d and %d\n", dom1->cpu, dom2->cpu);
} else {
raw_spin_lock(&dom2->slock);
raw_spin_lock(&dom1->slock);
TRACE("acquired %d and %d\n", dom2->cpu, dom1->cpu);
}
}
}
/* Directly insert a task in a remote ready queue. This function
* should only be called if this task is a migrating task and its
* last job for this CPU just completed (a new one is released for
* a remote CPU), but the new job is already tardy.
*/
static noinline void insert_task_in_remote_ready(struct task_struct *task)
{
edffm_domain_t *this = remote_edffm(task_cpu(task));
edffm_domain_t *remote = remote_edffm(get_partition(task));
BUG_ON(get_partition(task) != remote->cpu);
TRACE_TASK(task, "Migrate From P%d -> To P%d\n",
this->cpu, remote->cpu);
TRACE_TASK(task, "Inserting in remote ready queue\n");
WARN_ON(!irqs_disabled());
raw_spin_unlock(&this->slock);
mb();
TRACE_TASK(task,"edffm_lock %d released\n", this->cpu);
/* lock both ready queues */
double_domain_lock(this, remote);
mb();
__add_ready(&remote->domain, task);
/* release remote but keep ours */
raw_spin_unlock(&remote->slock);
TRACE_TASK(task,"edffm_lock %d released\n", remote->cpu);
/* ask remote cpu to reschedule, we are already rescheduling on this */
preempt(remote);
}
static void requeue(struct task_struct* t, rt_domain_t *edf)
{
if (t->state != TASK_RUNNING)
TRACE_TASK(t, "requeue: !TASK_RUNNING\n");
set_rt_flags(t, RT_F_RUNNING);
if (is_released(t, litmus_clock())) {
if (wrong_cpu(t)) {
/* this should only happen if t just completed, but
* its next release is already tardy, so it should be
* migrated and inserted in the remote ready queue
*/
TRACE_TASK(t, "Migrating task already released, "
"move from P%d to P%d\n",
task_cpu(t), get_partition(t));
insert_task_in_remote_ready(t);
} else {
/* not a migrat task or the job is on the right CPU */
__add_ready(edf, t);
}
} else {
if (wrong_cpu(t)) {
TRACE_TASK(t, "Migrating task, adding remote release\n");
edffm_add_release_remote(t);
} else {
TRACE_TASK(t, "Adding local release\n");
add_release(edf, t);
}
}
}
/* Update statistics for the _current_ job.
* - job_no was incremented _before_ starting this job
* (release_at / prepare_for_next_period)
* - cpu_job_no is incremented when the job completes
*/
static void update_job_counter(struct task_struct *t)
{
int cpu_pos;
/* Which CPU counter should be incremented? */
cpu_pos = edffm_cpu_pos(t->rt_param.task_params.cpu, t);
t->rt_param.semi_part.cpu_job_no[cpu_pos]++;
TRACE_TASK(t, "job_no = %d, cpu_job_no(pos %d) = %d, cpu %d\n",
t->rt_param.job_params.job_no, cpu_pos, cur_cpu_job_no(t),
t->rt_param.task_params.cpu);
}
/* What is the next cpu for this job? (eq. 8, in EDF-Fm paper) */
static int next_cpu_for_job(struct task_struct *t)
{
BUG_ON(!is_migrat_task(t));
if ((t->rt_param.job_params.job_no) ==
(((lt_t) cur_cpu_job_no(t) * cur_cpu_fract_den(t)) /
cur_cpu_fract_num(t)))
return edffm_params(t).cpus[0];
return edffm_params(t).cpus[1];
}
/* If needed (the share for task t on this CPU is exhausted), updates
* the task_params.cpu for the _migrating_ task t
*/
static void change_migrat_cpu_if_needed(struct task_struct *t)
{
BUG_ON(!is_migrat_task(t));
/* EDF-fm: if it is a migrating task and it has already executed
* the required number of jobs on this CPU, we need to move it
* on its next CPU; changing the cpu here will affect the requeue
* and the next release
*/
if (unlikely(next_cpu_for_job(t) != migrat_cur_cpu(t))) {
tsk_rt(t)->task_params.cpu = migrat_next_cpu(t);
TRACE_TASK(t, "EDF-fm: will migrate job %d -> %d\n",
task_cpu(t), tsk_rt(t)->task_params.cpu);
return;
}
TRACE_TASK(t, "EDF-fm: job will stay on %d -> %d\n",
task_cpu(t), tsk_rt(t)->task_params.cpu);
}
static void job_completion(struct task_struct* t, int forced)
{
sched_trace_task_completion(t,forced);
TRACE_TASK(t, "job_completion().\n");
if (unlikely(is_migrat_task(t))) {
update_job_counter(t);
change_migrat_cpu_if_needed(t);
}
set_rt_flags(t, RT_F_SLEEP);
prepare_for_next_period(t);
}
static void edffm_tick(struct task_struct *t)
{
edffm_domain_t *edffm = local_edffm;
BUG_ON(is_realtime(t) && t != edffm->scheduled);
if (is_realtime(t) && budget_enforced(t) && budget_exhausted(t)) {
set_tsk_need_resched(t);
TRACE("edffm_scheduler_tick: "
"%d is preemptable "
" => FORCE_RESCHED\n", t->pid);
}
}
static struct task_struct* edffm_schedule(struct task_struct * prev)
{
edffm_domain_t* edffm = local_edffm;
rt_domain_t* edf = &edffm->domain;
struct task_struct* next;
int out_of_time, sleep, preempt, exists, blocks, change_cpu, resched;
raw_spin_lock(&edffm->slock);
BUG_ON(edffm->scheduled && edffm->scheduled != prev);
BUG_ON(edffm->scheduled && !is_realtime(prev));
/* (0) Determine state */
exists = edffm->scheduled != NULL;
blocks = exists && !is_running(edffm->scheduled);
out_of_time = exists &&
budget_enforced(edffm->scheduled) &&
budget_exhausted(edffm->scheduled);
sleep = exists && get_rt_flags(edffm->scheduled) == RT_F_SLEEP;
change_cpu = exists && wrong_cpu(edffm->scheduled);
preempt = edffm_preemption_needed(edf, prev);
BUG_ON(blocks && change_cpu);
if (exists)
TRACE_TASK(prev,
"blocks:%d out_of_time:%d sleep:%d preempt:%d "
"wrong_cpu:%d state:%d sig:%d\n",
blocks, out_of_time, sleep, preempt,
change_cpu, prev->state, signal_pending(prev));
/* If we need to preempt do so. */
resched = preempt;
/* If a task blocks we have no choice but to reschedule. */
if (blocks)
resched = 1;
/* If a task has just woken up, it was tardy and the wake up
* raced with this schedule, a new job has already been released,
* but scheduled should be enqueued on a remote ready queue, and a
* new task should be selected for the current queue.
*/
if (change_cpu)
resched = 1;
/* Any task that is preemptable and either exhausts its execution
* budget or wants to sleep completes. We may have to reschedule after
* this.
*/
if ((out_of_time || sleep) && !blocks) {
job_completion(edffm->scheduled, !sleep);
resched = 1;
}
/* The final scheduling decision. Do we need to switch for some reason?
* Switch if we are in RT mode and have no task or if we need to
* resched.
*/
next = NULL;
if (resched || !exists) {
if (edffm->scheduled && !blocks)
requeue(edffm->scheduled, edf);
next = __take_ready(edf);
} else
/* Only override Linux scheduler if we have a real-time task
* scheduled that needs to continue.
*/
if (exists)
next = prev;
if (next) {
TRACE_TASK(next, "scheduled at %llu\n", litmus_clock());
set_rt_flags(next, RT_F_RUNNING);
} else {
TRACE("becoming idle at %llu\n", litmus_clock());
}
edffm->scheduled = next;
raw_spin_unlock(&edffm->slock);
return next;
}
/* Prepare a task for running in RT mode
*/
static void edffm_task_new(struct task_struct * t, int on_rq, int running)
{
rt_domain_t* edf = task_edf(t);
edffm_domain_t* edffm = task_edffm(t);
unsigned long flags;
TRACE_TASK(t, "EDF-fm: task new, cpu = %d\n",
t->rt_param.task_params.cpu);
release_at(t, litmus_clock());
update_job_counter(t);
/* The task should be running in the queue, otherwise signal
* code will try to wake it up with fatal consequences.
*/
raw_spin_lock_irqsave(&edffm->slock, flags);
if (running) {
/* there shouldn't be anything else running at the time */
BUG_ON(edffm->scheduled);
edffm->scheduled = t;
} else {
requeue(t, edf);
/* maybe we have to reschedule */
preempt(edffm);
}
raw_spin_unlock_irqrestore(&edffm->slock, flags);
}
static void edffm_task_wake_up(struct task_struct *task)
{
unsigned long flags;
edffm_domain_t* edffm = task_edffm(task);
rt_domain_t* edf = task_edf(task);
lt_t now;
TRACE_TASK(task, "wake_up at %llu\n", litmus_clock());
TRACE_TASK(task, "acquire edffm %d\n", edffm->cpu);
raw_spin_lock_irqsave(&edffm->slock, flags);
BUG_ON(edffm != task_edffm(task));
BUG_ON(is_queued(task));
now = litmus_clock();
if (is_tardy(task, now)) {
if (unlikely(is_migrat_task(task))) {
/* a new job will be released.
* Update current job counter */
update_job_counter(task);
/* Switch CPU if needed */
change_migrat_cpu_if_needed(task);
}
/* new sporadic release */
TRACE_TASK(task, "release new\n");
release_at(task, now);
sched_trace_task_release(task);
}
/* Only add to ready queue if it is not the currently-scheduled
* task. This could be the case if a task was woken up concurrently
* on a remote CPU before the executing CPU got around to actually
* de-scheduling the task, i.e., wake_up() raced with schedule()
* and won.
*/
if (edffm->scheduled != task)
requeue(task, edf);
raw_spin_unlock_irqrestore(&edffm->slock, flags);
TRACE_TASK(task, "release edffm %d\n", edffm->cpu);
TRACE_TASK(task, "wake up done\n");
}
static void edffm_task_block(struct task_struct *t)
{
TRACE_TASK(t, "block at %llu, state=%d\n", litmus_clock(), t->state);
BUG_ON(!is_realtime(t));
if (is_queued(t)) {
edffm_domain_t *edffm = local_edffm;
TRACE_TASK(t, "task blocked, race with wakeup, "
"remove from queue %d\n", edffm->cpu);
remove(&edffm->domain, t);
}
}
static void edffm_task_exit(struct task_struct * t)
{
unsigned long flags;
edffm_domain_t* edffm = task_edffm(t);
rt_domain_t* edf;
raw_spin_lock_irqsave(&edffm->slock, flags);
if (is_queued(t)) {
/* dequeue */
edf = task_edf(t);
remove(edf, t);
}
if (edffm->scheduled == t)
edffm->scheduled = NULL;
TRACE_TASK(t, "RIP\n");
preempt(edffm);
raw_spin_unlock_irqrestore(&edffm->slock, flags);
}
static long edffm_admit_task(struct task_struct* tsk)
{
return task_cpu(tsk) == tsk->rt_param.task_params.cpu ? 0 : -EINVAL;
}
/* Plugin object */
static struct sched_plugin edffm_plugin __cacheline_aligned_in_smp = {
.plugin_name = "EDF-fm",
.tick = edffm_tick,
.task_new = edffm_task_new,
.complete_job = complete_job,
.task_exit = edffm_task_exit,
.schedule = edffm_schedule,
.task_wake_up = edffm_task_wake_up,
.task_block = edffm_task_block,
.admit_task = edffm_admit_task
};
static int __init init_edffm(void)
{
int i;
edffm_domain_t *edffm;
/* Note, broken if num_online_cpus() may change */
for (i = 0; i < num_online_cpus(); i++) {
edffm = remote_edffm(i);
edffm->cpu = i;
edffm->scheduled = NULL;
edf_domain_init(&edffm->domain, NULL, edffm_release_jobs);
}
return register_sched_plugin(&edffm_plugin);
}
module_init(init_edffm);
|