Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

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