Subversion Repositories shark

Rev

Rev 14 | Go to most recent revision | Details | Compare with Previous | 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
 ------------
29 pj 23
 CVS :        $Id: tbs.c,v 1.3 2002-11-11 08:32:07 pj Exp $
2 pj 24
 
25
 File:        $File$
29 pj 26
 Revision:    $Revision: 1.3 $
27
 Last update: $Date: 2002-11-11 08:32:07 $
2 pj 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
 
29 pj 87
  IQUEUE wait;      /*+ the wait queue of the TBS              +*/
2 pj 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]);
29 pj 209
  PID p = iq_query_first(&lev->wait);
2 pj 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,
29 pj 224
              iq_query_timespec(lev->activated, &lev->wait)->tv_sec,
225
              iq_query_timespec(lev->activated, &lev->wait)->tv_nsec,
2 pj 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));
29 pj 234
    p = iq_query_next(p, &lev->wait);
2 pj 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
static void TBS_task_dispatch(LEVEL l, PID p, int nostop)
292
{
293
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
294
 
295
  /* there is at least one task ready inserted in an EDF or similar
296
     level */
297
 
298
  level_table[ lev->scheduling_level ]->
299
    guest_dispatch(lev->scheduling_level,p,nostop);
300
}
301
 
302
static void TBS_task_epilogue(LEVEL l, PID p)
303
{
304
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
305
 
306
  /* check if the wcet is finished... */
307
  if ((lev->flags & TBS_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) {
308
    /* if it is, raise a XWCET_VIOLATION exception */
309
    kern_raise(XWCET_VIOLATION,p);
310
    proc_table[p].status = TBS_WCET_VIOLATED;
311
 
312
    /* the current task have to die in the scheduling queue, and another
313
       have to be put in place... this code is identical to the
314
       TBS_task_end */
315
    level_table[ lev->scheduling_level ]->
316
      guest_end(lev->scheduling_level,p);
317
 
318
    /* we reclaim an avail time that can be <0 due to the timer
319
       approximations -> we have to postpone the deadline a little!
320
       we can use the ADDUSEC2TIMESPEC because the time postponed is
321
       less than 55ms */
322
    ADDUSEC2TIMESPEC((-proc_table[p].avail_time * lev->band_den)
323
                     / lev->band_num, &lev->lastdline);
324
 
325
    #ifdef TBS_TEST
326
    kern_printf("TBS_task_epilogue: Deadline posponed to %ds %dns\n",
327
              lev->lastdline.tv_sec, lev->lastdline.tv_nsec);
328
    #endif
329
 
29 pj 330
    lev->activated = iq_getfirst(&lev->wait);
2 pj 331
    if (lev->activated != NIL)
332
      TBS_activation(lev);
333
  }
334
  else
335
    /* the task has been preempted. it returns into the ready queue by
336
       calling the guest_epilogue... */
337
    level_table[ lev->scheduling_level ]->
338
      guest_epilogue(lev->scheduling_level,p);
339
}
340
 
341
static void TBS_task_activate(LEVEL l, PID p)
342
{
343
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
344
 
345
  if (proc_table[p].status == SLEEP ||
346
       proc_table[p].status == TBS_WCET_VIOLATED) {
347
 
348
    ll_gettime(TIME_EXACT, &proc_table[p].request_time);
349
 
350
    if (lev->activated == NIL) {
351
      /* This is the first task in the level, so we activate it immediately */
352
      lev->activated = p;
353
      TBS_activation(lev);
354
    }
355
    else {
356
      proc_table[p].status = TBS_WAIT;
29 pj 357
      iq_insertlast(p, &lev->wait);
2 pj 358
    }
359
  }
360
  else if (lev->flag[p] & TBS_SAVE_ARRIVALS)
361
    lev->nact[p]++;
362
/*  else
363
    kern_printf("TBSREJ!!!");*/
364
}
365
 
366
static void TBS_task_insert(LEVEL l, PID p)
367
{
368
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
369
 
370
  level_table[ lev->scheduling_level ]->
371
    guest_insert(lev->scheduling_level,p);
372
}
373
 
374
static void TBS_task_extract(LEVEL l, PID p)
375
{
376
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
377
 
378
  level_table[ lev->scheduling_level ]->
379
    guest_extract(lev->scheduling_level,p);
380
}
381
 
382
static void TBS_task_endcycle(LEVEL l, PID p)
383
{
384
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
385
 
386
  /* a task activation is finished, but we are using a JOB_TASK_MODEL
387
     that implements a single activation, so we have to call
388
     the guest_end, that representsa single activation... */
389
  level_table[ lev->scheduling_level ]->
390
    guest_end(lev->scheduling_level,p);
391
 
392
  TBS_bandwidth_reclaiming(lev,p);
393
 
394
  /* we reset the capacity counters... */
395
  if (lev->flags & TBS_ENABLE_WCET_CHECK)
396
    proc_table[p].avail_time = proc_table[p].wcet;
397
 
398
  if (lev->nact[p]) {
399
    // lev->nact[p] can be >0 only if the SAVE_ARRIVALS bit is set
400
    lev->nact[p]--;
401
    proc_table[p].status = TBS_WAIT;
29 pj 402
    iq_insertlast(p, &lev->wait);
2 pj 403
  }
404
  else
405
    proc_table[p].status = SLEEP;
406
 
29 pj 407
  lev->activated = iq_getfirst(&lev->wait);
2 pj 408
  if (lev->activated != NIL)
409
    TBS_activation(lev);
410
 
411
}
412
 
413
static void TBS_task_end(LEVEL l, PID p)
414
{
415
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
416
 
417
  level_table[ lev->scheduling_level ]->
418
    guest_end(lev->scheduling_level,p);
419
 
420
  TBS_bandwidth_reclaiming(lev,p);
421
 
422
  proc_table[p].status = FREE;
29 pj 423
  iq_insertfirst(p,&freedesc);
2 pj 424
 
29 pj 425
  lev->activated = iq_getfirst(&lev->wait);
2 pj 426
  if (lev->activated != NIL)
427
    TBS_activation(lev);
428
}
429
 
430
static void TBS_task_sleep(LEVEL l, PID p)
431
{
432
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
433
 
434
  /* a task activation is finished, but we are using a JOB_TASK_MODEL
435
     that implements a single activation, so we have to call
436
     the guest_end, that representsa single activation... */
437
  level_table[ lev->scheduling_level ]->
438
    guest_end(lev->scheduling_level,p);
439
 
440
  TBS_bandwidth_reclaiming(lev,p);
441
 
442
  /* we reset the capacity counters... */
443
  if (lev->flags & TBS_ENABLE_WCET_CHECK)
444
    proc_table[p].avail_time = proc_table[p].wcet;
445
 
446
  proc_table[p].status = SLEEP;
447
 
448
  lev->nact[p] = 0;
449
 
29 pj 450
  lev->activated = iq_getfirst(&lev->wait);
2 pj 451
  if (lev->activated != NIL)
452
    TBS_activation(lev);
453
 
454
}
455
 
456
static int TBS_guest_create(LEVEL l, PID p, TASK_MODEL *m)
14 pj 457
{ kern_raise(XINVALID_GUEST,exec_shadow); return 0; }
2 pj 458
 
459
static void TBS_guest_detach(LEVEL l, PID p)
14 pj 460
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 461
 
462
static void TBS_guest_dispatch(LEVEL l, PID p, int nostop)
14 pj 463
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 464
 
465
static void TBS_guest_epilogue(LEVEL l, PID p)
14 pj 466
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 467
 
468
static void TBS_guest_activate(LEVEL l, PID p)
14 pj 469
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 470
 
471
static void TBS_guest_insert(LEVEL l, PID p)
14 pj 472
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 473
 
474
static void TBS_guest_extract(LEVEL l, PID p)
14 pj 475
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 476
 
477
static void TBS_guest_endcycle(LEVEL l, PID p)
14 pj 478
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 479
 
480
static void TBS_guest_end(LEVEL l, PID p)
14 pj 481
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 482
 
483
static void TBS_guest_sleep(LEVEL l, PID p)
14 pj 484
{ kern_raise(XINVALID_GUEST,exec_shadow); }
2 pj 485
 
486
 
487
 
488
 
489
/* Registration functions */
490
 
491
/*+ Registration function:
492
    int flags                 the init flags ... see TBS.h +*/
493
void TBS_register_level(int flags, LEVEL master, int num, int den)
494
{
495
  LEVEL l;            /* the level that we register */
496
  TBS_level_des *lev;  /* for readableness only */
497
  PID i;              /* a counter */
498
 
499
  printk("TBS_register_level\n");
500
 
501
  /* request an entry in the level_table */
502
  l = level_alloc_descriptor();
503
 
504
  printk("    alloco descrittore %d %d\n",l,(int)sizeof(TBS_level_des));
505
 
506
  /* alloc the space needed for the TBS_level_des */
507
  lev = (TBS_level_des *)kern_alloc(sizeof(TBS_level_des));
508
 
509
  printk("    lev=%d\n",(int)lev);
510
 
511
  /* update the level_table with the new entry */
512
  level_table[l] = (level_des *)lev;
513
 
514
  /* fill the standard descriptor */
515
  strncpy(lev->l.level_name,  TBS_LEVELNAME, MAX_LEVELNAME);
516
  lev->l.level_code               = TBS_LEVEL_CODE;
517
  lev->l.level_version            = TBS_LEVEL_VERSION;
518
 
519
  lev->l.level_accept_task_model  = TBS_level_accept_task_model;
520
  lev->l.level_accept_guest_model = TBS_level_accept_guest_model;
521
  lev->l.level_status             = TBS_level_status;
522
  lev->l.level_scheduler          = TBS_level_scheduler;
523
 
524
  if (flags & TBS_ENABLE_GUARANTEE)
525
    lev->l.level_guarantee        = TBS_level_guarantee;
526
  else
527
    lev->l.level_guarantee        = NULL;
528
 
529
  lev->l.task_create              = TBS_task_create;
530
  lev->l.task_detach              = TBS_task_detach;
531
  lev->l.task_eligible            = TBS_task_eligible;
532
  lev->l.task_dispatch            = TBS_task_dispatch;
533
  lev->l.task_epilogue            = TBS_task_epilogue;
534
  lev->l.task_activate            = TBS_task_activate;
535
  lev->l.task_insert              = TBS_task_insert;
536
  lev->l.task_extract             = TBS_task_extract;
537
  lev->l.task_endcycle            = TBS_task_endcycle;
538
  lev->l.task_end                 = TBS_task_end;
539
  lev->l.task_sleep               = TBS_task_sleep;
540
 
541
  lev->l.guest_create             = TBS_guest_create;
542
  lev->l.guest_detach             = TBS_guest_detach;
543
  lev->l.guest_dispatch           = TBS_guest_dispatch;
544
  lev->l.guest_epilogue           = TBS_guest_epilogue;
545
  lev->l.guest_activate           = TBS_guest_activate;
546
  lev->l.guest_insert             = TBS_guest_insert;
547
  lev->l.guest_extract            = TBS_guest_extract;
548
  lev->l.guest_endcycle           = TBS_guest_endcycle;
549
  lev->l.guest_end                = TBS_guest_end;
550
  lev->l.guest_sleep              = TBS_guest_sleep;
551
 
552
  /* fill the TBS descriptor part */
553
 
554
  for (i = 0; i < MAX_PROC; i++) {
555
    lev->nact[i] = 0;
556
    lev->flag[i] = 0;
557
  }
558
 
559
  NULL_TIMESPEC(&lev->lastdline);
560
 
29 pj 561
  iq_init(&lev->wait, &freedesc, 0);
2 pj 562
  lev->activated = NIL;
563
 
564
  lev->U = (MAX_BANDWIDTH / den) * num;
565
  lev->band_num = num;
566
  lev->band_den = den;
567
 
568
  lev->scheduling_level = master;
569
 
570
  lev->flags = flags & 0x07;
571
}
572
 
573
bandwidth_t TBS_usedbandwidth(LEVEL l)
574
{
575
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
576
  if (lev->l.level_code    == TBS_LEVEL_CODE &&
577
      lev->l.level_version == TBS_LEVEL_VERSION)
578
    return lev->U;
579
  else
580
    return 0;
581
}
582
 
583
int TBS_get_nact(LEVEL l, PID p)
584
{
585
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
586
  if (lev->l.level_code    == TBS_LEVEL_CODE &&
587
      lev->l.level_version == TBS_LEVEL_VERSION)
588
    return lev->nact[p];
589
  else
590
    return -1;
591
}
592