Subversion Repositories shark

Rev

Rev 1118 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 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
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
1123 pj 21
 CVS :        $Id: edfact.c,v 1.4 2003-01-07 17:10:15 pj Exp $
1085 pj 22
 
23
 File:        $File$
1123 pj 24
 Revision:    $Revision: 1.4 $
25
 Last update: $Date: 2003-01-07 17:10:15 $
1085 pj 26
 ------------
27
**/
28
 
29
/*
30
 * Copyright (C) 2001 Paolo Gai
31
 *
32
 * This program is free software; you can redistribute it and/or modify
33
 * it under the terms of the GNU General Public License as published by
34
 * the Free Software Foundation; either version 2 of the License, or
35
 * (at your option) any later version.
36
 *
37
 * This program is distributed in the hope that it will be useful,
38
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
 * GNU General Public License for more details.
41
 *
42
 * You should have received a copy of the GNU General Public License
43
 * along with this program; if not, write to the Free Software
44
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
45
 *
46
 */
47
 
48
#include "edfact.h"
49
#include <ll/stdio.h>
50
#include <ll/string.h>
51
#include <kernel/model.h>
52
#include <kernel/descr.h>
53
#include <kernel/var.h>
54
#include <kernel/func.h>
55
#include <kernel/trace.h>
56
 
57
//#define edfact_printf kern_printf
58
#define edfact_printf printk
59
 
60
/*+ Status used in the level +*/
61
#define EDFACT_READY         MODULE_STATUS_BASE    /*+ - Ready status        +*/
62
#define EDFACT_IDLE          MODULE_STATUS_BASE+4  /*+ to wait the deadline  +*/
63
 
64
/*+ flags +*/
65
#define EDFACT_FLAG_NORAISEEXC  2
66
 
67
/*+ the level redefinition for the Earliest Deadline First level +*/
68
typedef struct {
69
  level_des l;     /*+ the standard level descriptor          +*/
70
 
71
  TIME period[MAX_PROC]; /*+ The task periods; the deadlines are
72
                       stored in the priority field           +*/
73
  int deadline_timer[MAX_PROC];
74
                   /*+ The task deadline timers               +*/
75
 
76
  struct timespec deadline_timespec[MAX_PROC];
77
 
78
  int dline_miss[MAX_PROC]; /*+ Deadline miss counter +*/
79
  int wcet_miss[MAX_PROC];  /*+ Wcet miss counter +*/
80
 
81
  int nact[MAX_PROC];       /*+ Wcet miss counter +*/
82
 
83
  int flag[MAX_PROC];
84
                   /*+ used to manage the JOB_TASK_MODEL and the
85
                       periodicity                            +*/
86
 
1118 pj 87
  IQUEUE ready;     /*+ the ready queue                        +*/
1085 pj 88
 
89
  int flags;       /*+ the init flags...                      +*/
90
 
91
  bandwidth_t U;   /*+ the used bandwidth                     +*/
92
 
93
} EDFACT_level_des;
94
 
95
 
96
static void EDFACT_timer_deadline(void *par);
97
 
1123 pj 98
static void EDFACT_internal_activate(EDFACT_level_des *lev, PID p,
99
                                     struct timespec *t)
1085 pj 100
{
1118 pj 101
  struct timespec *temp;
102
 
103
  temp = iq_query_timespec(p, &lev->ready);
104
 
1123 pj 105
  TIMESPEC_ASSIGN(temp,t);
1118 pj 106
  ADDUSEC2TIMESPEC(lev->period[p], temp);
1085 pj 107
 
108
  TIMESPEC_ASSIGN(&lev->deadline_timespec[p],
1118 pj 109
                  temp);
1085 pj 110
 
111
  /* Insert task in the correct position */
112
  proc_table[p].status = EDFACT_READY;
1118 pj 113
  iq_timespec_insert(p,&lev->ready);
1085 pj 114
 
115
  /* needed because when there is a wcet miss I disable CONTROL_CAP */
116
  proc_table[p].control |= CONTROL_CAP;
117
}
118
 
119
static void EDFACT_timer_deadline(void *par)
120
{
121
  PID p = (PID) par;
122
  EDFACT_level_des *lev;
123
 
124
  lev = (EDFACT_level_des *)level_table[proc_table[p].task_level];
125
 
126
  switch (proc_table[p].status) {
127
    case EDFACT_IDLE:
128
      edfact_printf("I%d",p);
129
 
1123 pj 130
      EDFACT_internal_activate(lev,p, &lev->deadline_timespec[p]);
1118 pj 131
 
1085 pj 132
      event_need_reschedule();
133
      break;
134
 
135
    default:
136
      edfact_printf("D%d",p);
137
      /* else, a deadline miss occurred!!! */
138
      lev->dline_miss[p]++;
139
 
140
      /* the task is into another state */
141
      lev->nact[p]++;
142
 
143
      /* Set the deadline timer */
144
      ADDUSEC2TIMESPEC(lev->period[p], &lev->deadline_timespec[p]);
145
  }
146
 
147
  /* Set the deadline timer */
148
  lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
149
                                           EDFACT_timer_deadline,
150
                                           (void *)p);
151
 
152
}
153
 
154
static void EDFACT_timer_guest_deadline(void *par)
155
{
156
  PID p = (PID) par;
157
 
158
  edfact_printf("AAARRRGGGHHH!!!");
159
  kern_raise(XDEADLINE_MISS,p);
160
}
161
 
162
 
163
/* The scheduler only gets the first task in the queue */
1123 pj 164
static PID EDFACT_public_scheduler(LEVEL l)
1085 pj 165
{
166
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
167
 
1118 pj 168
  return iq_query_first(&lev->ready);
1085 pj 169
}
170
 
171
/* The on-line guarantee is enabled only if the appropriate flag is set... */
1123 pj 172
static int EDFACT_public_guarantee(LEVEL l, bandwidth_t *freebandwidth)
1085 pj 173
{
174
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
175
 
176
  if (lev->flags & EDFACT_FAILED_GUARANTEE) {
177
    *freebandwidth = 0;
178
    return 0;
179
  }
180
  else
181
    if (*freebandwidth >= lev->U) {
182
      *freebandwidth -= lev->U;
183
      return 1;
184
    }
185
    else
186
      return 0;
187
 
188
}
189
 
1123 pj 190
static int EDFACT_public_create(LEVEL l, PID p, TASK_MODEL *m)
1085 pj 191
{
192
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
193
 
1123 pj 194
  HARD_TASK_MODEL *h;
1085 pj 195
 
1123 pj 196
  if (m->pclass != HARD_PCLASS) return -1;
197
  if (m->level != 0 && m->level != l) return -1;
198
  h = (HARD_TASK_MODEL *)m;
199
  if (!h->wcet || !h->mit || h->periodicity != PERIODIC) return -1;
200
  /* now we know that m is a valid model */
1085 pj 201
 
202
  lev->period[p] = h->mit;
203
 
204
  lev->flag[p] = 0;
205
  lev->deadline_timer[p] = -1;
206
  lev->dline_miss[p]     = 0;
207
  lev->wcet_miss[p]      = 0;
208
  lev->nact[p]           = 0;
209
 
210
  /* Enable wcet check */
211
  proc_table[p].avail_time = h->wcet;
212
  proc_table[p].wcet       = h->wcet;
213
  proc_table[p].control |= CONTROL_CAP;
214
 
215
  /* update the bandwidth... */
216
  if (lev->flags & EDFACT_ENABLE_GUARANTEE) {
217
    bandwidth_t b;
218
    b = (MAX_BANDWIDTH / h->mit) * h->wcet;
219
 
220
    /* really update lev->U, checking an overflow... */
221
    if (MAX_BANDWIDTH - lev->U > b)
222
      lev->U += b;
223
    else
224
      /* The task can NOT be guaranteed (U>MAX_BANDWIDTH)...
225
         in this case, we don't raise an exception... in fact, after the
226
         EDFACT_task_create the task_create will call level_guarantee that return
227
         -1... return -1 in EDFACT_task_create isn't correct, because:
228
           . generally, the guarantee must be done when also the resources
229
             are registered
230
           . returning -1 will cause the task_create to return with an errno
231
             ETASK_CREATE instead of ENO_GUARANTEE!!!
232
 
233
         Why I use the flag??? because if the lev->U overflows, if i.e. I set
234
         it to MAX_BANDWIDTH, I lose the correct allocated bandwidth...
235
      */
236
      lev->flags |= EDFACT_FAILED_GUARANTEE;
237
  }
238
 
239
  return 0; /* OK, also if the task cannot be guaranteed... */
240
}
241
 
1123 pj 242
static void EDFACT_public_detach(LEVEL l, PID p)
1085 pj 243
{
244
  /* the EDFACT level doesn't introduce any dinamic allocated new field.
245
     we have only to reset the NO_GUARANTEE FIELD and decrement the allocated
246
     bandwidth */
247
 
248
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
249
 
250
  if (lev->flags & EDFACT_FAILED_GUARANTEE)
251
    lev->flags &= ~EDFACT_FAILED_GUARANTEE;
252
  else
253
    lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet;
254
}
255
 
1123 pj 256
static void EDFACT_public_dispatch(LEVEL l, PID p, int nostop)
1085 pj 257
{
258
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
259
 
260
  /* the task state is set EXE by the scheduler()
261
     we extract the task from the ready queue
262
     NB: we can't assume that p is the first task in the queue!!! */
1118 pj 263
  iq_extract(p, &lev->ready);
1085 pj 264
}
265
 
1123 pj 266
static void EDFACT_public_epilogue(LEVEL l, PID p)
1085 pj 267
{
268
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
269
 
270
  /* check if the wcet is finished... */
271
  if (proc_table[p].avail_time <= 0 && proc_table[p].control&CONTROL_CAP) {
272
    /* wcet finished: disable wcet event and count wcet miss */
273
    edfact_printf("W%d",p);
274
    proc_table[p].control &= ~CONTROL_CAP;
275
    lev->wcet_miss[p]++;
276
  }
277
 
278
  /* the task it returns into the ready queue... */
1118 pj 279
  iq_timespec_insert(p,&lev->ready);
1085 pj 280
  proc_table[p].status = EDFACT_READY;
281
}
282
 
1123 pj 283
static void EDFACT_public_activate(LEVEL l, PID p)
1085 pj 284
{
285
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 286
  struct timespec t;
1085 pj 287
 
288
  /* Test if we are trying to activate a non sleeping task    */
289
  /* save activation (only if needed... */
290
  if (proc_table[p].status != SLEEP) {
291
    /* a periodic task cannot be activated when it is already active */
292
    kern_raise(XACTIVATION,p);
293
    return;
294
  }
295
 
1123 pj 296
  kern_gettime(&t);
297
  EDFACT_internal_activate(lev,p, &t);
1085 pj 298
 
299
  /* Set the deadline timer */
300
  lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
301
                                           EDFACT_timer_deadline,
302
                                           (void *)p);
303
 
304
}
305
 
1123 pj 306
static void EDFACT_public_unblock(LEVEL l, PID p)
1085 pj 307
{
308
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
309
 
310
  /* Insert task in the coEDFect position */
311
  proc_table[p].status = EDFACT_READY;
1118 pj 312
  iq_timespec_insert(p,&lev->ready);
1085 pj 313
}
314
 
1123 pj 315
static void EDFACT_public_block(LEVEL l, PID p)
1085 pj 316
{
317
}
318
 
1123 pj 319
static int EDFACT_public_message(LEVEL l, PID p, void *m)
1085 pj 320
{
321
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 322
  struct timespec t;
1085 pj 323
 
324
  /* we reset the capacity counters... */
325
  proc_table[p].avail_time = proc_table[p].wcet;
326
 
327
  if (lev->nact[p] > 0) {
328
    edfact_printf("E%d",p);
329
 
330
    /* Pending activation: reactivate the thread!!! */
331
    lev->nact[p]--;
332
 
333
    /* see also EDFACT_timer_deadline */
1123 pj 334
    kern_gettime(&t);
335
    EDFACT_internal_activate(lev,p, &t);
1085 pj 336
 
337
    /* check if the deadline has already expired */
1118 pj 338
    if (TIMESPEC_A_LT_B(iq_query_timespec(p, &lev->ready), &schedule_time)) {
1085 pj 339
      /* count the deadline miss */
340
      lev->dline_miss[p]++;
1123 pj 341
      kern_event_delete(lev->deadline_timer[p]);
1085 pj 342
    }
343
 
344
  }
345
  else {
346
    edfact_printf("e%d",p);
347
 
348
    /* the task has terminated his job before it consume the wcet. All OK! */
349
    proc_table[p].status = EDFACT_IDLE;
350
 
351
    /* when the deadline timer fire, it recognize the situation and set
1123 pj 352
       correctly all the stuffs (like reactivation, etc... ) */
1085 pj 353
  }
1123 pj 354
 
355
  jet_update_endcycle(); /* Update the Jet data... */
356
  trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
357
 
358
  return 0;
1085 pj 359
}
360
 
1123 pj 361
static void EDFACT_public_end(LEVEL l, PID p)
1085 pj 362
{
363
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
364
 
365
  edfact_printf("%d",p);
366
 
367
  /* we finally put the task in the ready queue */
368
  proc_table[p].status = FREE;
1118 pj 369
  iq_insertfirst(p,&freedesc);
1085 pj 370
  /* and free the allocated bandwidth */
371
  lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet;
372
 
373
  if (lev->deadline_timer[p] != -1) {
374
    edfact_printf("%d",p);
1123 pj 375
    kern_event_delete(lev->deadline_timer[p]);
1085 pj 376
  }
377
}
378
 
379
 
380
/* Guest Functions
381
   These functions manages a JOB_TASK_MODEL, that is used to put
382
   a guest task in the EDFACT ready queue. */
383
 
1123 pj 384
static void EDFACT_private_insert(LEVEL l, PID p, TASK_MODEL *m)
1085 pj 385
{
386
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
387
 
1123 pj 388
  JOB_TASK_MODEL *job;
1085 pj 389
 
1123 pj 390
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
391
    kern_raise(XINVALID_TASK, p);
392
    return;
393
  }
394
 
395
  job = (JOB_TASK_MODEL *)m;
396
 
1118 pj 397
  TIMESPEC_ASSIGN(iq_query_timespec(p, &lev->ready), &job->deadline);
1085 pj 398
 
399
  lev->deadline_timer[p] = -1;
400
  lev->dline_miss[p]     = 0;
401
  lev->wcet_miss[p]      = 0;
402
  lev->nact[p]           = 0;
403
 
404
  if (job->noraiseexc)
405
    lev->flag[p] = EDFACT_FLAG_NORAISEEXC;
1123 pj 406
  else {
1085 pj 407
    lev->flag[p] = 0;
1123 pj 408
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
409
                                             EDFACT_timer_guest_deadline,
410
                                             (void *)p);
411
  }
1085 pj 412
 
413
  lev->period[p] = job->period;
414
 
1123 pj 415
  /* Insert task in the correct position */
416
  iq_timespec_insert(p,&lev->ready);
417
  proc_table[p].status = EDFACT_READY;
418
 
1085 pj 419
  /* there is no bandwidth guarantee at this level, it is performed
420
     by the level that inserts guest tasks... */
421
}
422
 
1123 pj 423
static void EDFACT_private_dispatch(LEVEL l, PID p, int nostop)
1085 pj 424
{
425
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
426
 
427
  /* the task state is set to EXE by the scheduler()
428
     we extract the task from the ready queue
429
     NB: we can't assume that p is the first task in the queue!!! */
1118 pj 430
  iq_extract(p, &lev->ready);
1085 pj 431
}
432
 
1123 pj 433
static void EDFACT_private_epilogue(LEVEL l, PID p)
1085 pj 434
{
435
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
436
 
437
  /* the task has been preempted. it returns into the ready queue... */
1118 pj 438
  iq_timespec_insert(p,&lev->ready);
1085 pj 439
  proc_table[p].status = EDFACT_READY;
440
}
441
 
1123 pj 442
static void EDFACT_private_extract(LEVEL l, PID p)
1085 pj 443
{
444
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
445
 
446
  //kern_printf("EDFACT_guest_end: dline timer %d\n",lev->deadline_timer[p]);
447
  if (proc_table[p].status == EDFACT_READY)
448
  {
1118 pj 449
    iq_extract(p, &lev->ready);
1085 pj 450
    //kern_printf("(g_end rdy extr)");
451
  }
452
 
453
  /* we remove the deadline timer, because the slice is finished */
454
  if (lev->deadline_timer[p] != NIL) {
455
//    kern_printf("EDFACT_guest_end: dline timer %d\n",lev->deadline_timer[p]);
1123 pj 456
    kern_event_delete(lev->deadline_timer[p]);
1085 pj 457
    lev->deadline_timer[p] = NIL;
458
  }
459
 
460
}
461
 
462
/* Registration functions */
463
 
464
/*+ Registration function:
465
    int flags                 the init flags ... see EDFACT.h +*/
1123 pj 466
LEVEL EDFACT_register_level(int flags)
1085 pj 467
{
468
  LEVEL l;            /* the level that we register */
469
  EDFACT_level_des *lev;  /* for readableness only */
470
  PID i;              /* a counter */
471
 
472
  printk("EDFACT_register_level\n");
473
 
474
  /* request an entry in the level_table */
1123 pj 475
  l = level_alloc_descriptor(sizeof(EDFACT_level_des));
1085 pj 476
 
1123 pj 477
  lev = (EDFACT_level_des *)level_table[l];
1085 pj 478
 
479
  printk("    lev=%d\n",(int)lev);
480
 
481
  /* fill the standard descriptor */
1123 pj 482
  lev->l.private_insert   = EDFACT_private_insert;
483
  lev->l.private_extract  = EDFACT_private_extract;
484
  lev->l.private_dispatch = EDFACT_private_dispatch;
485
  lev->l.private_epilogue = EDFACT_private_epilogue;
1085 pj 486
 
1123 pj 487
  lev->l.public_scheduler = EDFACT_public_scheduler;
1085 pj 488
  if (flags & EDFACT_ENABLE_GUARANTEE)
1123 pj 489
    lev->l.public_guarantee = EDFACT_public_guarantee;
1085 pj 490
  else
1123 pj 491
    lev->l.public_guarantee = NULL;
492
  lev->l.public_create    = EDFACT_public_create;
493
  lev->l.public_detach    = EDFACT_public_detach;
494
  lev->l.public_end       = EDFACT_public_end;
495
  lev->l.public_dispatch  = EDFACT_public_dispatch;
496
  lev->l.public_epilogue  = EDFACT_public_epilogue;
497
  lev->l.public_activate  = EDFACT_public_activate;
498
  lev->l.public_unblock   = EDFACT_public_unblock;
499
  lev->l.public_block     = EDFACT_public_block;
500
  lev->l.public_message   = EDFACT_public_message;
1085 pj 501
 
502
  /* fill the EDFACT descriptor part */
503
  for(i=0; i<MAX_PROC; i++) {
504
    lev->period[i]         = 0;
505
    lev->deadline_timer[i] = -1;
506
    lev->flag[i]           = 0;
507
    lev->dline_miss[i]     = 0;
508
    lev->wcet_miss[i]      = 0;
509
    lev->nact[i]           = 0;
510
  }
511
 
1118 pj 512
  iq_init(&lev->ready,&freedesc, 0);
1085 pj 513
  lev->flags = flags & 0x07;
514
  lev->U     = 0;
1123 pj 515
 
516
  return l;
1085 pj 517
}
518
 
519
bandwidth_t EDFACT_usedbandwidth(LEVEL l)
520
{
521
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 522
 
523
  return lev->U;
1085 pj 524
}
525
 
526
int EDFACT_get_dline_miss(PID p)
527
{
528
  LEVEL l = proc_table[p].task_level;
529
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 530
 
531
  return lev->dline_miss[p];
1085 pj 532
}
533
 
534
int EDFACT_get_wcet_miss(PID p)
535
{
536
  LEVEL l = proc_table[p].task_level;
537
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 538
 
539
  return lev->wcet_miss[p];
1085 pj 540
}
541
 
542
int EDFACT_get_nact(PID p)
543
{
544
  LEVEL l = proc_table[p].task_level;
545
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 546
 
547
  return lev->nact[p];
1085 pj 548
}
549
 
550
int EDFACT_reset_dline_miss(PID p)
551
{
552
  LEVEL l = proc_table[p].task_level;
553
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 554
 
555
  lev->dline_miss[p] = 0;
556
  return 0;
1085 pj 557
}
558
 
559
int EDFACT_reset_wcet_miss(PID p)
560
{
561
  LEVEL l = proc_table[p].task_level;
562
  EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
1123 pj 563
 
564
  lev->wcet_miss[p] = 0;
565
  return 0;
1085 pj 566
}
567