Subversion Repositories shark

Rev

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