Subversion Repositories shark

Rev

Rev 931 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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