Subversion Repositories shark

Rev

Rev 759 | 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
 *
759 anton 8
 * Authors:
2 pj 9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
759 anton 12
 *   Anton Cervin
2 pj 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
 ------------
837 giacomo 23
 CVS :        $Id: edf.c,v 1.17 2004-09-09 13:13:21 giacomo Exp $
2 pj 24
 
25
 File:        $File$
837 giacomo 26
 Revision:    $Revision: 1.17 $
27
 Last update: $Date: 2004-09-09 13:13:21 $
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>
353 giacomo 63
#include <tracer.h>
64
 
837 giacomo 65
#define EDF_DEBUG
38 pj 66
#define edf_printf kern_printf
657 anton 67
#ifdef EDF_DEBUG
68
char *pnow() {
69
  static char buf[40];
70
  struct timespec t;
71
  sys_gettime(&t);
72
  sprintf(buf, "%ld.%06ld", t.tv_sec, t.tv_nsec/1000);
73
  return buf;
74
}
75
char *ptime1(struct timespec *t) {
76
  static char buf[40];
77
  sprintf(buf, "%ld.%06ld", t->tv_sec, t->tv_nsec/1000);
78
  return buf;
79
}
80
char *ptime2(struct timespec *t) {
81
  static char buf[40];
82
  sprintf(buf, "%ld.%06ld", t->tv_sec, t->tv_nsec/1000);
83
  return buf;
84
}
85
#endif
2 pj 86
 
759 anton 87
/* Statuses used in the level */
657 anton 88
#define EDF_READY      MODULE_STATUS_BASE    /* ready */
89
#define EDF_IDLE       MODULE_STATUS_BASE+1  /* idle, waiting for offset/eop */
90
#define EDF_WAIT       MODULE_STATUS_BASE+2  /* to sleep, waiting for eop */
759 anton 91
#define EDF_ZOMBIE     MODULE_STATUS_BASE+3  /* to free, waiting for eop */
2 pj 92
 
759 anton 93
/* Task flags */
657 anton 94
#define EDF_FLAG_SPORADIC    1   /* the task is sporadic */
95
#define EDF_FLAG_SPOR_LATE   2   /* sporadic task with period overrun */ 
2 pj 96
 
97
 
759 anton 98
/* Task descriptor */
657 anton 99
typedef struct {
759 anton 100
  int flags;                    /* task flags                         */
101
  TIME period;                  /* period (or inter-arrival interval) */
102
  TIME rdeadline;               /* relative deadline                  */
103
  TIME offset;                  /* release offset                     */
104
  struct timespec release;      /* release time of current instance   */
105
  struct timespec adeadline;    /* latest assigned deadline           */
106
  int dl_timer;                 /* deadline timer                     */
107
  int eop_timer;                /* end of period timer                */
108
  int dl_miss;                  /* deadline miss counter              */
109
  int wcet_miss;                /* WCET miss counter                  */
110
  int act_miss;                 /* activation miss counter            */
111
  int nact;                     /* number of pending periodic jobs    */
112
} EDF_task_des;
2 pj 113
 
759 anton 114
 
115
/* Level descriptor */
116
typedef struct {
117
  level_des l;                  /* standard level descriptor          */
118
  int flags;                    /* level flags                        */
119
  IQUEUE ready;                 /* the ready queue                    */
120
  bandwidth_t U;                /* used bandwidth                     */
121
  EDF_task_des tvec[MAX_PROC];  /* vector of task descriptors         */
657 anton 122
} EDF_level_des;
2 pj 123
 
124
 
759 anton 125
/* Module function cross-references */
126
static void EDF_intern_release(PID p, EDF_level_des *lev);
2 pj 127
 
128
 
759 anton 129
/**** Timer event handler functions ****/
2 pj 130
 
759 anton 131
/* This timer event handler is called at the end of the period */
132
static void EDF_timer_endperiod(void *par)
2 pj 133
{
134
  PID p = (PID) par;
759 anton 135
  EDF_level_des *lev = (EDF_level_des *)level_table[proc_table[p].task_level];
136
  EDF_task_des *td = &lev->tvec[p];
657 anton 137
 
759 anton 138
  td->eop_timer = -1;
2 pj 139
 
759 anton 140
  if (proc_table[p].status == EDF_ZOMBIE) {
141
    /* put the task in the FREE state */
142
    proc_table[p].status = FREE;
143
    iq_insertfirst(p,&freedesc);
144
    /* free the allocated bandwidth */
145
    lev->U -= (MAX_BANDWIDTH/td->rdeadline) * proc_table[p].wcet;
146
    return;
147
  }
148
 
149
  if (proc_table[p].status == EDF_WAIT) {
150
    proc_table[p].status = SLEEP;
151
    return;
152
  }
153
 
154
  if (td->flags & EDF_FLAG_SPORADIC) {
155
    /* the task is sporadic and still busy, mark it as late */
156
    td->flags |= EDF_FLAG_SPOR_LATE;
157
  } else {
158
    /* the task is periodic, release/queue another instance */
159
    EDF_intern_release(p, lev);
160
  }
161
}
162
 
163
/* This timer event handler is called when a task misses its deadline */
164
static void EDF_timer_deadline(void *par)
165
{
166
  PID p = (PID) par;
167
  EDF_level_des *lev = (EDF_level_des *)level_table[proc_table[p].task_level];
168
  EDF_task_des *td = &lev->tvec[p];
169
 
837 giacomo 170
  td->dl_timer = -1;
171
 
759 anton 172
  TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss,
173
                  (unsigned short int)proc_table[p].context,0);
174
 
657 anton 175
  if (lev->flags & EDF_ENABLE_DL_EXCEPTION) {
176
    kern_raise(XDEADLINE_MISS,p);
177
  } else {
759 anton 178
    td->dl_miss++;
657 anton 179
  }
180
}
181
 
759 anton 182
/* This timer event handler is called after waiting for an offset */
183
static void EDF_timer_offset(void *par)
184
{
185
  PID p = (PID) par;
186
  EDF_level_des *lev;
187
  lev = (EDF_level_des *)level_table[proc_table[p].task_level];
188
  /* release the task now */
189
  EDF_intern_release(p, lev);
190
}
657 anton 191
 
759 anton 192
/* This function is called when a guest task misses its deadline */
193
static void EDF_timer_guest_deadline(void *par)
194
{
195
  PID p = (PID) par;
837 giacomo 196
  EDF_level_des *lev = (EDF_level_des *)level_table[proc_table[p].task_level];
197
  EDF_task_des *td = &lev->tvec[p];
198
 
199
  td->dl_timer = -1;
200
 
759 anton 201
  TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss,
202
                  (unsigned short int)proc_table[p].context,0);
203
  kern_raise(XDEADLINE_MISS,p);
204
}
657 anton 205
 
759 anton 206
 
207
/**** Internal utility functions ****/
208
 
209
/* Release (or queue) a task, post deadline and endperiod timers */
657 anton 210
static void EDF_intern_release(PID p, EDF_level_des *lev)
211
{
212
  struct timespec temp;
759 anton 213
  EDF_task_des *td = &lev->tvec[p];
657 anton 214
 
215
  /* post deadline timer */
216
  if (lev->flags & EDF_ENABLE_DL_CHECK) {
759 anton 217
    temp = td->release;
218
    ADDUSEC2TIMESPEC(td->rdeadline, &temp);
837 giacomo 219
    if (td->dl_timer != -1) {
220
        kern_event_delete(td->dl_timer);
221
        td->dl_timer = -1;
222
    }
759 anton 223
    td->dl_timer = kern_event_post(&temp,EDF_timer_deadline,(void *)p);
657 anton 224
  }
225
 
226
  /* release or queue next job */
227
  if (proc_table[p].status == EDF_IDLE) {
228
    /* assign deadline, insert task in the ready queue */
229
    proc_table[p].status = EDF_READY;
759 anton 230
    *iq_query_timespec(p,&lev->ready) = td->adeadline;
657 anton 231
    iq_timespec_insert(p,&lev->ready);
232
#ifdef EDF_DEBUG
233
    edf_printf("At %s: releasing %s with deadline %s\n", pnow(),
759 anton 234
       proc_table[p].name, ptime1(&td->adeadline));
657 anton 235
#endif
236
    /* increase assigned deadline */
759 anton 237
    ADDUSEC2TIMESPEC(td->period, &td->adeadline);
657 anton 238
    /* reschedule */
239
    event_need_reschedule();
240
  } else {
241
    /* queue */
759 anton 242
    td->nact++;
657 anton 243
  }
244
 
245
  /* increase release time */
759 anton 246
  ADDUSEC2TIMESPEC(td->period, &td->release);
657 anton 247
  /* post end of period timer */
759 anton 248
  td->eop_timer = kern_event_post(&td->release, EDF_timer_endperiod,(void *)p);
657 anton 249
 
759 anton 250
  TRACER_LOGEVENT(FTrace_EVT_task_timer,
251
                  (unsigned short int)proc_table[p].context,
252
                  (unsigned int)proc_table[p].task_level);
657 anton 253
}
254
 
255
 
759 anton 256
/**** Public generic kernel interface functions ****/
657 anton 257
 
759 anton 258
/* Returns the first task in the ready queue */
38 pj 259
static PID EDF_public_scheduler(LEVEL l)
2 pj 260
{
261
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
29 pj 262
  return iq_query_first(&lev->ready);
2 pj 263
}
264
 
759 anton 265
/* Checks and decreases the available system bandwidth */
38 pj 266
static int EDF_public_guarantee(LEVEL l, bandwidth_t *freebandwidth)
2 pj 267
{
268
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
269
 
159 pj 270
  if (*freebandwidth >= lev->U) {
271
    *freebandwidth -= lev->U;
272
    return 1;
2 pj 273
  }
274
  else
159 pj 275
    return 0;
2 pj 276
}
277
 
759 anton 278
/* Called by task_create: Checks task model and creates a task */
38 pj 279
static int EDF_public_create(LEVEL l, PID p, TASK_MODEL *m)
2 pj 280
{
281
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 282
  EDF_task_des *td = &lev->tvec[p];
38 pj 283
  HARD_TASK_MODEL *h;
2 pj 284
 
38 pj 285
  if (m->pclass != HARD_PCLASS) return -1;
286
  if (m->level != 0 && m->level != l) return -1;
287
  h = (HARD_TASK_MODEL *)m;
288
  if (!h->wcet || !h->mit) return -1;
657 anton 289
  if (h->drel > h->mit) return -1;  /* only D <= T supported */
159 pj 290
 
657 anton 291
  if (!h->drel) {
759 anton 292
    td->rdeadline = h->mit;
657 anton 293
  } else {
759 anton 294
    td->rdeadline = h->drel;
657 anton 295
  }
296
 
159 pj 297
  /* check the free bandwidth... */
298
  if (lev->flags & EDF_ENABLE_GUARANTEE) {
299
    bandwidth_t b;
759 anton 300
    b = (MAX_BANDWIDTH / td->rdeadline) * h->wcet;
159 pj 301
 
302
    /* really update lev->U, checking an overflow... */
657 anton 303
    if (MAX_BANDWIDTH - lev->U > b) {
159 pj 304
      lev->U += b;
657 anton 305
    } else {
159 pj 306
      return -1;
657 anton 307
    }
159 pj 308
  }
309
 
759 anton 310
  td->flags = 0;
311
  if (h->periodicity == APERIODIC) {
312
    td->flags |= EDF_FLAG_SPORADIC;
657 anton 313
  }
759 anton 314
  td->period = h->mit;
315
  if (td->rdeadline == td->period) {
316
    /* Ensure that D <= T-eps to make dl_timer trigger before eop_timer */
317
    td->rdeadline = td->period - 1;
657 anton 318
  }
759 anton 319
  td->offset = h->offset;
320
  td->dl_timer = -1;
321
  td->eop_timer = -1;
322
  td->dl_miss = 0;
323
  td->wcet_miss = 0;
324
  td->act_miss = 0;
325
  td->nact = 0;
2 pj 326
 
327
  /* Enable wcet check */
328
  if (lev->flags & EDF_ENABLE_WCET_CHECK) {
329
    proc_table[p].avail_time = h->wcet;
330
    proc_table[p].wcet       = h->wcet;
657 anton 331
    proc_table[p].control |= CONTROL_CAP; /* turn on measurement */
2 pj 332
  }
333
 
334
  return 0; /* OK, also if the task cannot be guaranteed... */
335
}
336
 
759 anton 337
/* Reclaim the bandwidth used by the task */
38 pj 338
static void EDF_public_detach(LEVEL l, PID p)
2 pj 339
{
340
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 341
  EDF_task_des *td = &lev->tvec[p];
2 pj 342
 
159 pj 343
  if (lev->flags & EDF_ENABLE_GUARANTEE) {
759 anton 344
    lev->U -= (MAX_BANDWIDTH / td->rdeadline) * proc_table[p].wcet;
159 pj 345
  }
2 pj 346
}
347
 
759 anton 348
/* Extracts the running task from the ready queue */
38 pj 349
static void EDF_public_dispatch(LEVEL l, PID p, int nostop)
2 pj 350
{
351
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
29 pj 352
  iq_extract(p, &lev->ready);
2 pj 353
}
354
 
759 anton 355
/* Called when the task is preempted or when its budget is exhausted */
38 pj 356
static void EDF_public_epilogue(LEVEL l, PID p)
2 pj 357
{
358
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 359
  EDF_task_des *td = &lev->tvec[p];
2 pj 360
 
361
  /* check if the wcet is finished... */
657 anton 362
  if (lev->flags & EDF_ENABLE_WCET_CHECK) {
363
    if (proc_table[p].avail_time <= 0) {
759 anton 364
      TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation,
365
                      (unsigned short int)proc_table[p].context,0);
657 anton 366
      if (lev->flags & EDF_ENABLE_WCET_EXCEPTION) {
367
        kern_raise(XWCET_VIOLATION,p);
368
      } else {
369
        proc_table[p].control &= ~CONTROL_CAP;
759 anton 370
        td->wcet_miss++;
657 anton 371
      }
372
    }
2 pj 373
  }
657 anton 374
 
375
  /* the task returns to the ready queue */
376
  iq_timespec_insert(p,&lev->ready);
377
  proc_table[p].status = EDF_READY;
378
 
2 pj 379
}
380
 
759 anton 381
/* Called by task_activate or group_activate: Activates the task at time t */
657 anton 382
static void EDF_public_activate(LEVEL l, PID p, struct timespec *t)
2 pj 383
{
657 anton 384
  struct timespec clocktime;
2 pj 385
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 386
  EDF_task_des *td = &lev->tvec[p];
2 pj 387
 
657 anton 388
  kern_gettime(&clocktime);
38 pj 389
 
657 anton 390
  /* check if we are not in the SLEEP state */
391
  if (proc_table[p].status != SLEEP) {
392
    if (lev->flags & EDF_ENABLE_ACT_EXCEPTION) {
393
      /* too frequent or wrongful activation: raise exception */
394
      kern_raise(XACTIVATION,p);
395
    } else {
396
      /* skip the sporadic job, but increase a counter */
397
#ifdef EDF_DEBUG
759 anton 398
      edf_printf("At %s: activation of %s skipped\n", pnow(),
399
                 proc_table[p].name);
657 anton 400
#endif
759 anton 401
      td->act_miss++;
657 anton 402
    }
212 giacomo 403
    return;
404
  }
405
 
657 anton 406
  /* set the release time to the activation time + offset */
759 anton 407
  td->release = *t;
408
  ADDUSEC2TIMESPEC(td->offset, &td->release);
2 pj 409
 
657 anton 410
  /* set the absolute deadline to the activation time + offset + rdeadline */
759 anton 411
  td->adeadline = td->release;
412
  ADDUSEC2TIMESPEC(td->rdeadline, &td->adeadline);
2 pj 413
 
759 anton 414
  /* Check if release > clocktime. If yes, release it later,
657 anton 415
     otherwise release it now. */
2 pj 416
 
657 anton 417
  proc_table[p].status = EDF_IDLE;
2 pj 418
 
759 anton 419
  if (TIMESPEC_A_GT_B(&td->release, &clocktime)) {
420
    /* release later, post an offset timer */
421
    kern_event_post(&td->release,EDF_timer_offset,(void *)p);
657 anton 422
  } else {
423
    /* release now */
424
    EDF_intern_release(p, lev);
425
  }
2 pj 426
}
427
 
759 anton 428
/* Reinserts a task that has been blocked into the ready queue */
38 pj 429
static void EDF_public_unblock(LEVEL l, PID p)
2 pj 430
{
431
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
432
 
657 anton 433
  /* Insert task in the correct position */
2 pj 434
  proc_table[p].status = EDF_READY;
29 pj 435
  iq_timespec_insert(p,&lev->ready);
2 pj 436
}
437
 
759 anton 438
/* Called when a task experiences a synchronization block */
38 pj 439
static void EDF_public_block(LEVEL l, PID p)
2 pj 440
{
441
  /* Extract the running task from the level
442
     . we have already extract it from the ready queue at the dispatch time.
443
     . the capacity event have to be removed by the generic kernel
444
     . the wcet don't need modification...
445
     . the state of the task is set by the calling function
446
     . the deadline must remain...
447
 
448
     So, we do nothing!!!
449
  */
450
}
451
 
759 anton 452
/* Called by task_endcycle or task_sleep: Ends the current instance */
38 pj 453
static int EDF_public_message(LEVEL l, PID p, void *m)
2 pj 454
{
455
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 456
  EDF_task_des *td = &lev->tvec[p];
2 pj 457
 
212 giacomo 458
  switch((long)(m)) {
657 anton 459
    /* task_endcycle() */
460
  case 0:
461
    /* if there are no pending jobs */
759 anton 462
    if (td->nact == 0) {
657 anton 463
      /* remove deadline timer, if any */
759 anton 464
      if (td->dl_timer != -1) {
465
        kern_event_delete(td->dl_timer);
466
        td->dl_timer = -1;
657 anton 467
      }
759 anton 468
      if (td->flags & EDF_FLAG_SPORADIC) {
657 anton 469
        /* sporadic task */
759 anton 470
        if (!(td->flags & EDF_FLAG_SPOR_LATE)) {
657 anton 471
          proc_table[p].status = EDF_WAIT;
472
        } else {
473
          /* it's late, move it directly to SLEEP */
474
          proc_table[p].status = SLEEP;
759 anton 475
          td->flags &= ~EDF_FLAG_SPOR_LATE;
657 anton 476
        }
477
      } else {
478
        /* periodic task */
479
        proc_table[p].status = EDF_IDLE;
480
      }
481
    } else {
482
      /* we are late / there are pending jobs */
759 anton 483
      td->nact--;
657 anton 484
      /* compute and assign absolute deadline */
759 anton 485
      *iq_query_timespec(p,&lev->ready) = td->adeadline;
657 anton 486
      iq_timespec_insert(p,&lev->ready);
487
      /* increase assigned deadline */
759 anton 488
      ADDUSEC2TIMESPEC(td->period, &td->adeadline);
657 anton 489
#ifdef EDF_DEBUG
490
      edf_printf("(Late) At %s: releasing %s with deadline %s\n",
759 anton 491
         pnow(),proc_table[p].name,ptime1(&td->adeadline));
657 anton 492
#endif
493
    }
494
    break;
495
 
496
    /* task_sleep() */
497
  case 1:
498
    /* remove deadline timer, if any */
759 anton 499
    if (td->dl_timer != -1) {
500
      kern_event_delete(td->dl_timer);
501
      td->dl_timer = -1;
657 anton 502
    }
759 anton 503
    if (td->flags & EDF_FLAG_SPORADIC) {
657 anton 504
      /* sporadic task */
759 anton 505
      if (!(td->flags & EDF_FLAG_SPOR_LATE)) {
212 giacomo 506
        proc_table[p].status = EDF_WAIT;
657 anton 507
      } else {
508
        /* it's late, move it directly to SLEEP */
509
        proc_table[p].status = SLEEP;
759 anton 510
        td->flags &= ~EDF_FLAG_SPOR_LATE;
657 anton 511
      }
512
    } else {
513
      /* periodic task */
759 anton 514
      if (!(td->nact > 0)) {
657 anton 515
        /* we are on time. go to the EDF_WAIT state */
516
        proc_table[p].status = EDF_WAIT;
517
      } else {
518
        /* we are late. delete pending activations and go to SLEEP */
759 anton 519
        td->nact = 0;
657 anton 520
        proc_table[p].status = SLEEP;
521
        /* remove end of period timer */
759 anton 522
        if (td->eop_timer != -1) {
523
          kern_event_delete(td->eop_timer);
524
          td->eop_timer = -1;
657 anton 525
        }
526
      }
527
    }
528
    break;
529
  }
212 giacomo 530
 
657 anton 531
  if (lev->flags & EDF_ENABLE_WCET_CHECK) {
532
    proc_table[p].control |= CONTROL_CAP;
212 giacomo 533
  }
691 anton 534
  jet_update_endcycle(); /* Update the Jet data... */
657 anton 535
  proc_table[p].avail_time = proc_table[p].wcet;
759 anton 536
  TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,
537
                  (unsigned short int)proc_table[p].context,(unsigned int)l);
657 anton 538
 
38 pj 539
  return 0;
212 giacomo 540
 
2 pj 541
}
542
 
759 anton 543
/* End the task and free the resources at the end of the period */
38 pj 544
static void EDF_public_end(LEVEL l, PID p)
2 pj 545
{
657 anton 546
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 547
  EDF_task_des *td = &lev->tvec[p];
2 pj 548
 
759 anton 549
  if (!(td->flags & EDF_FLAG_SPOR_LATE)) {
657 anton 550
    /* remove the deadline timer (if any) */
759 anton 551
    if (td->dl_timer != -1) {
552
      kern_event_delete(td->dl_timer);
553
      td->dl_timer = -1;
657 anton 554
    }
555
    proc_table[p].status = EDF_ZOMBIE;
556
  } else {
557
    /* no endperiod timer will be fired, free the task now! */
558
    proc_table[p].status = FREE;
559
    iq_insertfirst(p,&freedesc);
560
    /* free the allocated bandwidth */
759 anton 561
    lev->U -= (MAX_BANDWIDTH/td->rdeadline) * proc_table[p].wcet;
657 anton 562
  }
2 pj 563
}
564
 
759 anton 565
/**** Private generic kernel interface functions (guest calls) ****/
566
 
567
/* Insert a guest task */
38 pj 568
static void EDF_private_insert(LEVEL l, PID p, TASK_MODEL *m)
2 pj 569
{
570
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 571
  EDF_task_des *td = &lev->tvec[p];
38 pj 572
  JOB_TASK_MODEL *job;
2 pj 573
 
38 pj 574
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
575
    kern_raise(XINVALID_TASK, p);
576
    return;
577
  }
2 pj 578
 
38 pj 579
  job = (JOB_TASK_MODEL *)m;
2 pj 580
 
38 pj 581
  /* Insert task in the correct position */
29 pj 582
  *iq_query_timespec(p, &lev->ready) = job->deadline;
38 pj 583
  iq_timespec_insert(p,&lev->ready);
584
  proc_table[p].status = EDF_READY;
2 pj 585
 
837 giacomo 586
  if (td->dl_timer != -1) {
587
        kern_event_delete(td->dl_timer);
588
        td->dl_timer = -1;
589
  }
2 pj 590
 
759 anton 591
  td->period = job->period;
38 pj 592
 
657 anton 593
  if (!job->noraiseexc) {
759 anton 594
    td->dl_timer = kern_event_post(iq_query_timespec(p, &lev->ready),
657 anton 595
                                       EDF_timer_guest_deadline,(void *)p);
38 pj 596
  }
2 pj 597
}
598
 
759 anton 599
/* Dispatch a guest task */
38 pj 600
static void EDF_private_dispatch(LEVEL l, PID p, int nostop)
2 pj 601
{
602
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
603
 
604
  /* the task state is set to EXE by the scheduler()
605
     we extract the task from the ready queue
606
     NB: we can't assume that p is the first task in the queue!!! */
29 pj 607
  iq_extract(p, &lev->ready);
2 pj 608
}
609
 
759 anton 610
/* Called when a guest task is preempted/out of budget */
38 pj 611
static void EDF_private_epilogue(LEVEL l, PID p)
2 pj 612
{
613
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
614
 
615
  /* the task has been preempted. it returns into the ready queue... */
29 pj 616
  iq_timespec_insert(p,&lev->ready);
2 pj 617
  proc_table[p].status = EDF_READY;
618
}
619
 
759 anton 620
/* Extract a guest task */
38 pj 621
static void EDF_private_extract(LEVEL l, PID p)
2 pj 622
{
623
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 624
  EDF_task_des *td = &lev->tvec[p];
2 pj 625
 
626
  if (proc_table[p].status == EDF_READY)
29 pj 627
    iq_extract(p, &lev->ready);
2 pj 628
 
629
  /* we remove the deadline timer, because the slice is finished */
759 anton 630
  if (td->dl_timer != -1) {
631
    kern_event_delete(td->dl_timer);
632
    td->dl_timer = -1;
2 pj 633
  }
634
 
635
}
636
 
637
 
759 anton 638
/**** Level registration function ****/
639
 
38 pj 640
LEVEL EDF_register_level(int flags)
2 pj 641
{
642
  LEVEL l;            /* the level that we register */
643
  EDF_level_des *lev;  /* for readableness only */
644
 
645
  printk("EDF_register_level\n");
646
 
647
  /* request an entry in the level_table */
38 pj 648
  l = level_alloc_descriptor(sizeof(EDF_level_des));
2 pj 649
 
38 pj 650
  lev = (EDF_level_des *)level_table[l];
2 pj 651
 
652
  /* fill the standard descriptor */
38 pj 653
  lev->l.private_insert   = EDF_private_insert;
654
  lev->l.private_extract  = EDF_private_extract;
655
  lev->l.private_dispatch = EDF_private_dispatch;
656
  lev->l.private_epilogue = EDF_private_epilogue;
2 pj 657
 
38 pj 658
  lev->l.public_scheduler = EDF_public_scheduler;
2 pj 659
  if (flags & EDF_ENABLE_GUARANTEE)
38 pj 660
    lev->l.public_guarantee = EDF_public_guarantee;
2 pj 661
  else
38 pj 662
    lev->l.public_guarantee = NULL;
2 pj 663
 
38 pj 664
  lev->l.public_create    = EDF_public_create;
665
  lev->l.public_detach    = EDF_public_detach;
666
  lev->l.public_end       = EDF_public_end;
667
  lev->l.public_dispatch  = EDF_public_dispatch;
668
  lev->l.public_epilogue  = EDF_public_epilogue;
669
  lev->l.public_activate  = EDF_public_activate;
670
  lev->l.public_unblock   = EDF_public_unblock;
671
  lev->l.public_block     = EDF_public_block;
672
  lev->l.public_message   = EDF_public_message;
2 pj 673
 
759 anton 674
  iq_init(&lev->ready, &freedesc, 0);
2 pj 675
 
159 pj 676
  lev->flags = flags;
759 anton 677
  if (lev->flags & EDF_ENABLE_WCET_EXCEPTION) {
678
    lev->flags |= EDF_ENABLE_WCET_CHECK;
679
  }
680
  if (lev->flags & EDF_ENABLE_DL_EXCEPTION) {
681
    lev->flags |= EDF_ENABLE_DL_CHECK;
682
  }
38 pj 683
 
759 anton 684
  lev->U = 0;
685
 
38 pj 686
  return l;
2 pj 687
}
688
 
759 anton 689
 
690
/**** Public utility functions ****/
691
 
692
/* Get the bandwidth used by the level */
2 pj 693
bandwidth_t EDF_usedbandwidth(LEVEL l)
694
{
695
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
38 pj 696
 
697
  return lev->U;
2 pj 698
}
699
 
759 anton 700
/* Get the number of missed deadlines for a task */
701
int EDF_get_dl_miss(PID p)
657 anton 702
{
703
  LEVEL l = proc_table[p].task_level;
704
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 705
  EDF_task_des *td = &lev->tvec[p];
706
 
707
  return td->dl_miss;
657 anton 708
}
709
 
759 anton 710
/* Get the number of execution overruns for a task */
711
int EDF_get_wcet_miss(PID p)
657 anton 712
{
713
  LEVEL l = proc_table[p].task_level;
714
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 715
  EDF_task_des *td = &lev->tvec[p];
657 anton 716
 
759 anton 717
  return td->wcet_miss;
657 anton 718
}
719
 
759 anton 720
/* Get the number of skipped activations for a task */
721
int EDF_get_act_miss(PID p)
657 anton 722
{
723
  LEVEL l = proc_table[p].task_level;
724
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 725
  EDF_task_des *td = &lev->tvec[p];
726
 
727
  return td->act_miss;
657 anton 728
}
729
 
759 anton 730
/* Get the current number of queued activations for a task */
731
int EDF_get_nact(PID p)
657 anton 732
{
733
  LEVEL l = proc_table[p].task_level;
759 anton 734
 
657 anton 735
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
759 anton 736
  EDF_task_des *td = &lev->tvec[p];
657 anton 737
 
759 anton 738
  return td->nact;
657 anton 739
}
740