Subversion Repositories shark

Rev

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