Subversion Repositories shark

Rev

Rev 241 | Rev 355 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
235 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 "rmstar.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
#include <kernel/trace.h>
46
 
47
 
48
#define RMSTAR_CHANGE_LEVEL 1
49
 
50
/* for iqueues */
51
/* #include "iqueue.h" Now iqueues are the only queue type into the kernel */
52
 
53
/* for BUDGET_TASK_MODEL */
241 giacomo 54
#include "fsf_server.h"
235 giacomo 55
 
56
/*
57
 * DEBUG stuffs begin
58
 */
59
 
60
//#define RMSTAR_DEBUG
61
 
62
#ifdef RMSTAR_DEBUG
63
 
64
static __inline__ fake_printf(char *fmt, ...) {}
65
 
66
#define rmstar_printf fake_printf
67
#define rmstar_printf2 fake_printf
68
#define rmstar_printf3 fake_printf
69
 
70
//#define rmstar_printf kern_printf
71
//#define rmstar_printf2 kern_printf
72
//#define rmstar_printf3 kern_printf
73
#endif
74
 
75
/*
76
 * DEBUG stuffs end
77
 */
78
 
79
/* Status used in the level */
80
#define RMSTAR_READY         MODULE_STATUS_BASE    /* - Ready status        */
81
#define RMSTAR_IDLE          MODULE_STATUS_BASE+4  /* to wait the deadline  */
82
 
83
/* flags */
84
#define RMSTAR_FLAG_NORAISEEXC  2
85
 
86
/* the level redefinition for the Earliest Deadline First level */
87
typedef struct {
88
  level_des l;     /* the standard level descriptor          */
89
 
90
  TIME period[MAX_PROC]; /* The task periods; the deadlines are
91
                       stored in the priority field           */
92
  int deadline_timer[MAX_PROC];
93
                   /* The task deadline timers               */
94
 
95
  struct timespec deadline_timespec[MAX_PROC];
96
 
97
  int dline_miss[MAX_PROC]; /* Deadline miss counter */
98
  int wcet_miss[MAX_PROC];  /* Wcet miss counter */
99
 
100
  int nact[MAX_PROC];       /* Wcet miss counter */
101
 
102
  int flag[MAX_PROC];
103
                   /* used to manage the JOB_TASK_MODEL and the
104
                       periodicity                            */
105
 
106
  IQUEUE ready;     /* the ready queue                        */
107
 
108
  PID activated;   /* the task that has been inserted into the
109
                       master module */
110
 
111
  int budget[MAX_PROC];
112
 
113
  int scheduling_level;
114
 
115
} RMSTAR_level_des;
116
 
117
static void RMSTAR_check_preemption(RMSTAR_level_des *lev)
118
{
119
  PID first;
120
 
121
#ifdef RMSTAR_DEBUG
122
  rmstar_printf("(E:chk)");
123
#endif
124
 
125
  if ((first = iq_query_first(&lev->ready)) != lev->activated) {
126
    if (lev->activated != NIL)
127
      level_table[ lev->scheduling_level ]->
128
        private_extract(lev->scheduling_level, lev->activated);
129
 
130
    lev->activated = first;
131
 
132
    if (first != NIL) {
133
      BUDGET_TASK_MODEL b;
134
      budget_task_default_model(b, lev->budget[first]);
135
 
136
      level_table[ lev->scheduling_level ]->
137
        private_insert(lev->scheduling_level, first, (TASK_MODEL *)&b);
138
    }
139
  }
140
}
141
 
142
static void RMSTAR_timer_deadline(void *par);
143
 
144
static void RMSTAR_internal_activate(RMSTAR_level_des *lev, PID p,
145
                                     struct timespec *t)
146
{
147
#ifdef RMSTAR_DEBUG
148
  rmstar_printf("(E:iact)");
149
#endif
150
 
151
  ADDUSEC2TIMESPEC(lev->period[p], t);
152
 
153
  *iq_query_timespec(p, &lev->ready) = *t;
154
  lev->deadline_timespec[p] = *t;
155
 
156
  /* Insert task in the correct position */
157
  proc_table[p].status = RMSTAR_READY;
158
  iq_priority_insert(p,&lev->ready);
159
 
160
  /* needed because when there is a wcet miss I disable CONTROL_CAP */
161
  proc_table[p].control |= CONTROL_CAP;
162
 
163
  /* check for preemption */
164
  RMSTAR_check_preemption(lev);
165
}
166
 
167
static void RMSTAR_timer_deadline(void *par)
168
{
169
  PID p = (PID) par;
170
  RMSTAR_level_des *lev;
171
 
172
#ifdef RMSTAR_DEBUG
173
//  rmstar_printf("(E:tdl ");
174
#endif
175
 
176
  lev = (RMSTAR_level_des *)level_table[proc_table[p].task_level];
177
 
178
  switch (proc_table[p].status) {
179
    case RMSTAR_IDLE:
180
#ifdef RMSTAR_DEBUG
181
//      rmstar_printf2("I%d",p);
182
#endif
183
      /* set the request time */
184
      RMSTAR_internal_activate(lev,p,iq_query_timespec(p, &lev->ready));
185
 
186
      event_need_reschedule();
187
      break;
188
 
189
    default:
190
#ifdef RMSTAR_DEBUG
191
//      rmstar_printf2("D%d",p);
192
#endif
193
      /* else, a deadline miss occurred!!! */
194
      lev->dline_miss[p]++;
195
 
196
      /* the task is into another state */
197
      lev->nact[p]++;
198
 
199
      /* Set the deadline timer */
200
      ADDUSEC2TIMESPEC(lev->period[p], &lev->deadline_timespec[p]);
201
  }
202
 
203
  /* Set the deadline timer */
204
  lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
205
                                           RMSTAR_timer_deadline,
206
                                           (void *)p);
207
 
208
#ifdef RMSTAR_DEBUG
209
//  rmstar_printf(")");
210
#endif
211
}
212
 
213
static void RMSTAR_timer_guest_deadline(void *par)
214
{
215
  PID p = (PID) par;
216
 
217
#ifdef RMSTAR_DEBUG
218
  rmstar_printf("(E:gdl)");
219
#endif
220
 
221
  kern_raise(XDEADLINE_MISS,p);
222
}
223
 
224
static int RMSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
225
{
226
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
227
 
228
  /* if the RMSTAR_task_create is called, then the pclass must be a
229
     valid pclass. */
230
  HARD_TASK_MODEL *h;
231
 
232
  if (m->pclass != HARD_PCLASS) return -1;
233
  if (m->level != 0 && m->level != l) return -1;
234
  h = (HARD_TASK_MODEL *)m;
235
  if (!h->wcet || !h->mit || h->periodicity != PERIODIC) return -1;
236
  /* now we know that m is a valid model */
237
 
238
#ifdef RMSTAR_DEBUG
239
  rmstar_printf("(E:tcr)");
240
#endif
241
 
242
  lev->period[p] = h->mit;
243
  *iq_query_priority(p, &lev->ready) = h->mit;
244
 
245
  lev->flag[p] = 0;
246
  lev->deadline_timer[p] = -1;
247
  lev->dline_miss[p]     = 0;
248
  lev->wcet_miss[p]      = 0;
249
  lev->nact[p]           = 0;
250
 
251
  /* Enable wcet check */
252
  proc_table[p].avail_time = h->wcet;
253
  proc_table[p].wcet       = h->wcet;
254
  proc_table[p].control |= CONTROL_CAP;
255
 
256
  return 0; /* OK, also if the task cannot be guaranteed... */
257
}
258
 
259
static void RMSTAR_public_dispatch(LEVEL l, PID p, int nostop)
260
{
261
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
262
 
263
#ifdef RMSTAR_DEBUG
264
  rmstar_printf("(E:dis)");
265
 
266
  rmstar_printf3("(%d %d)",
267
                  iq_query_timespec(p, &lev->ready)->tv_nsec/1000000,
268
                  schedule_time.tv_nsec/1000000);
269
#endif
270
 
271
  level_table[ lev->scheduling_level ]->
272
    private_dispatch(lev->scheduling_level,p,nostop);
273
}
274
 
275
static void RMSTAR_public_epilogue(LEVEL l, PID p)
276
{
277
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
278
 
279
#ifdef RMSTAR_DEBUG
280
  rmstar_printf("(E:epi ");
281
#endif
282
 
283
  /* check if the wcet is finished... */
284
  if (proc_table[p].avail_time <= 0 && proc_table[p].control&CONTROL_CAP) {
285
    /* wcet finished: disable wcet event and count wcet miss */
286
#ifdef RMSTAR_DEBUG
287
    rmstar_printf2("W%d",p);
288
#endif
289
    proc_table[p].control &= ~CONTROL_CAP;
290
    lev->wcet_miss[p]++;
291
  }
292
#ifdef RMSTAR_DEBUG
293
  rmstar_printf(")");
294
#endif
295
 
296
  level_table[ lev->scheduling_level ]->
297
    private_epilogue(lev->scheduling_level,p);
298
 
299
  proc_table[p].status = RMSTAR_READY;
300
}
301
 
302
static void RMSTAR_public_activate(LEVEL l, PID p)
303
{
304
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
305
  struct timespec t;
306
 
307
#ifdef RMSTAR_DEBUG
308
  rmstar_printf("(E:act)");
309
#endif
310
 
311
  /* Test if we are trying to activate a non sleeping task    */
312
  /* save activation (only if needed... */
313
  if (proc_table[p].status != SLEEP) {
314
    /* a periodic task cannot be activated when it is already active */
315
    kern_raise(XACTIVATION,p);
316
    return;
317
  }
318
 
319
  kern_gettime(&t);
320
 
321
  RMSTAR_internal_activate(lev,p, &t);
322
 
323
  /* Set the deadline timer */
324
  lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
325
                                           RMSTAR_timer_deadline,
326
                                           (void *)p);
327
 
328
}
329
 
330
static void RMSTAR_public_unblock(LEVEL l, PID p)
331
{
332
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
333
 
334
#ifdef RMSTAR_DEBUG
335
  rmstar_printf("(E:ins)");
336
#endif
337
 
338
  /* Insert task in the correct position */
339
  proc_table[p].status = RMSTAR_READY;
340
  iq_priority_insert(p,&lev->ready);
341
 
342
  /* and check for preemption! */
343
  RMSTAR_check_preemption(lev);
344
}
345
 
346
static void RMSTAR_public_block(LEVEL l, PID p)
347
{
348
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
349
 
350
#ifdef RMSTAR_DEBUG
351
  rmstar_printf("(E:ext)");
352
#endif
353
 
354
  /* the task is blocked on a synchronization primitive. we have to
355
     remove it from the master module -and- from the local queue! */
356
  iq_extract(p,&lev->ready);
357
 
358
  /* and finally, a preemption check! (it will also call guest_end) */
359
  RMSTAR_check_preemption(lev);
360
}
361
 
362
static int RMSTAR_public_message(LEVEL l, PID p, void *m)
363
{
364
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
365
  struct timespec temp;
366
 
367
#ifdef RMSTAR_DEBUG
368
  rmstar_printf("(E:ecy ");
369
#endif
370
 
371
  switch ((long)(m))
372
    {
373
 
374
      /* Task EndCycle */
375
    case (long)(NULL):
376
 
377
      /* we call guest_end directly here because the same task may
378
         be reinserted in the queue before calling the preemption check! */
379
      level_table[ lev->scheduling_level ]->
380
        private_extract(lev->scheduling_level,p);  lev->activated = NIL;
381
 
382
      iq_extract(p,&lev->ready);
383
 
384
      /* we reset the capacity counters... */
385
      proc_table[p].avail_time = proc_table[p].wcet;
386
 
387
      if (lev->nact[p] > 0) {
388
#ifdef RMSTAR_DEBUG
389
        rmstar_printf2("E%d",p);
390
#endif
391
 
392
        /* Pending activation: reactivate the thread!!! */
393
        lev->nact[p]--;
394
 
395
        /* see also RMSTAR_timer_deadline */
396
        kern_gettime(&temp);
397
 
398
        RMSTAR_internal_activate(lev,p,&temp);
399
 
400
        /* check if the deadline has already expired */
401
        temp = *iq_query_timespec(p, &lev->ready);
402
        if (TIMESPEC_A_LT_B(&temp, &schedule_time)) {
403
          /* count the deadline miss */
404
          lev->dline_miss[p]++;
405
          kern_event_delete(lev->deadline_timer[p]);
406
        }
407
 
408
      }
409
      else {
410
#ifdef RMSTAR_DEBUG
411
        rmstar_printf("e%d",p);
412
#endif
413
 
414
        /* the task has terminated his job before it consume the wcet. All OK! */
415
        proc_table[p].status = RMSTAR_IDLE;
416
 
417
        /* and finally, a preemption check! */
418
        RMSTAR_check_preemption(lev);
419
 
420
        /* when the deadline timer fire, it recognize the situation and set
421
           correctly all the stuffs (like reactivation, etc... ) */
422
      }
423
#ifdef RMSTAR_DEBUG
424
      rmstar_printf(")");
425
#endif
426
 
427
      jet_update_endcycle(); /* Update the Jet data... */
428
      trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
429
      break;
430
    default:
431
 
432
      break;
433
 
434
    }
435
  return 0;
436
}
437
 
438
static void RMSTAR_public_end(LEVEL l, PID p)
439
{
440
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
441
 
442
#ifdef RMSTAR_DEBUG
443
  rmstar_printf("(E:end)");
444
#endif
445
 
446
  iq_extract(p,&lev->ready);
447
 
448
  /* we finally put the task in the ready queue */
449
  proc_table[p].status = FREE;
450
 
451
  iq_insertfirst(p,&freedesc);
452
 
453
  if (lev->deadline_timer[p] != -1) {
454
    kern_event_delete(lev->deadline_timer[p]);
455
  }
456
 
457
  /* and finally, a preemption check! (it will also call guest_end) */
458
  RMSTAR_check_preemption(lev);
459
}
460
 
461
 
462
/* Guest Functions
463
   These functions manages a JOB_TASK_MODEL, that is used to put
464
   a guest task in the RMSTAR ready queue. */
465
 
466
static void RMSTAR_private_insert(LEVEL l, PID p, TASK_MODEL *m)
467
{
468
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
469
  JOB_TASK_MODEL *job;
470
 
471
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
472
    kern_raise(XINVALID_TASK, p);
473
    return;
474
  }
475
 
476
  job = (JOB_TASK_MODEL *)m;
477
 
478
  *iq_query_timespec(p, &lev->ready) = job->deadline;
479
 
480
  lev->deadline_timer[p] = -1;
481
  lev->dline_miss[p]     = 0;
482
  lev->wcet_miss[p]      = 0;
483
  lev->nact[p]           = 0;
484
 
485
  if (job->noraiseexc)
486
    lev->flag[p] = RMSTAR_FLAG_NORAISEEXC;
487
  else {
488
    lev->flag[p] = 0;
489
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
490
                                             RMSTAR_timer_guest_deadline,
491
                                             (void *)p);
492
   }
493
 
494
  lev->period[p] = job->period;
495
  *iq_query_priority(p, &lev->ready) = job->period;
496
 
497
  /* there is no bandwidth guarantee at this level, it is performed
498
     by the level that inserts guest tasks... */
499
 
500
  /* Insert task in the correct position */
501
  iq_priority_insert(p,&lev->ready);
502
  proc_table[p].status = RMSTAR_READY;
503
 
504
  /* check for preemption */
505
  RMSTAR_check_preemption(lev);
506
}
507
 
508
 
509
static void RMSTAR_private_dispatch(LEVEL l, PID p, int nostop)
510
{
511
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
512
 
513
  level_table[ lev->scheduling_level ]->
514
    private_dispatch(lev->scheduling_level,p,nostop);
515
}
516
 
517
static void RMSTAR_private_epilogue(LEVEL l, PID p)
518
{
519
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
520
 
521
  /* the task has been preempted. it returns into the ready queue... */
522
  level_table[ lev->scheduling_level ]->
523
    private_epilogue(lev->scheduling_level,p);
524
 
525
  proc_table[p].status = RMSTAR_READY;
526
}
527
 
528
static void RMSTAR_private_extract(LEVEL l, PID p)
529
{
530
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
531
 
532
#ifdef RMSTAR_DEBUG
533
  //kern_printf("RMSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
534
#endif
535
 
536
  iq_extract(p, &lev->ready);
537
 
538
  /* we remove the deadline timer, because the slice is finished */
539
  if (lev->deadline_timer[p] != NIL) {
540
#ifdef RMSTAR_DEBUG
541
//    kern_printf("RMSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
542
#endif
543
    kern_event_delete(lev->deadline_timer[p]);
544
    lev->deadline_timer[p] = NIL;
545
  }
546
 
547
  /* and finally, a preemption check! (it will also call guest_end() */
548
  RMSTAR_check_preemption(lev);
549
}
550
 
551
/* Registration functions */
552
 
553
/* Registration function:
554
    int flags                 the init flags ... see RMSTAR.h */
555
LEVEL RMSTAR_register_level(int master)
556
{
557
  LEVEL l;            /* the level that we register */
558
  RMSTAR_level_des *lev;  /* for readableness only */
559
  PID i;              /* a counter */
560
 
561
#ifdef RMSTAR_DEBUG
562
  printk("RMSTAR_register_level\n");
563
#endif
564
 
565
  /* request an entry in the level_table */
566
  l = level_alloc_descriptor(sizeof(RMSTAR_level_des));
567
 
568
  lev = (RMSTAR_level_des *)level_table[l];
569
 
570
  printk("    lev=%d\n",(int)lev);
571
 
572
  /* fill the standard descriptor */
573
  lev->l.private_insert   = RMSTAR_private_insert;
574
  lev->l.private_extract  = RMSTAR_private_extract;
575
  lev->l.private_dispatch = RMSTAR_private_dispatch;
576
  lev->l.private_epilogue = RMSTAR_private_epilogue;
577
 
578
  lev->l.public_guarantee = NULL;
579
  lev->l.public_create    = RMSTAR_public_create;
580
  lev->l.public_end       = RMSTAR_public_end;
581
  lev->l.public_dispatch  = RMSTAR_public_dispatch;
582
  lev->l.public_epilogue  = RMSTAR_public_epilogue;
583
  lev->l.public_activate  = RMSTAR_public_activate;
584
  lev->l.public_unblock   = RMSTAR_public_unblock;
585
  lev->l.public_block     = RMSTAR_public_block;
586
  lev->l.public_message   = RMSTAR_public_message;
587
 
588
  /* fill the RMSTAR descriptor part */
589
  for(i=0; i<MAX_PROC; i++) {
590
    lev->period[i]         = 0;
591
    lev->deadline_timer[i] = -1;
592
    lev->flag[i]           = 0;
593
    lev->dline_miss[i]     = 0;
594
    lev->wcet_miss[i]      = 0;
595
    lev->nact[i]           = 0;
596
    lev->budget[i]         = NIL;
597
  }
598
 
599
  iq_init(&lev->ready, NULL, 0);
600
  lev->activated = NIL;
601
 
602
  lev->scheduling_level = master;
603
 
604
  return l;
605
}
606
 
607
int RMSTAR_get_dline_miss(PID p)
608
{
609
  LEVEL l = proc_table[p].task_level;
610
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
611
 
612
  return lev->dline_miss[p];
613
}
614
 
615
int RMSTAR_get_wcet_miss(PID p)
616
{
617
  LEVEL l = proc_table[p].task_level;
618
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
619
 
620
  return lev->wcet_miss[p];
621
}
622
 
623
int RMSTAR_get_nact(PID p)
624
{
625
  LEVEL l = proc_table[p].task_level;
626
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
627
 
628
  return lev->nact[p];
629
}
630
 
631
int RMSTAR_reset_dline_miss(PID p)
632
{
633
  LEVEL l = proc_table[p].task_level;
634
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
635
 
636
  lev->dline_miss[p] = 0;
637
  return 0;
638
}
639
 
640
int RMSTAR_reset_wcet_miss(PID p)
641
{
642
  LEVEL l = proc_table[p].task_level;
643
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
644
 
645
  lev->wcet_miss[p] = 0;
646
  return 0;
647
}
648
 
649
 
650
int RMSTAR_setbudget(LEVEL l, PID p, int budget)
651
{
652
 
653
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
654
 
655
  lev->budget[p] = budget;
656
 
657
  return 0;
658
 
659
}
660
 
661
int RMSTAR_getbudget(LEVEL l, PID p)
662
{
663
 
664
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
665
 
666
  return lev->budget[p];
667
 
668
}
669
 
670
int RMSTAR_budget_has_thread(LEVEL l, int budget)
671
{
672
 
673
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
674
  int i;
675
 
676
  for(i = 0; i< MAX_PROC; i++)
677
    if (lev->budget[i] == budget) return 1;
678
 
679
  return 0;
680
 
681
}