Subversion Repositories shark

Rev

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