Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
961 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 2005-02-25 10:43:38 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1 $
27
 Last update: $Date: 2005-02-25 10:43:38 $
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 <tbs/tbs/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
#include <tracer.h>
65
 
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
 
89
  IQUEUE wait;      /*+ the wait queue of the TBS              +*/
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);
136
    level_table[m]->private_insert(m,p,(TASK_MODEL *)&j);
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... */
165
static int TBS_public_guarantee(LEVEL l, bandwidth_t *freebandwidth)
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
 
177
static int TBS_public_create(LEVEL l, PID p, TASK_MODEL *m)
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. */
183
  SOFT_TASK_MODEL *s;
184
 
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
 
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
 
205
static void TBS_public_dispatch(LEVEL l, PID p, int nostop)
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 ]->
213
    private_dispatch(lev->scheduling_level,p,nostop);
214
}
215
 
216
static void TBS_public_epilogue(LEVEL l, PID p)
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 ]->
230
      private_extract(lev->scheduling_level,p);
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
 
244
    lev->activated = iq_getfirst(&lev->wait);
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 ]->
252
      private_epilogue(lev->scheduling_level,p);
253
}
254
 
255
static void TBS_public_activate(LEVEL l, PID p, struct timespec *t)
256
{
257
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
258
 
259
  if (proc_table[p].status == SLEEP ||
260
       proc_table[p].status == TBS_WCET_VIOLATED) {
261
 
262
    if (TIMESPEC_A_GT_B(t, &lev->lastdline))
263
      TIMESPEC_ASSIGN(&lev->lastdline, t );
264
 
265
 
266
    if (lev->activated == NIL) {
267
      /* This is the first task in the level, so we activate it immediately */
268
      lev->activated = p;
269
      TBS_activation(lev);
270
    }
271
    else {
272
      proc_table[p].status = TBS_WAIT;
273
      iq_insertlast(p, &lev->wait);
274
    }
275
  }
276
  else if (lev->flag[p] & TBS_SAVE_ARRIVALS)
277
    lev->nact[p]++;
278
/*  else
279
    kern_printf("TBSREJ!!!");*/
280
}
281
 
282
static void TBS_public_unblock(LEVEL l, PID p)
283
{
284
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
285
  JOB_TASK_MODEL j;
286
 
287
  job_task_default_model(j,lev->lastdline);
288
  level_table[lev->scheduling_level]->
289
    private_insert(lev->scheduling_level,p,(TASK_MODEL *)&j);
290
}
291
 
292
static void TBS_public_block(LEVEL l, PID p)
293
{
294
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
295
 
296
  level_table[ lev->scheduling_level ]->
297
    private_extract(lev->scheduling_level,p);
298
}
299
 
300
static int TBS_public_message(LEVEL l, PID p, void *m)
301
{
302
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
303
 
304
  /* a task activation is finished, but we are using a JOB_TASK_MODEL
305
     that implements a single activation, so we have to call
306
     the guest_end, that representsa single activation... */
307
  level_table[ lev->scheduling_level ]->
308
    private_extract(lev->scheduling_level,p);
309
 
310
  TBS_bandwidth_reclaiming(lev,p);
311
 
312
  /* we reset the capacity counters... */
313
  if (lev->flags & TBS_ENABLE_WCET_CHECK)
314
    proc_table[p].avail_time = proc_table[p].wcet;
315
 
316
  if (lev->nact[p]) {
317
    // lev->nact[p] can be >0 only if the SAVE_ARRIVALS bit is set
318
    lev->nact[p]--;
319
    proc_table[p].status = TBS_WAIT;
320
    iq_insertlast(p, &lev->wait);
321
  }
322
  else
323
    proc_table[p].status = SLEEP;
324
 
325
  lev->activated = iq_getfirst(&lev->wait);
326
  if (lev->activated != NIL)
327
    TBS_activation(lev);
328
 
329
  jet_update_endcycle(); /* Update the Jet data... */
330
  TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,(unsigned short int)proc_table[p].context,(unsigned int)l);
331
 
332
  return 0;
333
}
334
 
335
static void TBS_public_end(LEVEL l, PID p)
336
{
337
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
338
 
339
  level_table[ lev->scheduling_level ]->
340
    private_extract(lev->scheduling_level,p);
341
 
342
  TBS_bandwidth_reclaiming(lev,p);
343
 
344
  proc_table[p].status = FREE;
345
  iq_insertfirst(p,&freedesc);
346
 
347
  lev->activated = iq_getfirst(&lev->wait);
348
  if (lev->activated != NIL)
349
    TBS_activation(lev);
350
}
351
 
352
/* Registration functions */
353
 
354
/*+ Registration function:
355
    int flags                 the init flags ... see TBS.h +*/
356
void TBS_register_level(int flags, LEVEL master, int num, int den)
357
{
358
  LEVEL l;            /* the level that we register */
359
  TBS_level_des *lev;  /* for readableness only */
360
  PID i;              /* a counter */
361
 
362
  printk("TBS_register_level\n");
363
 
364
  /* request an entry in the level_table */
365
  l = level_alloc_descriptor(sizeof(TBS_level_des));
366
 
367
  lev = (TBS_level_des *)level_table[l];
368
 
369
  /* fill the standard descriptor */
370
  if (flags & TBS_ENABLE_GUARANTEE)
371
    lev->l.public_guarantee = TBS_public_guarantee;
372
  else
373
    lev->l.public_guarantee = NULL;
374
 
375
  lev->l.public_guarantee = TBS_public_guarantee;
376
  lev->l.public_create    = TBS_public_create;
377
  lev->l.public_end       = TBS_public_end;
378
  lev->l.public_dispatch  = TBS_public_dispatch;
379
  lev->l.public_epilogue  = TBS_public_epilogue;
380
  lev->l.public_activate  = TBS_public_activate;
381
  lev->l.public_unblock   = TBS_public_unblock;
382
  lev->l.public_block     = TBS_public_block;
383
  lev->l.public_message   = TBS_public_message;
384
 
385
  /* fill the TBS descriptor part */
386
 
387
  for (i = 0; i < MAX_PROC; i++) {
388
    lev->nact[i] = 0;
389
    lev->flag[i] = 0;
390
  }
391
 
392
  NULL_TIMESPEC(&lev->lastdline);
393
 
394
  iq_init(&lev->wait, &freedesc, 0);
395
  lev->activated = NIL;
396
 
397
  lev->U = (MAX_BANDWIDTH / den) * num;
398
  lev->band_num = num;
399
  lev->band_den = den;
400
 
401
  lev->scheduling_level = master;
402
 
403
  lev->flags = flags & 0x07;
404
}
405
 
406
bandwidth_t TBS_usedbandwidth(LEVEL l)
407
{
408
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
409
 
410
  return lev->U;
411
}
412
 
413
int TBS_get_nact(LEVEL l, PID p)
414
{
415
  TBS_level_des *lev = (TBS_level_des *)(level_table[l]);
416
 
417
  return lev->nact[p];
418
}
419