Subversion Repositories shark

Rev

Rev 876 | Rev 880 | 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"
876 trimarchi 57
#include <modules/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
{
141
  PID first;
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:
351 giacomo 208
      #ifdef EDFSTAR_DEBUG
209
        kern_printf("(E:Dl:%d)",p);
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]);
252
 
253
    EDFSTAR_check_preemption(lev);
254
 
255
    lev->nact[p] = 0;
256
    lev->budget[p] = -1;
257
    proc_table[p].task_level = lev->new_level[p];
258
 
259
    /* Send change level command to local scheduler */
260
 
261
    msg.command = STD_ACTIVATE_TASK;
262
    msg.param = NULL;
263
 
264
    level_table[ lev->new_level[p] ]->public_message(lev->new_level[p],p,&msg);
265
 
266
    return 1;
267
 
268
  }
269
 
270
  return 0;
271
 
272
}
273
 
274
 
221 giacomo 275
static void EDFSTAR_timer_guest_deadline(void *par)
276
{
277
  PID p = (PID) par;
278
 
351 giacomo 279
  #ifdef EDFSTAR_DEBUG
280
    edfstar_printf("(E:gdl)");
281
  #endif
221 giacomo 282
 
283
  kern_raise(XDEADLINE_MISS,p);
284
}
285
 
286
static int EDFSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
287
{
288
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
289
 
290
  /* if the EDFSTAR_task_create is called, then the pclass must be a
291
     valid pclass. */
292
  HARD_TASK_MODEL *h;
293
 
294
  if (m->pclass != HARD_PCLASS) return -1;
295
  if (m->level != 0 && m->level != l) return -1;
296
  h = (HARD_TASK_MODEL *)m;
351 giacomo 297
  if (!h->wcet || !h->mit) return -1;
221 giacomo 298
  /* now we know that m is a valid model */
299
 
351 giacomo 300
  #ifdef EDFSTAR_DEBUG
301
    edfstar_printf("(E:Crt)");
302
  #endif
221 giacomo 303
 
304
  lev->period[p] = h->mit;
305
 
306
  lev->flag[p] = 0;
348 trimarchi 307
 
308
  if (h->periodicity == APERIODIC)
309
       lev->flag[p] |= EDFSTAR_FLAG_SPORADIC;
310
 
221 giacomo 311
  lev->deadline_timer[p] = -1;
312
  lev->dline_miss[p]     = 0;
313
  lev->wcet_miss[p]      = 0;
314
  lev->nact[p]           = 0;
315
 
316
  /* Enable wcet check */
317
  proc_table[p].avail_time = h->wcet;
318
  proc_table[p].wcet       = h->wcet;
319
 
320
  return 0; /* OK, also if the task cannot be guaranteed... */
321
}
322
 
296 trimarchi 323
 
324
static void EDFSTAR_account_capacity(EDFSTAR_level_des *lev, PID p)
325
{
326
  struct timespec ty;
327
  TIME tx;
328
 
329
  SUBTIMESPEC(&schedule_time, &lev->cap_lasttime, &ty);
330
  tx = TIMESPEC2USEC(&ty);
331
 
332
  proc_table[p].avail_time -= tx;
351 giacomo 333
}
296 trimarchi 334
 
221 giacomo 335
static int EDFSTAR_public_eligible(LEVEL l, PID p)
336
{
337
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
338
 
351 giacomo 339
  #ifdef EDFSTAR_DEBUG
877 trimarchi 340
    edfstar_printf2("(E:eli:%d)",p);
351 giacomo 341
  #endif
221 giacomo 342
 
343
  return level_table[ lev->scheduling_level ]->
344
    private_eligible(lev->scheduling_level,p);
351 giacomo 345
 
221 giacomo 346
}
347
 
348
static void EDFSTAR_public_dispatch(LEVEL l, PID p, int nostop)
349
{
350
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
296 trimarchi 351
  struct timespec ty;
221 giacomo 352
 
351 giacomo 353
  #ifdef EDFSTAR_DEBUG
354
    edfstar_printf("(E:dis)");
355
  #endif
356
 
296 trimarchi 357
  if (!nostop || proc_table[exec].task_level==l) {
358
      TIMESPEC_ASSIGN(&ty, &schedule_time);
359
      TIMESPEC_ASSIGN(&lev->cap_lasttime, &schedule_time);
221 giacomo 360
 
296 trimarchi 361
      /* ...and finally, we have to post a capacity event on exec task because the shadow_task consume
362
       *      *        capacity on exe task always */
784 giacomo 363
      if (proc_table[exec].avail_time > 0) {
364
        ADDUSEC2TIMESPEC(proc_table[exec].avail_time ,&ty);
365
        lev->cap_lev = kern_event_post(&ty,capacity_handler, lev);
366
      }
296 trimarchi 367
      level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop);
368
  }
369
  else
370
      level_table[proc_table[exec].task_level]->public_dispatch(proc_table[exec].task_level, p, nostop);
371
 
221 giacomo 372
}
373
 
374
static void EDFSTAR_public_epilogue(LEVEL l, PID p)
375
{
376
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
377
 
351 giacomo 378
  #ifdef EDFSTAR_DEBUG
379
    edfstar_printf("(E:epi ");
380
  #endif
221 giacomo 381
 
296 trimarchi 382
  if (lev->cap_lev!=NIL) {
383
       kern_event_delete(lev->cap_lev);
384
       lev->cap_lev=NIL;
385
  }
386
 
876 trimarchi 387
 
296 trimarchi 388
  if ( proc_table[exec].task_level==l ) {
389
 
785 giacomo 390
     if (proc_table[exec].avail_time > 0) EDFSTAR_account_capacity(lev,exec);
296 trimarchi 391
 
876 trimarchi 392
     if (EDFSTAR_private_change_level(l, p)) return;
393
 
296 trimarchi 394
     /* check if the wcet is finished... */
784 giacomo 395
     if (proc_table[exec].avail_time < 0) {
296 trimarchi 396
        /* wcet finished: disable wcet event and count wcet miss */
351 giacomo 397
 
398
       #ifdef EDFSTAR_DEBUG
399
         edfstar_printf2("W%d",p);
400
       #endif
296 trimarchi 401
        //proc_table[p].control &= ~CONTROL_CAP;
402
        lev->wcet_miss[exec]++;
784 giacomo 403
        proc_table[exec].avail_time = 0;
404
        TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation,proc_table[exec].context,proc_table[exec].task_level);
296 trimarchi 405
     }
221 giacomo 406
 
351 giacomo 407
     #ifdef EDFSTAR_DEBUG
408
       edfstar_printf(")");
409
     #endif
221 giacomo 410
 
351 giacomo 411
     level_table[ lev->scheduling_level ]->
412
       private_epilogue(lev->scheduling_level,p);
413
 
414
     proc_table[exec].status = EDFSTAR_READY;
296 trimarchi 415
    } else
416
        level_table[proc_table[exec].task_level]->public_epilogue(proc_table[exec].task_level,p);
417
 
221 giacomo 418
}
419
 
783 giacomo 420
static void EDFSTAR_public_activate(LEVEL l, PID p, struct timespec *o)
221 giacomo 421
{
422
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
423
  struct timespec t;
424
 
425
  #ifdef EDFSTAR_DEBUG
426
    edfstar_printf("(E:act:%d)",p);
427
  #endif
428
 
429
  /* Test if we are trying to activate a non sleeping task    */
430
  /* save activation (only if needed... */
431
  if (proc_table[p].status != SLEEP) {
432
    /* a periodic task cannot be activated when it is already active */
348 trimarchi 433
    /* but aperiodic task can be reactivate before */
802 giacomo 434
    if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC) {
435
        if (proc_table[p].status != EDFSTAR_IDLE) {
436
          lev->nact[p]++;
437
          return;
438
        }
439
    } else {
440
        kern_raise(XACTIVATION,p);
351 giacomo 441
    }
221 giacomo 442
  }
443
 
444
  kern_gettime(&t);
445
 
446
  EDFSTAR_internal_activate(lev,p, &t);
447
 
448
  /* Set the deadline timer */
449
  lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
450
                                           EDFSTAR_timer_deadline,
451
                                           (void *)p);
452
 
453
}
454
 
455
static void EDFSTAR_public_unblock(LEVEL l, PID p)
456
{
457
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
458
 
351 giacomo 459
  #ifdef EDFSTAR_DEBUG
460
    edfstar_printf("(E:ins)");
461
  #endif
221 giacomo 462
 
351 giacomo 463
  /* Insert task in the correct position */
464
  proc_table[p].status = EDFSTAR_READY;
465
  iq_timespec_insert(p,&lev->ready);
221 giacomo 466
 
351 giacomo 467
  /* and check for preemption! */
468
  EDFSTAR_check_preemption(lev);
469
 
221 giacomo 470
}
471
 
472
static void EDFSTAR_public_block(LEVEL l, PID p)
473
{
474
 
351 giacomo 475
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
221 giacomo 476
 
351 giacomo 477
  #ifdef EDFSTAR_DEBUG
478
   edfstar_printf("(E:ext)");
479
  #endif
221 giacomo 480
 
351 giacomo 481
  /* the task is blocked on a synchronization primitive. we have to
482
     remove it from the master module -and- from the local queue! */
483
  iq_extract(p,&lev->ready);
484
 
485
  /* and finally, a preemption check! (it will also call guest_end) */
486
  EDFSTAR_check_preemption(lev);
221 giacomo 487
}
488
 
489
static int EDFSTAR_public_message(LEVEL l, PID p, void *m)
490
{
491
 
351 giacomo 492
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
493
  struct timespec temp;
876 trimarchi 494
  STD_command_message *msg;
495
  HARD_TASK_MODEL *h;
221 giacomo 496
 
351 giacomo 497
  #ifdef EDFSTAR_DEBUG
498
    edfstar_printf("(E:ecy ");
499
  #endif
221 giacomo 500
 
351 giacomo 501
  switch ((long)(m)) {
221 giacomo 502
 
351 giacomo 503
    /* Task EndCycle */
504
    case (long)(NULL):
348 trimarchi 505
 
876 trimarchi 506
      if (EDFSTAR_private_change_level(l,p)) return 0;
507
 
351 giacomo 508
       /* we call guest_end directly here because the same task may
509
         be reinserted in the queue before calling the preemption check! */
510
       level_table[ lev->scheduling_level ]->
511
         private_extract(lev->scheduling_level,p);
512
       lev->activated = NIL;
348 trimarchi 513
 
351 giacomo 514
       iq_extract(p,&lev->ready);
348 trimarchi 515
 
351 giacomo 516
       /* we reset the capacity counters... */
517
       proc_table[p].avail_time = proc_table[p].wcet;
518
 
519
       if (lev->nact[p] > 0) {
221 giacomo 520
 
351 giacomo 521
         #ifdef EDFSTAR_DEBUG
522
           edfstar_printf2("E%d",p);
523
         #endif
221 giacomo 524
 
351 giacomo 525
         /* Pending activation: reactivate the thread!!! */
526
         lev->nact[p]--;
221 giacomo 527
 
351 giacomo 528
         /* see also EDFSTAR_timer_deadline */
529
         kern_gettime(&temp);
221 giacomo 530
 
351 giacomo 531
         EDFSTAR_internal_activate(lev,p, &temp);
221 giacomo 532
 
351 giacomo 533
         /* check if the deadline has already expired */
534
         temp = *iq_query_timespec(p, &lev->ready);
535
         if (TIMESPEC_A_LT_B(&temp, &schedule_time)) {
536
           /* count the deadline miss */
537
           lev->dline_miss[p]++;
538
           kern_event_delete(lev->deadline_timer[p]);
799 giacomo 539
           lev->deadline_timer[p] = NIL;
351 giacomo 540
         }
783 giacomo 541
 
351 giacomo 542
       } else {
221 giacomo 543
 
351 giacomo 544
         #ifdef EDFSTAR_DEBUG
545
           edfstar_printf("e%d",p);
546
         #endif
221 giacomo 547
 
351 giacomo 548
         /* the task has terminated his job before it consume the wcet. All OK! */
799 giacomo 549
         if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC)
550
                proc_table[p].status = SLEEP;
551
         else
552
                proc_table[p].status = EDFSTAR_IDLE;
351 giacomo 553
 
799 giacomo 554
         if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC && lev->deadline_timer[p] != NIL) {
351 giacomo 555
           kern_event_delete(lev->deadline_timer[p]);
799 giacomo 556
           lev->deadline_timer[p] = NIL;
557
         }
221 giacomo 558
 
351 giacomo 559
         /* and finally, a preemption check! */
560
         EDFSTAR_check_preemption(lev);
221 giacomo 561
 
351 giacomo 562
         /* when the deadline timer fire, it recognize the situation and set
563
           correctly all the stuffs (like reactivation, etc... ) */
564
       }
565
 
566
    #ifdef EDFSTAR_DEBUG
567
      edfstar_printf(")");
568
    #endif
783 giacomo 569
 
570
    TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,proc_table[p].context,proc_table[p].task_level);
221 giacomo 571
    jet_update_endcycle(); /* Update the Jet data... */
572
    break;
573
 
574
  default:
876 trimarchi 575
    msg = (STD_command_message *)m;
576
 
577
#ifdef EDFSTAR_DEBUG
578
    edfstar_printf("(E:MSG %d)",msg->command);
579
#endif   
580
 
581
    switch(msg->command) {
582
    case STD_SET_NEW_MODEL:
583
      /* if the EDFSTAR_task_create is called, then the pclass must be a
584
         valid pclass. */
585
      h=(HARD_TASK_MODEL *)(msg->param);
877 trimarchi 586
 
876 trimarchi 587
      /* now we know that m is a valid model */
588
      lev->wcet[p] = h->wcet;
589
      lev->period[p] = h->mit;
877 trimarchi 590
 
591
#ifdef EDFSTAR_DEBUG      
592
      kern_printf("(EDF:NM p%d w%d m%d)", p, h->wcet, h->mit);
593
#endif       
876 trimarchi 594
      lev->flag[p] = 0;
595
      lev->deadline_timer[p] = -1;
596
      lev->dline_miss[p]     = 0;
597
      lev->wcet_miss[p]      = 0;
598
      lev->nact[p]           = 0;
221 giacomo 599
 
876 trimarchi 600
      break;
601
 
602
    case STD_SET_NEW_LEVEL:
603
 
604
      lev->flag[p] |= EDFSTAR_CHANGE_LEVEL;
605
      lev->new_level[p] = (int)(msg->param);
606
 
607
      break;
608
 
609
    case STD_ACTIVATE_TASK:
877 trimarchi 610
#ifdef EDFSTAR_DEBUG
611
      kern_printf("(EDF:SA)");
612
#endif       
876 trimarchi 613
      /* Enable wcet check */
614
      proc_table[p].avail_time = lev->wcet[p];
615
      proc_table[p].wcet       = lev->wcet[p];
616
      proc_table[p].control &= ~CONTROL_CAP;
617
 
618
      EDFSTAR_public_activate(l, p,NULL);
619
 
620
      break;
621
 
622
 
623
    }
624
 
241 giacomo 625
    break;
221 giacomo 626
 
627
  }
628
  return 0;
629
}
630
 
631
static void EDFSTAR_public_end(LEVEL l, PID p)
632
{
633
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
634
 
351 giacomo 635
  #ifdef EDFSTAR_DEBUG
636
    edfstar_printf("(E:end)");
637
  #endif
221 giacomo 638
 
639
  iq_extract(p,&lev->ready);
640
 
641
  /* we finally put the task in the ready queue */
642
  proc_table[p].status = FREE;
643
 
644
  iq_insertfirst(p,&freedesc);
348 trimarchi 645
  lev->activated=NIL;
221 giacomo 646
 
647
  if (lev->deadline_timer[p] != -1) {
648
    kern_event_delete(lev->deadline_timer[p]);
799 giacomo 649
    lev->deadline_timer[p] = NIL;
221 giacomo 650
  }
651
 
652
  /* and finally, a preemption check! (it will also call guest_end) */
653
  EDFSTAR_check_preemption(lev);
654
}
655
 
656
/* Guest Functions
657
   These functions manages a JOB_TASK_MODEL, that is used to put
658
   a guest task in the EDFSTAR ready queue. */
659
 
660
static void EDFSTAR_private_insert(LEVEL l, PID p, TASK_MODEL *m)
661
{
662
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
663
  JOB_TASK_MODEL *job;
664
 
665
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
666
    kern_raise(XINVALID_TASK, p);
667
    return;
668
  }
669
 
670
  job = (JOB_TASK_MODEL *)m;
671
 
672
  /* if the EDFSTAR_guest_create is called, then the pclass must be a
673
     valid pclass. */
674
 
675
  *iq_query_timespec(p, &lev->ready) = job->deadline;
676
 
677
  lev->deadline_timer[p] = -1;
678
  lev->dline_miss[p]     = 0;
679
  lev->wcet_miss[p]      = 0;
680
  lev->nact[p]           = 0;
681
 
682
  if (job->noraiseexc)
683
    lev->flag[p] |= EDFSTAR_FLAG_NORAISEEXC;
684
  else {
685
    lev->flag[p] &= ~EDFSTAR_FLAG_NORAISEEXC;
686
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
687
                                             EDFSTAR_timer_guest_deadline,
688
                                             (void *)p);
689
  }
690
 
691
  lev->period[p] = job->period;
692
 
693
  /* Insert task in the correct position */
694
  iq_timespec_insert(p,&lev->ready);
695
  proc_table[p].status = EDFSTAR_READY;
696
 
697
  /* check for preemption */
698
  EDFSTAR_check_preemption(lev);
699
 
700
  /* there is no bandwidth guarantee at this level, it is performed
701
     by the level that inserts guest tasks... */
702
}
703
 
704
static void EDFSTAR_private_dispatch(LEVEL l, PID p, int nostop)
705
{
706
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
707
 
708
  level_table[ lev->scheduling_level ]->
709
    private_dispatch(lev->scheduling_level,p,nostop);
710
}
711
 
712
static void EDFSTAR_private_epilogue(LEVEL l, PID p)
713
{
714
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
715
 
716
  /* the task has been preempted. it returns into the ready queue... */
717
  level_table[ lev->scheduling_level ]->
718
    private_epilogue(lev->scheduling_level,p);
719
 
720
  proc_table[p].status = EDFSTAR_READY;
721
}
722
 
723
static void EDFSTAR_private_extract(LEVEL l, PID p)
724
{
725
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
726
 
727
#ifdef EDFSTAR_DEBUG
877 trimarchi 728
  kern_printf("EDFSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
221 giacomo 729
#endif
730
 
731
  iq_extract(p, &lev->ready);
732
 
733
  /* we remove the deadline timer, because the slice is finished */
734
  if (lev->deadline_timer[p] != NIL) {
735
#ifdef EDFSTAR_DEBUG
877 trimarchi 736
    kern_printf("EDFSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
221 giacomo 737
#endif
738
    kern_event_delete(lev->deadline_timer[p]);
739
    lev->deadline_timer[p] = NIL;
740
  }
741
 
742
  /* and finally, a preemption check! (it will also call guest_end() */
743
  EDFSTAR_check_preemption(lev);
744
}
745
 
746
/* Registration functions */
747
 
748
/* Registration function:
749
    int flags                 the init flags ... see EDFSTAR.h */
750
 
751
LEVEL EDFSTAR_register_level(int master)
752
{
753
  LEVEL l;            /* the level that we register */
754
  EDFSTAR_level_des *lev;  /* for readableness only */
755
  PID i;              /* a counter */
756
 
757
#ifdef EDFSTAR_DEBUG
758
  printk("EDFSTAR_register_level\n");
759
#endif
760
 
761
  /* request an entry in the level_table */
762
  l = level_alloc_descriptor(sizeof(EDFSTAR_level_des));
763
 
764
  lev = (EDFSTAR_level_des *)level_table[l];
765
 
766
  /* fill the standard descriptor */
767
  lev->l.private_insert   = EDFSTAR_private_insert;
768
  lev->l.private_extract  = EDFSTAR_private_extract;
769
  lev->l.private_dispatch = EDFSTAR_private_dispatch;
770
  lev->l.private_epilogue = EDFSTAR_private_epilogue;
771
 
772
  lev->l.public_guarantee = NULL;
773
  lev->l.public_eligible  = EDFSTAR_public_eligible;
774
  lev->l.public_create    = EDFSTAR_public_create;
775
  lev->l.public_end       = EDFSTAR_public_end;
776
  lev->l.public_dispatch  = EDFSTAR_public_dispatch;
777
  lev->l.public_epilogue  = EDFSTAR_public_epilogue;
778
  lev->l.public_activate  = EDFSTAR_public_activate;
779
  lev->l.public_unblock   = EDFSTAR_public_unblock;
780
  lev->l.public_block     = EDFSTAR_public_block;
781
  lev->l.public_message   = EDFSTAR_public_message;
782
 
783
  /* fill the EDFSTAR descriptor part */
784
  for(i=0; i<MAX_PROC; i++) {
785
    lev->period[i]         = 0;
786
    lev->deadline_timer[i] = -1;
787
    lev->flag[i]           = 0;
788
    lev->dline_miss[i]     = 0;
789
    lev->wcet_miss[i]      = 0;
790
    lev->nact[i]           = 0;
791
    lev->budget[i]         = NIL;
792
  }
793
 
794
  iq_init(&lev->ready, NULL, IQUEUE_NO_PRIORITY);
795
  lev->activated = NIL;
796
 
797
  lev->scheduling_level = master;
298 giacomo 798
  lev->cap_lev = NIL;
296 trimarchi 799
  NULL_TIMESPEC(&lev->cap_lasttime);
221 giacomo 800
 
801
  return l;
802
}
803
 
804
int EDFSTAR_get_dline_miss(PID p)
805
{
806
  LEVEL l = proc_table[p].task_level;
807
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
808
 
809
  return lev->dline_miss[p];
810
}
811
 
812
int EDFSTAR_get_wcet_miss(PID p)
813
{
814
  LEVEL l = proc_table[p].task_level;
815
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
816
 
817
  return lev->wcet_miss[p];
818
}
819
 
820
int EDFSTAR_get_nact(PID p)
821
{
822
  LEVEL l = proc_table[p].task_level;
823
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
824
 
825
  return lev->nact[p];
826
}
827
 
828
int EDFSTAR_reset_dline_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->dline_miss[p] = 0;
834
  return 0;
835
}
836
 
837
int EDFSTAR_reset_wcet_miss(PID p)
838
{
839
  LEVEL l = proc_table[p].task_level;
840
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
841
 
842
  lev->wcet_miss[p] = 0;
843
  return 0;
844
}
845
 
846
int EDFSTAR_setbudget(LEVEL l, PID p, int budget)
847
{
848
 
849
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
850
 
851
  lev->budget[p] = budget;
852
 
853
  return 0;
854
 
855
}
856
 
857
int EDFSTAR_getbudget(LEVEL l, PID p)
858
{
859
 
860
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
861
 
862
  return lev->budget[p];
863
 
864
}
236 giacomo 865
 
792 trimarchi 866
void EDFSTAR_set_nopreemtive_current(LEVEL l) {
867
 
868
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
869
 
870
  lev->flag[lev->activated]|=EDFSTAR_FLAG_NOPREEMPT;
871
}
872
 
873
void EDFSTAR_unset_nopreemtive_current(LEVEL l) {
874
 
875
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
876
 
877
  lev->flag[lev->activated]&=~EDFSTAR_FLAG_NOPREEMPT;
878
}
879
 
236 giacomo 880
int EDFSTAR_budget_has_thread(LEVEL l, int budget)
881
{
882
 
883
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
884
  int i;
885
 
886
  for(i = 0; i< MAX_PROC; i++)
887
    if (lev->budget[i] == budget) return 1;
888
 
889
  return 0;
890
 
891
}