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
563
|
#include <linux/sched.h>
#include <litmus/litmus.h>
#include <litmus/reservation.h>
#include <litmus/polling_reservations.h>
static void periodic_polling_client_arrives(
struct reservation* res,
struct reservation_client *client
)
{
struct polling_reservation *pres =
container_of(res, struct polling_reservation, res);
lt_t instances, tmp;
list_add_tail(&client->list, &res->clients);
switch (res->state) {
case RESERVATION_INACTIVE:
/* Figure out next replenishment time. */
if (res->env->time_zero == 0) {
tmp = res->env->current_time - res->env->time_zero;
instances = div64_u64(tmp, pres->period);
res->next_replenishment =
(instances + 1) * pres->period + pres->offset;
}
else {
tmp = res->env->current_time - res->env->time_zero;
instances = div64_u64(tmp, pres->period);
res->next_replenishment = res->env->time_zero + instances * pres->period;
}
TRACE("ENV_TIME_ZERO %llu\n", res->env->time_zero);
TRACE("pol-res: R%d activate tmp=%llu instances=%llu period=%llu nextrp=%llu cur=%llu\n",
res->id, tmp, instances, pres->period, res->next_replenishment,
res->env->current_time);
res->env->change_state(res->env, res,
RESERVATION_DEPLETED);
break;
case RESERVATION_ACTIVE:
case RESERVATION_DEPLETED:
/* do nothing */
break;
case RESERVATION_ACTIVE_IDLE:
res->blocked_by_ghost = 0;
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
}
}
static void periodic_polling_client_departs(
struct reservation *res,
struct reservation_client *client,
int did_signal_job_completion
)
{
list_del(&client->list);
switch (res->state) {
case RESERVATION_INACTIVE:
case RESERVATION_ACTIVE_IDLE:
BUG(); /* INACTIVE or IDLE <=> no client */
break;
case RESERVATION_ACTIVE:
if (list_empty(&res->clients)) {
res->env->change_state(res->env, res,
// RESERVATION_ACTIVE_IDLE);
res->cur_budget ?
RESERVATION_ACTIVE_IDLE :
RESERVATION_DEPLETED);
// did_signal_job_completion ?
// RESERVATION_DEPLETED :
// RESERVATION_ACTIVE_IDLE);
} /* else: nothing to do, more clients ready */
break;
case RESERVATION_DEPLETED:
/* do nothing */
break;
}
}
static void periodic_polling_on_replenishment(
struct reservation *res
)
{
struct polling_reservation *pres =
container_of(res, struct polling_reservation, res);
/* replenish budget */
res->cur_budget = pres->max_budget;
res->next_replenishment += pres->period;
res->budget_consumed = 0;
TRACE("polling_replenish(%u): next_replenishment=%llu\n", res->id, res->next_replenishment);
switch (res->state) {
case RESERVATION_DEPLETED:
case RESERVATION_INACTIVE:
case RESERVATION_ACTIVE_IDLE:
if (list_empty(&res->clients))
/* no clients => poll again later */
res->env->change_state(res->env, res,
RESERVATION_INACTIVE);
else
/* we have clients & budget => ACTIVE */
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
case RESERVATION_ACTIVE:
/* Replenished while active => tardy? In any case,
* go ahead and stay active. */
break;
}
}
static void periodic_polling_on_replenishment_edf(
struct reservation *res
)
{
struct polling_reservation *pres =
container_of(res, struct polling_reservation, res);
/* update current priority */
res->priority = res->next_replenishment + pres->deadline;
/* do common updates */
periodic_polling_on_replenishment(res);
}
static void common_drain_budget(
struct reservation *res,
lt_t how_much)
{
if (how_much >= res->cur_budget)
res->cur_budget = 0;
else
res->cur_budget -= how_much;
res->budget_consumed += how_much;
res->budget_consumed_total += how_much;
switch (res->state) {
case RESERVATION_DEPLETED:
case RESERVATION_INACTIVE:
//BUG();
TRACE("!!!!!!!!!!!!!!!STATE ERROR R%d STATE(%d)\n", res->id, res->state);
break;
case RESERVATION_ACTIVE_IDLE:
case RESERVATION_ACTIVE:
if (!res->cur_budget) {
res->env->change_state(res->env, res,
RESERVATION_DEPLETED);
} /* else: stay in current state */
break;
}
}
static struct reservation_ops periodic_polling_ops_fp = {
.dispatch_client = default_dispatch_client,
.client_arrives = periodic_polling_client_arrives,
.client_departs = periodic_polling_client_departs,
.replenish = periodic_polling_on_replenishment,
.drain_budget = common_drain_budget,
};
static struct reservation_ops periodic_polling_ops_edf = {
.dispatch_client = default_dispatch_client,
.client_arrives = periodic_polling_client_arrives,
.client_departs = periodic_polling_client_departs,
.replenish = periodic_polling_on_replenishment_edf,
.drain_budget = common_drain_budget,
};
static void sporadic_polling_client_arrives_fp(
struct reservation* res,
struct reservation_client *client
)
{
struct polling_reservation *pres =
container_of(res, struct polling_reservation, res);
list_add_tail(&client->list, &res->clients);
switch (res->state) {
case RESERVATION_INACTIVE:
/* Replenish now. */
res->cur_budget = pres->max_budget;
res->next_replenishment =
res->env->current_time + pres->period;
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
case RESERVATION_ACTIVE:
case RESERVATION_DEPLETED:
/* do nothing */
break;
case RESERVATION_ACTIVE_IDLE:
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
}
}
static void sporadic_polling_client_arrives_edf(
struct reservation* res,
struct reservation_client *client
)
{
struct polling_reservation *pres =
container_of(res, struct polling_reservation, res);
list_add_tail(&client->list, &res->clients);
switch (res->state) {
case RESERVATION_INACTIVE:
/* Replenish now. */
res->cur_budget = pres->max_budget;
res->next_replenishment =
res->env->current_time + pres->period;
res->priority =
res->env->current_time + pres->deadline;
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
case RESERVATION_ACTIVE:
case RESERVATION_DEPLETED:
/* do nothing */
break;
case RESERVATION_ACTIVE_IDLE:
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
}
}
static struct reservation_ops sporadic_polling_ops_fp = {
.dispatch_client = default_dispatch_client,
.client_arrives = sporadic_polling_client_arrives_fp,
.client_departs = periodic_polling_client_departs,
.replenish = periodic_polling_on_replenishment,
.drain_budget = common_drain_budget,
};
static struct reservation_ops sporadic_polling_ops_edf = {
.dispatch_client = default_dispatch_client,
.client_arrives = sporadic_polling_client_arrives_edf,
.client_departs = periodic_polling_client_departs,
.replenish = periodic_polling_on_replenishment_edf,
.drain_budget = common_drain_budget,
};
void polling_reservation_init(
struct polling_reservation *pres,
int use_edf_prio,
int use_periodic_polling,
lt_t budget, lt_t period, lt_t deadline, lt_t offset
)
{
if (!deadline)
deadline = period;
BUG_ON(budget > period);
BUG_ON(budget > deadline);
BUG_ON(offset >= period);
reservation_init(&pres->res);
pres->max_budget = budget;
pres->period = period;
pres->deadline = deadline;
pres->offset = offset;
TRACE_TASK(current, "polling_reservation_init: periodic %d, use_edf %d\n", use_periodic_polling, use_edf_prio);
if (use_periodic_polling) {
if (use_edf_prio)
pres->res.ops = &periodic_polling_ops_edf;
else
pres->res.ops = &periodic_polling_ops_fp;
} else {
if (use_edf_prio)
pres->res.ops = &sporadic_polling_ops_edf;
else
pres->res.ops = &sporadic_polling_ops_fp;
}
}
static lt_t td_cur_major_cycle_start(struct table_driven_reservation *tdres)
{
lt_t x, tmp;
tmp = tdres->res.env->current_time - tdres->res.env->time_zero;
x = div64_u64(tmp, tdres->major_cycle);
x *= tdres->major_cycle;
return x;
}
static lt_t td_next_major_cycle_start(struct table_driven_reservation *tdres)
{
lt_t x, tmp;
tmp = tdres->res.env->current_time - tdres->res.env->time_zero;
x = div64_u64(tmp, tdres->major_cycle) + 1;
x *= tdres->major_cycle;
return x;
}
static void td_client_arrives(
struct reservation* res,
struct reservation_client *client
)
{
struct table_driven_reservation *tdres =
container_of(res, struct table_driven_reservation, res);
list_add_tail(&client->list, &res->clients);
switch (res->state) {
case RESERVATION_INACTIVE:
/* Figure out first replenishment time. */
tdres->major_cycle_start = td_next_major_cycle_start(tdres);
res->next_replenishment = tdres->major_cycle_start;
res->next_replenishment += tdres->intervals[0].start;
tdres->next_interval = 0;
res->env->change_state(res->env, res,
RESERVATION_DEPLETED);
break;
case RESERVATION_ACTIVE:
case RESERVATION_DEPLETED:
/* do nothing */
break;
case RESERVATION_ACTIVE_IDLE:
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
}
}
static void td_client_departs(
struct reservation *res,
struct reservation_client *client,
int did_signal_job_completion
)
{
list_del(&client->list);
switch (res->state) {
case RESERVATION_INACTIVE:
case RESERVATION_ACTIVE_IDLE:
//BUG(); /* INACTIVE or IDLE <=> no client */
break;
case RESERVATION_ACTIVE:
if (list_empty(&res->clients)) {
res->env->change_state(res->env, res,
RESERVATION_ACTIVE_IDLE);
} /* else: nothing to do, more clients ready */
break;
case RESERVATION_DEPLETED:
/* do nothing */
break;
}
}
static lt_t td_time_remaining_until_end(struct table_driven_reservation *tdres)
{
lt_t now = tdres->res.env->current_time;
lt_t end = tdres->cur_interval.end;
//TRACE("td_remaining(%u): start=%llu now=%llu end=%llu state=%d\n", tdres->res.id, tdres->cur_interval.start, now, end, tdres->res.state);
if (now >= end)
return 0;
else
return end - now;
}
static void td_replenish(
struct reservation *res)
{
struct table_driven_reservation *tdres =
container_of(res, struct table_driven_reservation, res);
//TRACE("td_replenish(%u): expected_replenishment=%llu\n", res->id, res->next_replenishment);
/* figure out current interval */
tdres->cur_interval.start = tdres->major_cycle_start +
tdres->intervals[tdres->next_interval].start;
tdres->cur_interval.end = tdres->major_cycle_start +
tdres->intervals[tdres->next_interval].end;
/* TRACE("major_cycle_start=%llu => [%llu, %llu]\n",
tdres->major_cycle_start,
tdres->cur_interval.start,
tdres->cur_interval.end);
*/
/* reset budget */
res->cur_budget = td_time_remaining_until_end(tdres);
res->budget_consumed = 0;
//TRACE("td_replenish(%u): %s budget=%llu\n", res->id, res->cur_budget ? "" : "WARNING", res->cur_budget);
/* prepare next slot */
tdres->next_interval = (tdres->next_interval + 1) % tdres->num_intervals;
if (!tdres->next_interval)
/* wrap to next major cycle */
tdres->major_cycle_start += tdres->major_cycle;
/* determine next time this reservation becomes eligible to execute */
res->next_replenishment = tdres->major_cycle_start;
res->next_replenishment += tdres->intervals[tdres->next_interval].start;
//TRACE("td_replenish(%u): next_replenishment=%llu\n", res->id, res->next_replenishment);
switch (res->state) {
case RESERVATION_DEPLETED:
case RESERVATION_ACTIVE:
case RESERVATION_ACTIVE_IDLE:
if (list_empty(&res->clients))
res->env->change_state(res->env, res,
RESERVATION_ACTIVE_IDLE);
else
/* we have clients & budget => ACTIVE */
res->env->change_state(res->env, res,
RESERVATION_ACTIVE);
break;
case RESERVATION_INACTIVE:
BUG();
break;
}
}
static void td_drain_budget(
struct reservation *res,
lt_t how_much)
{
struct table_driven_reservation *tdres =
container_of(res, struct table_driven_reservation, res);
res->budget_consumed += how_much;
res->budget_consumed_total += how_much;
/* Table-driven scheduling: instead of tracking the budget, we compute
* how much time is left in this allocation interval. */
/* sanity check: we should never try to drain from future slots */
//TRACE("TD_DRAIN STATE(%d) [%llu,%llu] %llu ?\n", res->state, tdres->cur_interval.start, tdres->cur_interval.end, res->env->current_time);
//BUG_ON(tdres->cur_interval.start > res->env->current_time);
if (tdres->cur_interval.start > res->env->current_time)
TRACE("TD_DRAIN BUG!!!!!!!!!!\n");
switch (res->state) {
case RESERVATION_DEPLETED:
case RESERVATION_INACTIVE:
//BUG();
TRACE("TD_DRAIN!!!!!!!!! RES_STATE = %d\n", res->state);
break;
case RESERVATION_ACTIVE_IDLE:
case RESERVATION_ACTIVE:
res->cur_budget = td_time_remaining_until_end(tdres);
//TRACE("td_drain_budget(%u): drained to budget=%llu\n", res->id, res->cur_budget);
if (!res->cur_budget) {
res->env->change_state(res->env, res,
RESERVATION_DEPLETED);
} else {
/* sanity check budget calculation */
//BUG_ON(res->env->current_time >= tdres->cur_interval.end);
//BUG_ON(res->env->current_time < tdres->cur_interval.start);
if (res->env->current_time >= tdres->cur_interval.end)
printk(KERN_ALERT "TD_DRAIN_BUDGET WARNING1\n");
if (res->env->current_time < tdres->cur_interval.start)
printk(KERN_ALERT "TD_DRAIN_BUDGET WARNING2\n");
}
break;
}
}
static struct task_struct* td_dispatch_client(
struct reservation *res,
lt_t *for_at_most)
{
struct task_struct *t;
struct table_driven_reservation *tdres =
container_of(res, struct table_driven_reservation, res);
/* usual logic for selecting a client */
t = default_dispatch_client(res, for_at_most);
TRACE_TASK(t, "td_dispatch_client(%u): selected, budget=%llu\n",
res->id, res->cur_budget);
/* check how much budget we have left in this time slot */
res->cur_budget = td_time_remaining_until_end(tdres);
TRACE_TASK(t, "td_dispatch_client(%u): updated to budget=%llu next=%d\n",
res->id, res->cur_budget, tdres->next_interval);
if (unlikely(!res->cur_budget)) {
/* Unlikely case: if we ran out of budget, the user configured
* a broken scheduling table (overlapping table slots).
* Not much we can do about this, but we can't dispatch a job
* now without causing overload. So let's register this reservation
* as depleted and wait for the next allocation. */
TRACE("td_dispatch_client(%u): budget unexpectedly depleted "
"(check scheduling table for unintended overlap)\n",
res->id);
res->env->change_state(res->env, res,
RESERVATION_DEPLETED);
return NULL;
} else
return t;
}
static struct reservation_ops td_ops = {
.dispatch_client = td_dispatch_client,
.client_arrives = td_client_arrives,
.client_departs = td_client_departs,
.replenish = td_replenish,
.drain_budget = td_drain_budget,
};
void table_driven_reservation_init(
struct table_driven_reservation *tdres,
lt_t major_cycle,
struct lt_interval *intervals,
unsigned int num_intervals)
{
unsigned int i;
/* sanity checking */
BUG_ON(!num_intervals);
for (i = 0; i < num_intervals; i++)
BUG_ON(intervals[i].end <= intervals[i].start);
for (i = 0; i + 1 < num_intervals; i++)
BUG_ON(intervals[i + 1].start <= intervals[i].end);
BUG_ON(intervals[num_intervals - 1].end > major_cycle);
reservation_init(&tdres->res);
tdres->major_cycle = major_cycle;
tdres->intervals = intervals;
tdres->cur_interval.start = 0;
tdres->cur_interval.end = 0;
tdres->num_intervals = num_intervals;
tdres->res.ops = &td_ops;
}
|