Subversion Repositories shark

Rev

Rev 980 | Rev 988 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
221 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
 * Copyright (C) 2001 Paolo Gai
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 */
37
 
38
#include "edfstar.h"
39
#include <ll/stdio.h>
40
#include <ll/string.h>
41
#include <kernel/model.h>
42
#include <kernel/descr.h>
43
#include <kernel/var.h>
44
#include <kernel/func.h>
45
 
783 giacomo 46
#include <tracer.h>
221 giacomo 47
 
48
/* for iqueues */
49
/* #include "iqueue.h" Now iqueues are the only queue type available
50
   into the kernel */
51
#include <kernel/iqueue.h>
52
 
53
/* for BUDGET_TASK_MODEL */
867 trimarchi 54
#include "fsf_configuration_parameters.h"
55
#include "fsf_core.h"
241 giacomo 56
#include "fsf_server.h"
973 trimarchi 57
#include <posix/posix/comm_message.h>
221 giacomo 58
 
876 trimarchi 59
 
221 giacomo 60
/*
61
 * DEBUG stuffs begin
62
 */
63
 
224 giacomo 64
//#define EDFSTAR_DEBUG
221 giacomo 65
 
66
#ifdef EDFSTAR_DEBUG
67
 
68
static __inline__ fake_printf(char *fmt, ...) {}
69
 
70
//#define edfstar_printf fake_printf
71
//#define edfstar_printf2 fake_printf
72
//#define edfstar_printf3 fake_printf
73
 
74
#define edfstar_printf kern_printf
75
#define edfstar_printf2 kern_printf
76
#define edfstar_printf3 kern_printf
77
#endif
78
 
79
/*
80
 * DEBUG stuffs end
81
 */
82
 
83
/* Status used in the level */
84
#define EDFSTAR_READY         MODULE_STATUS_BASE    /* - Ready status        */
85
#define EDFSTAR_IDLE          MODULE_STATUS_BASE+4  /* to wait the deadline  */
86
 
87
/* flags */
877 trimarchi 88
#define EDFSTAR_CHANGE_LEVEL     8 
792 trimarchi 89
#define EDFSTAR_FLAG_NOPREEMPT   4 
221 giacomo 90
#define EDFSTAR_FLAG_NORAISEEXC  2
348 trimarchi 91
#define EDFSTAR_FLAG_SPORADIC    1
221 giacomo 92
 
348 trimarchi 93
 
221 giacomo 94
/* the level redefinition for the Earliest Deadline First level */
95
typedef struct {
96
  level_des l;     /* the standard level descriptor          */
97
 
98
  TIME period[MAX_PROC]; /* The task periods; the deadlines are
99
                       stored in the priority field           */
100
  int deadline_timer[MAX_PROC];
101
                   /* The task deadline timers               */
102
 
103
  struct timespec deadline_timespec[MAX_PROC];
104
 
105
  int dline_miss[MAX_PROC]; /* Deadline miss counter */
106
  int wcet_miss[MAX_PROC];  /* Wcet miss counter */
107
 
108
  int nact[MAX_PROC];       /* Wcet miss counter */
109
 
110
  int flag[MAX_PROC];
111
                   /* used to manage the JOB_TASK_MODEL and the
112
                       periodicity                            */
113
 
114
  IQUEUE ready;     /* the ready queue                        */
115
 
116
  PID activated;   /* the task that has been inserted into the
117
                       master module */
118
 
119
  int budget[MAX_PROC];
120
 
121
  int scheduling_level;
122
 
296 trimarchi 123
  int cap_lev;
124
  struct timespec cap_lasttime;
125
 
876 trimarchi 126
  int new_level[MAX_PROC];
127
  int wcet[MAX_PROC]; /* save the wcet fields */
128
 
296 trimarchi 129
 
221 giacomo 130
} EDFSTAR_level_des;
131
 
296 trimarchi 132
static void capacity_handler(void *l)
133
{
134
  EDFSTAR_level_des *lev = l;
135
  lev->cap_lev = NIL;
136
  event_need_reschedule();
137
}
138
 
221 giacomo 139
static void EDFSTAR_check_preemption(EDFSTAR_level_des *lev)
140
{
982 trimarchi 141
  PID first=NIL;
221 giacomo 142
 
351 giacomo 143
  #ifdef EDFSTAR_DEBUG
144
    edfstar_printf("(E:chk)");
145
  #endif
792 trimarchi 146
  /* check if the task is preempteble or not */
821 trimarchi 147
  if (lev->activated != NIL && lev->flag[lev->activated] & EDFSTAR_FLAG_NOPREEMPT) return;
221 giacomo 148
 
149
  if ((first = iq_query_first(&lev->ready)) != lev->activated) {
150
    if (lev->activated != NIL)
151
      level_table[ lev->scheduling_level ]->
152
        private_extract(lev->scheduling_level, lev->activated);
153
 
154
    lev->activated = first;
155
 
156
    if (first != NIL) {
157
      BUDGET_TASK_MODEL b;
158
      budget_task_default_model(b, lev->budget[first]);
159
 
160
      level_table[ lev->scheduling_level ]->
161
        private_insert(lev->scheduling_level, first, (TASK_MODEL *)&b);
162
    }
163
  }
164
}
165
 
166
static void EDFSTAR_timer_deadline(void *par);
167
 
168
static void EDFSTAR_internal_activate(EDFSTAR_level_des *lev, PID p,
169
                                      struct timespec *t)
170
{
171
 
351 giacomo 172
  #ifdef EDFSTAR_DEBUG
173
    edfstar_printf("(E:iact)");
174
  #endif
175
 
221 giacomo 176
  ADDUSEC2TIMESPEC(lev->period[p], t);
177
 
178
  *iq_query_timespec(p, &lev->ready) = *t;
179
  lev->deadline_timespec[p] = *t;
180
 
181
  /* Insert task in the correct position */
182
  proc_table[p].status = EDFSTAR_READY;
183
  iq_timespec_insert(p,&lev->ready);
296 trimarchi 184
  proc_table[p].control &= ~CONTROL_CAP;
221 giacomo 185
 
186
  /* check for preemption */
187
  EDFSTAR_check_preemption(lev);
188
}
189
 
190
static void EDFSTAR_timer_deadline(void *par)
191
{
192
  PID p = (PID) par;
193
  EDFSTAR_level_des *lev;
194
 
195
  lev = (EDFSTAR_level_des *)level_table[proc_table[p].task_level];
799 giacomo 196
  lev->deadline_timer[p] = NIL;  
221 giacomo 197
 
198
  switch (proc_table[p].status) {
199
    case EDFSTAR_IDLE:
200
      /* set the request time */
350 giacomo 201
      if (!(lev->flag[p] & EDFSTAR_FLAG_SPORADIC))
202
        EDFSTAR_internal_activate(lev,p,iq_query_timespec(p, &lev->ready));
221 giacomo 203
 
204
      event_need_reschedule();
205
      break;
206
 
207
    default:
982 trimarchi 208
      //      #ifdef EDFSTAR_DEBUG
351 giacomo 209
        kern_printf("(E:Dl:%d)",p);
982 trimarchi 210
      //      #endif
221 giacomo 211
      /* else, a deadline miss occurred!!! */
212
      lev->dline_miss[p]++;
783 giacomo 213
      TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss,proc_table[p].context,proc_table[p].task_level);
221 giacomo 214
 
215
      /* the task is into another state */
351 giacomo 216
      if (!(lev->flag[p] & EDFSTAR_FLAG_SPORADIC)) {
217
        lev->nact[p]++;
218
        ADDUSEC2TIMESPEC(lev->period[p], &lev->deadline_timespec[p]);
219
      }
221 giacomo 220
  }
221
 
222
  /* Set the deadline timer */
350 giacomo 223
  if (!(lev->flag[p] & EDFSTAR_FLAG_SPORADIC))
224
    lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
225
                                             EDFSTAR_timer_deadline,
226
                                             (void *)p);
221 giacomo 227
 
228
}
229
 
876 trimarchi 230
static int EDFSTAR_private_change_level(LEVEL l, PID p)
231
{
232
 
233
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
234
 
235
  /* Change task level */
236
  if (lev->flag[p] & EDFSTAR_CHANGE_LEVEL) {
237
 
877 trimarchi 238
#ifdef EDFSTAR_DEBUG
239
    edfstar_printf("(E:clev)");
240
#endif
241
 
876 trimarchi 242
    STD_command_message msg;
243
 
244
    proc_table[p].status = SLEEP;
877 trimarchi 245
    lev->flag[p] &= ~ EDFSTAR_CHANGE_LEVEL;
876 trimarchi 246
 
247
    level_table[lev->scheduling_level]->private_extract(lev->scheduling_level,p);
248
    iq_extract(p,&lev->ready);
249
 
250
    if (lev->deadline_timer[p] != -1)
251
      kern_event_delete(lev->deadline_timer[p]);
980 trimarchi 252
    lev->deadline_timer[p]=NIL;
876 trimarchi 253
 
254
    EDFSTAR_check_preemption(lev);
255
 
256
    lev->nact[p] = 0;
257
    lev->budget[p] = -1;
258
    proc_table[p].task_level = lev->new_level[p];
259
 
260
    /* Send change level command to local scheduler */
261
 
262
    msg.command = STD_ACTIVATE_TASK;
263
    msg.param = NULL;
264
 
265
    level_table[ lev->new_level[p] ]->public_message(lev->new_level[p],p,&msg);
266
 
267
    return 1;
268
 
269
  }
270
 
271
  return 0;
272
 
273
}
274
 
275
 
221 giacomo 276
static void EDFSTAR_timer_guest_deadline(void *par)
277
{
278
  PID p = (PID) par;
279
 
351 giacomo 280
  #ifdef EDFSTAR_DEBUG
281
    edfstar_printf("(E:gdl)");
282
  #endif
221 giacomo 283
 
284
  kern_raise(XDEADLINE_MISS,p);
285
}
286
 
287
static int EDFSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
288
{
289
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
290
 
291
  /* if the EDFSTAR_task_create is called, then the pclass must be a
292
     valid pclass. */
293
  HARD_TASK_MODEL *h;
294
 
295
  if (m->pclass != HARD_PCLASS) return -1;
296
  if (m->level != 0 && m->level != l) return -1;
297
  h = (HARD_TASK_MODEL *)m;
351 giacomo 298
  if (!h->wcet || !h->mit) return -1;
221 giacomo 299
  /* now we know that m is a valid model */
300
 
351 giacomo 301
  #ifdef EDFSTAR_DEBUG
302
    edfstar_printf("(E:Crt)");
303
  #endif
221 giacomo 304
 
305
  lev->period[p] = h->mit;
306
 
307
  lev->flag[p] = 0;
348 trimarchi 308
 
309
  if (h->periodicity == APERIODIC)
310
       lev->flag[p] |= EDFSTAR_FLAG_SPORADIC;
311
 
221 giacomo 312
  lev->deadline_timer[p] = -1;
313
  lev->dline_miss[p]     = 0;
314
  lev->wcet_miss[p]      = 0;
315
  lev->nact[p]           = 0;
316
 
317
  /* Enable wcet check */
318
  proc_table[p].avail_time = h->wcet;
319
  proc_table[p].wcet       = h->wcet;
982 trimarchi 320
  proc_table[p].status     = SLEEP;
221 giacomo 321
 
322
  return 0; /* OK, also if the task cannot be guaranteed... */
323
}
324
 
296 trimarchi 325
 
326
static void EDFSTAR_account_capacity(EDFSTAR_level_des *lev, PID p)
327
{
328
  struct timespec ty;
329
  TIME tx;
330
 
982 trimarchi 331
 
296 trimarchi 332
  SUBTIMESPEC(&schedule_time, &lev->cap_lasttime, &ty);
333
  tx = TIMESPEC2USEC(&ty);
334
 
335
  proc_table[p].avail_time -= tx;
351 giacomo 336
}
296 trimarchi 337
 
221 giacomo 338
static int EDFSTAR_public_eligible(LEVEL l, PID p)
339
{
340
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
341
 
351 giacomo 342
  #ifdef EDFSTAR_DEBUG
877 trimarchi 343
    edfstar_printf2("(E:eli:%d)",p);
351 giacomo 344
  #endif
221 giacomo 345
 
346
  return level_table[ lev->scheduling_level ]->
347
    private_eligible(lev->scheduling_level,p);
351 giacomo 348
 
221 giacomo 349
}
350
 
351
static void EDFSTAR_public_dispatch(LEVEL l, PID p, int nostop)
352
{
353
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
296 trimarchi 354
  struct timespec ty;
221 giacomo 355
 
351 giacomo 356
  #ifdef EDFSTAR_DEBUG
357
    edfstar_printf("(E:dis)");
358
  #endif
359
 
296 trimarchi 360
  if (!nostop || proc_table[exec].task_level==l) {
361
      TIMESPEC_ASSIGN(&ty, &schedule_time);
362
      TIMESPEC_ASSIGN(&lev->cap_lasttime, &schedule_time);
221 giacomo 363
 
296 trimarchi 364
      /* ...and finally, we have to post a capacity event on exec task because the shadow_task consume
365
       *      *        capacity on exe task always */
784 giacomo 366
      if (proc_table[exec].avail_time > 0) {
367
        ADDUSEC2TIMESPEC(proc_table[exec].avail_time ,&ty);
368
        lev->cap_lev = kern_event_post(&ty,capacity_handler, lev);
369
      }
296 trimarchi 370
      level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop);
371
  }
372
  else
373
      level_table[proc_table[exec].task_level]->public_dispatch(proc_table[exec].task_level, p, nostop);
374
 
221 giacomo 375
}
376
 
377
static void EDFSTAR_public_epilogue(LEVEL l, PID p)
378
{
379
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
380
 
351 giacomo 381
  #ifdef EDFSTAR_DEBUG
382
    edfstar_printf("(E:epi ");
383
  #endif
221 giacomo 384
 
296 trimarchi 385
  if (lev->cap_lev!=NIL) {
386
       kern_event_delete(lev->cap_lev);
387
       lev->cap_lev=NIL;
388
  }
389
 
876 trimarchi 390
 
296 trimarchi 391
  if ( proc_table[exec].task_level==l ) {
392
 
785 giacomo 393
     if (proc_table[exec].avail_time > 0) EDFSTAR_account_capacity(lev,exec);
296 trimarchi 394
 
876 trimarchi 395
     if (EDFSTAR_private_change_level(l, p)) return;
396
 
296 trimarchi 397
     /* check if the wcet is finished... */
784 giacomo 398
     if (proc_table[exec].avail_time < 0) {
296 trimarchi 399
        /* wcet finished: disable wcet event and count wcet miss */
351 giacomo 400
 
401
       #ifdef EDFSTAR_DEBUG
402
         edfstar_printf2("W%d",p);
403
       #endif
296 trimarchi 404
        //proc_table[p].control &= ~CONTROL_CAP;
405
        lev->wcet_miss[exec]++;
784 giacomo 406
        proc_table[exec].avail_time = 0;
407
        TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation,proc_table[exec].context,proc_table[exec].task_level);
296 trimarchi 408
     }
221 giacomo 409
 
351 giacomo 410
     #ifdef EDFSTAR_DEBUG
411
       edfstar_printf(")");
412
     #endif
221 giacomo 413
 
351 giacomo 414
     level_table[ lev->scheduling_level ]->
415
       private_epilogue(lev->scheduling_level,p);
416
 
982 trimarchi 417
      proc_table[exec].status = EDFSTAR_READY;
296 trimarchi 418
    } else
419
        level_table[proc_table[exec].task_level]->public_epilogue(proc_table[exec].task_level,p);
420
 
221 giacomo 421
}
422
 
783 giacomo 423
static void EDFSTAR_public_activate(LEVEL l, PID p, struct timespec *o)
221 giacomo 424
{
425
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
426
  struct timespec t;
427
 
428
  #ifdef EDFSTAR_DEBUG
429
    edfstar_printf("(E:act:%d)",p);
430
  #endif
431
 
432
  /* Test if we are trying to activate a non sleeping task    */
433
  /* save activation (only if needed... */
434
  if (proc_table[p].status != SLEEP) {
435
    /* a periodic task cannot be activated when it is already active */
348 trimarchi 436
    /* but aperiodic task can be reactivate before */
802 giacomo 437
    if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC) {
438
        if (proc_table[p].status != EDFSTAR_IDLE) {
439
          lev->nact[p]++;
982 trimarchi 440
          //kern_printf("(Inc nact %d)",p);
441
          //kern_printf("(%d STATUS %d %ds %dns)", p, proc_table[p].status, o->tv_sec, o->tv_nsec/1000);
802 giacomo 442
          return;
443
        }
444
    } else {
445
        kern_raise(XACTIVATION,p);
351 giacomo 446
    }
221 giacomo 447
  }
448
 
449
  kern_gettime(&t);
450
 
451
  EDFSTAR_internal_activate(lev,p, &t);
452
 
453
  /* Set the deadline timer */
454
  lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
455
                                           EDFSTAR_timer_deadline,
456
                                           (void *)p);
457
 
458
}
459
 
460
static void EDFSTAR_public_unblock(LEVEL l, PID p)
461
{
462
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
463
 
351 giacomo 464
  #ifdef EDFSTAR_DEBUG
465
    edfstar_printf("(E:ins)");
466
  #endif
221 giacomo 467
 
351 giacomo 468
  /* Insert task in the correct position */
469
  proc_table[p].status = EDFSTAR_READY;
470
  iq_timespec_insert(p,&lev->ready);
221 giacomo 471
 
351 giacomo 472
  /* and check for preemption! */
473
  EDFSTAR_check_preemption(lev);
474
 
221 giacomo 475
}
476
 
477
static void EDFSTAR_public_block(LEVEL l, PID p)
478
{
479
 
351 giacomo 480
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
221 giacomo 481
 
351 giacomo 482
  #ifdef EDFSTAR_DEBUG
483
   edfstar_printf("(E:ext)");
484
  #endif
221 giacomo 485
 
351 giacomo 486
  /* the task is blocked on a synchronization primitive. we have to
487
     remove it from the master module -and- from the local queue! */
488
  iq_extract(p,&lev->ready);
489
 
490
  /* and finally, a preemption check! (it will also call guest_end) */
491
  EDFSTAR_check_preemption(lev);
221 giacomo 492
}
493
 
494
static int EDFSTAR_public_message(LEVEL l, PID p, void *m)
495
{
496
 
351 giacomo 497
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
498
  struct timespec temp;
876 trimarchi 499
  STD_command_message *msg;
500
  HARD_TASK_MODEL *h;
221 giacomo 501
 
351 giacomo 502
  #ifdef EDFSTAR_DEBUG
503
    edfstar_printf("(E:ecy ");
504
  #endif
221 giacomo 505
 
351 giacomo 506
  switch ((long)(m)) {
221 giacomo 507
 
351 giacomo 508
    /* Task EndCycle */
509
    case (long)(NULL):
348 trimarchi 510
 
876 trimarchi 511
      if (EDFSTAR_private_change_level(l,p)) return 0;
512
 
932 trimarchi 513
      if (proc_table[p].avail_time > 0) EDFSTAR_account_capacity(lev,p);
514
 
351 giacomo 515
       /* we call guest_end directly here because the same task may
516
         be reinserted in the queue before calling the preemption check! */
517
       level_table[ lev->scheduling_level ]->
518
         private_extract(lev->scheduling_level,p);
519
       lev->activated = NIL;
348 trimarchi 520
 
351 giacomo 521
       iq_extract(p,&lev->ready);
348 trimarchi 522
 
351 giacomo 523
       /* we reset the capacity counters... */
524
       proc_table[p].avail_time = proc_table[p].wcet;
525
 
526
       if (lev->nact[p] > 0) {
221 giacomo 527
 
351 giacomo 528
         #ifdef EDFSTAR_DEBUG
982 trimarchi 529
           kern_printf("E%d",p);
530
         #endif
221 giacomo 531
 
351 giacomo 532
         /* Pending activation: reactivate the thread!!! */
533
         lev->nact[p]--;
221 giacomo 534
 
351 giacomo 535
         /* see also EDFSTAR_timer_deadline */
536
         kern_gettime(&temp);
221 giacomo 537
 
351 giacomo 538
         EDFSTAR_internal_activate(lev,p, &temp);
221 giacomo 539
 
351 giacomo 540
         /* check if the deadline has already expired */
541
         temp = *iq_query_timespec(p, &lev->ready);
542
         if (TIMESPEC_A_LT_B(&temp, &schedule_time)) {
543
           /* count the deadline miss */
544
           lev->dline_miss[p]++;
545
           kern_event_delete(lev->deadline_timer[p]);
799 giacomo 546
           lev->deadline_timer[p] = NIL;
351 giacomo 547
         }
783 giacomo 548
 
351 giacomo 549
       } else {
221 giacomo 550
 
351 giacomo 551
         #ifdef EDFSTAR_DEBUG
552
           edfstar_printf("e%d",p);
553
         #endif
221 giacomo 554
 
351 giacomo 555
         /* the task has terminated his job before it consume the wcet. All OK! */
799 giacomo 556
         if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC)
557
                proc_table[p].status = SLEEP;
558
         else
559
                proc_table[p].status = EDFSTAR_IDLE;
351 giacomo 560
 
799 giacomo 561
         if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC && lev->deadline_timer[p] != NIL) {
351 giacomo 562
           kern_event_delete(lev->deadline_timer[p]);
799 giacomo 563
           lev->deadline_timer[p] = NIL;
564
         }
221 giacomo 565
 
351 giacomo 566
         /* and finally, a preemption check! */
567
         EDFSTAR_check_preemption(lev);
221 giacomo 568
 
351 giacomo 569
         /* when the deadline timer fire, it recognize the situation and set
570
           correctly all the stuffs (like reactivation, etc... ) */
571
       }
572
 
573
    #ifdef EDFSTAR_DEBUG
574
      edfstar_printf(")");
575
    #endif
783 giacomo 576
 
577
    TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,proc_table[p].context,proc_table[p].task_level);
221 giacomo 578
    jet_update_endcycle(); /* Update the Jet data... */
579
    break;
580
 
581
  default:
876 trimarchi 582
    msg = (STD_command_message *)m;
583
 
584
#ifdef EDFSTAR_DEBUG
585
    edfstar_printf("(E:MSG %d)",msg->command);
586
#endif   
587
 
588
    switch(msg->command) {
589
    case STD_SET_NEW_MODEL:
590
      /* if the EDFSTAR_task_create is called, then the pclass must be a
591
         valid pclass. */
592
      h=(HARD_TASK_MODEL *)(msg->param);
877 trimarchi 593
 
876 trimarchi 594
      /* now we know that m is a valid model */
595
      lev->wcet[p] = h->wcet;
596
      lev->period[p] = h->mit;
877 trimarchi 597
 
598
#ifdef EDFSTAR_DEBUG      
599
      kern_printf("(EDF:NM p%d w%d m%d)", p, h->wcet, h->mit);
600
#endif       
876 trimarchi 601
      lev->flag[p] = 0;
602
      lev->deadline_timer[p] = -1;
603
      lev->dline_miss[p]     = 0;
604
      lev->wcet_miss[p]      = 0;
605
      lev->nact[p]           = 0;
221 giacomo 606
 
876 trimarchi 607
      break;
608
 
609
    case STD_SET_NEW_LEVEL:
610
 
611
      lev->flag[p] |= EDFSTAR_CHANGE_LEVEL;
612
      lev->new_level[p] = (int)(msg->param);
613
 
614
      break;
615
 
616
    case STD_ACTIVATE_TASK:
877 trimarchi 617
#ifdef EDFSTAR_DEBUG
618
      kern_printf("(EDF:SA)");
619
#endif       
876 trimarchi 620
      /* Enable wcet check */
621
      proc_table[p].avail_time = lev->wcet[p];
622
      proc_table[p].wcet       = lev->wcet[p];
623
      proc_table[p].control &= ~CONTROL_CAP;
624
 
625
      EDFSTAR_public_activate(l, p,NULL);
626
 
627
      break;
628
 
629
 
630
    }
631
 
241 giacomo 632
    break;
221 giacomo 633
 
634
  }
635
  return 0;
636
}
637
 
638
static void EDFSTAR_public_end(LEVEL l, PID p)
639
{
640
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
641
 
351 giacomo 642
  #ifdef EDFSTAR_DEBUG
643
    edfstar_printf("(E:end)");
644
  #endif
221 giacomo 645
 
646
  iq_extract(p,&lev->ready);
982 trimarchi 647
  level_table[ lev->scheduling_level ]->
648
    private_extract(lev->scheduling_level, p);
221 giacomo 649
 
982 trimarchi 650
 
221 giacomo 651
  /* we finally put the task in the ready queue */
652
  proc_table[p].status = FREE;
653
 
654
  iq_insertfirst(p,&freedesc);
348 trimarchi 655
  lev->activated=NIL;
221 giacomo 656
 
657
  if (lev->deadline_timer[p] != -1) {
658
    kern_event_delete(lev->deadline_timer[p]);
799 giacomo 659
    lev->deadline_timer[p] = NIL;
221 giacomo 660
  }
661
 
662
  /* and finally, a preemption check! (it will also call guest_end) */
663
  EDFSTAR_check_preemption(lev);
664
}
665
 
666
/* Guest Functions
667
   These functions manages a JOB_TASK_MODEL, that is used to put
668
   a guest task in the EDFSTAR ready queue. */
669
 
670
static void EDFSTAR_private_insert(LEVEL l, PID p, TASK_MODEL *m)
671
{
672
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
673
  JOB_TASK_MODEL *job;
674
 
675
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
676
    kern_raise(XINVALID_TASK, p);
677
    return;
678
  }
679
 
680
  job = (JOB_TASK_MODEL *)m;
681
 
682
  /* if the EDFSTAR_guest_create is called, then the pclass must be a
683
     valid pclass. */
684
 
685
  *iq_query_timespec(p, &lev->ready) = job->deadline;
686
 
687
  lev->deadline_timer[p] = -1;
688
  lev->dline_miss[p]     = 0;
689
  lev->wcet_miss[p]      = 0;
690
  lev->nact[p]           = 0;
691
 
692
  if (job->noraiseexc)
693
    lev->flag[p] |= EDFSTAR_FLAG_NORAISEEXC;
694
  else {
695
    lev->flag[p] &= ~EDFSTAR_FLAG_NORAISEEXC;
696
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
697
                                             EDFSTAR_timer_guest_deadline,
698
                                             (void *)p);
699
  }
700
 
701
  lev->period[p] = job->period;
702
 
703
  /* Insert task in the correct position */
704
  iq_timespec_insert(p,&lev->ready);
705
  proc_table[p].status = EDFSTAR_READY;
706
 
707
  /* check for preemption */
708
  EDFSTAR_check_preemption(lev);
709
 
710
  /* there is no bandwidth guarantee at this level, it is performed
711
     by the level that inserts guest tasks... */
712
}
713
 
714
static void EDFSTAR_private_dispatch(LEVEL l, PID p, int nostop)
715
{
716
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
717
 
718
  level_table[ lev->scheduling_level ]->
719
    private_dispatch(lev->scheduling_level,p,nostop);
720
}
721
 
722
static void EDFSTAR_private_epilogue(LEVEL l, PID p)
723
{
724
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
725
 
726
  /* the task has been preempted. it returns into the ready queue... */
727
  level_table[ lev->scheduling_level ]->
728
    private_epilogue(lev->scheduling_level,p);
729
 
730
  proc_table[p].status = EDFSTAR_READY;
731
}
732
 
733
static void EDFSTAR_private_extract(LEVEL l, PID p)
734
{
735
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
736
 
737
#ifdef EDFSTAR_DEBUG
877 trimarchi 738
  kern_printf("EDFSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
221 giacomo 739
#endif
740
 
741
  iq_extract(p, &lev->ready);
742
 
743
  /* we remove the deadline timer, because the slice is finished */
744
  if (lev->deadline_timer[p] != NIL) {
745
#ifdef EDFSTAR_DEBUG
877 trimarchi 746
    kern_printf("EDFSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
221 giacomo 747
#endif
748
    kern_event_delete(lev->deadline_timer[p]);
749
    lev->deadline_timer[p] = NIL;
750
  }
751
 
752
  /* and finally, a preemption check! (it will also call guest_end() */
753
  EDFSTAR_check_preemption(lev);
754
}
755
 
756
/* Registration functions */
757
 
758
/* Registration function:
759
    int flags                 the init flags ... see EDFSTAR.h */
760
 
761
LEVEL EDFSTAR_register_level(int master)
762
{
763
  LEVEL l;            /* the level that we register */
764
  EDFSTAR_level_des *lev;  /* for readableness only */
765
  PID i;              /* a counter */
766
 
767
#ifdef EDFSTAR_DEBUG
768
  printk("EDFSTAR_register_level\n");
769
#endif
770
 
771
  /* request an entry in the level_table */
772
  l = level_alloc_descriptor(sizeof(EDFSTAR_level_des));
773
 
774
  lev = (EDFSTAR_level_des *)level_table[l];
775
 
776
  /* fill the standard descriptor */
777
  lev->l.private_insert   = EDFSTAR_private_insert;
778
  lev->l.private_extract  = EDFSTAR_private_extract;
779
  lev->l.private_dispatch = EDFSTAR_private_dispatch;
780
  lev->l.private_epilogue = EDFSTAR_private_epilogue;
781
 
782
  lev->l.public_guarantee = NULL;
783
  lev->l.public_eligible  = EDFSTAR_public_eligible;
784
  lev->l.public_create    = EDFSTAR_public_create;
785
  lev->l.public_end       = EDFSTAR_public_end;
786
  lev->l.public_dispatch  = EDFSTAR_public_dispatch;
787
  lev->l.public_epilogue  = EDFSTAR_public_epilogue;
788
  lev->l.public_activate  = EDFSTAR_public_activate;
789
  lev->l.public_unblock   = EDFSTAR_public_unblock;
790
  lev->l.public_block     = EDFSTAR_public_block;
791
  lev->l.public_message   = EDFSTAR_public_message;
792
 
793
  /* fill the EDFSTAR descriptor part */
794
  for(i=0; i<MAX_PROC; i++) {
795
    lev->period[i]         = 0;
796
    lev->deadline_timer[i] = -1;
797
    lev->flag[i]           = 0;
798
    lev->dline_miss[i]     = 0;
799
    lev->wcet_miss[i]      = 0;
800
    lev->nact[i]           = 0;
801
    lev->budget[i]         = NIL;
880 trimarchi 802
    lev->new_level[i]      = -1;
221 giacomo 803
  }
804
 
805
  iq_init(&lev->ready, NULL, IQUEUE_NO_PRIORITY);
806
  lev->activated = NIL;
807
 
808
  lev->scheduling_level = master;
298 giacomo 809
  lev->cap_lev = NIL;
296 trimarchi 810
  NULL_TIMESPEC(&lev->cap_lasttime);
221 giacomo 811
 
812
  return l;
813
}
814
 
815
int EDFSTAR_get_dline_miss(PID p)
816
{
817
  LEVEL l = proc_table[p].task_level;
818
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
819
 
820
  return lev->dline_miss[p];
821
}
822
 
823
int EDFSTAR_get_wcet_miss(PID p)
824
{
825
  LEVEL l = proc_table[p].task_level;
826
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
827
 
828
  return lev->wcet_miss[p];
829
}
830
 
831
int EDFSTAR_get_nact(PID p)
832
{
833
  LEVEL l = proc_table[p].task_level;
834
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
835
 
836
  return lev->nact[p];
837
}
838
 
839
int EDFSTAR_reset_dline_miss(PID p)
840
{
841
  LEVEL l = proc_table[p].task_level;
842
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
843
 
844
  lev->dline_miss[p] = 0;
845
  return 0;
846
}
847
 
848
int EDFSTAR_reset_wcet_miss(PID p)
849
{
850
  LEVEL l = proc_table[p].task_level;
851
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
852
 
853
  lev->wcet_miss[p] = 0;
854
  return 0;
855
}
856
 
857
int EDFSTAR_setbudget(LEVEL l, PID p, int budget)
858
{
859
 
860
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
861
 
862
  lev->budget[p] = budget;
863
 
864
  return 0;
865
 
866
}
867
 
868
int EDFSTAR_getbudget(LEVEL l, PID p)
869
{
870
 
871
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
872
 
873
  return lev->budget[p];
874
 
875
}
236 giacomo 876
 
792 trimarchi 877
void EDFSTAR_set_nopreemtive_current(LEVEL l) {
878
 
879
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
880
 
881
  lev->flag[lev->activated]|=EDFSTAR_FLAG_NOPREEMPT;
882
}
883
 
884
void EDFSTAR_unset_nopreemtive_current(LEVEL l) {
885
 
886
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
887
 
888
  lev->flag[lev->activated]&=~EDFSTAR_FLAG_NOPREEMPT;
889
}
890
 
236 giacomo 891
int EDFSTAR_budget_has_thread(LEVEL l, int budget)
892
{
893
 
894
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
895
  int i;
896
 
897
  for(i = 0; i< MAX_PROC; i++)
898
    if (lev->budget[i] == budget) return 1;
899
 
900
  return 0;
901
 
902
}