Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 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
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/**
22
 ------------
23
 CVS :        $Id: tbs.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1.1.1 $
27
 Last update: $Date: 2002-03-29 14:12:52 $
28
 ------------
29
 
30
 This file contains the aperiodic server TBS (Total Bandwidth Server)
31
 
32
 Read tbs.h for further details.
33
 
34
**/
35
 
36
/*
37
 * Copyright (C) 2000 Paolo Gai
38
 *
39
 * This program is free software; you can redistribute it and/or modify
40
 * it under the terms of the GNU General Public License as published by
41
 * the Free Software Foundation; either version 2 of the License, or
42
 * (at your option) any later version.
43
 *
44
 * This program is distributed in the hope that it will be useful,
45
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
 * GNU General Public License for more details.
48
 *
49
 * You should have received a copy of the GNU General Public License
50
 * along with this program; if not, write to the Free Software
51
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
52
 *
53
 */
54
 
55
 
56
#include <modules/tbs.h>
57
#include <ll/stdio.h>
58
#include <ll/string.h>
59
#include <kernel/model.h>
60
#include <kernel/descr.h>
61
#include <kernel/var.h>
62
#include <kernel/func.h>
63
 
64
/*+ 4 debug purposes +*/
65
#undef TBS_TEST
66
 
67
/*+ Status used in the level +*/
68
#define TBS_WCET_VIOLATED APER_STATUS_BASE+2  /*+ when wcet is finished +*/
69
#define TBS_WAIT          APER_STATUS_BASE    /*+ waiting the service   +*/
70
 
71
/*+ task flags +*/
72
#define TBS_SAVE_ARRIVALS 1
73
 
74
/*+ the level redefinition for the Total Bandwidth Server level +*/
75
typedef struct {
76
  level_des l;     /*+ the standard level descriptor          +*/
77
 
78
  /* The wcet are stored in the task descriptor's priority
79
     field.                     */
80
 
81
  int nact[MAX_PROC];    /*+ used to record activations +*/
82
  BYTE flag[MAX_PROC];
83
 
84
  struct timespec lastdline; /*+ the last deadline assigned to
85
                                 a TBS task                   +*/
86
 
87
  QQUEUE wait;      /*+ the wait queue of the TBS              +*/
88
  PID activated;   /*+ the task inserted in another queue     +*/
89
 
90
  int flags;       /*+ the init flags...                      +*/
91
 
92
  bandwidth_t U;   /*+ the used bandwidth by the server       +*/
93
  int band_num;
94
  int band_den;
95
 
96
  LEVEL scheduling_level;
97
 
98
} TBS_level_des;
99
 
100
 
101
static char *TBS_status_to_a(WORD status)
102
{
103
  if (status < MODULE_STATUS_BASE)
104
    return status_to_a(status);
105
 
106
  switch (status) {
107
    case TBS_WCET_VIOLATED: return "TBS_Wcet_Violated";
108
    case TBS_WAIT         : return "TBS_Wait";
109
    default               : return "TBS_Unknown";
110
  }
111
}
112
 
113
#ifdef TESTG
114
#include "drivers/glib.h"
115
#endif
116
 
117
/* This static function activates the task pointed by lev->activated) */
118
static __inline__ void TBS_activation(TBS_level_des *lev)
119
{
120
    PID p;             /* for readableness    */
121
    JOB_TASK_MODEL j;  /* the guest model     */
122
    TIME drel;         /* the relative deadline of the task */
123
    LEVEL m;           /* the master level... only for readableness */
124
 
125
#ifdef TESTG
126
    TIME x;
127
    extern TIME starttime;
128
#endif
129
 
130
    p = lev->activated;
131
    /* we compute a suitable deadline for the task */
132
    drel = (proc_table[p].wcet * lev->band_den) / lev->band_num;
133
 
134
    if (TIMESPEC_A_GT_B(&proc_table[p].request_time, &lev->lastdline))
135
      TIMESPEC_ASSIGN(&lev->lastdline, &proc_table[p].request_time );
136
 
137
    ADDUSEC2TIMESPEC(drel, &lev->lastdline);
138
 
139
#ifdef TESTG
140
    if (starttime) {
141
    x = ((lev->lastdline.tv_sec*1000000+lev->lastdline.tv_nsec/1000)/5000 - starttime) + 20;
142
    if (x<640)
143
      grx_plot(x, 15, 7);
144
    }
145
#endif
146
 
147
    /* and we insert the task in another level */
148
    m = lev->scheduling_level;
149
    job_task_default_model(j,lev->lastdline);
150
    level_table[m]->guest_create(m,p,(TASK_MODEL *)&j);
151
    level_table[m]->guest_activate(m,p);
152
 
153
    #ifdef TBS_TEST
154
    kern_printf("TBS_activation: lastdline %ds %dns\n",lev->lastdline.tv_sec,lev->lastdline.tv_nsec);
155
    #endif
156
}
157
 
158
/* This static function reclaims the unused time of the task p */
159
static __inline__ void TBS_bandwidth_reclaiming(TBS_level_des *lev, PID p)
160
{
161
    TIME reclaimed;
162
    struct timespec r, sos;
163
 
164
//    kern_printf("%d ", proc_table[p].avail_time);
165
    reclaimed = (proc_table[p].avail_time * lev->band_den) / lev->band_num;
166
 
167
    r.tv_nsec = (reclaimed % 1000000) * 1000;
168
    r.tv_sec  = reclaimed / 1000000;
169
 
170
    SUBTIMESPEC(&lev->lastdline, &r, &sos);
171
    TIMESPEC_ASSIGN(&lev->lastdline, &sos);
172
 
173
    #ifdef TBS_TEST
174
    kern_printf("TBS_bandwidth_reclaiming: lastdline %ds %dns, reclaimed %d, avail %d\n",
175
              lev->lastdline.tv_sec, lev->lastdline.tv_nsec, reclaimed, proc_table[p].avail_time);
176
    #endif
177
}
178
 
179
 
180
 
181
static int TBS_level_accept_task_model(LEVEL l, TASK_MODEL *m)
182
{
183
  if (m->pclass == SOFT_PCLASS || m->pclass == (SOFT_PCLASS | l) ) {
184
    SOFT_TASK_MODEL *s = (SOFT_TASK_MODEL *)m;
185
 
186
    if (s->wcet && s->periodicity == APERIODIC)
187
      return 0;
188
  }
189
 
190
  return -1;
191
}
192
 
193
static int TBS_level_accept_guest_model(LEVEL l, TASK_MODEL *m)
194
{
195
  return -1;
196
}
197
 
198
static char *onoff(int i)
199
{
200
  if (i)
201
    return "On ";
202
  else
203
    return "Off";
204
}
205
 
206
static void TBS_level_status(LEVEL l)
207
{
208
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
209
  PID p = qq_queryfirst(&lev->wait);
210
 
211
  kern_printf("Wcet     Check    : %s\n",
212
            onoff(lev->flags & TBS_ENABLE_WCET_CHECK));
213
  kern_printf("On-line guarantee : %s\n",
214
            onoff(lev->flags & TBS_ENABLE_GUARANTEE));
215
  kern_printf("Used Bandwidth    : %u/%u\n",
216
            lev->U, MAX_BANDWIDTH);
217
  kern_printf("Last deadline     : %lds %ldns\n",lev->lastdline.tv_sec,
218
                                             lev->lastdline.tv_nsec);
219
 
220
  if (lev->activated != -1)
221
    kern_printf("Activated: Pid: %2d Name: %10s Dl: %ld.%9ld nact: %d Stat: %s\n",
222
              lev->activated,
223
              proc_table[lev->activated].name,
224
              proc_table[lev->activated].timespec_priority.tv_sec,
225
              proc_table[lev->activated].timespec_priority.tv_nsec,
226
              lev->nact[lev->activated],
227
              TBS_status_to_a(proc_table[lev->activated].status));
228
 
229
  while (p != NIL) {
230
    kern_printf("Pid: %2d Name: %10s Stat: %s\n",
231
              p,
232
              proc_table[p].name,
233
              TBS_status_to_a(proc_table[p].status));
234
    p = proc_table[p].next;
235
  }
236
}
237
 
238
static PID TBS_level_scheduler(LEVEL l)
239
{
240
  /* the TBS don't schedule anything...
241
     it's an EDF level or similar that do it! */
242
  return NIL;
243
}
244
 
245
/* The on-line guarantee is enabled only if the appropriate flag is set... */
246
static int TBS_level_guarantee(LEVEL l, bandwidth_t *freebandwidth)
247
{
248
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
249
 
250
  if (*freebandwidth >= lev->U) {
251
    *freebandwidth -= lev->U;
252
    return 1;
253
  }
254
  else
255
    return 0;
256
}
257
 
258
static int TBS_task_create(LEVEL l, PID p, TASK_MODEL *m)
259
{
260
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
261
 
262
  /* if the TBS_task_create is called, then the pclass must be a
263
     valid pclass. */
264
  SOFT_TASK_MODEL *s = (SOFT_TASK_MODEL *)m;
265
 
266
  proc_table[p].wcet       = s->wcet;
267
 
268
  /* Enable wcet check */
269
  if (lev->flags & TBS_ENABLE_WCET_CHECK) {
270
    proc_table[p].avail_time = s->wcet;
271
    proc_table[p].control   |= CONTROL_CAP;
272
  }
273
 
274
  lev->nact[p] = 0;
275
  if (s->arrivals == SAVE_ARRIVALS)
276
    lev->flag[p] = TBS_SAVE_ARRIVALS;
277
 
278
  return 0; /* OK, also if the task cannot be guaranteed... */
279
}
280
 
281
static void TBS_task_detach(LEVEL l, PID p)
282
{
283
  /* the TBS level doesn't introduce any dinamic allocated new field. */
284
}
285
 
286
static int TBS_task_eligible(LEVEL l, PID p)
287
{
288
  return 0; /* if the task p is chosen, it is always eligible */
289
}
290
 
291
#ifdef __TEST1__
292
extern int testactive;
293
extern struct timespec s_stime[];
294
extern TIME s_curr[];
295
extern TIME s_PID[];
296
extern int useds;
297
#endif
298
 
299
static void TBS_task_dispatch(LEVEL l, PID p, int nostop)
300
{
301
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
302
 
303
  /* there is at least one task ready inserted in an EDF or similar
304
     level */
305
 
306
  level_table[ lev->scheduling_level ]->
307
    guest_dispatch(lev->scheduling_level,p,nostop);
308
 
309
  #ifdef __TEST1__
310
  if (testactive)
311
  {
312
    TIMESPEC_ASSIGN(&s_stime[useds], &schedule_time);
313
    s_curr[useds] = proc_table[p].avail_time;
314
    s_PID[useds]  = p;
315
    useds++;
316
  }
317
  #endif
318
}
319
 
320
static void TBS_task_epilogue(LEVEL l, PID p)
321
{
322
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
323
 
324
  /* check if the wcet is finished... */
325
  if ((lev->flags & TBS_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) {
326
    /* if it is, raise a XWCET_VIOLATION exception */
327
    kern_raise(XWCET_VIOLATION,p);
328
    proc_table[p].status = TBS_WCET_VIOLATED;
329
 
330
    /* the current task have to die in the scheduling queue, and another
331
       have to be put in place... this code is identical to the
332
       TBS_task_end */
333
    level_table[ lev->scheduling_level ]->
334
      guest_end(lev->scheduling_level,p);
335
 
336
    /* we reclaim an avail time that can be <0 due to the timer
337
       approximations -> we have to postpone the deadline a little!
338
       we can use the ADDUSEC2TIMESPEC because the time postponed is
339
       less than 55ms */
340
    ADDUSEC2TIMESPEC((-proc_table[p].avail_time * lev->band_den)
341
                     / lev->band_num, &lev->lastdline);
342
 
343
    #ifdef TBS_TEST
344
    kern_printf("TBS_task_epilogue: Deadline posponed to %ds %dns\n",
345
              lev->lastdline.tv_sec, lev->lastdline.tv_nsec);
346
    #endif
347
 
348
    lev->activated = qq_getfirst(&lev->wait);
349
    if (lev->activated != NIL)
350
      TBS_activation(lev);
351
  }
352
  else
353
    /* the task has been preempted. it returns into the ready queue by
354
       calling the guest_epilogue... */
355
    level_table[ lev->scheduling_level ]->
356
      guest_epilogue(lev->scheduling_level,p);
357
}
358
 
359
static void TBS_task_activate(LEVEL l, PID p)
360
{
361
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
362
 
363
  if (proc_table[p].status == SLEEP ||
364
       proc_table[p].status == TBS_WCET_VIOLATED) {
365
 
366
    ll_gettime(TIME_EXACT, &proc_table[p].request_time);
367
 
368
    if (lev->activated == NIL) {
369
      /* This is the first task in the level, so we activate it immediately */
370
      lev->activated = p;
371
      TBS_activation(lev);
372
    }
373
    else {
374
      proc_table[p].status = TBS_WAIT;
375
      qq_insertlast(p, &lev->wait);
376
    }
377
  }
378
  else if (lev->flag[p] & TBS_SAVE_ARRIVALS)
379
    lev->nact[p]++;
380
/*  else
381
    kern_printf("TBSREJ!!!");*/
382
}
383
 
384
static void TBS_task_insert(LEVEL l, PID p)
385
{
386
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
387
 
388
  level_table[ lev->scheduling_level ]->
389
    guest_insert(lev->scheduling_level,p);
390
}
391
 
392
static void TBS_task_extract(LEVEL l, PID p)
393
{
394
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
395
 
396
  level_table[ lev->scheduling_level ]->
397
    guest_extract(lev->scheduling_level,p);
398
}
399
 
400
static void TBS_task_endcycle(LEVEL l, PID p)
401
{
402
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
403
 
404
  /* a task activation is finished, but we are using a JOB_TASK_MODEL
405
     that implements a single activation, so we have to call
406
     the guest_end, that representsa single activation... */
407
  level_table[ lev->scheduling_level ]->
408
    guest_end(lev->scheduling_level,p);
409
 
410
  TBS_bandwidth_reclaiming(lev,p);
411
 
412
  /* we reset the capacity counters... */
413
  if (lev->flags & TBS_ENABLE_WCET_CHECK)
414
    proc_table[p].avail_time = proc_table[p].wcet;
415
 
416
  if (lev->nact[p]) {
417
    // lev->nact[p] can be >0 only if the SAVE_ARRIVALS bit is set
418
    lev->nact[p]--;
419
    proc_table[p].status = TBS_WAIT;
420
    qq_insertlast(p, &lev->wait);
421
  }
422
  else
423
    proc_table[p].status = SLEEP;
424
 
425
  lev->activated = qq_getfirst(&lev->wait);
426
  if (lev->activated != NIL)
427
    TBS_activation(lev);
428
 
429
}
430
 
431
static void TBS_task_end(LEVEL l, PID p)
432
{
433
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
434
 
435
  level_table[ lev->scheduling_level ]->
436
    guest_end(lev->scheduling_level,p);
437
 
438
  TBS_bandwidth_reclaiming(lev,p);
439
 
440
  proc_table[p].status = FREE;
441
  q_insertfirst(p,&freedesc);
442
 
443
  lev->activated = qq_getfirst(&lev->wait);
444
  if (lev->activated != NIL)
445
    TBS_activation(lev);
446
}
447
 
448
static void TBS_task_sleep(LEVEL l, PID p)
449
{
450
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
451
 
452
  /* a task activation is finished, but we are using a JOB_TASK_MODEL
453
     that implements a single activation, so we have to call
454
     the guest_end, that representsa single activation... */
455
  level_table[ lev->scheduling_level ]->
456
    guest_end(lev->scheduling_level,p);
457
 
458
  TBS_bandwidth_reclaiming(lev,p);
459
 
460
  /* we reset the capacity counters... */
461
  if (lev->flags & TBS_ENABLE_WCET_CHECK)
462
    proc_table[p].avail_time = proc_table[p].wcet;
463
 
464
  proc_table[p].status = SLEEP;
465
 
466
  lev->nact[p] = 0;
467
 
468
  lev->activated = qq_getfirst(&lev->wait);
469
  if (lev->activated != NIL)
470
    TBS_activation(lev);
471
 
472
}
473
 
474
static void TBS_task_delay(LEVEL l, PID p, TIME usdelay)
475
{
476
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
477
 
478
  level_table[ lev->scheduling_level ]->
479
    guest_delay(lev->scheduling_level,p,usdelay);
480
}
481
 
482
 
483
static int TBS_guest_create(LEVEL l, PID p, TASK_MODEL *m)
484
{ kern_raise(XUNVALID_GUEST,exec_shadow); return 0; }
485
 
486
static void TBS_guest_detach(LEVEL l, PID p)
487
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
488
 
489
static void TBS_guest_dispatch(LEVEL l, PID p, int nostop)
490
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
491
 
492
static void TBS_guest_epilogue(LEVEL l, PID p)
493
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
494
 
495
static void TBS_guest_activate(LEVEL l, PID p)
496
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
497
 
498
static void TBS_guest_insert(LEVEL l, PID p)
499
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
500
 
501
static void TBS_guest_extract(LEVEL l, PID p)
502
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
503
 
504
static void TBS_guest_endcycle(LEVEL l, PID p)
505
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
506
 
507
static void TBS_guest_end(LEVEL l, PID p)
508
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
509
 
510
static void TBS_guest_sleep(LEVEL l, PID p)
511
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
512
 
513
static void TBS_guest_delay(LEVEL l, PID p,DWORD tickdelay)
514
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
515
 
516
 
517
 
518
 
519
/* Registration functions */
520
 
521
/*+ Registration function:
522
    int flags                 the init flags ... see TBS.h +*/
523
void TBS_register_level(int flags, LEVEL master, int num, int den)
524
{
525
  LEVEL l;            /* the level that we register */
526
  TBS_level_des *lev;  /* for readableness only */
527
  PID i;              /* a counter */
528
 
529
  printk("TBS_register_level\n");
530
 
531
  /* request an entry in the level_table */
532
  l = level_alloc_descriptor();
533
 
534
  printk("    alloco descrittore %d %d\n",l,(int)sizeof(TBS_level_des));
535
 
536
  /* alloc the space needed for the TBS_level_des */
537
  lev = (TBS_level_des *)kern_alloc(sizeof(TBS_level_des));
538
 
539
  printk("    lev=%d\n",(int)lev);
540
 
541
  /* update the level_table with the new entry */
542
  level_table[l] = (level_des *)lev;
543
 
544
  /* fill the standard descriptor */
545
  strncpy(lev->l.level_name,  TBS_LEVELNAME, MAX_LEVELNAME);
546
  lev->l.level_code               = TBS_LEVEL_CODE;
547
  lev->l.level_version            = TBS_LEVEL_VERSION;
548
 
549
  lev->l.level_accept_task_model  = TBS_level_accept_task_model;
550
  lev->l.level_accept_guest_model = TBS_level_accept_guest_model;
551
  lev->l.level_status             = TBS_level_status;
552
  lev->l.level_scheduler          = TBS_level_scheduler;
553
 
554
  if (flags & TBS_ENABLE_GUARANTEE)
555
    lev->l.level_guarantee        = TBS_level_guarantee;
556
  else
557
    lev->l.level_guarantee        = NULL;
558
 
559
  lev->l.task_create              = TBS_task_create;
560
  lev->l.task_detach              = TBS_task_detach;
561
  lev->l.task_eligible            = TBS_task_eligible;
562
  lev->l.task_dispatch            = TBS_task_dispatch;
563
  lev->l.task_epilogue            = TBS_task_epilogue;
564
  lev->l.task_activate            = TBS_task_activate;
565
  lev->l.task_insert              = TBS_task_insert;
566
  lev->l.task_extract             = TBS_task_extract;
567
  lev->l.task_endcycle            = TBS_task_endcycle;
568
  lev->l.task_end                 = TBS_task_end;
569
  lev->l.task_sleep               = TBS_task_sleep;
570
  lev->l.task_delay               = TBS_task_delay;
571
 
572
  lev->l.guest_create             = TBS_guest_create;
573
  lev->l.guest_detach             = TBS_guest_detach;
574
  lev->l.guest_dispatch           = TBS_guest_dispatch;
575
  lev->l.guest_epilogue           = TBS_guest_epilogue;
576
  lev->l.guest_activate           = TBS_guest_activate;
577
  lev->l.guest_insert             = TBS_guest_insert;
578
  lev->l.guest_extract            = TBS_guest_extract;
579
  lev->l.guest_endcycle           = TBS_guest_endcycle;
580
  lev->l.guest_end                = TBS_guest_end;
581
  lev->l.guest_sleep              = TBS_guest_sleep;
582
  lev->l.guest_delay              = TBS_guest_delay;
583
 
584
  /* fill the TBS descriptor part */
585
 
586
  for (i = 0; i < MAX_PROC; i++) {
587
    lev->nact[i] = 0;
588
    lev->flag[i] = 0;
589
  }
590
 
591
  NULL_TIMESPEC(&lev->lastdline);
592
 
593
  qq_init(&lev->wait);
594
  lev->activated = NIL;
595
 
596
  lev->U = (MAX_BANDWIDTH / den) * num;
597
  lev->band_num = num;
598
  lev->band_den = den;
599
 
600
  lev->scheduling_level = master;
601
 
602
  lev->flags = flags & 0x07;
603
}
604
 
605
bandwidth_t TBS_usedbandwidth(LEVEL l)
606
{
607
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
608
  if (lev->l.level_code    == TBS_LEVEL_CODE &&
609
      lev->l.level_version == TBS_LEVEL_VERSION)
610
    return lev->U;
611
  else
612
    return 0;
613
}
614
 
615
int TBS_get_nact(LEVEL l, PID p)
616
{
617
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
618
  if (lev->l.level_code    == TBS_LEVEL_CODE &&
619
      lev->l.level_version == TBS_LEVEL_VERSION)
620
    return lev->nact[p];
621
  else
622
    return -1;
623
}
624