Subversion Repositories shark

Rev

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