Subversion Repositories shark

Rev

Rev 1108 | 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
 ------------
1123 pj 23
 CVS :        $Id: cash.c,v 1.4 2003-01-07 17:10:16 pj Exp $
1085 pj 24
 
25
 File:        $File$
1123 pj 26
 Revision:    $Revision: 1.4 $
27
 Last update: $Date: 2003-01-07 17:10:16 $
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>
1123 pj 63
#include <kernel/trace.h>
1085 pj 64
 
65
 
66
/*+ Status used in the level +*/
67
#define CBSGHD_IDLE          APER_STATUS_BASE   /*+ waiting the activation +*/
68
#define CBSGHD_ZOMBIE        APER_STATUS_BASE+1 /*+ waiting the period end +*/
69
 
70
/* structure of an element of the capacity queue  */
71
struct cap_queue {
72
  int              cap;
73
  struct timespec  dead;
74
  struct cap_queue *next;
75
};
76
 
77
/*+ the level redefinition for the CBS_HD level +*/
78
typedef struct {
79
  level_des l;     /*+ the standard level descriptor          +*/
80
 
81
  /* The wcet are stored in the task descriptor, but we need
82
     an array for the deadlines. We can't use the timespec_priority
83
     field because it is used by the master level!!!...
84
     Notice that however the use of the timespec_priority field
85
     does not cause any problem...                     */
86
 
87
  struct timespec cbsghd_dline[MAX_PROC]; /*+ CBSGHD deadlines      +*/
88
 
89
  TIME period[MAX_PROC]; /*+ CBSGHD activation period            +*/
90
 
91
  TIME maxperiod[MAX_PROC]; /*+ maximum period of each elastic task    +*/
92
 
93
  int cremaining[MAX_PROC]; /*+ instance remaining computation time +*/
94
 
95
  TIME act_period[MAX_PROC]; /*+ actual period of each elastic task: it
96
                               must be less than maxperiod!!!        +*/
97
 
1123 pj 98
  struct timespec request_time[MAX_PROC]; /* used for the response time */
1085 pj 99
  TIME last_response_time[MAX_PROC]; /* response time of the last instance */
100
 
101
  TIME cnormal[MAX_PROC]; /*+ CBSGHD normal computation time    +*/
102
 
103
  struct timespec reactivation_time[MAX_PROC];
104
        /*+ the time at witch  the reactivation timer is post +*/
105
  int reactivation_timer[MAX_PROC];
106
                                   /*+ the recativation timer +*/
107
 
108
  struct cap_queue *queue;         /* pointer to the spare capacity queue */
109
 
110
  int flags;       /*+ the init flags...                      +*/
111
 
112
  bandwidth_t U;   /*+ the used bandwidth by the server       +*/
113
 
114
  int idle;         /* the idle flag...  */
115
 
116
  struct timespec start_idle; /*gives the start time of the last idle period */
117
 
118
  LEVEL scheduling_level;
119
 
120
} CBSGHD_level_des;
121
 
122
 
123
/* insert a capacity in the queue capacity ordering by deadline */
124
 
125
static int c_insert(struct timespec dead, int cap, struct cap_queue **que,
126
                     PID p)
127
{
128
  struct cap_queue *prev, *n, *new;
129
 
130
    prev = NULL;
131
    n = *que;
132
 
133
    while ((n != NULL) &&
134
           !TIMESPEC_A_LT_B(&dead, &n->dead)) {
135
        prev = n;
136
        n = n->next;
137
    }
138
 
139
 
140
    new = (struct cap_queue *)kern_alloc(sizeof(struct cap_queue));
141
    if (new == NULL) {
142
      kern_printf("\nNew cash_queue element failed\n");
1100 pj 143
      kern_raise(XINVALID_TASK, p);
1085 pj 144
      return -1;
145
    }
146
    new->next = NULL;
147
    new->cap = cap;
148
    new->dead = dead;
149
 
150
    if (prev != NULL)
151
      prev->next = new;
152
    else
153
      *que = new;
154
 
155
    if (n != NULL)
156
      new->next = n;
157
    return 0;
158
}
159
 
160
/* extract the first element from the capacity queue */
161
 
162
static int c_extractfirst(struct cap_queue **que)
163
{
164
    struct cap_queue *p = *que;
165
 
166
 
167
    if (*que == NULL) return(-1);
168
 
169
    *que = (*que)->next;
170
 
171
    kern_free(p, sizeof(struct cap_queue));
172
    return(1);
173
}
174
 
175
/* read data of the first element from the capacity queue */
176
 
177
static void c_readfirst(struct timespec *d, int *c, struct cap_queue *que)
178
{
179
    *d = que->dead;
180
    *c = que->cap;
181
}
182
 
183
/* write data of the first element from the capacity queue */
184
 
185
static void c_writefirst(struct timespec dead, int cap, struct cap_queue *que)
186
{
187
    que->dead = dead;
188
    que->cap = cap;
189
}
190
 
191
 
192
static void CBSGHD_activation(CBSGHD_level_des *lev,
193
                             PID p,
194
                             struct timespec *acttime)
195
{
196
  JOB_TASK_MODEL job;
197
 
198
 
199
  /* This rule is used when we recharge the budget at initial task activation
200
     and each time a new task instance must be activated  */
201
 
202
  if (TIMESPEC_A_GT_B(acttime, &lev->cbsghd_dline[p])) {
203
    /* we modify the deadline ... */
204
    TIMESPEC_ASSIGN(&lev->cbsghd_dline[p], acttime);
205
  }
206
 
207
  lev->act_period[p] = 0;
208
 
209
  if (proc_table[p].avail_time > 0)
210
    proc_table[p].avail_time = 0;
211
 
212
 
213
 
214
 
215
  /* there is a while because if the wcet is << than the system tick
216
     we need to postpone the deadline many times */
217
  while (proc_table[p].avail_time <= 0) {
218
 
219
    /* A spare capacity is inserted in the capacity queue!! */
220
    ADDUSEC2TIMESPEC(lev->period[p], &lev->cbsghd_dline[p]);
221
    lev->act_period[p] += lev->period[p];
222
    c_insert(lev->cbsghd_dline[p], lev->cnormal[p], &lev->queue, p);
223
 
224
 
225
    /* it exploits available capacities from the capacity queue */
226
    while (proc_table[p].avail_time < (int)lev->cnormal[p] &&
227
           lev->queue != NULL) {
228
      struct timespec dead;
229
      int             cap, delta;
230
      delta = lev->cnormal[p] - proc_table[p].avail_time;
231
      c_readfirst(&dead, &cap, lev->queue);
232
      if (!TIMESPEC_A_GT_B(&dead, &lev->cbsghd_dline[p])) {
233
        if (cap > delta) {
234
          proc_table[p].avail_time += delta;
235
          c_writefirst(dead, cap - delta, lev->queue);
236
        }
237
        else {
238
          proc_table[p].avail_time += cap;
239
          c_extractfirst(&lev->queue);
240
        }
241
      }
242
      else
243
        break;
244
    }
245
  }
246
 
247
  lev->cremaining[p] = proc_table[p].wcet - proc_table[p].avail_time;
248
 
249
 
250
#ifdef TESTG
251
  if (starttime && p == 3) {
252
    oldx = x;
253
    x = ((lev->cbsghd_dline[p].tv_sec*1000000+lev->cbsghd_dline[p].tv_nsec/1000)/5000 - starttime) + 20;
254
    //      kern_printf("(a%d)",lev->cbsghd_dline[p].tv_sec*1000000+lev->cbsghd_dline[p].tv_nsec/1000);
255
    if (oldx > x) sys_end();
256
    if (x<640)
257
      grx_plot(x, 15, 8);
258
  }
259
#endif
260
 
261
  /* and, finally, we reinsert the task in the master level */
262
  job_task_default_model(job, lev->cbsghd_dline[p]);
263
  job_task_def_yesexc(job);
264
  level_table[ lev->scheduling_level ]->
1123 pj 265
    private_insert(lev->scheduling_level, p, (TASK_MODEL *)&job);
1085 pj 266
}
267
 
268
 
269
/* this is the periodic reactivation of the task... */
270
static void CBSGHD_timer_reactivate(void *par)
271
{
272
  PID p = (PID) par;
273
  CBSGHD_level_des *lev;
274
 
275
  lev = (CBSGHD_level_des *)level_table[proc_table[p].task_level];
276
 
277
  if (proc_table[p].status == CBSGHD_IDLE) {
278
    /* the task has finished the current activation and must be
279
       reactivated */
280
 
281
    /* request_time represents the time of the last instance release!! */
1123 pj 282
    TIMESPEC_ASSIGN(&lev->request_time[p], &lev->reactivation_time[p]);
1085 pj 283
 
284
    /* If idle=1, then we have to discharge the capacities stored in
285
       the capacity queue up to the length of the idle interval */
286
    if (lev->idle == 1) {
287
      TIME interval;
288
      struct timespec delta;
289
      lev->idle = 0;
1123 pj 290
      SUBTIMESPEC(&lev->request_time[p], &lev->start_idle, &delta);
1085 pj 291
      /* length of the idle interval expressed in usec! */
292
      interval = TIMESPEC2NANOSEC(&delta) / 1000;
293
 
294
      /* it discharge the available capacities from the capacity queue */
295
      while (interval > 0 && lev->queue != NULL) {
296
        struct timespec dead;
297
        int             cap;
298
        c_readfirst(&dead, &cap, lev->queue);
299
        if (cap > interval) {
300
          c_writefirst(dead, cap - interval, lev->queue);
301
          interval = 0;
302
        }
303
        else {
304
          interval -= cap;
305
          c_extractfirst(&lev->queue);
306
        }      
307
      }
308
    }
309
 
310
    CBSGHD_activation(lev,p,&lev->reactivation_time[p]);
311
 
312
    /* check the constraint on the maximum period permitted... */
313
    if (lev->act_period[p] > lev->maxperiod[p]) {
314
      kern_printf("Deadline miss(timer_react.! process:%d act_period:%lu maxperiod:%lu\n",
315
                  p, lev->act_period[p], lev->maxperiod[p]);
316
      kern_raise(XDEADLINE_MISS,p);
317
    }
318
 
319
 
320
    /* Set the reactivation timer */
321
    TIMESPEC_ASSIGN(&lev->reactivation_time[p], &lev->cbsghd_dline[p]);
322
    lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
323
                                                 CBSGHD_timer_reactivate,
324
                                                 (void *)p);
325
    event_need_reschedule();
326
  }
327
  else {
328
    /* this situation cannot occur */
329
    kern_printf("Trying to reactivate a task which is not IDLE!!!/n");
1100 pj 330
    kern_raise(XINVALID_TASK,p);
1085 pj 331
  }
332
}
333
 
334
 
335
 
336
 
337
 
338
static void CBSGHD_avail_time_check(CBSGHD_level_des *lev, PID p)
339
{
340
 
341
  /*+ if the capacity became negative the remaining computation time
342
    is diminuished.... +*/
343
  /* if (p==4)
344
    kern_printf("(old dead:%d av_time:%d crem:%d)\n",
345
                lev->cbsghd_dline[p].tv_sec*1000000+
346
                lev->cbsghd_dline[p].tv_nsec/1000, proc_table[p].avail_time,
347
                lev->cremaining[p]);  */
348
 
349
 
350
  if (proc_table[p].avail_time < 0)
351
    lev->cremaining[p] += proc_table[p].avail_time;
352
 
353
  if (lev->cremaining[p] <= 0) {
354
    kern_printf("Task:%d   WCET violation \n", p);
355
    kern_raise(XWCET_VIOLATION, p);
356
    ll_abort(666);
357
  }
358
 
359
 
360
  /* there is a while because if the wcet is << than the system tick
361
     we need to postpone the deadline many times */
362
  while (proc_table[p].avail_time <= 0) {
363
    /* it exploits available capacities from the capacity queue */
364
    while (proc_table[p].avail_time < lev->cremaining[p]
365
           && lev->queue != NULL) {
366
      struct timespec dead;
367
      int             cap, delta;
368
      delta = lev->cremaining[p] - proc_table[p].avail_time;
369
      c_readfirst(&dead, &cap, lev->queue);
370
      if (!TIMESPEC_A_GT_B(&dead, &lev->cbsghd_dline[p])) {
371
        if (cap > delta) {
372
          proc_table[p].avail_time += delta;
373
          c_writefirst(dead, cap - delta, lev->queue);
374
        }
375
        else {
376
          proc_table[p].avail_time += cap;
377
          c_extractfirst(&lev->queue);
378
        }
379
      }
380
      else
381
        break;
382
    }
383
 
384
    /* if (p==5 && proc_table[p].avail_time <= 0 &&
385
       lev->cremaining[p] > lev->cnormal[p])
386
       kern_printf("(inter dead:%d av_time:%d crem:%d)\n",
387
       lev->cbsghd_dline[p].tv_sec*1000000+
388
       lev->cbsghd_dline[p].tv_nsec/1000, proc_table[p].avail_time,
389
       lev->cremaining[p]); */
390
 
391
 
392
    /* The remaining computation time is modified according
393
       to the new budget! */
394
    if (proc_table[p].avail_time > 0)
395
      lev->cremaining[p] -= proc_table[p].avail_time;
396
    else {
397
      /* the CBSGHD rule for recharging the capacity:  */
398
      if (lev->cremaining[p] > lev->cnormal[p]) {
399
        ADDUSEC2TIMESPEC(lev->period[p], &lev->cbsghd_dline[p]);
400
        lev->act_period[p] += lev->period[p];
401
        /* A spare capacity is inserted in the capacity queue!! */
402
        c_insert(lev->cbsghd_dline[p], lev->cnormal[p], &lev->queue, p);
403
      }
404
      else {
405
        TIME t;
406
        t = (lev->cremaining[p] * lev->period[p]) / lev->cnormal[p];
407
        ADDUSEC2TIMESPEC(t, &lev->cbsghd_dline[p]);
408
        lev->act_period[p] += t;
409
        /* A spare capacity is inserted in the capacity queue!! */
410
        c_insert(lev->cbsghd_dline[p], lev->cremaining[p], &lev->queue, p);
411
      }
412
    }
413
  }
414
 
415
  /*  if (p==4)
416
    kern_printf("n 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
  /* check the constraint on the maximum period permitted... */
422
  if (lev->act_period[p] > lev->maxperiod[p]) {
423
    /*kern_printf("n dead:%d av_time:%d crem:%d)\n",
424
                lev->cbsghd_dline[p].tv_sec*1000000+
425
                lev->cbsghd_dline[p].tv_nsec/1000, proc_table[p].avail_time,
426
                lev->cremaining[p]);  */
427
    kern_printf("Deadline miss(av.time_check! process:%d act_period:%lu maxperiod:%lu\n",
428
                p, lev->act_period[p], lev->maxperiod[p]);
429
    kern_raise(XDEADLINE_MISS,p);
430
  }
431
 
432
 
433
 
434
  if (TIMESPEC_A_LT_B(&lev->reactivation_time[p], &lev->cbsghd_dline[p])) {
435
    /* we delete the reactivation timer */
1123 pj 436
    kern_event_delete(lev->reactivation_timer[p]);
1085 pj 437
    /* repost the event at the next instance deadline... */
438
    lev->reactivation_time[p] = lev->cbsghd_dline[p];
439
    lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
440
                                                 CBSGHD_timer_reactivate,
441
                                                 (void *)p);
442
  }
443
 
444
#ifdef TESTG
445
  if (starttime && p == 3) {
446
    oldx = x;
447
    x = ((lev->cbsghd_dline[p].tv_sec*1000000+
448
          lev->cbsghd_dline[p].tv_nsec/1000)/5000 - starttime) + 20;
449
    //      kern_printf("(e%d avail%d)",lev->cbsghd_dline[p].tv_sec*1000000+
450
                        lev->cbsghd_dline[p].tv_nsec/1000,proc_table[p].avail_time);
451
    if (oldx > x) sys_end();
452
    if (x<640)
453
      grx_plot(x, 15, 2);
454
  }
455
#endif
456
 
457
}
458
 
459
 
460
/*+ this function is called when a killed or ended task reach the
461
  period end +*/
462
static void CBSGHD_timer_zombie(void *par)
463
{
464
  PID p = (PID) par;
465
  CBSGHD_level_des *lev;
466
 
467
  lev = (CBSGHD_level_des *)level_table[proc_table[p].task_level];
468
 
469
  /* we finally put the task in the FREE status */
470
  proc_table[p].status = FREE;
1108 pj 471
  iq_insertfirst(p,&freedesc);
1085 pj 472
 
473
  /* and free the allocated bandwidth */
474
  lev->U -= (MAX_BANDWIDTH/lev->period[p]) * lev->cnormal[p];
475
 
476
}
477
 
1123 pj 478
static PID CBSGHD_public_scheduler(LEVEL l)
1085 pj 479
{
480
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
481
 
482
  /* it stores the actual time and set the IDLE flag in order to handle
483
     the capacity queue discharging!!! */
484
  lev->idle = 1;
1123 pj 485
  kern_gettime(&lev->start_idle);
1085 pj 486
 
487
 
488
  /* the CBSGHD don't schedule anything...
489
     it's an EDF level or similar that do it! */
490
  return NIL;
491
}
492
 
493
/* The on-line guarantee is enabled only if the appropriate flag is set... */
1123 pj 494
static int CBSGHD_public_guarantee(LEVEL l, bandwidth_t *freebandwidth)
1085 pj 495
{
496
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
497
 
498
  if (lev->flags & CBSGHD_FAILED_GUARANTEE) {
499
    *freebandwidth = 0;
500
    //kern_printf("guarantee :garanzia fallita!!!!!!\n");
501
    return 0;
502
  }
503
  else if (*freebandwidth >= lev->U) {
504
    *freebandwidth -= lev->U;
505
    return 1;
506
  }
507
  else {
508
    //kern_printf("guarantee :garanzia fallita per mancanza di banda!!!!!!\n");
509
    //kern_printf("freeband: %d request band: %d", *freebandwidth, lev->U);
510
    return 0;
511
  }
512
}
513
 
1123 pj 514
static int CBSGHD_public_create(LEVEL l, PID p, TASK_MODEL *m)
1085 pj 515
{
516
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
1123 pj 517
  ELASTIC_HARD_TASK_MODEL *s;
518
  bandwidth_t b1, b2;
1085 pj 519
 
1123 pj 520
  if (m->pclass != ELASTIC_HARD_PCLASS) return -1;
521
  if (m->level != 0 && m->level != l) return -1;
522
  s = (ELASTIC_HARD_TASK_MODEL *)m;
1085 pj 523
 
1123 pj 524
  /*  kern_printf("accept :ELASTIC TASK found!!!!!!\n"); */
525
  b1 = (MAX_BANDWIDTH / s->period) * s->cnormal;
526
  b2 = (MAX_BANDWIDTH / s->maxperiod) * s->wcet;
527
  if (!(s->wcet && s->cnormal && s->period && s->maxperiod &&
528
        s->wcet >= s->cnormal && b1 >= b2) )
529
    return -1;
530
  /*  kern_printf("period: %d maxperiod: %d cnormal: %d wcet: %d, b1: %d b2:
531
      %d\n", s->period, s->maxperiod, s->cnormal, s->wcet, b1, b2); */
532
 
533
  /* now we know that m is a valid model */
534
 
535
 
1085 pj 536
  /* Enable wcet check */
537
  proc_table[p].avail_time = 0;
538
  proc_table[p].wcet       = s->wcet;
539
  proc_table[p].control   |= CONTROL_CAP;
540
 
541
  lev->period[p] = s->period;
542
  lev->maxperiod[p] = s->maxperiod;
543
  lev->cnormal[p] = s->cnormal;
544
  NULL_TIMESPEC(&lev->cbsghd_dline[p]);
1123 pj 545
  NULL_TIMESPEC(&lev->request_time[p]);
1085 pj 546
 
1123 pj 547
 
1085 pj 548
  /* update the bandwidth... */
549
  if (lev->flags & CBSGHD_ENABLE_GUARANTEE) {
550
    bandwidth_t b;
551
    b = (MAX_BANDWIDTH / s->period) * s->cnormal;
552
 
553
    /* really update lev->U, checking an overflow... */
554
    if (MAX_BANDWIDTH - lev->U > b)
555
      lev->U += b;
556
    else
557
      /* The task can NOT be guaranteed (U>MAX_BANDWIDTH)...
558
         (see EDF.c) */
559
      lev->flags |= CBSGHD_FAILED_GUARANTEE;
560
  }
561
 
562
 
563
 
564
  return 0; /* OK, also if the task cannot be guaranteed... */
565
}
566
 
1123 pj 567
static void CBSGHD_public_detach(LEVEL l, PID p)
1085 pj 568
{
569
  /* the CBSGHD level doesn't introduce any dinamic allocated new field.
570
     we have only to reset the NO_GUARANTEE FIELD and decrement the allocated
571
     bandwidth */
572
 
573
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
574
 
575
  if (lev->flags & CBSGHD_FAILED_GUARANTEE)
576
    lev->flags &= ~CBSGHD_FAILED_GUARANTEE;
577
  else
578
    lev->U -= (MAX_BANDWIDTH / lev->period[p]) * lev->cnormal[p];
579
 
580
 
581
}
582
 
1123 pj 583
static void CBSGHD_public_dispatch(LEVEL l, PID p, int nostop)
1085 pj 584
{
585
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
586
  level_table[ lev->scheduling_level ]->
1123 pj 587
    private_dispatch(lev->scheduling_level,p,nostop);
1085 pj 588
 
589
}
590
 
1123 pj 591
static void CBSGHD_public_epilogue(LEVEL l, PID p)
1085 pj 592
{
593
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
594
  JOB_TASK_MODEL job;
595
 
596
  /* check if the budget is finished... */
597
  if ( proc_table[p].avail_time <= 0) {
598
    /* we kill the current activation */
599
    level_table[ lev->scheduling_level ]->
1123 pj 600
      private_extract(lev->scheduling_level, p);
1085 pj 601
 
602
    /* we modify the deadline */
603
    CBSGHD_avail_time_check(lev, p);
604
 
605
    /* and, finally, we reinsert the task in the master level */
606
    job_task_default_model(job, lev->cbsghd_dline[p]);
607
    job_task_def_yesexc(job);
608
    level_table[ lev->scheduling_level ]->
1123 pj 609
      private_insert(lev->scheduling_level, p, (TASK_MODEL *)&job);
1085 pj 610
    //    kern_printf("epil : dl %d per %d p %d |\n",
611
                      //              lev->cbsghd_dline[p].tv_nsec/1000,lev->period[p],p);
612
 
613
  }
614
  else
615
    /* the task has been preempted. it returns into the ready queue by
616
       calling the guest_epilogue... */
617
    level_table[ lev->scheduling_level ]->
1123 pj 618
      private_epilogue(lev->scheduling_level,p);
1085 pj 619
}
620
 
1123 pj 621
static void CBSGHD_public_activate(LEVEL l, PID p)
1085 pj 622
{
623
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
624
 
1123 pj 625
  kern_gettime(&lev->request_time[p]);
1085 pj 626
 
627
  /* If idle=1, then we have to discharge the capacities stored in
628
     the capacity queue up to the length of the idle interval */
629
  if (lev->idle == 1) {
630
    TIME interval;
631
    struct timespec delta;
632
    lev->idle = 0;
1123 pj 633
    SUBTIMESPEC(&lev->request_time[p], &lev->start_idle, &delta);
1085 pj 634
    /* length of the idle interval expressed in usec! */
635
    interval = TIMESPEC2NANOSEC(&delta) / 1000;
636
 
637
    /* it discharge the available capacities from the capacity queue */
638
    while (interval > 0 && lev->queue != NULL) {
639
      struct timespec dead;
640
      int             cap;
641
      c_readfirst(&dead, &cap, lev->queue);
642
      if (cap > interval) {
643
        c_writefirst(dead, cap - interval, lev->queue);
644
        interval = 0;
645
      }
646
      else {
647
        interval -= cap;
648
        c_extractfirst(&lev->queue);
649
      }
650
    }
651
  }
652
 
1123 pj 653
  CBSGHD_activation(lev, p, &lev->request_time[p]);
1085 pj 654
 
655
 
656
  /* check the constraint on the maximum period permitted... */
657
  if (lev->act_period[p] > lev->maxperiod[p]) {
658
      kern_printf("Deadline miss(task_activ.! process:%d act_period:%lu maxperiod:%lu\n",
659
                  p, lev->act_period[p], lev->maxperiod[p]);
660
    kern_raise(XDEADLINE_MISS,p);
661
  }
662
 
663
  /* Set the reactivation timer */
664
  TIMESPEC_ASSIGN(&lev->reactivation_time[p], &lev->cbsghd_dline[p]);
665
  lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
666
                                                   CBSGHD_timer_reactivate,
667
                                                   (void *)p);
668
 
669
  //  kern_printf("act : %d %d |",lev->cbsghd_dline[p].tv_nsec/1000,p);
670
}
671
 
1123 pj 672
static void CBSGHD_public_unblock(LEVEL l, PID p)
1085 pj 673
{
674
  printk("CBSGHD_task_insert\n");
1100 pj 675
  kern_raise(XINVALID_TASK,p);
1085 pj 676
}
677
 
1123 pj 678
static void CBSGHD_public_block(LEVEL l, PID p)
1085 pj 679
{
680
  printk("CBSGHD_task_extract\n");
1100 pj 681
  kern_raise(XINVALID_TASK,p);
1085 pj 682
}
683
 
1123 pj 684
static int CBSGHD_public_message(LEVEL l, PID p, void *m)
1085 pj 685
{
686
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
687
  struct timespec act_time, res;
688
 
689
  /* It computes the response time of the current instance... */
1123 pj 690
  kern_gettime(&act_time);
691
  SUBTIMESPEC(&act_time, &lev->request_time[p], &res);
1085 pj 692
  /* response time expressed in usec! */
693
  lev->last_response_time[p] = TIMESPEC2NANOSEC(&res) / 1000;
694
 
695
  level_table[ lev->scheduling_level ]->
1123 pj 696
    private_extract(lev->scheduling_level,p);
1085 pj 697
 
698
 
699
  /* A spare capacity is inserted in the capacity queue!! */
700
  if (proc_table[p].avail_time > 0) {
701
    c_insert(lev->cbsghd_dline[p], proc_table[p].avail_time, &lev->queue, p);
702
    proc_table[p].avail_time = 0;
703
  }  
704
 
705
 
706
  proc_table[p].status = CBSGHD_IDLE;
1123 pj 707
 
708
  jet_update_endcycle(); /* Update the Jet data... */
709
  trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */
710
 
711
  return 0;
1085 pj 712
}
713
 
1123 pj 714
static void CBSGHD_public_end(LEVEL l, PID p)
1085 pj 715
{
716
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
717
 
718
  /* check if the capacity became negative... */
719
  /* there is a while because if the wcet is << than the system tick
720
     we need to postpone the deadline many times */
721
  while (proc_table[p].avail_time < 0) {
722
    /* the CBSGHD rule for recharging the capacity */
723
    proc_table[p].avail_time += lev->cnormal[p];
724
    ADDUSEC2TIMESPEC(lev->period[p], &lev->cbsghd_dline[p]);
725
  }
726
 
727
  level_table[ lev->scheduling_level ]->
1123 pj 728
    private_extract(lev->scheduling_level,p);
1085 pj 729
 
730
  /* we delete the reactivation timer */
1123 pj 731
  kern_event_delete(lev->reactivation_timer[p]);
1085 pj 732
  lev->reactivation_timer[p] = -1;
733
 
734
 
735
  /* Finally, we post the zombie event. when the end period is reached,
736
     the task descriptor and banwidth are freed */
737
  proc_table[p].status = CBSGHD_ZOMBIE;
738
  lev->reactivation_timer[p] = kern_event_post(&lev->cbsghd_dline[p],
739
                                               CBSGHD_timer_zombie,
740
                                               (void *)p);
741
}
742
 
743
/* Registration functions */
744
 
745
/*+ Registration function:
746
    int flags                 the init flags ... see CBS.h +*/
1123 pj 747
LEVEL CBSGHD_register_level(int flags, LEVEL master)
1085 pj 748
{
749
  LEVEL l;            /* the level that we register */
750
  CBSGHD_level_des *lev;  /* for readableness only */
751
  PID i;              /* a counter */
752
 
753
  printk("CBSGHD_register_level\n");
754
 
755
  /* request an entry in the level_table */
1123 pj 756
  l = level_alloc_descriptor(sizeof(CBSGHD_level_des));
1085 pj 757
 
1123 pj 758
  lev = (CBSGHD_level_des *)level_table[l];
1085 pj 759
 
760
  printk("    lev=%d\n",(int)lev);
761
 
762
  /* fill the standard descriptor */
1123 pj 763
  lev->l.public_scheduler = CBSGHD_public_scheduler;
1085 pj 764
 
765
  if (flags & CBSGHD_ENABLE_GUARANTEE)
1123 pj 766
    lev->l.public_guarantee = CBSGHD_public_guarantee;
1085 pj 767
  else
1123 pj 768
    lev->l.public_guarantee = NULL;
1085 pj 769
 
1123 pj 770
  lev->l.public_create    = CBSGHD_public_create;
771
  lev->l.public_detach    = CBSGHD_public_detach;
772
  lev->l.public_end       = CBSGHD_public_end;
773
  lev->l.public_dispatch  = CBSGHD_public_dispatch;
774
  lev->l.public_epilogue  = CBSGHD_public_epilogue;
775
  lev->l.public_activate  = CBSGHD_public_activate;
776
  lev->l.public_unblock   = CBSGHD_public_unblock;
777
  lev->l.public_block     = CBSGHD_public_block;
778
  lev->l.public_message   = CBSGHD_public_message;
1085 pj 779
 
780
  /* fill the CBSGHD descriptor part */
781
  for (i=0; i<MAX_PROC; i++) {
782
     NULL_TIMESPEC(&lev->cbsghd_dline[i]);
783
     lev->period[i] = 0;
1123 pj 784
     NULL_TIMESPEC(&lev->request_time[i]);
1085 pj 785
     lev->last_response_time[i] = 0;
786
     NULL_TIMESPEC(&lev->reactivation_time[i]);
787
     lev->reactivation_timer[i] = -1;
788
  }
789
 
790
 
791
  lev->U = 0;
792
  lev->idle = 0;
793
  lev->queue = NULL;
794
 
795
  lev->scheduling_level = master;
796
 
797
  lev->flags = flags & 0x07;
1123 pj 798
 
799
  return l;
1085 pj 800
}
801
 
802
 
803
int CBSGHD_get_response_time(LEVEL l, PID p)
804
{
805
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
1123 pj 806
 
1085 pj 807
  return lev->last_response_time[p];
808
}
809
 
810
 
811
bandwidth_t CBSGHD_usedbandwidth(LEVEL l)
812
{
813
  CBSGHD_level_des *lev = (CBSGHD_level_des *)(level_table[l]);
1123 pj 814
 
815
  return lev->U;
1085 pj 816
}
817