Subversion Repositories shark

Rev

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