Subversion Repositories shark

Rev

Rev 212 | Rev 240 | 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
 ------------
214 giacomo 23
 CVS :        $Id: edf.c,v 1.7 2003-07-24 12:24:51 giacomo Exp $
2 pj 24
 
25
 File:        $File$
214 giacomo 26
 Revision:    $Revision: 1.7 $
27
 Last update: $Date: 2003-07-24 12:24:51 $
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
212 giacomo 78
#define EDF_FLAG_SLEEP       4
2 pj 79
 
212 giacomo 80
#undef EDFDEBUG
81
 
2 pj 82
/*+ the level redefinition for the Earliest Deadline First level +*/
83
typedef struct {
84
  level_des l;     /*+ the standard level descriptor          +*/
85
 
86
  TIME period[MAX_PROC]; /*+ The task periods; the deadlines are
87
                       stored in the priority field           +*/
88
  int deadline_timer[MAX_PROC];
89
                   /*+ The task deadline timers               +*/
90
 
91
  int flag[MAX_PROC];
92
                   /*+ used to manage the JOB_TASK_MODEL and the
93
                       periodicity                            +*/
94
 
29 pj 95
  IQUEUE ready;     /*+ the ready queue                        +*/
2 pj 96
 
97
  int flags;       /*+ the init flags...                      +*/
98
 
99
  bandwidth_t U;   /*+ the used bandwidth                     +*/
100
 
101
} EDF_level_des;
102
 
103
 
104
static void EDF_timer_deadline(void *par)
105
{
106
  PID p = (PID) par;
107
  EDF_level_des *lev;
29 pj 108
  struct timespec *temp;
2 pj 109
 
38 pj 110
#ifdef EDFDEBUG
2 pj 111
  edf_printf("$");
38 pj 112
#endif
2 pj 113
 
114
  lev = (EDF_level_des *)level_table[proc_table[p].task_level];
115
 
116
  switch (proc_table[p].status) {
117
    case EDF_ZOMBIE:
118
      /* we finally put the task in the ready queue */
119
      proc_table[p].status = FREE;
29 pj 120
      iq_insertfirst(p,&freedesc);
2 pj 121
      /* and free the allocated bandwidth */
122
      lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet;
123
      break;
124
 
125
    case EDF_IDLE:
126
      /* tracer stuff */
127
      trc_logevent(TRC_INTACTIVATION,&p);
128
      /* similar to EDF_task_activate */
29 pj 129
      temp = iq_query_timespec(p,&lev->ready);
130
      ADDUSEC2TIMESPEC(lev->period[p], temp);
2 pj 131
      proc_table[p].status = EDF_READY;
29 pj 132
      iq_timespec_insert(p,&lev->ready);
133
      lev->deadline_timer[p] = kern_event_post(temp,
2 pj 134
                                               EDF_timer_deadline,
135
                                               (void *)p);
38 pj 136
#ifdef EDFDEBUG
29 pj 137
      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 138
#endif
2 pj 139
      event_need_reschedule();
140
      break;
141
 
142
    case EDF_WAIT:
143
      /* Without this, the task cannot be reactivated!!! */
144
      proc_table[p].status = SLEEP;
212 giacomo 145
 
146
      /* Reset the EDF_FLAG_SLEEP */
147
      lev->flag[p] &= ~EDF_FLAG_SLEEP;
148
 
2 pj 149
      break;
150
 
151
    default:
152
      /* else, a deadline miss occurred!!! */
38 pj 153
#ifdef EDFDEBUG
2 pj 154
      edf_printf("\nstatus %d\n", (int)proc_table[p].status);
155
      edf_printf("timer_deadline:AAARRRGGGHHH!!!");
38 pj 156
#endif
2 pj 157
      kern_raise(XDEADLINE_MISS,p);
158
  }
159
}
160
 
161
static void EDF_timer_guest_deadline(void *par)
162
{
163
  PID p = (PID) par;
164
 
38 pj 165
#ifdef EDFDEBUG
2 pj 166
  edf_printf("AAARRRGGGHHH!!!");
38 pj 167
#endif
2 pj 168
  kern_raise(XDEADLINE_MISS,p);
169
}
170
 
38 pj 171
/* The scheduler only gets the first task in the queue */
172
static PID EDF_public_scheduler(LEVEL l)
2 pj 173
{
174
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
175
 
38 pj 176
#ifdef EDFDEBUG
177
  edf_printf("(s%d)", iq_query_first(&lev->ready));
178
#endif
2 pj 179
 
29 pj 180
  return iq_query_first(&lev->ready);
2 pj 181
}
182
 
183
/* The on-line guarantee is enabled only if the appropriate flag is set... */
38 pj 184
static int EDF_public_guarantee(LEVEL l, bandwidth_t *freebandwidth)
2 pj 185
{
186
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
187
 
159 pj 188
  if (*freebandwidth >= lev->U) {
189
    *freebandwidth -= lev->U;
190
    return 1;
2 pj 191
  }
192
  else
159 pj 193
    return 0;
2 pj 194
}
195
 
38 pj 196
static int EDF_public_create(LEVEL l, PID p, TASK_MODEL *m)
2 pj 197
{
198
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
38 pj 199
  HARD_TASK_MODEL *h;
2 pj 200
 
38 pj 201
  if (m->pclass != HARD_PCLASS) return -1;
202
  if (m->level != 0 && m->level != l) return -1;
203
  h = (HARD_TASK_MODEL *)m;
204
  if (!h->wcet || !h->mit) return -1;
159 pj 205
 
206
  /* check the free bandwidth... */
207
  if (lev->flags & EDF_ENABLE_GUARANTEE) {
208
    bandwidth_t b;
209
    b = (MAX_BANDWIDTH / h->mit) * h->wcet;
210
 
211
    /* really update lev->U, checking an overflow... */
212
    if (MAX_BANDWIDTH - lev->U > b)
213
      lev->U += b;
214
    else
215
      return -1;
216
  }
217
 
38 pj 218
  /* now we know that m is a valid model */
2 pj 219
 
38 pj 220
#ifdef EDFDEBUG
221
  edf_printf("(cr%d)", p);
222
#endif
2 pj 223
 
224
  lev->period[p] = h->mit;
212 giacomo 225
 
226
  lev->flag[p] = 0;
2 pj 227
 
228
  if (h->periodicity == APERIODIC)
212 giacomo 229
    lev->flag[p] |= EDF_FLAG_SPORADIC;
230
 
2 pj 231
  lev->deadline_timer[p] = -1;
232
 
233
  /* Enable wcet check */
234
  if (lev->flags & EDF_ENABLE_WCET_CHECK) {
235
    proc_table[p].avail_time = h->wcet;
236
    proc_table[p].wcet       = h->wcet;
237
    proc_table[p].control |= CONTROL_CAP;
238
  }
239
 
240
  return 0; /* OK, also if the task cannot be guaranteed... */
241
}
242
 
38 pj 243
static void EDF_public_detach(LEVEL l, PID p)
2 pj 244
{
245
  /* the EDF level doesn't introduce any dinamic allocated new field.
159 pj 246
     we have only to decrement the allocated bandwidth */
2 pj 247
 
248
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
249
 
38 pj 250
#ifdef EDFDEBUG
251
  edf_printf("(det%d)", p);
252
#endif
253
 
159 pj 254
  if (lev->flags & EDF_ENABLE_GUARANTEE) {
2 pj 255
    lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet;
159 pj 256
  }
2 pj 257
}
258
 
38 pj 259
static void EDF_public_dispatch(LEVEL l, PID p, int nostop)
2 pj 260
{
261
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
262
 
38 pj 263
#ifdef EDFDEBUG
2 pj 264
  edf_printf("(disp p%d %d.%d)",(int)p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);
38 pj 265
#endif
2 pj 266
 
267
  /* the task state is set EXE by the scheduler()
268
     we extract the task from the ready queue
269
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 270
  iq_extract(p, &lev->ready);
2 pj 271
}
272
 
38 pj 273
static void EDF_public_epilogue(LEVEL l, PID p)
2 pj 274
{
275
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
276
 
38 pj 277
#ifdef EDFDEBUG
2 pj 278
  edf_printf("(epil p%d %d.%d)",p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);
38 pj 279
#endif
2 pj 280
 
281
  /* check if the wcet is finished... */
282
  if ((lev->flags & EDF_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) {
283
    /* if it is, raise a XWCET_VIOLATION exception */
284
    kern_raise(XWCET_VIOLATION,p);
285
    proc_table[p].status = EDF_WCET_VIOLATED;
286
  }
287
  else {
288
    /* the task has been preempted. it returns into the ready queue... */
29 pj 289
    iq_timespec_insert(p,&lev->ready);
2 pj 290
    proc_table[p].status = EDF_READY;
291
  }
292
}
293
 
38 pj 294
static void EDF_public_activate(LEVEL l, PID p)
2 pj 295
{
296
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
29 pj 297
  struct timespec *temp;
2 pj 298
 
38 pj 299
#ifdef EDFDEBUG
212 giacomo 300
  edf_printf("(act%d sleep%d)", p, lev->flag[p]&EDF_FLAG_SLEEP);
38 pj 301
#endif
302
 
212 giacomo 303
  if (lev->flag[p] & EDF_FLAG_SLEEP) {
304
    lev->flag[p] &= ~EDF_FLAG_SLEEP;
305
    if (!(lev->flag[p] & EDF_FLAG_SPORADIC))
306
      proc_table[p].status = EDF_IDLE;
307
    return;
308
  }
309
 
2 pj 310
  if (proc_table[p].status == EDF_WAIT) {
311
    kern_raise(XACTIVATION,p);
312
    return;
313
  }
212 giacomo 314
 
2 pj 315
  /* Test if we are trying to activate a non sleeping task    */
316
  /* Ignore this; the task is already active                  */
317
  if (proc_table[p].status != SLEEP &&
318
      proc_table[p].status != EDF_WCET_VIOLATED)
319
    return;
320
 
321
 
322
  /* see also EDF_timer_deadline */
29 pj 323
  temp = iq_query_timespec(p, &lev->ready);
38 pj 324
  kern_gettime(temp);
29 pj 325
  ADDUSEC2TIMESPEC(lev->period[p], temp);
2 pj 326
 
327
  /* Insert task in the correct position */
328
  proc_table[p].status = EDF_READY;
29 pj 329
  iq_timespec_insert(p,&lev->ready);
2 pj 330
 
331
  /* Set the deadline timer */
29 pj 332
  lev->deadline_timer[p] = kern_event_post(temp,
2 pj 333
                                           EDF_timer_deadline,
334
                                           (void *)p);
38 pj 335
#ifdef EDFDEBUG
29 pj 336
  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 337
#endif
2 pj 338
}
339
 
38 pj 340
static void EDF_public_unblock(LEVEL l, PID p)
2 pj 341
{
342
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
343
 
38 pj 344
  /* Similar to EDF_task_activate,
345
     but we don't check in what state the task is */
2 pj 346
 
347
  /* Insert task in the coEDFect position */
348
  proc_table[p].status = EDF_READY;
29 pj 349
  iq_timespec_insert(p,&lev->ready);
2 pj 350
}
351
 
38 pj 352
static void EDF_public_block(LEVEL l, PID p)
2 pj 353
{
354
  /* Extract the running task from the level
355
     . we have already extract it from the ready queue at the dispatch time.
356
     . the capacity event have to be removed by the generic kernel
357
     . the wcet don't need modification...
358
     . the state of the task is set by the calling function
359
     . the deadline must remain...
360
 
361
     So, we do nothing!!!
362
  */
363
}
364
 
38 pj 365
static int EDF_public_message(LEVEL l, PID p, void *m)
2 pj 366
{
367
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
368
 
212 giacomo 369
  //task_message evaluation
370
  switch((long)(m)) {
2 pj 371
 
212 giacomo 372
    //task_endcycle
373
    case (long)(NULL):
2 pj 374
 
212 giacomo 375
      #ifdef EDFDEBUG
376
        edf_printf("(ecyc p%d %d.%d)",p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);
377
      #endif
2 pj 378
 
212 giacomo 379
      /* the task has terminated his job before it consume the wcet. All OK! */
380
      if (!(lev->flag[p] & EDF_FLAG_SPORADIC) &&
381
          !(lev->flag[p] & EDF_FLAG_SLEEP))
382
        proc_table[p].status = EDF_IDLE;
383
      else
384
        proc_table[p].status = EDF_WAIT;
38 pj 385
 
212 giacomo 386
      /* we reset the capacity counters... */
387
      if (lev->flags & EDF_ENABLE_WCET_CHECK)
388
        proc_table[p].avail_time = proc_table[p].wcet;
38 pj 389
 
212 giacomo 390
      jet_update_endcycle(); /* Update the Jet data... */
214 giacomo 391
      trc_logevent(TRC_ENDCYCLE,&p); /* tracer stuff */
212 giacomo 392
 
393
      break;
394
 
395
    //task_disable
396
    case 1:
397
 
398
      #ifdef EDFDEBUG
399
        edf_printf("(disable%d)",p);
400
      #endif
401
 
402
      /* Set the EDF_FLAG_SLEEP, in the next endcycle the task will
403
         be set in EDF_WAIT */
404
      lev->flag[p] |= EDF_FLAG_SLEEP;
405
 
406
      /* If the task is EDF_IDLE, set to EDF_WAIT now */
407
      if (proc_table[p].status == EDF_IDLE)
408
        proc_table[p].status = EDF_WAIT;
409
 
214 giacomo 410
      trc_logevent(TRC_DISABLE,&p);
411
 
212 giacomo 412
      break;
413
 
414
  }
415
 
38 pj 416
  return 0;
212 giacomo 417
 
2 pj 418
}
419
 
38 pj 420
static void EDF_public_end(LEVEL l, PID p)
2 pj 421
{
422
  proc_table[p].status = EDF_ZOMBIE;
423
 
424
  /* When the deadline timer fire, it put the task descriptor in
425
     the free queue, and free the allocated bandwidth... */
426
}
427
 
38 pj 428
static void EDF_private_insert(LEVEL l, PID p, TASK_MODEL *m)
2 pj 429
{
430
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
38 pj 431
  JOB_TASK_MODEL *job;
2 pj 432
 
38 pj 433
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
434
    kern_raise(XINVALID_TASK, p);
435
    return;
436
  }
2 pj 437
 
38 pj 438
  job = (JOB_TASK_MODEL *)m;
2 pj 439
 
38 pj 440
  /* Insert task in the correct position */
29 pj 441
  *iq_query_timespec(p, &lev->ready) = job->deadline;
38 pj 442
  iq_timespec_insert(p,&lev->ready);
443
  proc_table[p].status = EDF_READY;
2 pj 444
 
445
  lev->deadline_timer[p] = -1;
446
 
38 pj 447
  lev->period[p] = job->period;
448
 
449
  /* Set the deadline timer */
450
  if (!(job->noraiseexc))
2 pj 451
    lev->flag[p] = EDF_FLAG_NORAISEEXC;
38 pj 452
  else {
2 pj 453
    lev->flag[p] = 0;
38 pj 454
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
455
                                             EDF_timer_guest_deadline,
456
                                             (void *)p);
457
  }
2 pj 458
}
459
 
38 pj 460
static void EDF_private_dispatch(LEVEL l, PID p, int nostop)
2 pj 461
{
462
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
463
 
464
  /* the task state is set to EXE by the scheduler()
465
     we extract the task from the ready queue
466
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 467
  iq_extract(p, &lev->ready);
2 pj 468
}
469
 
38 pj 470
static void EDF_private_epilogue(LEVEL l, PID p)
2 pj 471
{
472
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
473
 
474
  /* the task has been preempted. it returns into the ready queue... */
29 pj 475
  iq_timespec_insert(p,&lev->ready);
2 pj 476
  proc_table[p].status = EDF_READY;
477
}
478
 
38 pj 479
static void EDF_private_extract(LEVEL l, PID p)
2 pj 480
{
481
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
482
 
38 pj 483
#ifdef EDFDEBUG
484
  edf_printf("EDF_guest_end: dline timer %d\n",lev->deadline_timer[p]);
485
#endif
2 pj 486
  if (proc_table[p].status == EDF_READY)
29 pj 487
    iq_extract(p, &lev->ready);
2 pj 488
 
489
  /* we remove the deadline timer, because the slice is finished */
490
  if (lev->deadline_timer[p] != NIL) {
38 pj 491
    kern_event_delete(lev->deadline_timer[p]);
2 pj 492
    lev->deadline_timer[p] = NIL;
493
  }
494
 
495
}
496
 
497
 
498
/* Registration functions */
499
 
500
/*+ Registration function:
501
    int flags                 the init flags ... see edf.h +*/
38 pj 502
LEVEL EDF_register_level(int flags)
2 pj 503
{
504
  LEVEL l;            /* the level that we register */
505
  EDF_level_des *lev;  /* for readableness only */
506
  PID i;              /* a counter */
507
 
508
  printk("EDF_register_level\n");
509
 
510
  /* request an entry in the level_table */
38 pj 511
  l = level_alloc_descriptor(sizeof(EDF_level_des));
2 pj 512
 
38 pj 513
  lev = (EDF_level_des *)level_table[l];
2 pj 514
 
515
  printk("    lev=%d\n",(int)lev);
516
 
517
  /* fill the standard descriptor */
38 pj 518
  lev->l.private_insert   = EDF_private_insert;
519
  lev->l.private_extract  = EDF_private_extract;
520
  lev->l.private_dispatch = EDF_private_dispatch;
521
  lev->l.private_epilogue = EDF_private_epilogue;
2 pj 522
 
38 pj 523
  lev->l.public_scheduler = EDF_public_scheduler;
2 pj 524
  if (flags & EDF_ENABLE_GUARANTEE)
38 pj 525
    lev->l.public_guarantee = EDF_public_guarantee;
2 pj 526
  else
38 pj 527
    lev->l.public_guarantee = NULL;
2 pj 528
 
38 pj 529
  lev->l.public_create    = EDF_public_create;
530
  lev->l.public_detach    = EDF_public_detach;
531
  lev->l.public_end       = EDF_public_end;
532
  lev->l.public_dispatch  = EDF_public_dispatch;
533
  lev->l.public_epilogue  = EDF_public_epilogue;
534
  lev->l.public_activate  = EDF_public_activate;
535
  lev->l.public_unblock   = EDF_public_unblock;
536
  lev->l.public_block     = EDF_public_block;
537
  lev->l.public_message   = EDF_public_message;
2 pj 538
 
539
  /* fill the EDF descriptor part */
540
  for(i=0; i<MAX_PROC; i++) {
541
    lev->period[i]         = 0;
542
    lev->deadline_timer[i] = -1;
543
    lev->flag[i]          = 0;
544
  }
545
 
29 pj 546
  iq_init(&lev->ready, &freedesc, 0);
159 pj 547
  lev->flags = flags;
2 pj 548
  lev->U     = 0;
38 pj 549
 
550
  return l;
2 pj 551
}
552
 
553
bandwidth_t EDF_usedbandwidth(LEVEL l)
554
{
555
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
38 pj 556
 
557
  return lev->U;
2 pj 558
}
559