Subversion Repositories shark

Rev

Rev 38 | Rev 212 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/**
22
 ------------
159 pj 23
 CVS :        $Id: edf.c,v 1.5 2003-05-05 07:31:43 pj Exp $
2 pj 24
 
25
 File:        $File$
159 pj 26
 Revision:    $Revision: 1.5 $
27
 Last update: $Date: 2003-05-05 07:31:43 $
2 pj 28
 ------------
29
 
30
 This file contains the scheduling module EDF (Earliest Deadline First)
31
 
32
 Read edf.h for further details.
33
 
34
**/
35
 
36
/*
38 pj 37
 * Copyright (C) 2000,2002 Paolo Gai
2 pj 38
 *
39
 * This program is free software; you can redistribute it and/or modify
40
 * it under the terms of the GNU General Public License as published by
41
 * the Free Software Foundation; either version 2 of the License, or
42
 * (at your option) any later version.
43
 *
44
 * This program is distributed in the hope that it will be useful,
45
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
 * GNU General Public License for more details.
48
 *
49
 * You should have received a copy of the GNU General Public License
50
 * along with this program; if not, write to the Free Software
51
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
52
 *
53
 */
54
 
55
 
56
#include <modules/edf.h>
57
#include <ll/stdio.h>
58
#include <ll/string.h>
59
#include <kernel/model.h>
60
#include <kernel/descr.h>
61
#include <kernel/var.h>
62
#include <kernel/func.h>
63
#include <kernel/trace.h>
64
 
38 pj 65
//#define EDFDEBUG
66
#define edf_printf kern_printf
2 pj 67
 
68
/*+ Status used in the level +*/
69
#define EDF_READY         MODULE_STATUS_BASE    /*+ - Ready status        +*/
70
#define EDF_WCET_VIOLATED MODULE_STATUS_BASE+2  /*+ when wcet is finished +*/
71
#define EDF_WAIT          MODULE_STATUS_BASE+3  /*+ to wait the deadline  +*/
72
#define EDF_IDLE          MODULE_STATUS_BASE+4  /*+ to wait the deadline  +*/
73
#define EDF_ZOMBIE        MODULE_STATUS_BASE+5  /*+ to wait the free time +*/
74
 
75
/*+ flags +*/
76
#define EDF_FLAG_SPORADIC    1
77
#define EDF_FLAG_NORAISEEXC  2
78
 
79
/*+ the level redefinition for the Earliest Deadline First level +*/
80
typedef struct {
81
  level_des l;     /*+ the standard level descriptor          +*/
82
 
83
  TIME period[MAX_PROC]; /*+ The task periods; the deadlines are
84
                       stored in the priority field           +*/
85
  int deadline_timer[MAX_PROC];
86
                   /*+ The task deadline timers               +*/
87
 
88
  int flag[MAX_PROC];
89
                   /*+ used to manage the JOB_TASK_MODEL and the
90
                       periodicity                            +*/
91
 
29 pj 92
  IQUEUE ready;     /*+ the ready queue                        +*/
2 pj 93
 
94
  int flags;       /*+ the init flags...                      +*/
95
 
96
  bandwidth_t U;   /*+ the used bandwidth                     +*/
97
 
98
} EDF_level_des;
99
 
100
 
101
static void EDF_timer_deadline(void *par)
102
{
103
  PID p = (PID) par;
104
  EDF_level_des *lev;
29 pj 105
  struct timespec *temp;
2 pj 106
 
38 pj 107
#ifdef EDFDEBUG
2 pj 108
  edf_printf("$");
38 pj 109
#endif
2 pj 110
 
111
  lev = (EDF_level_des *)level_table[proc_table[p].task_level];
112
 
113
  switch (proc_table[p].status) {
114
    case EDF_ZOMBIE:
115
      /* we finally put the task in the ready queue */
116
      proc_table[p].status = FREE;
29 pj 117
      iq_insertfirst(p,&freedesc);
2 pj 118
      /* and free the allocated bandwidth */
119
      lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet;
120
      break;
121
 
122
    case EDF_IDLE:
123
      /* tracer stuff */
124
      trc_logevent(TRC_INTACTIVATION,&p);
125
      /* similar to EDF_task_activate */
29 pj 126
      temp = iq_query_timespec(p,&lev->ready);
127
      ADDUSEC2TIMESPEC(lev->period[p], temp);
2 pj 128
      proc_table[p].status = EDF_READY;
29 pj 129
      iq_timespec_insert(p,&lev->ready);
130
      lev->deadline_timer[p] = kern_event_post(temp,
2 pj 131
                                               EDF_timer_deadline,
132
                                               (void *)p);
38 pj 133
#ifdef EDFDEBUG
29 pj 134
      edf_printf("(dline p%d ev%d %d.%d)",(int)p,(int)lev->deadline_timer[p],(int)temp->tv_sec,(int)temp->tv_nsec/1000);
38 pj 135
#endif
2 pj 136
      event_need_reschedule();
137
      break;
138
 
139
    case EDF_WAIT:
140
      /* Without this, the task cannot be reactivated!!! */
141
      proc_table[p].status = SLEEP;
142
      break;
143
 
144
    default:
145
      /* else, a deadline miss occurred!!! */
38 pj 146
#ifdef EDFDEBUG
2 pj 147
      edf_printf("\nstatus %d\n", (int)proc_table[p].status);
148
      edf_printf("timer_deadline:AAARRRGGGHHH!!!");
38 pj 149
#endif
2 pj 150
      kern_raise(XDEADLINE_MISS,p);
151
  }
152
}
153
 
154
static void EDF_timer_guest_deadline(void *par)
155
{
156
  PID p = (PID) par;
157
 
38 pj 158
#ifdef EDFDEBUG
2 pj 159
  edf_printf("AAARRRGGGHHH!!!");
38 pj 160
#endif
2 pj 161
  kern_raise(XDEADLINE_MISS,p);
162
}
163
 
38 pj 164
/* The scheduler only gets the first task in the queue */
165
static PID EDF_public_scheduler(LEVEL l)
2 pj 166
{
167
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
168
 
38 pj 169
#ifdef EDFDEBUG
170
  edf_printf("(s%d)", iq_query_first(&lev->ready));
171
#endif
2 pj 172
 
29 pj 173
  return iq_query_first(&lev->ready);
2 pj 174
}
175
 
176
/* The on-line guarantee is enabled only if the appropriate flag is set... */
38 pj 177
static int EDF_public_guarantee(LEVEL l, bandwidth_t *freebandwidth)
2 pj 178
{
179
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
180
 
159 pj 181
  if (*freebandwidth >= lev->U) {
182
    *freebandwidth -= lev->U;
183
    return 1;
2 pj 184
  }
185
  else
159 pj 186
    return 0;
2 pj 187
}
188
 
38 pj 189
static int EDF_public_create(LEVEL l, PID p, TASK_MODEL *m)
2 pj 190
{
191
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
38 pj 192
  HARD_TASK_MODEL *h;
2 pj 193
 
38 pj 194
  if (m->pclass != HARD_PCLASS) return -1;
195
  if (m->level != 0 && m->level != l) return -1;
196
  h = (HARD_TASK_MODEL *)m;
197
  if (!h->wcet || !h->mit) return -1;
159 pj 198
 
199
  /* check the free bandwidth... */
200
  if (lev->flags & EDF_ENABLE_GUARANTEE) {
201
    bandwidth_t b;
202
    b = (MAX_BANDWIDTH / h->mit) * h->wcet;
203
 
204
    /* really update lev->U, checking an overflow... */
205
    if (MAX_BANDWIDTH - lev->U > b)
206
      lev->U += b;
207
    else
208
      return -1;
209
  }
210
 
38 pj 211
  /* now we know that m is a valid model */
2 pj 212
 
38 pj 213
#ifdef EDFDEBUG
214
  edf_printf("(cr%d)", p);
215
#endif
2 pj 216
 
217
  lev->period[p] = h->mit;
218
 
219
  if (h->periodicity == APERIODIC)
220
    lev->flag[p] = EDF_FLAG_SPORADIC;
221
  else
222
    lev->flag[p] = 0;
223
  lev->deadline_timer[p] = -1;
224
 
225
  /* Enable wcet check */
226
  if (lev->flags & EDF_ENABLE_WCET_CHECK) {
227
    proc_table[p].avail_time = h->wcet;
228
    proc_table[p].wcet       = h->wcet;
229
    proc_table[p].control |= CONTROL_CAP;
230
  }
231
 
232
  return 0; /* OK, also if the task cannot be guaranteed... */
233
}
234
 
38 pj 235
static void EDF_public_detach(LEVEL l, PID p)
2 pj 236
{
237
  /* the EDF level doesn't introduce any dinamic allocated new field.
159 pj 238
     we have only to decrement the allocated bandwidth */
2 pj 239
 
240
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
241
 
38 pj 242
#ifdef EDFDEBUG
243
  edf_printf("(det%d)", p);
244
#endif
245
 
159 pj 246
  if (lev->flags & EDF_ENABLE_GUARANTEE) {
2 pj 247
    lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet;
159 pj 248
  }
2 pj 249
}
250
 
38 pj 251
static void EDF_public_dispatch(LEVEL l, PID p, int nostop)
2 pj 252
{
253
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
254
 
38 pj 255
#ifdef EDFDEBUG
2 pj 256
  edf_printf("(disp p%d %d.%d)",(int)p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);
38 pj 257
#endif
2 pj 258
 
259
  /* the task state is set EXE by the scheduler()
260
     we extract the task from the ready queue
261
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 262
  iq_extract(p, &lev->ready);
2 pj 263
}
264
 
38 pj 265
static void EDF_public_epilogue(LEVEL l, PID p)
2 pj 266
{
267
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
268
 
38 pj 269
#ifdef EDFDEBUG
2 pj 270
  edf_printf("(epil p%d %d.%d)",p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);
38 pj 271
#endif
2 pj 272
 
273
  /* check if the wcet is finished... */
274
  if ((lev->flags & EDF_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) {
275
    /* if it is, raise a XWCET_VIOLATION exception */
276
    kern_raise(XWCET_VIOLATION,p);
277
    proc_table[p].status = EDF_WCET_VIOLATED;
278
  }
279
  else {
280
    /* the task has been preempted. it returns into the ready queue... */
29 pj 281
    iq_timespec_insert(p,&lev->ready);
2 pj 282
    proc_table[p].status = EDF_READY;
283
  }
284
}
285
 
38 pj 286
static void EDF_public_activate(LEVEL l, PID p)
2 pj 287
{
288
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
29 pj 289
  struct timespec *temp;
2 pj 290
 
38 pj 291
#ifdef EDFDEBUG
292
  edf_printf("(act%d)", p);
293
#endif
294
 
2 pj 295
  if (proc_table[p].status == EDF_WAIT) {
296
    kern_raise(XACTIVATION,p);
297
    return;
298
  }
299
 
300
  /* Test if we are trying to activate a non sleeping task    */
301
  /* Ignore this; the task is already active                  */
302
  if (proc_table[p].status != SLEEP &&
303
      proc_table[p].status != EDF_WCET_VIOLATED)
304
    return;
305
 
306
 
307
  /* see also EDF_timer_deadline */
29 pj 308
  temp = iq_query_timespec(p, &lev->ready);
38 pj 309
  kern_gettime(temp);
29 pj 310
  ADDUSEC2TIMESPEC(lev->period[p], temp);
2 pj 311
 
312
  /* Insert task in the correct position */
313
  proc_table[p].status = EDF_READY;
29 pj 314
  iq_timespec_insert(p,&lev->ready);
2 pj 315
 
316
  /* Set the deadline timer */
29 pj 317
  lev->deadline_timer[p] = kern_event_post(temp,
2 pj 318
                                           EDF_timer_deadline,
319
                                           (void *)p);
38 pj 320
#ifdef EDFDEBUG
29 pj 321
  edf_printf("(dline p%d ev%d %d.%d)",p,(int)lev->deadline_timer[p],(int)temp->tv_sec,(int)temp->tv_nsec/1000);
38 pj 322
#endif
2 pj 323
}
324
 
38 pj 325
static void EDF_public_unblock(LEVEL l, PID p)
2 pj 326
{
327
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
328
 
38 pj 329
  /* Similar to EDF_task_activate,
330
     but we don't check in what state the task is */
2 pj 331
 
332
  /* Insert task in the coEDFect position */
333
  proc_table[p].status = EDF_READY;
29 pj 334
  iq_timespec_insert(p,&lev->ready);
2 pj 335
}
336
 
38 pj 337
static void EDF_public_block(LEVEL l, PID p)
2 pj 338
{
339
  /* Extract the running task from the level
340
     . we have already extract it from the ready queue at the dispatch time.
341
     . the capacity event have to be removed by the generic kernel
342
     . the wcet don't need modification...
343
     . the state of the task is set by the calling function
344
     . the deadline must remain...
345
 
346
     So, we do nothing!!!
347
  */
348
}
349
 
38 pj 350
static int EDF_public_message(LEVEL l, PID p, void *m)
2 pj 351
{
352
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
353
 
38 pj 354
#ifdef EDFDEBUG
2 pj 355
  edf_printf("(ecyc p%d %d.%d)",p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);
38 pj 356
#endif
2 pj 357
 
358
  /* the task has terminated his job before it consume the wcet. All OK! */
38 pj 359
  if (!lev->flag[p] & EDF_FLAG_SPORADIC)
360
    proc_table[p].status = EDF_IDLE;
361
  else
2 pj 362
    proc_table[p].status = EDF_WAIT;
363
 
364
  /* we reset the capacity counters... */
365
  if (lev->flags & EDF_ENABLE_WCET_CHECK)
366
    proc_table[p].avail_time = proc_table[p].wcet;
367
 
38 pj 368
  jet_update_endcycle(); /* Update the Jet data... */
369
  trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
370
 
2 pj 371
  /* when the deadline timer fire, it recognize the situation and set
38 pj 372
     correctly all the stuffs (like reactivation, sleep, etc... ) */
373
 
374
  return 0;
2 pj 375
}
376
 
38 pj 377
static void EDF_public_end(LEVEL l, PID p)
2 pj 378
{
379
  proc_table[p].status = EDF_ZOMBIE;
380
 
381
  /* When the deadline timer fire, it put the task descriptor in
382
     the free queue, and free the allocated bandwidth... */
383
}
384
 
38 pj 385
static void EDF_private_insert(LEVEL l, PID p, TASK_MODEL *m)
2 pj 386
{
387
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
38 pj 388
  JOB_TASK_MODEL *job;
2 pj 389
 
38 pj 390
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
391
    kern_raise(XINVALID_TASK, p);
392
    return;
393
  }
2 pj 394
 
38 pj 395
  job = (JOB_TASK_MODEL *)m;
2 pj 396
 
38 pj 397
  /* Insert task in the correct position */
29 pj 398
  *iq_query_timespec(p, &lev->ready) = job->deadline;
38 pj 399
  iq_timespec_insert(p,&lev->ready);
400
  proc_table[p].status = EDF_READY;
2 pj 401
 
402
  lev->deadline_timer[p] = -1;
403
 
38 pj 404
  lev->period[p] = job->period;
405
 
406
  /* Set the deadline timer */
407
  if (!(job->noraiseexc))
2 pj 408
    lev->flag[p] = EDF_FLAG_NORAISEEXC;
38 pj 409
  else {
2 pj 410
    lev->flag[p] = 0;
38 pj 411
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
412
                                             EDF_timer_guest_deadline,
413
                                             (void *)p);
414
  }
2 pj 415
}
416
 
38 pj 417
static void EDF_private_dispatch(LEVEL l, PID p, int nostop)
2 pj 418
{
419
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
420
 
421
  /* the task state is set to EXE by the scheduler()
422
     we extract the task from the ready queue
423
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 424
  iq_extract(p, &lev->ready);
2 pj 425
}
426
 
38 pj 427
static void EDF_private_epilogue(LEVEL l, PID p)
2 pj 428
{
429
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
430
 
431
  /* the task has been preempted. it returns into the ready queue... */
29 pj 432
  iq_timespec_insert(p,&lev->ready);
2 pj 433
  proc_table[p].status = EDF_READY;
434
}
435
 
38 pj 436
static void EDF_private_extract(LEVEL l, PID p)
2 pj 437
{
438
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
439
 
38 pj 440
#ifdef EDFDEBUG
441
  edf_printf("EDF_guest_end: dline timer %d\n",lev->deadline_timer[p]);
442
#endif
2 pj 443
  if (proc_table[p].status == EDF_READY)
29 pj 444
    iq_extract(p, &lev->ready);
2 pj 445
 
446
  /* we remove the deadline timer, because the slice is finished */
447
  if (lev->deadline_timer[p] != NIL) {
38 pj 448
    kern_event_delete(lev->deadline_timer[p]);
2 pj 449
    lev->deadline_timer[p] = NIL;
450
  }
451
 
452
}
453
 
454
 
455
/* Registration functions */
456
 
457
/*+ Registration function:
458
    int flags                 the init flags ... see edf.h +*/
38 pj 459
LEVEL EDF_register_level(int flags)
2 pj 460
{
461
  LEVEL l;            /* the level that we register */
462
  EDF_level_des *lev;  /* for readableness only */
463
  PID i;              /* a counter */
464
 
465
  printk("EDF_register_level\n");
466
 
467
  /* request an entry in the level_table */
38 pj 468
  l = level_alloc_descriptor(sizeof(EDF_level_des));
2 pj 469
 
38 pj 470
  lev = (EDF_level_des *)level_table[l];
2 pj 471
 
472
  printk("    lev=%d\n",(int)lev);
473
 
474
  /* fill the standard descriptor */
38 pj 475
  lev->l.private_insert   = EDF_private_insert;
476
  lev->l.private_extract  = EDF_private_extract;
477
  lev->l.private_dispatch = EDF_private_dispatch;
478
  lev->l.private_epilogue = EDF_private_epilogue;
2 pj 479
 
38 pj 480
  lev->l.public_scheduler = EDF_public_scheduler;
2 pj 481
  if (flags & EDF_ENABLE_GUARANTEE)
38 pj 482
    lev->l.public_guarantee = EDF_public_guarantee;
2 pj 483
  else
38 pj 484
    lev->l.public_guarantee = NULL;
2 pj 485
 
38 pj 486
  lev->l.public_create    = EDF_public_create;
487
  lev->l.public_detach    = EDF_public_detach;
488
  lev->l.public_end       = EDF_public_end;
489
  lev->l.public_dispatch  = EDF_public_dispatch;
490
  lev->l.public_epilogue  = EDF_public_epilogue;
491
  lev->l.public_activate  = EDF_public_activate;
492
  lev->l.public_unblock   = EDF_public_unblock;
493
  lev->l.public_block     = EDF_public_block;
494
  lev->l.public_message   = EDF_public_message;
2 pj 495
 
496
  /* fill the EDF descriptor part */
497
  for(i=0; i<MAX_PROC; i++) {
498
    lev->period[i]         = 0;
499
    lev->deadline_timer[i] = -1;
500
    lev->flag[i]          = 0;
501
  }
502
 
29 pj 503
  iq_init(&lev->ready, &freedesc, 0);
159 pj 504
  lev->flags = flags;
2 pj 505
  lev->U     = 0;
38 pj 506
 
507
  return l;
2 pj 508
}
509
 
510
bandwidth_t EDF_usedbandwidth(LEVEL l)
511
{
512
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
38 pj 513
 
514
  return lev->U;
2 pj 515
}
516