Subversion Repositories shark

Rev

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