Subversion Repositories shark

Rev

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