Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1085 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: cash.c,v 1.1.1.1 2002-09-02 09:37:41 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1.1.1 $
27
 Last update: $Date: 2002-09-02 09:37:41 $
28
 ------------
29
 
30
 This file contains the aperiodic server CBS (Total Bandwidth Server)
31
 
32
 Read CBS.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 "cash.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 CBS_TEST
66
 
67
#ifdef TESTG
68
#include "drivers/glib.h"
69
TIME x,oldx;
70
extern TIME starttime;
71
#endif
72
 
73
 
74
 
75
 
76
 
77
 
78
/*+ Status used in the level +*/
79
#define CBSGHD_IDLE          APER_STATUS_BASE   /*+ waiting the activation +*/
80
#define CBSGHD_ZOMBIE        APER_STATUS_BASE+1 /*+ waiting the period end +*/
81
#define CBSGHD_DELAY         APER_STATUS_BASE+2 /*+ waiting the delay end  +*/
82
 
83
/* structure of an element of the capacity queue  */
84
struct cap_queue {
85
  int              cap;
86
  struct timespec  dead;
87
  struct cap_queue *next;
88
};
89
 
90
/*+ the level redefinition for the CBS_HD level +*/
91
typedef struct {
92
  level_des l;     /*+ the standard level descriptor          +*/
93
 
94
  /* The wcet are stored in the task descriptor, but we need
95
     an array for the deadlines. We can't use the timespec_priority
96
     field because it is used by the master level!!!...
97
     Notice that however the use of the timespec_priority field
98
     does not cause any problem...                     */
99
 
100
  struct timespec cbsghd_dline[MAX_PROC]; /*+ CBSGHD deadlines      +*/
101
 
102
  TIME period[MAX_PROC]; /*+ CBSGHD activation period            +*/
103
 
104
  TIME maxperiod[MAX_PROC]; /*+ maximum period of each elastic task    +*/
105
 
106
  int cremaining[MAX_PROC]; /*+ instance remaining computation time +*/
107
 
108
  TIME act_period[MAX_PROC]; /*+ actual period of each elastic task: it
109
                               must be less than maxperiod!!!        +*/
110
 
111
  TIME last_response_time[MAX_PROC]; /* response time of the last instance */
112
 
113
  TIME cnormal[MAX_PROC]; /*+ CBSGHD normal computation time    +*/
114
 
115
  struct timespec reactivation_time[MAX_PROC];
116
        /*+ the time at witch  the reactivation timer is post +*/
117
  int reactivation_timer[MAX_PROC];
118
                                   /*+ the recativation timer +*/
119
 
120
  struct cap_queue *queue;         /* pointer to the spare capacity queue */
121
 
122
  int flags;       /*+ the init flags...                      +*/
123
 
124
  bandwidth_t U;   /*+ the used bandwidth by the server       +*/
125
 
126
  int idle;         /* the idle flag...  */
127
 
128
  struct timespec start_idle; /*gives the start time of the last idle period */
129
 
130
  LEVEL scheduling_level;
131
 
132
} CBSGHD_level_des;
133
 
134
 
135
/* insert a capacity in the queue capacity ordering by deadline */
136
 
137
static int c_insert(struct timespec dead, int cap, struct cap_queue **que,
138
                     PID p)
139
{
140
  struct cap_queue *prev, *n, *new;
141
 
142
    prev = NULL;
143
    n = *que;
144
 
145
    while ((n != NULL) &&
146
           !TIMESPEC_A_LT_B(&dead, &n->dead)) {
147
        prev = n;
148
        n = n->next;
149
    }
150
 
151
 
152
    new = (struct cap_queue *)kern_alloc(sizeof(struct cap_queue));
153
    if (new == NULL) {
154
      kern_printf("\nNew cash_queue element failed\n");
155
      kern_raise(XUNVALID_TASK, p);
156
      return -1;
157
    }
158
    new->next = NULL;
159
    new->cap = cap;
160
    new->dead = dead;
161
 
162
    if (prev != NULL)
163
      prev->next = new;
164
    else
165
      *que = new;
166
 
167
    if (n != NULL)
168
      new->next = n;
169
    return 0;
170
}
171
 
172
/* extract the first element from the capacity queue */
173
 
174
static int c_extractfirst(struct cap_queue **que)
175
{
176
    struct cap_queue *p = *que;
177
 
178
 
179
    if (*que == NULL) return(-1);
180
 
181
    *que = (*que)->next;
182
 
183
    kern_free(p, sizeof(struct cap_queue));
184
    return(1);
185
}
186
 
187
/* read data of the first element from the capacity queue */
188
 
189
static void c_readfirst(struct timespec *d, int *c, struct cap_queue *que)
190
{
191
    *d = que->dead;
192
    *c = que->cap;
193
}
194
 
195
/* write data of the first element from the capacity queue */
196
 
197
static void c_writefirst(struct timespec dead, int cap, struct cap_queue *que)
198
{
199
    que->dead = dead;
200
    que->cap = cap;
201
}
202
 
203
 
204
static void CBSGHD_activation(CBSGHD_level_des *lev,
205
                             PID p,
206
                             struct timespec *acttime)
207
{
208
  JOB_TASK_MODEL job;
209
 
210
 
211
  /* This rule is used when we recharge the budget at initial task activation
212
     and each time a new task instance must be activated  */
213
 
214
  if (TIMESPEC_A_GT_B(acttime, &lev->cbsghd_dline[p])) {
215
    /* we modify the deadline ... */
216
    TIMESPEC_ASSIGN(&lev->cbsghd_dline[p], acttime);
217
  }
218
 
219
  lev->act_period[p] = 0;
220
 
221
  if (proc_table[p].avail_time > 0)
222
    proc_table[p].avail_time = 0;
223
 
224
 
225
 
226
 
227
  /* there is a while because if the wcet is << than the system tick
228
     we need to postpone the deadline many times */
229
  while (proc_table[p].avail_time <= 0) {
230
 
231
    /* A spare capacity is inserted in the capacity queue!! */
232
    ADDUSEC2TIMESPEC(lev->period[p], &lev->cbsghd_dline[p]);
233
    lev->act_period[p] += lev->period[p];
234
    c_insert(lev->cbsghd_dline[p], lev->cnormal[p], &lev->queue, p);
235
 
236
 
237
    /* it exploits available capacities from the capacity queue */
238
    while (proc_table[p].avail_time < (int)lev->cnormal[p] &&
239
           lev->queue != NULL) {
240
      struct timespec dead;
241
      int             cap, delta;
242
      delta = lev->cnormal[p] - proc_table[p].avail_time;
243
      c_readfirst(&dead, &cap, lev->queue);
244
      if (!TIMESPEC_A_GT_B(&dead, &lev->cbsghd_dline[p])) {
245
        if (cap > delta) {
246
          proc_table[p].avail_time += delta;
247
          c_writefirst(dead, cap - delta, lev->queue);
248
        }
249
        else {
250
          proc_table[p].avail_time += cap;
251
          c_extractfirst(&lev->queue);
252
        }
253
      }
254
      else
255
        break;
256
    }
257
  }
258
 
259
  lev->cremaining[p] = proc_table[p].wcet - proc_table[p].avail_time;
260
 
261
 
262
#ifdef TESTG
263
  if (starttime && p == 3) {
264
    oldx = x;
265
    x = ((lev->cbsghd_dline[p].tv_sec*1000000+lev->cbsghd_dline[p].tv_nsec/1000)/5000 - starttime) + 20;
266
    //      kern_printf("(a%d)",lev->cbsghd_dline[p].tv_sec*1000000+lev->cbsghd_dline[p].tv_nsec/1000);
267
    if (oldx > x) sys_end();
268
    if (x<640)
269
      grx_plot(x, 15, 8);
270
  }
271
#endif
272
 
273
  /* and, finally, we reinsert the task in the master level */
274
  job_task_default_model(job, lev->cbsghd_dline[p]);
275
  job_task_def_yesexc(job);
276
  level_table[ lev->scheduling_level ]->
277
    guest_create(lev->scheduling_level, p, (TASK_MODEL *)&job);
278
  level_table[ lev->scheduling_level ]->
279
    guest_activate(lev->scheduling_level, p);
280
}
281
 
282
 
283
static char *CBSGHD_status_to_a(WORD status)
284
{
285
  if (status < MODULE_STATUS_BASE)
286
    return status_to_a(status);
287
 
288
  switch (status) {
289
    case CBSGHD_IDLE   : return "CBSGHD_Idle";
290
    case CBSGHD_ZOMBIE : return "CBSGHD_Zombie";
291
    case CBSGHD_DELAY  : return "CBSGHD_Delay";
292
    default         : return "CBSGHD_Unknown";
293
  }
294
}
295
 
296
 
297
 
298
 
299
/* this is the periodic reactivation of the task... */
300
static void CBSGHD_timer_reactivate(void *par)
301
{
302
  PID p = (PID) par;
303
  CBSGHD_level_des *lev;
304
 
305
  lev = (CBSGHD_level_des *)level_table[proc_table[p].task_level];
306
 
307
  if (proc_table[p].status == CBSGHD_IDLE) {
308
    /* the task has finished the current activation and must be
309
       reactivated */
310
 
311
    /* request_time represents the time of the last instance release!! */
312
    TIMESPEC_ASSIGN(&proc_table[p].request_time, &lev->reactivation_time[p]);
313
 
314
    /* If idle=1, then we have to discharge the capacities stored in
315
       the capacity queue up to the length of the idle interval */
316
    if (lev->idle == 1) {
317
      TIME interval;
318
      struct timespec delta;
319
      lev->idle = 0;
320
      SUBTIMESPEC(&proc_table[p].request_time, &lev->start_idle, &delta);
321
      /* length of the idle interval expressed in usec! */
322
      interval = TIMESPEC2NANOSEC(&delta) / 1000;
323
 
324
      /* it discharge the available capacities from the capacity queue */
325
      while (interval > 0 && lev->queue != NULL) {
326
        struct timespec dead;
327
        int             cap;
328
        c_readfirst(&dead, &cap, lev->queue);
329
        if (cap > interval) {
330
          c_writefirst(dead, cap - interval, lev->queue);
331
          interval = 0;
332
        }
333
        else {
334
          interval -= cap;
335
          c_extractfirst(&lev->queue);
336
        }      
337
      }
338
    }
339
 
340
    CBSGHD_activation(lev,p,&lev->reactivation_time[p]);
341
 
342
    /* check the constraint on the maximum period permitted... */
343
    if (lev->act_period[p] > lev->maxperiod[p]) {
344
      kern_printf("Deadline miss(timer_react.! process:%d act_period:%lu maxperiod:%lu\n",
345
                  p, lev->act_period[p], lev->maxperiod[p]);
346
      kern_raise(XDEADLINE_MISS,p);
347
    }
348
 
349
 
350
    /* Set the reactivation timer */
351
    TIMESPEC_ASSIGN(&lev->reactivation_time[p], &lev->cbsghd_dline[p]);
352
    lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
353
                                                 CBSGHD_timer_reactivate,
354
                                                 (void *)p);
355
    event_need_reschedule();
356
  }
357
  else {
358
    /* this situation cannot occur */
359
    kern_printf("Trying to reactivate a task which is not IDLE!!!/n");
360
    kern_raise(XUNVALID_TASK,p);
361
  }
362
}
363
 
364
 
365
 
366
 
367
 
368
static void CBSGHD_avail_time_check(CBSGHD_level_des *lev, PID p)
369
{
370
 
371
  /*+ if the capacity became negative the remaining computation time
372
    is diminuished.... +*/
373
  /* if (p==4)
374
    kern_printf("(old dead:%d av_time:%d crem:%d)\n",
375
                lev->cbsghd_dline[p].tv_sec*1000000+
376
                lev->cbsghd_dline[p].tv_nsec/1000, proc_table[p].avail_time,
377
                lev->cremaining[p]);  */
378
 
379
 
380
  if (proc_table[p].avail_time < 0)
381
    lev->cremaining[p] += proc_table[p].avail_time;
382
 
383
  if (lev->cremaining[p] <= 0) {
384
    kern_printf("Task:%d   WCET violation \n", p);
385
    kern_raise(XWCET_VIOLATION, p);
386
    ll_abort(666);
387
  }
388
 
389
 
390
  /* there is a while because if the wcet is << than the system tick
391
     we need to postpone the deadline many times */
392
  while (proc_table[p].avail_time <= 0) {
393
    /* it exploits available capacities from the capacity queue */
394
    while (proc_table[p].avail_time < lev->cremaining[p]
395
           && lev->queue != NULL) {
396
      struct timespec dead;
397
      int             cap, delta;
398
      delta = lev->cremaining[p] - proc_table[p].avail_time;
399
      c_readfirst(&dead, &cap, lev->queue);
400
      if (!TIMESPEC_A_GT_B(&dead, &lev->cbsghd_dline[p])) {
401
        if (cap > delta) {
402
          proc_table[p].avail_time += delta;
403
          c_writefirst(dead, cap - delta, lev->queue);
404
        }
405
        else {
406
          proc_table[p].avail_time += cap;
407
          c_extractfirst(&lev->queue);
408
        }
409
      }
410
      else
411
        break;
412
    }
413
 
414
    /* if (p==5 && proc_table[p].avail_time <= 0 &&
415
       lev->cremaining[p] > lev->cnormal[p])
416
       kern_printf("(inter dead:%d av_time:%d crem:%d)\n",
417
       lev->cbsghd_dline[p].tv_sec*1000000+
418
       lev->cbsghd_dline[p].tv_nsec/1000, proc_table[p].avail_time,
419
       lev->cremaining[p]); */
420
 
421
 
422
    /* The remaining computation time is modified according
423
       to the new budget! */
424
    if (proc_table[p].avail_time > 0)
425
      lev->cremaining[p] -= proc_table[p].avail_time;
426
    else {
427
      /* the CBSGHD rule for recharging the capacity:  */
428
      if (lev->cremaining[p] > lev->cnormal[p]) {
429
        ADDUSEC2TIMESPEC(lev->period[p], &lev->cbsghd_dline[p]);
430
        lev->act_period[p] += lev->period[p];
431
        /* A spare capacity is inserted in the capacity queue!! */
432
        c_insert(lev->cbsghd_dline[p], lev->cnormal[p], &lev->queue, p);
433
      }
434
      else {
435
        TIME t;
436
        t = (lev->cremaining[p] * lev->period[p]) / lev->cnormal[p];
437
        ADDUSEC2TIMESPEC(t, &lev->cbsghd_dline[p]);
438
        lev->act_period[p] += t;
439
        /* A spare capacity is inserted in the capacity queue!! */
440
        c_insert(lev->cbsghd_dline[p], lev->cremaining[p], &lev->queue, p);
441
      }
442
    }
443
  }
444
 
445
  /*  if (p==4)
446
    kern_printf("n dead:%d av_time:%d crem:%d)\n",
447
                lev->cbsghd_dline[p].tv_sec*1000000+
448
                lev->cbsghd_dline[p].tv_nsec/1000, proc_table[p].avail_time,
449
                lev->cremaining[p]);  */
450
 
451
  /* check the constraint on the maximum period permitted... */
452
  if (lev->act_period[p] > lev->maxperiod[p]) {
453
    /*kern_printf("n dead:%d av_time:%d crem:%d)\n",
454
                lev->cbsghd_dline[p].tv_sec*1000000+
455
                lev->cbsghd_dline[p].tv_nsec/1000, proc_table[p].avail_time,
456
                lev->cremaining[p]);  */
457
    kern_printf("Deadline miss(av.time_check! process:%d act_period:%lu maxperiod:%lu\n",
458
                p, lev->act_period[p], lev->maxperiod[p]);
459
    kern_raise(XDEADLINE_MISS,p);
460
  }
461
 
462
 
463
 
464
  if (TIMESPEC_A_LT_B(&lev->reactivation_time[p], &lev->cbsghd_dline[p])) {
465
    /* we delete the reactivation timer */
466
    event_delete(lev->reactivation_timer[p]);
467
    /* repost the event at the next instance deadline... */
468
    lev->reactivation_time[p] = lev->cbsghd_dline[p];
469
    lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
470
                                                 CBSGHD_timer_reactivate,
471
                                                 (void *)p);
472
  }
473
 
474
#ifdef TESTG
475
  if (starttime && p == 3) {
476
    oldx = x;
477
    x = ((lev->cbsghd_dline[p].tv_sec*1000000+
478
          lev->cbsghd_dline[p].tv_nsec/1000)/5000 - starttime) + 20;
479
    //      kern_printf("(e%d avail%d)",lev->cbsghd_dline[p].tv_sec*1000000+
480
                        lev->cbsghd_dline[p].tv_nsec/1000,proc_table[p].avail_time);
481
    if (oldx > x) sys_end();
482
    if (x<640)
483
      grx_plot(x, 15, 2);
484
  }
485
#endif
486
 
487
}
488
 
489
 
490
/*+ this function is called when a killed or ended task reach the
491
  period end +*/
492
static void CBSGHD_timer_zombie(void *par)
493
{
494
  PID p = (PID) par;
495
  CBSGHD_level_des *lev;
496
 
497
  lev = (CBSGHD_level_des *)level_table[proc_table[p].task_level];
498
 
499
  /* we finally put the task in the FREE status */
500
  proc_table[p].status = FREE;
501
  q_insertfirst(p,&freedesc);
502
 
503
  /* and free the allocated bandwidth */
504
  lev->U -= (MAX_BANDWIDTH/lev->period[p]) * lev->cnormal[p];
505
 
506
}
507
 
508
 
509
static int CBSGHD_level_accept_task_model(LEVEL l, TASK_MODEL *m)
510
{
511
 
512
 
513
  if (m->pclass == ELASTIC_HARD_PCLASS || m->pclass ==
514
      (ELASTIC_HARD_PCLASS | l)) {
515
    ELASTIC_HARD_TASK_MODEL *s = (ELASTIC_HARD_TASK_MODEL *)m;
516
    bandwidth_t b1, b2;
517
    /*  kern_printf("accept :ELASTIC TASK found!!!!!!\n"); */
518
    b1 = (MAX_BANDWIDTH / s->period) * s->cnormal;
519
    b2 = (MAX_BANDWIDTH / s->maxperiod) * s->wcet;
520
    if (s->wcet && s->cnormal && s->period && s->maxperiod &&
521
        s->wcet >= s->cnormal && b1 >= b2)
522
      return 0;
523
    /*  kern_printf("period: %d maxperiod: %d cnormal: %d wcet: %d, b1: %d b2:
524
              %d\n", s->period, s->maxperiod, s->cnormal, s->wcet, b1, b2); */
525
  }
526
  return -1;
527
}
528
 
529
static int CBSGHD_level_accept_guest_model(LEVEL l, TASK_MODEL *m)
530
{
531
  return -1;
532
}
533
 
534
static char *onoff(int i)
535
{
536
  if (i)
537
    return "On ";
538
  else
539
    return "Off";
540
}
541
 
542
static void CBSGHD_level_status(LEVEL l)
543
{
544
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
545
  PID p;
546
 
547
  kern_printf("On-line guarantee : %s\n",
548
              onoff(lev->flags & CBSGHD_ENABLE_GUARANTEE));
549
  kern_printf("Used Bandwidth    : %u/%u\n",
550
              lev->U, MAX_BANDWIDTH);
551
 
552
  for (p=0; p<MAX_PROC; p++)
553
    if (proc_table[p].task_level == l && proc_table[p].status != FREE )
554
      kern_printf("Pid: %2d Name: %10s Period: %9ld Dline: %9ld.%6ld Stat: %s\n",
555
                  p,
556
                  proc_table[p].name,
557
                  lev->period[p],
558
                  lev->cbsghd_dline[p].tv_sec,
559
                  lev->cbsghd_dline[p].tv_nsec/1000,
560
                  CBSGHD_status_to_a(proc_table[p].status));
561
}
562
 
563
static PID CBSGHD_level_scheduler(LEVEL l)
564
{
565
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
566
 
567
  /* it stores the actual time and set the IDLE flag in order to handle
568
     the capacity queue discharging!!! */
569
  lev->idle = 1;
570
  ll_gettime(TIME_EXACT, &lev->start_idle);
571
 
572
 
573
  /* the CBSGHD don't schedule anything...
574
     it's an EDF level or similar that do it! */
575
  return NIL;
576
}
577
 
578
/* The on-line guarantee is enabled only if the appropriate flag is set... */
579
static int CBSGHD_level_guarantee(LEVEL l, bandwidth_t *freebandwidth)
580
{
581
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
582
 
583
  if (lev->flags & CBSGHD_FAILED_GUARANTEE) {
584
    *freebandwidth = 0;
585
    //kern_printf("guarantee :garanzia fallita!!!!!!\n");
586
    return 0;
587
  }
588
  else if (*freebandwidth >= lev->U) {
589
    *freebandwidth -= lev->U;
590
    return 1;
591
  }
592
  else {
593
    //kern_printf("guarantee :garanzia fallita per mancanza di banda!!!!!!\n");
594
    //kern_printf("freeband: %d request band: %d", *freebandwidth, lev->U);
595
    return 0;
596
  }
597
}
598
 
599
static int CBSGHD_task_create(LEVEL l, PID p, TASK_MODEL *m)
600
{
601
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
602
 
603
  /* if the CBSGHD_task_create is called, then the pclass must be a
604
     valid pclass. */
605
  ELASTIC_HARD_TASK_MODEL *s = (ELASTIC_HARD_TASK_MODEL *)m;
606
 
607
  /* Enable wcet check */
608
  proc_table[p].avail_time = 0;
609
  proc_table[p].wcet       = s->wcet;
610
  proc_table[p].control   |= CONTROL_CAP;
611
 
612
  lev->period[p] = s->period;
613
  lev->maxperiod[p] = s->maxperiod;
614
  lev->cnormal[p] = s->cnormal;
615
  NULL_TIMESPEC(&lev->cbsghd_dline[p]);
616
 
617
 
618
  /* update the bandwidth... */
619
  if (lev->flags & CBSGHD_ENABLE_GUARANTEE) {
620
    bandwidth_t b;
621
    b = (MAX_BANDWIDTH / s->period) * s->cnormal;
622
 
623
    /* really update lev->U, checking an overflow... */
624
    if (MAX_BANDWIDTH - lev->U > b)
625
      lev->U += b;
626
    else
627
      /* The task can NOT be guaranteed (U>MAX_BANDWIDTH)...
628
         (see EDF.c) */
629
      lev->flags |= CBSGHD_FAILED_GUARANTEE;
630
  }
631
 
632
 
633
 
634
  return 0; /* OK, also if the task cannot be guaranteed... */
635
}
636
 
637
static void CBSGHD_task_detach(LEVEL l, PID p)
638
{
639
  /* the CBSGHD level doesn't introduce any dinamic allocated new field.
640
     we have only to reset the NO_GUARANTEE FIELD and decrement the allocated
641
     bandwidth */
642
 
643
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
644
 
645
  if (lev->flags & CBSGHD_FAILED_GUARANTEE)
646
    lev->flags &= ~CBSGHD_FAILED_GUARANTEE;
647
  else
648
    lev->U -= (MAX_BANDWIDTH / lev->period[p]) * lev->cnormal[p];
649
 
650
 
651
}
652
 
653
static int CBSGHD_task_eligible(LEVEL l, PID p)
654
{
655
  return 0; /* if the task p is chosen, it is always eligible */
656
}
657
 
658
#ifdef __TEST1__
659
  extern int testactive;
660
  extern struct timespec s_stime[];
661
  extern TIME s_curr[];
662
  extern TIME s_PID[];
663
  extern int useds;
664
#endif
665
 
666
static void CBSGHD_task_dispatch(LEVEL l, PID p, int nostop)
667
{
668
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
669
  level_table[ lev->scheduling_level ]->
670
    guest_dispatch(lev->scheduling_level,p,nostop);
671
 
672
#ifdef __TEST1__
673
  if (testactive)
674
    {
675
      TIMESPEC_ASSIGN(&s_stime[useds], &schedule_time);
676
      s_curr[useds] = proc_table[p].avail_time;
677
      s_PID[useds]  = p;
678
      useds++;
679
    }
680
#endif
681
}
682
 
683
static void CBSGHD_task_epilogue(LEVEL l, PID p)
684
{
685
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
686
  JOB_TASK_MODEL job;
687
 
688
  /* check if the budget is finished... */
689
  if ( proc_table[p].avail_time <= 0) {
690
    /* we kill the current activation */
691
    level_table[ lev->scheduling_level ]->
692
      guest_end(lev->scheduling_level, p);
693
 
694
    /* we modify the deadline */
695
    CBSGHD_avail_time_check(lev, p);
696
 
697
    /* and, finally, we reinsert the task in the master level */
698
    job_task_default_model(job, lev->cbsghd_dline[p]);
699
    job_task_def_yesexc(job);
700
    level_table[ lev->scheduling_level ]->
701
      guest_create(lev->scheduling_level, p, (TASK_MODEL *)&job);
702
    level_table[ lev->scheduling_level ]->
703
      guest_activate(lev->scheduling_level, p);
704
    //    kern_printf("epil : dl %d per %d p %d |\n",
705
                      //              lev->cbsghd_dline[p].tv_nsec/1000,lev->period[p],p);
706
 
707
  }
708
  else
709
    /* the task has been preempted. it returns into the ready queue by
710
       calling the guest_epilogue... */
711
    level_table[ lev->scheduling_level ]->
712
      guest_epilogue(lev->scheduling_level,p);
713
}
714
 
715
static void CBSGHD_task_activate(LEVEL l, PID p)
716
{
717
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
718
 
719
  ll_gettime(TIME_EXACT, &proc_table[p].request_time);
720
 
721
  /* If idle=1, then we have to discharge the capacities stored in
722
     the capacity queue up to the length of the idle interval */
723
  if (lev->idle == 1) {
724
    TIME interval;
725
    struct timespec delta;
726
    lev->idle = 0;
727
    SUBTIMESPEC(&proc_table[p].request_time, &lev->start_idle, &delta);
728
    /* length of the idle interval expressed in usec! */
729
    interval = TIMESPEC2NANOSEC(&delta) / 1000;
730
 
731
    /* it discharge the available capacities from the capacity queue */
732
    while (interval > 0 && lev->queue != NULL) {
733
      struct timespec dead;
734
      int             cap;
735
      c_readfirst(&dead, &cap, lev->queue);
736
      if (cap > interval) {
737
        c_writefirst(dead, cap - interval, lev->queue);
738
        interval = 0;
739
      }
740
      else {
741
        interval -= cap;
742
        c_extractfirst(&lev->queue);
743
      }
744
    }
745
  }
746
 
747
  CBSGHD_activation(lev, p, &proc_table[p].request_time);
748
 
749
 
750
  /* check the constraint on the maximum period permitted... */
751
  if (lev->act_period[p] > lev->maxperiod[p]) {
752
      kern_printf("Deadline miss(task_activ.! process:%d act_period:%lu maxperiod:%lu\n",
753
                  p, lev->act_period[p], lev->maxperiod[p]);
754
    kern_raise(XDEADLINE_MISS,p);
755
  }
756
 
757
  /* Set the reactivation timer */
758
  TIMESPEC_ASSIGN(&lev->reactivation_time[p], &lev->cbsghd_dline[p]);
759
  lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
760
                                                   CBSGHD_timer_reactivate,
761
                                                   (void *)p);
762
 
763
  //  kern_printf("act : %d %d |",lev->cbsghd_dline[p].tv_nsec/1000,p);
764
}
765
 
766
static void CBSGHD_task_insert(LEVEL l, PID p)
767
{
768
  printk("CBSGHD_task_insert\n");
769
  kern_raise(XUNVALID_TASK,p);
770
}
771
 
772
static void CBSGHD_task_extract(LEVEL l, PID p)
773
{
774
  printk("CBSGHD_task_extract\n");
775
  kern_raise(XUNVALID_TASK,p);
776
}
777
 
778
static void CBSGHD_task_endcycle(LEVEL l, PID p)
779
{
780
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
781
  struct timespec act_time, res;
782
 
783
  /* It computes the response time of the current instance... */
784
  ll_gettime(TIME_EXACT, &act_time);
785
  SUBTIMESPEC(&act_time, &proc_table[p].request_time, &res);
786
  /* response time expressed in usec! */
787
  lev->last_response_time[p] = TIMESPEC2NANOSEC(&res) / 1000;
788
 
789
  level_table[ lev->scheduling_level ]->
790
    guest_end(lev->scheduling_level,p);
791
 
792
 
793
  /* A spare capacity is inserted in the capacity queue!! */
794
  if (proc_table[p].avail_time > 0) {
795
    c_insert(lev->cbsghd_dline[p], proc_table[p].avail_time, &lev->queue, p);
796
    proc_table[p].avail_time = 0;
797
  }  
798
 
799
 
800
  proc_table[p].status = CBSGHD_IDLE;
801
}
802
 
803
static void CBSGHD_task_end(LEVEL l, PID p)
804
{
805
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
806
 
807
  /* check if the capacity became negative... */
808
  /* there is a while because if the wcet is << than the system tick
809
     we need to postpone the deadline many times */
810
  while (proc_table[p].avail_time < 0) {
811
    /* the CBSGHD rule for recharging the capacity */
812
    proc_table[p].avail_time += lev->cnormal[p];
813
    ADDUSEC2TIMESPEC(lev->period[p], &lev->cbsghd_dline[p]);
814
  }
815
 
816
  level_table[ lev->scheduling_level ]->
817
    guest_end(lev->scheduling_level,p);
818
 
819
  /* we delete the reactivation timer */
820
  event_delete(lev->reactivation_timer[p]);
821
  lev->reactivation_timer[p] = -1;
822
 
823
 
824
  /* Finally, we post the zombie event. when the end period is reached,
825
     the task descriptor and banwidth are freed */
826
  proc_table[p].status = CBSGHD_ZOMBIE;
827
  lev->reactivation_timer[p] = kern_event_post(&lev->cbsghd_dline[p],
828
                                               CBSGHD_timer_zombie,
829
                                               (void *)p);
830
}
831
 
832
static void CBSGHD_task_sleep(LEVEL l, PID p)
833
{
834
  printk("CBSGHD_task_sleep\n");
835
  kern_raise(XUNVALID_TASK,p);
836
}
837
 
838
static void CBSGHD_task_delay(LEVEL l, PID p, TIME usdelay)
839
{
840
  printk("CBSGHD_task_delay\n");
841
  kern_raise(XUNVALID_TASK,p);
842
}
843
 
844
 
845
static int CBSGHD_guest_create(LEVEL l, PID p, TASK_MODEL *m)
846
{ kern_raise(XUNVALID_GUEST,exec_shadow); return 0; }
847
 
848
static void CBSGHD_guest_detach(LEVEL l, PID p)
849
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
850
 
851
static void CBSGHD_guest_dispatch(LEVEL l, PID p, int nostop)
852
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
853
 
854
static void CBSGHD_guest_epilogue(LEVEL l, PID p)
855
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
856
 
857
static void CBSGHD_guest_activate(LEVEL l, PID p)
858
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
859
 
860
static void CBSGHD_guest_insert(LEVEL l, PID p)
861
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
862
 
863
static void CBSGHD_guest_extract(LEVEL l, PID p)
864
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
865
 
866
static void CBSGHD_guest_endcycle(LEVEL l, PID p)
867
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
868
 
869
static void CBSGHD_guest_end(LEVEL l, PID p)
870
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
871
 
872
static void CBSGHD_guest_sleep(LEVEL l, PID p)
873
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
874
 
875
static void CBSGHD_guest_delay(LEVEL l, PID p,DWORD tickdelay)
876
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
877
 
878
 
879
 
880
 
881
/* Registration functions */
882
 
883
/*+ Registration function:
884
    int flags                 the init flags ... see CBS.h +*/
885
void CBSGHD_register_level(int flags, LEVEL master)
886
{
887
  LEVEL l;            /* the level that we register */
888
  CBSGHD_level_des *lev;  /* for readableness only */
889
  PID i;              /* a counter */
890
 
891
  printk("CBSGHD_register_level\n");
892
 
893
  /* request an entry in the level_table */
894
  l = level_alloc_descriptor();
895
 
896
  printk("    alloco descrittore %d %d\n",l,sizeof(CBSGHD_level_des));
897
 
898
  /* alloc the space needed for the CBSGHD_level_des */
899
  lev = (CBSGHD_level_des *)kern_alloc(sizeof(CBSGHD_level_des));
900
 
901
  printk("    lev=%d\n",(int)lev);
902
 
903
  /* update the level_table with the new entry */
904
  level_table[l] = (level_des *)lev;
905
 
906
  /* fill the standard descriptor */
907
  strncpy(lev->l.level_name,  CBSGHD_LEVELNAME, MAX_LEVELNAME);
908
  lev->l.level_code               = CBSGHD_LEVEL_CODE;
909
  lev->l.level_version            = CBSGHD_LEVEL_VERSION;
910
 
911
  lev->l.level_accept_task_model  = CBSGHD_level_accept_task_model;
912
  lev->l.level_accept_guest_model = CBSGHD_level_accept_guest_model;
913
  lev->l.level_status             = CBSGHD_level_status;
914
  lev->l.level_scheduler          = CBSGHD_level_scheduler;
915
 
916
  if (flags & CBSGHD_ENABLE_GUARANTEE)
917
    lev->l.level_guarantee        = CBSGHD_level_guarantee;
918
  else
919
    lev->l.level_guarantee        = NULL;
920
 
921
  lev->l.task_create              = CBSGHD_task_create;
922
  lev->l.task_detach              = CBSGHD_task_detach;
923
  lev->l.task_eligible            = CBSGHD_task_eligible;
924
  lev->l.task_dispatch            = CBSGHD_task_dispatch;
925
  lev->l.task_epilogue            = CBSGHD_task_epilogue;
926
  lev->l.task_activate            = CBSGHD_task_activate;
927
  lev->l.task_insert              = CBSGHD_task_insert;
928
  lev->l.task_extract             = CBSGHD_task_extract;
929
  lev->l.task_endcycle            = CBSGHD_task_endcycle;
930
  lev->l.task_end                 = CBSGHD_task_end;
931
  lev->l.task_sleep               = CBSGHD_task_sleep;
932
  lev->l.task_delay               = CBSGHD_task_delay;
933
 
934
  lev->l.guest_create             = CBSGHD_guest_create;
935
  lev->l.guest_detach             = CBSGHD_guest_detach;
936
  lev->l.guest_dispatch           = CBSGHD_guest_dispatch;
937
  lev->l.guest_epilogue           = CBSGHD_guest_epilogue;
938
  lev->l.guest_activate           = CBSGHD_guest_activate;
939
  lev->l.guest_insert             = CBSGHD_guest_insert;
940
  lev->l.guest_extract            = CBSGHD_guest_extract;
941
  lev->l.guest_endcycle           = CBSGHD_guest_endcycle;
942
  lev->l.guest_end                = CBSGHD_guest_end;
943
  lev->l.guest_sleep              = CBSGHD_guest_sleep;
944
  lev->l.guest_delay              = CBSGHD_guest_delay;
945
 
946
  /* fill the CBSGHD descriptor part */
947
  for (i=0; i<MAX_PROC; i++) {
948
     NULL_TIMESPEC(&lev->cbsghd_dline[i]);
949
     lev->period[i] = 0;
950
     lev->last_response_time[i] = 0;
951
     NULL_TIMESPEC(&lev->reactivation_time[i]);
952
     lev->reactivation_timer[i] = -1;
953
  }
954
 
955
 
956
  lev->U = 0;
957
  lev->idle = 0;
958
  lev->queue = NULL;
959
 
960
  lev->scheduling_level = master;
961
 
962
  lev->flags = flags & 0x07;
963
}
964
 
965
 
966
int CBSGHD_get_response_time(LEVEL l, PID p)
967
{
968
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
969
  return lev->last_response_time[p];
970
}
971
 
972
 
973
bandwidth_t CBSGHD_usedbandwidth(LEVEL l)
974
{
975
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
976
  if (lev->l.level_code    == CBSGHD_LEVEL_CODE &&
977
      lev->l.level_version == CBSGHD_LEVEL_VERSION)
978
    return lev->U;
979
  else
980
    return 0;
981
}
982