Subversion Repositories shark

Rev

Rev 385 | 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
 ------------
502 giacomo 23
 CVS :        $Id: tbs.c,v 1.7 2004-03-10 14:51:45 giacomo Exp $
2 pj 24
 
25
 File:        $File$
502 giacomo 26
 Revision:    $Revision: 1.7 $
27
 Last update: $Date: 2004-03-10 14:51:45 $
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
 
353 giacomo 64
#include <tracer.h>
65
 
2 pj 66
/*+ 4 debug purposes +*/
67
#undef TBS_TEST
68
 
69
/*+ Status used in the level +*/
70
#define TBS_WCET_VIOLATED APER_STATUS_BASE+2  /*+ when wcet is finished +*/
71
#define TBS_WAIT          APER_STATUS_BASE    /*+ waiting the service   +*/
72
 
73
/*+ task flags +*/
74
#define TBS_SAVE_ARRIVALS 1
75
 
76
/*+ the level redefinition for the Total Bandwidth Server level +*/
77
typedef struct {
78
  level_des l;     /*+ the standard level descriptor          +*/
79
 
80
  /* The wcet are stored in the task descriptor's priority
81
     field.                     */
82
 
83
  int nact[MAX_PROC];    /*+ used to record activations +*/
84
  BYTE flag[MAX_PROC];
85
 
86
  struct timespec lastdline; /*+ the last deadline assigned to
87
                                 a TBS task                   +*/
88
 
29 pj 89
  IQUEUE wait;      /*+ the wait queue of the TBS              +*/
2 pj 90
  PID activated;   /*+ the task inserted in another queue     +*/
91
 
92
  int flags;       /*+ the init flags...                      +*/
93
 
94
  bandwidth_t U;   /*+ the used bandwidth by the server       +*/
95
  int band_num;
96
  int band_den;
97
 
98
  LEVEL scheduling_level;
99
 
100
} TBS_level_des;
101
 
102
#ifdef TESTG
103
#include "drivers/glib.h"
104
#endif
105
 
106
/* This static function activates the task pointed by lev->activated) */
107
static __inline__ void TBS_activation(TBS_level_des *lev)
108
{
109
    PID p;             /* for readableness    */
110
    JOB_TASK_MODEL j;  /* the guest model     */
111
    TIME drel;         /* the relative deadline of the task */
112
    LEVEL m;           /* the master level... only for readableness */
113
 
114
#ifdef TESTG
115
    TIME x;
116
    extern TIME starttime;
117
#endif
118
 
119
    p = lev->activated;
120
    /* we compute a suitable deadline for the task */
121
    drel = (proc_table[p].wcet * lev->band_den) / lev->band_num;
122
 
123
    ADDUSEC2TIMESPEC(drel, &lev->lastdline);
124
 
125
#ifdef TESTG
126
    if (starttime) {
127
    x = ((lev->lastdline.tv_sec*1000000+lev->lastdline.tv_nsec/1000)/5000 - starttime) + 20;
128
    if (x<640)
129
      grx_plot(x, 15, 7);
130
    }
131
#endif
132
 
133
    /* and we insert the task in another level */
134
    m = lev->scheduling_level;
135
    job_task_default_model(j,lev->lastdline);
38 pj 136
    level_table[m]->private_insert(m,p,(TASK_MODEL *)&j);
2 pj 137
 
138
    #ifdef TBS_TEST
139
    kern_printf("TBS_activation: lastdline %ds %dns\n",lev->lastdline.tv_sec,lev->lastdline.tv_nsec);
140
    #endif
141
}
142
 
143
/* This static function reclaims the unused time of the task p */
144
static __inline__ void TBS_bandwidth_reclaiming(TBS_level_des *lev, PID p)
145
{
146
    TIME reclaimed;
147
    struct timespec r, sos;
148
 
149
//    kern_printf("%d ", proc_table[p].avail_time);
150
    reclaimed = (proc_table[p].avail_time * lev->band_den) / lev->band_num;
151
 
152
    r.tv_nsec = (reclaimed % 1000000) * 1000;
153
    r.tv_sec  = reclaimed / 1000000;
154
 
155
    SUBTIMESPEC(&lev->lastdline, &r, &sos);
156
    TIMESPEC_ASSIGN(&lev->lastdline, &sos);
157
 
158
    #ifdef TBS_TEST
159
    kern_printf("TBS_bandwidth_reclaiming: lastdline %ds %dns, reclaimed %d, avail %d\n",
160
              lev->lastdline.tv_sec, lev->lastdline.tv_nsec, reclaimed, proc_table[p].avail_time);
161
    #endif
162
}
163
 
164
/* The on-line guarantee is enabled only if the appropriate flag is set... */
38 pj 165
static int TBS_public_guarantee(LEVEL l, bandwidth_t *freebandwidth)
2 pj 166
{
167
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
168
 
169
  if (*freebandwidth >= lev->U) {
170
    *freebandwidth -= lev->U;
171
    return 1;
172
  }
173
  else
174
    return 0;
175
}
176
 
38 pj 177
static int TBS_public_create(LEVEL l, PID p, TASK_MODEL *m)
2 pj 178
{
179
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
180
 
181
  /* if the TBS_task_create is called, then the pclass must be a
182
     valid pclass. */
38 pj 183
  SOFT_TASK_MODEL *s;
2 pj 184
 
38 pj 185
  if (m->pclass != SOFT_PCLASS) return -1;
186
  if (m->level != 0 && m->level != l) return -1;
187
  s = (SOFT_TASK_MODEL *)m;
188
  if (!(s->wcet && s->periodicity == APERIODIC)) return -1;
189
 
2 pj 190
  proc_table[p].wcet       = s->wcet;
191
 
192
  /* Enable wcet check */
193
  if (lev->flags & TBS_ENABLE_WCET_CHECK) {
194
    proc_table[p].avail_time = s->wcet;
195
    proc_table[p].control   |= CONTROL_CAP;
196
  }
197
 
198
  lev->nact[p] = 0;
199
  if (s->arrivals == SAVE_ARRIVALS)
200
    lev->flag[p] = TBS_SAVE_ARRIVALS;
201
 
202
  return 0; /* OK, also if the task cannot be guaranteed... */
203
}
204
 
38 pj 205
static void TBS_public_dispatch(LEVEL l, PID p, int nostop)
2 pj 206
{
207
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
208
 
209
  /* there is at least one task ready inserted in an EDF or similar
210
     level */
211
 
212
  level_table[ lev->scheduling_level ]->
38 pj 213
    private_dispatch(lev->scheduling_level,p,nostop);
2 pj 214
}
215
 
38 pj 216
static void TBS_public_epilogue(LEVEL l, PID p)
2 pj 217
{
218
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
219
 
220
  /* check if the wcet is finished... */
221
  if ((lev->flags & TBS_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) {
222
    /* if it is, raise a XWCET_VIOLATION exception */
223
    kern_raise(XWCET_VIOLATION,p);
224
    proc_table[p].status = TBS_WCET_VIOLATED;
225
 
226
    /* the current task have to die in the scheduling queue, and another
227
       have to be put in place... this code is identical to the
228
       TBS_task_end */
229
    level_table[ lev->scheduling_level ]->
38 pj 230
      private_extract(lev->scheduling_level,p);
2 pj 231
 
232
    /* we reclaim an avail time that can be <0 due to the timer
233
       approximations -> we have to postpone the deadline a little!
234
       we can use the ADDUSEC2TIMESPEC because the time postponed is
235
       less than 55ms */
236
    ADDUSEC2TIMESPEC((-proc_table[p].avail_time * lev->band_den)
237
                     / lev->band_num, &lev->lastdline);
238
 
239
    #ifdef TBS_TEST
240
    kern_printf("TBS_task_epilogue: Deadline posponed to %ds %dns\n",
241
              lev->lastdline.tv_sec, lev->lastdline.tv_nsec);
242
    #endif
243
 
29 pj 244
    lev->activated = iq_getfirst(&lev->wait);
2 pj 245
    if (lev->activated != NIL)
246
      TBS_activation(lev);
247
  }
248
  else
249
    /* the task has been preempted. it returns into the ready queue by
250
       calling the guest_epilogue... */
251
    level_table[ lev->scheduling_level ]->
38 pj 252
      private_epilogue(lev->scheduling_level,p);
2 pj 253
}
254
 
38 pj 255
static void TBS_public_activate(LEVEL l, PID p)
2 pj 256
{
257
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
38 pj 258
  struct timespec t;
2 pj 259
 
260
  if (proc_table[p].status == SLEEP ||
261
       proc_table[p].status == TBS_WCET_VIOLATED) {
262
 
38 pj 263
    kern_gettime(&t);
264
    if (TIMESPEC_A_GT_B(&t, &lev->lastdline))
265
      TIMESPEC_ASSIGN(&lev->lastdline, &t );
266
 
267
 
2 pj 268
    if (lev->activated == NIL) {
269
      /* This is the first task in the level, so we activate it immediately */
270
      lev->activated = p;
271
      TBS_activation(lev);
272
    }
273
    else {
274
      proc_table[p].status = TBS_WAIT;
29 pj 275
      iq_insertlast(p, &lev->wait);
2 pj 276
    }
277
  }
278
  else if (lev->flag[p] & TBS_SAVE_ARRIVALS)
279
    lev->nact[p]++;
280
/*  else
281
    kern_printf("TBSREJ!!!");*/
282
}
283
 
38 pj 284
static void TBS_public_unblock(LEVEL l, PID p)
2 pj 285
{
286
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
38 pj 287
  JOB_TASK_MODEL j;
2 pj 288
 
38 pj 289
  job_task_default_model(j,lev->lastdline);
290
  level_table[lev->scheduling_level]->
291
    private_insert(lev->scheduling_level,p,(TASK_MODEL *)&j);
2 pj 292
}
293
 
38 pj 294
static void TBS_public_block(LEVEL l, PID p)
2 pj 295
{
296
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
297
 
298
  level_table[ lev->scheduling_level ]->
38 pj 299
    private_extract(lev->scheduling_level,p);
2 pj 300
}
301
 
38 pj 302
static int TBS_public_message(LEVEL l, PID p, void *m)
2 pj 303
{
304
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
305
 
306
  /* a task activation is finished, but we are using a JOB_TASK_MODEL
307
     that implements a single activation, so we have to call
308
     the guest_end, that representsa single activation... */
309
  level_table[ lev->scheduling_level ]->
38 pj 310
    private_extract(lev->scheduling_level,p);
2 pj 311
 
312
  TBS_bandwidth_reclaiming(lev,p);
313
 
314
  /* we reset the capacity counters... */
315
  if (lev->flags & TBS_ENABLE_WCET_CHECK)
316
    proc_table[p].avail_time = proc_table[p].wcet;
317
 
318
  if (lev->nact[p]) {
319
    // lev->nact[p] can be >0 only if the SAVE_ARRIVALS bit is set
320
    lev->nact[p]--;
321
    proc_table[p].status = TBS_WAIT;
29 pj 322
    iq_insertlast(p, &lev->wait);
2 pj 323
  }
324
  else
325
    proc_table[p].status = SLEEP;
326
 
29 pj 327
  lev->activated = iq_getfirst(&lev->wait);
2 pj 328
  if (lev->activated != NIL)
329
    TBS_activation(lev);
330
 
38 pj 331
  jet_update_endcycle(); /* Update the Jet data... */
502 giacomo 332
  TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,(unsigned short int)proc_table[p].context,(unsigned int)l);
385 giacomo 333
 
38 pj 334
  return 0;
2 pj 335
}
336
 
38 pj 337
static void TBS_public_end(LEVEL l, PID p)
2 pj 338
{
339
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
340
 
341
  level_table[ lev->scheduling_level ]->
38 pj 342
    private_extract(lev->scheduling_level,p);
2 pj 343
 
344
  TBS_bandwidth_reclaiming(lev,p);
345
 
346
  proc_table[p].status = FREE;
29 pj 347
  iq_insertfirst(p,&freedesc);
2 pj 348
 
29 pj 349
  lev->activated = iq_getfirst(&lev->wait);
2 pj 350
  if (lev->activated != NIL)
351
    TBS_activation(lev);
352
}
353
 
354
/* Registration functions */
355
 
356
/*+ Registration function:
357
    int flags                 the init flags ... see TBS.h +*/
358
void TBS_register_level(int flags, LEVEL master, int num, int den)
359
{
360
  LEVEL l;            /* the level that we register */
361
  TBS_level_des *lev;  /* for readableness only */
362
  PID i;              /* a counter */
363
 
364
  printk("TBS_register_level\n");
365
 
366
  /* request an entry in the level_table */
38 pj 367
  l = level_alloc_descriptor(sizeof(TBS_level_des));
2 pj 368
 
38 pj 369
  lev = (TBS_level_des *)level_table[l];
2 pj 370
 
371
  /* fill the standard descriptor */
372
  if (flags & TBS_ENABLE_GUARANTEE)
38 pj 373
    lev->l.public_guarantee = TBS_public_guarantee;
2 pj 374
  else
38 pj 375
    lev->l.public_guarantee = NULL;
2 pj 376
 
38 pj 377
  lev->l.public_guarantee = TBS_public_guarantee;
378
  lev->l.public_create    = TBS_public_create;
379
  lev->l.public_end       = TBS_public_end;
380
  lev->l.public_dispatch  = TBS_public_dispatch;
381
  lev->l.public_epilogue  = TBS_public_epilogue;
382
  lev->l.public_activate  = TBS_public_activate;
383
  lev->l.public_unblock   = TBS_public_unblock;
384
  lev->l.public_block     = TBS_public_block;
385
  lev->l.public_message   = TBS_public_message;
2 pj 386
 
387
  /* fill the TBS descriptor part */
388
 
389
  for (i = 0; i < MAX_PROC; i++) {
390
    lev->nact[i] = 0;
391
    lev->flag[i] = 0;
392
  }
393
 
394
  NULL_TIMESPEC(&lev->lastdline);
395
 
29 pj 396
  iq_init(&lev->wait, &freedesc, 0);
2 pj 397
  lev->activated = NIL;
398
 
399
  lev->U = (MAX_BANDWIDTH / den) * num;
400
  lev->band_num = num;
401
  lev->band_den = den;
402
 
403
  lev->scheduling_level = master;
404
 
405
  lev->flags = flags & 0x07;
406
}
407
 
408
bandwidth_t TBS_usedbandwidth(LEVEL l)
409
{
410
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
38 pj 411
 
412
  return lev->U;
2 pj 413
}
414
 
415
int TBS_get_nact(LEVEL l, PID p)
416
{
417
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
38 pj 418
 
419
  return lev->nact[p];
2 pj 420
}
421