Subversion Repositories shark

Rev

Rev 932 | Rev 980 | 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]);
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
 
932 trimarchi 508
      if (proc_table[p].avail_time > 0) EDFSTAR_account_capacity(lev,p);
509
 
351 giacomo 510
       /* we call guest_end directly here because the same task may
511
         be reinserted in the queue before calling the preemption check! */
512
       level_table[ lev->scheduling_level ]->
513
         private_extract(lev->scheduling_level,p);
514
       lev->activated = NIL;
348 trimarchi 515
 
351 giacomo 516
       iq_extract(p,&lev->ready);
348 trimarchi 517
 
351 giacomo 518
       /* we reset the capacity counters... */
519
       proc_table[p].avail_time = proc_table[p].wcet;
520
 
521
       if (lev->nact[p] > 0) {
221 giacomo 522
 
351 giacomo 523
         #ifdef EDFSTAR_DEBUG
524
           edfstar_printf2("E%d",p);
525
         #endif
221 giacomo 526
 
351 giacomo 527
         /* Pending activation: reactivate the thread!!! */
528
         lev->nact[p]--;
221 giacomo 529
 
351 giacomo 530
         /* see also EDFSTAR_timer_deadline */
531
         kern_gettime(&temp);
221 giacomo 532
 
351 giacomo 533
         EDFSTAR_internal_activate(lev,p, &temp);
221 giacomo 534
 
351 giacomo 535
         /* check if the deadline has already expired */
536
         temp = *iq_query_timespec(p, &lev->ready);
537
         if (TIMESPEC_A_LT_B(&temp, &schedule_time)) {
538
           /* count the deadline miss */
539
           lev->dline_miss[p]++;
540
           kern_event_delete(lev->deadline_timer[p]);
799 giacomo 541
           lev->deadline_timer[p] = NIL;
351 giacomo 542
         }
783 giacomo 543
 
351 giacomo 544
       } else {
221 giacomo 545
 
351 giacomo 546
         #ifdef EDFSTAR_DEBUG
547
           edfstar_printf("e%d",p);
548
         #endif
221 giacomo 549
 
351 giacomo 550
         /* the task has terminated his job before it consume the wcet. All OK! */
799 giacomo 551
         if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC)
552
                proc_table[p].status = SLEEP;
553
         else
554
                proc_table[p].status = EDFSTAR_IDLE;
351 giacomo 555
 
799 giacomo 556
         if (lev->flag[p] & EDFSTAR_FLAG_SPORADIC && lev->deadline_timer[p] != NIL) {
351 giacomo 557
           kern_event_delete(lev->deadline_timer[p]);
799 giacomo 558
           lev->deadline_timer[p] = NIL;
559
         }
221 giacomo 560
 
351 giacomo 561
         /* and finally, a preemption check! */
562
         EDFSTAR_check_preemption(lev);
221 giacomo 563
 
351 giacomo 564
         /* when the deadline timer fire, it recognize the situation and set
565
           correctly all the stuffs (like reactivation, etc... ) */
566
       }
567
 
568
    #ifdef EDFSTAR_DEBUG
569
      edfstar_printf(")");
570
    #endif
783 giacomo 571
 
572
    TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,proc_table[p].context,proc_table[p].task_level);
221 giacomo 573
    jet_update_endcycle(); /* Update the Jet data... */
574
    break;
575
 
576
  default:
876 trimarchi 577
    msg = (STD_command_message *)m;
578
 
579
#ifdef EDFSTAR_DEBUG
580
    edfstar_printf("(E:MSG %d)",msg->command);
581
#endif   
582
 
583
    switch(msg->command) {
584
    case STD_SET_NEW_MODEL:
585
      /* if the EDFSTAR_task_create is called, then the pclass must be a
586
         valid pclass. */
587
      h=(HARD_TASK_MODEL *)(msg->param);
877 trimarchi 588
 
876 trimarchi 589
      /* now we know that m is a valid model */
590
      lev->wcet[p] = h->wcet;
591
      lev->period[p] = h->mit;
877 trimarchi 592
 
593
#ifdef EDFSTAR_DEBUG      
594
      kern_printf("(EDF:NM p%d w%d m%d)", p, h->wcet, h->mit);
595
#endif       
876 trimarchi 596
      lev->flag[p] = 0;
597
      lev->deadline_timer[p] = -1;
598
      lev->dline_miss[p]     = 0;
599
      lev->wcet_miss[p]      = 0;
600
      lev->nact[p]           = 0;
221 giacomo 601
 
876 trimarchi 602
      break;
603
 
604
    case STD_SET_NEW_LEVEL:
605
 
606
      lev->flag[p] |= EDFSTAR_CHANGE_LEVEL;
607
      lev->new_level[p] = (int)(msg->param);
608
 
609
      break;
610
 
611
    case STD_ACTIVATE_TASK:
877 trimarchi 612
#ifdef EDFSTAR_DEBUG
613
      kern_printf("(EDF:SA)");
614
#endif       
876 trimarchi 615
      /* Enable wcet check */
616
      proc_table[p].avail_time = lev->wcet[p];
617
      proc_table[p].wcet       = lev->wcet[p];
618
      proc_table[p].control &= ~CONTROL_CAP;
619
 
620
      EDFSTAR_public_activate(l, p,NULL);
621
 
622
      break;
623
 
624
 
625
    }
626
 
241 giacomo 627
    break;
221 giacomo 628
 
629
  }
630
  return 0;
631
}
632
 
633
static void EDFSTAR_public_end(LEVEL l, PID p)
634
{
635
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
636
 
351 giacomo 637
  #ifdef EDFSTAR_DEBUG
638
    edfstar_printf("(E:end)");
639
  #endif
221 giacomo 640
 
641
  iq_extract(p,&lev->ready);
642
 
643
  /* we finally put the task in the ready queue */
644
  proc_table[p].status = FREE;
645
 
646
  iq_insertfirst(p,&freedesc);
348 trimarchi 647
  lev->activated=NIL;
221 giacomo 648
 
649
  if (lev->deadline_timer[p] != -1) {
650
    kern_event_delete(lev->deadline_timer[p]);
799 giacomo 651
    lev->deadline_timer[p] = NIL;
221 giacomo 652
  }
653
 
654
  /* and finally, a preemption check! (it will also call guest_end) */
655
  EDFSTAR_check_preemption(lev);
656
}
657
 
658
/* Guest Functions
659
   These functions manages a JOB_TASK_MODEL, that is used to put
660
   a guest task in the EDFSTAR ready queue. */
661
 
662
static void EDFSTAR_private_insert(LEVEL l, PID p, TASK_MODEL *m)
663
{
664
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
665
  JOB_TASK_MODEL *job;
666
 
667
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
668
    kern_raise(XINVALID_TASK, p);
669
    return;
670
  }
671
 
672
  job = (JOB_TASK_MODEL *)m;
673
 
674
  /* if the EDFSTAR_guest_create is called, then the pclass must be a
675
     valid pclass. */
676
 
677
  *iq_query_timespec(p, &lev->ready) = job->deadline;
678
 
679
  lev->deadline_timer[p] = -1;
680
  lev->dline_miss[p]     = 0;
681
  lev->wcet_miss[p]      = 0;
682
  lev->nact[p]           = 0;
683
 
684
  if (job->noraiseexc)
685
    lev->flag[p] |= EDFSTAR_FLAG_NORAISEEXC;
686
  else {
687
    lev->flag[p] &= ~EDFSTAR_FLAG_NORAISEEXC;
688
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
689
                                             EDFSTAR_timer_guest_deadline,
690
                                             (void *)p);
691
  }
692
 
693
  lev->period[p] = job->period;
694
 
695
  /* Insert task in the correct position */
696
  iq_timespec_insert(p,&lev->ready);
697
  proc_table[p].status = EDFSTAR_READY;
698
 
699
  /* check for preemption */
700
  EDFSTAR_check_preemption(lev);
701
 
702
  /* there is no bandwidth guarantee at this level, it is performed
703
     by the level that inserts guest tasks... */
704
}
705
 
706
static void EDFSTAR_private_dispatch(LEVEL l, PID p, int nostop)
707
{
708
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
709
 
710
  level_table[ lev->scheduling_level ]->
711
    private_dispatch(lev->scheduling_level,p,nostop);
712
}
713
 
714
static void EDFSTAR_private_epilogue(LEVEL l, PID p)
715
{
716
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
717
 
718
  /* the task has been preempted. it returns into the ready queue... */
719
  level_table[ lev->scheduling_level ]->
720
    private_epilogue(lev->scheduling_level,p);
721
 
722
  proc_table[p].status = EDFSTAR_READY;
723
}
724
 
725
static void EDFSTAR_private_extract(LEVEL l, PID p)
726
{
727
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
728
 
729
#ifdef EDFSTAR_DEBUG
877 trimarchi 730
  kern_printf("EDFSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
221 giacomo 731
#endif
732
 
733
  iq_extract(p, &lev->ready);
734
 
735
  /* we remove the deadline timer, because the slice is finished */
736
  if (lev->deadline_timer[p] != NIL) {
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
    kern_event_delete(lev->deadline_timer[p]);
741
    lev->deadline_timer[p] = NIL;
742
  }
743
 
744
  /* and finally, a preemption check! (it will also call guest_end() */
745
  EDFSTAR_check_preemption(lev);
746
}
747
 
748
/* Registration functions */
749
 
750
/* Registration function:
751
    int flags                 the init flags ... see EDFSTAR.h */
752
 
753
LEVEL EDFSTAR_register_level(int master)
754
{
755
  LEVEL l;            /* the level that we register */
756
  EDFSTAR_level_des *lev;  /* for readableness only */
757
  PID i;              /* a counter */
758
 
759
#ifdef EDFSTAR_DEBUG
760
  printk("EDFSTAR_register_level\n");
761
#endif
762
 
763
  /* request an entry in the level_table */
764
  l = level_alloc_descriptor(sizeof(EDFSTAR_level_des));
765
 
766
  lev = (EDFSTAR_level_des *)level_table[l];
767
 
768
  /* fill the standard descriptor */
769
  lev->l.private_insert   = EDFSTAR_private_insert;
770
  lev->l.private_extract  = EDFSTAR_private_extract;
771
  lev->l.private_dispatch = EDFSTAR_private_dispatch;
772
  lev->l.private_epilogue = EDFSTAR_private_epilogue;
773
 
774
  lev->l.public_guarantee = NULL;
775
  lev->l.public_eligible  = EDFSTAR_public_eligible;
776
  lev->l.public_create    = EDFSTAR_public_create;
777
  lev->l.public_end       = EDFSTAR_public_end;
778
  lev->l.public_dispatch  = EDFSTAR_public_dispatch;
779
  lev->l.public_epilogue  = EDFSTAR_public_epilogue;
780
  lev->l.public_activate  = EDFSTAR_public_activate;
781
  lev->l.public_unblock   = EDFSTAR_public_unblock;
782
  lev->l.public_block     = EDFSTAR_public_block;
783
  lev->l.public_message   = EDFSTAR_public_message;
784
 
785
  /* fill the EDFSTAR descriptor part */
786
  for(i=0; i<MAX_PROC; i++) {
787
    lev->period[i]         = 0;
788
    lev->deadline_timer[i] = -1;
789
    lev->flag[i]           = 0;
790
    lev->dline_miss[i]     = 0;
791
    lev->wcet_miss[i]      = 0;
792
    lev->nact[i]           = 0;
793
    lev->budget[i]         = NIL;
880 trimarchi 794
    lev->new_level[i]      = -1;
221 giacomo 795
  }
796
 
797
  iq_init(&lev->ready, NULL, IQUEUE_NO_PRIORITY);
798
  lev->activated = NIL;
799
 
800
  lev->scheduling_level = master;
298 giacomo 801
  lev->cap_lev = NIL;
296 trimarchi 802
  NULL_TIMESPEC(&lev->cap_lasttime);
221 giacomo 803
 
804
  return l;
805
}
806
 
807
int EDFSTAR_get_dline_miss(PID p)
808
{
809
  LEVEL l = proc_table[p].task_level;
810
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
811
 
812
  return lev->dline_miss[p];
813
}
814
 
815
int EDFSTAR_get_wcet_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->wcet_miss[p];
821
}
822
 
823
int EDFSTAR_get_nact(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->nact[p];
829
}
830
 
831
int EDFSTAR_reset_dline_miss(PID p)
832
{
833
  LEVEL l = proc_table[p].task_level;
834
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
835
 
836
  lev->dline_miss[p] = 0;
837
  return 0;
838
}
839
 
840
int EDFSTAR_reset_wcet_miss(PID p)
841
{
842
  LEVEL l = proc_table[p].task_level;
843
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
844
 
845
  lev->wcet_miss[p] = 0;
846
  return 0;
847
}
848
 
849
int EDFSTAR_setbudget(LEVEL l, PID p, int budget)
850
{
851
 
852
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
853
 
854
  lev->budget[p] = budget;
855
 
856
  return 0;
857
 
858
}
859
 
860
int EDFSTAR_getbudget(LEVEL l, PID p)
861
{
862
 
863
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
864
 
865
  return lev->budget[p];
866
 
867
}
236 giacomo 868
 
792 trimarchi 869
void EDFSTAR_set_nopreemtive_current(LEVEL l) {
870
 
871
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
872
 
873
  lev->flag[lev->activated]|=EDFSTAR_FLAG_NOPREEMPT;
874
}
875
 
876
void EDFSTAR_unset_nopreemtive_current(LEVEL l) {
877
 
878
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
879
 
880
  lev->flag[lev->activated]&=~EDFSTAR_FLAG_NOPREEMPT;
881
}
882
 
236 giacomo 883
int EDFSTAR_budget_has_thread(LEVEL l, int budget)
884
{
885
 
886
  EDFSTAR_level_des *lev = (EDFSTAR_level_des *)(level_table[l]);
887
  int i;
888
 
889
  for(i = 0; i< MAX_PROC; i++)
890
    if (lev->budget[i] == budget) return 1;
891
 
892
  return 0;
893
 
894
}