Subversion Repositories shark

Rev

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