Subversion Repositories shark

Rev

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

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