Subversion Repositories shark

Rev

Rev 3 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
 *   Luca Abeni          <luca@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/**
 ------------
 CVS :        $Id: cbs.c,v 1.2 2002-10-28 07:55:54 pj Exp $

 File:        $File$
 Revision:    $Revision: 1.2 $
 Last update: $Date: 2002-10-28 07:55:54 $
 ------------

 This file contains the aperiodic server CBS (Total Bandwidth Server)

 Read CBS.h for further details.

**/


/*
 * Copyright (C) 2000 Paolo Gai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */



#include <modules/cbs.h>
#include <ll/stdio.h>
#include <ll/string.h>
#include <kernel/model.h>
#include <kernel/descr.h>
#include <kernel/var.h>
#include <kernel/func.h>
#include <kernel/trace.h>

/*+ 4 debug purposes +*/
#undef CBS_TEST
#undef CBS_COUNTER

#ifdef TESTG
#include "drivers/glib.h"
TIME x,oldx;
extern TIME starttime;
#endif


/*+ Status used in the level +*/
#define CBS_IDLE          APER_STATUS_BASE   /*+ waiting the activation +*/
#define CBS_ZOMBIE        APER_STATUS_BASE+1 /*+ waiting the period end +*/
#define CBS_DELAY         APER_STATUS_BASE+2 /*+ waiting the delay end  +*/

/*+ task flags +*/
#define CBS_SAVE_ARRIVALS 1
#define CBS_APERIODIC     2

/*+ the level redefinition for the Total Bandwidth Server level +*/
typedef struct {
  level_des l;     /*+ the standard level descriptor          +*/

  /* The wcet are stored in the task descriptor, but we need
     an array for the deadlines. We can't use the timespec_priority
     field because it is used by the master level!!!...
     Notice that however the use of the timespec_priority field
     does not cause any problem...                     */


  struct timespec cbs_dline[MAX_PROC]; /*+ CBS deadlines      +*/

  TIME period[MAX_PROC]; /*+ CBS activation period            +*/

  struct timespec reactivation_time[MAX_PROC];
        /*+ the time at witch  the reactivation timer is post +*/
  int reactivation_timer[MAX_PROC];
                                   /*+ the recativation timer +*/

  int nact[MAX_PROC]; /*+ number of pending activations       +*/

  BYTE flag[MAX_PROC]; /*+ task flags                         +*/

  int flags;       /*+ the init flags...                      +*/

  bandwidth_t U;   /*+ the used bandwidth by the server       +*/

  LEVEL scheduling_level;

} CBS_level_des;

#ifdef CBS_COUNTER
int cbs_counter=0;
int cbs_counter2=0;
#endif


static void CBS_activation(CBS_level_des *lev,
                           PID p,
                           struct timespec *acttime)
{
  JOB_TASK_MODEL job;

  /* we have to check if the deadline and the wcet are correct before
     activating a new task or an old task... */


  /* check 1: if the deadline is before than the actual scheduling time */

  /* check 2: if ( avail_time >= (cbs_dline - acttime)* (wcet/period) )
     (rule 7 in the CBS article!) */

  TIME t;
  struct timespec t2,t3;

  t = (lev->period[p] * proc_table[p].avail_time) / proc_table[p].wcet;
  t3.tv_sec = t / 1000000;
  t3.tv_nsec = (t % 1000000) * 1000;

  SUBTIMESPEC(&lev->cbs_dline[p], acttime, &t2);

  if (/* 1 */ TIMESPEC_A_LT_B(&lev->cbs_dline[p], acttime) ||
      /* 2 */ TIMESPEC_A_GT_B(&t3, &t2) ) {
/*    if (TIMESPEC_A_LT_B(&lev->cbs_dline[p], acttime) )
      kern_printf("$");
    else
      kern_printf("(Ûdline%d.%d act%d.%d wcet%d per%d avail%dÛ)",
                  lev->cbs_dline[p].tv_sec,lev->cbs_dline[p].tv_nsec/1000,
                  acttime->tv_sec, acttime->tv_nsec/1000,
                  proc_table[p].wcet, lev->period[p], proc_table[p].avail_time);
*/
 /* we modify the deadline ... */
    TIMESPEC_ASSIGN(&lev->cbs_dline[p], acttime);
    ADDUSEC2TIMESPEC(lev->period[p], &lev->cbs_dline[p]);

    /* and the capacity */
    proc_table[p].avail_time = proc_table[p].wcet;
  }

#ifdef TESTG
      if (starttime && p == 3) {
      oldx = x;
      x = ((lev->cbs_dline[p].tv_sec*1000000+lev->cbs_dline[p].tv_nsec/1000)/5000 - starttime) + 20;
//      kern_printf("(a%d)",lev->cbs_dline[p].tv_sec*1000000+lev->cbs_dline[p].tv_nsec/1000);
      if (oldx > x) sys_end();
      if (x<640)
        grx_plot(x, 15, 8);
      }
#endif

  /* and, finally, we reinsert the task in the master level */
  job_task_default_model(job, lev->cbs_dline[p]);
  job_task_def_noexc(job);
  level_table[ lev->scheduling_level ]->
    guest_create(lev->scheduling_level, p, (TASK_MODEL *)&job);
  level_table[ lev->scheduling_level ]->
    guest_activate(lev->scheduling_level, p);

}


static char *CBS_status_to_a(WORD status)
{
  if (status < MODULE_STATUS_BASE)
    return status_to_a(status);

  switch (status) {
    case CBS_IDLE   : return "CBS_Idle";
    case CBS_ZOMBIE : return "CBS_Zombie";
    case CBS_DELAY  : return "CBS_Delay";
    default         : return "CBS_Unknown";
  }
}

static void CBS_avail_time_check(CBS_level_des *lev, PID p)
{
  /* there is a while because if the wcet is << than the system tick
     we need to postpone the deadline many times */

  while (proc_table[p].avail_time <= 0) {
    ADDUSEC2TIMESPEC(lev->period[p], &lev->cbs_dline[p]);
    proc_table[p].avail_time += proc_table[p].wcet;

#ifdef TESTG
    if (starttime && p == 3) {
    oldx = x;
    x = ((lev->cbs_dline[p].tv_sec*1000000+lev->cbs_dline[p].tv_nsec/1000)/5000 - starttime) + 20;
//      kern_printf("(e%d avail%d)",lev->cbs_dline[p].tv_sec*1000000+lev->cbs_dline[p].tv_nsec/1000,proc_table[p].avail_time);
    if (oldx > x) sys_end();
    if (x<640)
      grx_plot(x, 15, 2);
    }
#endif
  }
}


/* this is the periodic reactivation of the task... it is posted only
   if the task is a periodic task */

static void CBS_timer_reactivate(void *par)
{
  PID p = (PID) par;
  CBS_level_des *lev;

  lev = (CBS_level_des *)level_table[proc_table[p].task_level];

  #ifdef CBS_COUNTER
    if (p==5) cbs_counter++;
  #endif

  if (proc_table[p].status == CBS_IDLE) {
    /* the task has finished the current activation and must be
       reactivated */

    CBS_activation(lev,p,&lev->reactivation_time[p]);

    event_need_reschedule();
  }
  else if (lev->flag[p] & CBS_SAVE_ARRIVALS)
    /* the task has not completed the current activation, so we save
       the activation incrementing nact... */

    lev->nact[p]++;

  /* repost the event at the next period end... */
  ADDUSEC2TIMESPEC(lev->period[p], &lev->reactivation_time[p]);
  lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
                                               CBS_timer_reactivate,
                                               (void *)p);
  #ifdef CBS_COUNTER
    if (p==5) cbs_counter2++;
  #endif
  /* tracer stuff */
  trc_logevent(TRC_INTACTIVATION,&p);

}

/*+ this function is called when a task finish his delay +*/
static void CBS_timer_delay(void *par)
{
  PID p = (PID) par;
  CBS_level_des *lev;

  lev = (CBS_level_des *)level_table[proc_table[p].task_level];

  CBS_activation(lev,p,&proc_table[p].timespec_priority);

  event_need_reschedule();
}


/*+ this function is called when a killed or ended task reach the
    period end +*/

static void CBS_timer_zombie(void *par)
{
  PID p = (PID) par;
  CBS_level_des *lev;

  lev = (CBS_level_des *)level_table[proc_table[p].task_level];

  /* we finally put the task in the ready queue */
  proc_table[p].status = FREE;
  q_insertfirst(p,&freedesc);

  /* and free the allocated bandwidth */
  lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet;

}


static int CBS_level_accept_task_model(LEVEL l, TASK_MODEL *m)
{
  if (m->pclass == SOFT_PCLASS || m->pclass == (SOFT_PCLASS | l)) {
    SOFT_TASK_MODEL *s = (SOFT_TASK_MODEL *)m;
    if (s->met && s->period)
      return 0;
  }

  return -1;
}

static int CBS_level_accept_guest_model(LEVEL l, TASK_MODEL *m)
{
  return -1;
}

static char *onoff(int i)
{
  if (i)
    return "On ";
  else
    return "Off";
}

static void CBS_level_status(LEVEL l)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);
  PID p;

  kern_printf("On-line guarantee : %s\n",
            onoff(lev->flags & CBS_ENABLE_GUARANTEE));
  kern_printf("Used Bandwidth    : %u/%u\n",
            lev->U, MAX_BANDWIDTH);

  for (p=0; p<MAX_PROC; p++)
    if (proc_table[p].task_level == l && proc_table[p].status != FREE )
      kern_printf("Pid: %2d Name: %10s Period: %9ld Dline: %9ld.%6ld Stat: %s\n",
                p,
                proc_table[p].name,
                lev->period[p],
                lev->cbs_dline[p].tv_sec,
                lev->cbs_dline[p].tv_nsec/1000,
                CBS_status_to_a(proc_table[p].status));
}

static PID CBS_level_scheduler(LEVEL l)
{
  /* the CBS don't schedule anything...
     it's an EDF level or similar that do it! */

  return NIL;
}

/* The on-line guarantee is enabled only if the appropriate flag is set... */
static int CBS_level_guarantee(LEVEL l, bandwidth_t *freebandwidth)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  if (lev->flags & CBS_FAILED_GUARANTEE) {
    *freebandwidth = 0;
    return 0;
  }
  else
    if (*freebandwidth >= lev->U) {
      *freebandwidth -= lev->U;
      return 1;
    }
    else
      return 0;
}

static int CBS_task_create(LEVEL l, PID p, TASK_MODEL *m)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  /* if the CBS_task_create is called, then the pclass must be a
     valid pclass. */

  SOFT_TASK_MODEL *soft = (SOFT_TASK_MODEL *)m;

  /* Enable wcet check */
  proc_table[p].avail_time = soft->met;
  proc_table[p].wcet       = soft->met;
  proc_table[p].control   |= CONTROL_CAP;

  lev->nact[p] = 0;
  lev->period[p] = soft->period;
  NULL_TIMESPEC(&lev->cbs_dline[p]);

  if (soft->periodicity == APERIODIC)
    lev->flag[p] = CBS_APERIODIC;
  else
    lev->flag[p] = 0;

  if (soft->arrivals == SAVE_ARRIVALS)
    lev->flag[p] |= CBS_SAVE_ARRIVALS;

  /* update the bandwidth... */
  if (lev->flags & CBS_ENABLE_GUARANTEE) {
    bandwidth_t b;
    b = (MAX_BANDWIDTH / soft->period) * soft->met;

    /* really update lev->U, checking an overflow... */
    if (MAX_BANDWIDTH - lev->U > b)
      lev->U += b;
    else
      /* The task can NOT be guaranteed (U>MAX_BANDWIDTH)...
         (see EDF.c) */

      lev->flags |= CBS_FAILED_GUARANTEE;
  }


  return 0; /* OK, also if the task cannot be guaranteed... */
}

static void CBS_task_detach(LEVEL l, PID p)
{
  /* the CBS level doesn't introduce any dinamic allocated new field.
     we have only to reset the NO_GUARANTEE FIELD and decrement the allocated
     bandwidth */


  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  if (lev->flags & CBS_FAILED_GUARANTEE)
    lev->flags &= ~CBS_FAILED_GUARANTEE;
  else
    lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet;
}

static int CBS_task_eligible(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);
  JOB_TASK_MODEL job;

  /* we have to check if the deadline and the wcet are correct...
     if the CBS level schedules in background with respect to others
     levels, there can be the case in witch a task is scheduled by
     schedule_time > CBS_deadline; in this case (not covered in the
     article because if there is only the standard scheduling policy
     this never apply) we reassign the deadline */


  if ( TIMESPEC_A_LT_B(&lev->cbs_dline[p], &schedule_time) ) {
    /* we kill the current activation */
    level_table[ lev->scheduling_level ]->
      guest_end(lev->scheduling_level, p);

    /* we modify the deadline ... */
    TIMESPEC_ASSIGN(&lev->cbs_dline[p], &schedule_time);
    ADDUSEC2TIMESPEC(lev->period[p], &lev->cbs_dline[p]);

    /* and the capacity */
    proc_table[p].avail_time = proc_table[p].wcet;
 
    /* and, finally, we reinsert the task in the master level */
    job_task_default_model(job, lev->cbs_dline[p]);
    job_task_def_noexc(job);
    level_table[ lev->scheduling_level ]->
      guest_create(lev->scheduling_level, p, (TASK_MODEL *)&job);
    level_table[ lev->scheduling_level ]->
      guest_activate(lev->scheduling_level, p);

    return -1;
  }

  return 0;
}

#ifdef __TEST1__
extern int testactive;
extern struct timespec s_stime[];
extern TIME s_curr[];
extern TIME s_PID[];
extern int useds;
#endif

static void CBS_task_dispatch(LEVEL l, PID p, int nostop)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);
  level_table[ lev->scheduling_level ]->
    guest_dispatch(lev->scheduling_level,p,nostop);

  #ifdef __TEST1__
  if (testactive)
  {
    TIMESPEC_ASSIGN(&s_stime[useds], &schedule_time);
    s_curr[useds] = proc_table[p].avail_time;
    s_PID[useds]  = p;
    useds++;
  }
  #endif
}

static void CBS_task_epilogue(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);
  JOB_TASK_MODEL job;

  /* check if the wcet is finished... */
  if ( proc_table[p].avail_time <= 0) {
    /* we kill the current activation */
    level_table[ lev->scheduling_level ]->
      guest_end(lev->scheduling_level, p);

    /* we modify the deadline according to rule 4 ... */
    CBS_avail_time_check(lev, p);

    /* and, finally, we reinsert the task in the master level */
    job_task_default_model(job, lev->cbs_dline[p]);
    job_task_def_noexc(job);
    level_table[ lev->scheduling_level ]->
      guest_create(lev->scheduling_level, p, (TASK_MODEL *)&job);
    level_table[ lev->scheduling_level ]->
      guest_activate(lev->scheduling_level, p);
//    kern_printf("epil : dl %d per %d p %d |\n",
//              lev->cbs_dline[p].tv_nsec/1000,lev->period[p],p);

  }
  else
    /* the task has been preempted. it returns into the ready queue by
       calling the guest_epilogue... */

    level_table[ lev->scheduling_level ]->
      guest_epilogue(lev->scheduling_level,p);
}

static void CBS_task_activate(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  /* save activation (only if needed... */
  if (proc_table[p].status != SLEEP) {
    if (lev->flag[p] & CBS_SAVE_ARRIVALS)
      lev->nact[p]++;
    return;
  }

  ll_gettime(TIME_EXACT, &proc_table[p].request_time);

  CBS_activation(lev, p, &proc_table[p].request_time);

  /* Set the reactivation timer */
  if (!(lev->flag[p] & CBS_APERIODIC))
  {
    /* we cannot use the deadline computed by CBS_activation because
       the deadline may be != from actual_time + period
       (if we call the task_activate after a task_sleep, and the
       deadline was postponed a lot...) */

    TIMESPEC_ASSIGN(&lev->reactivation_time[p], &proc_table[p].request_time);
    ADDUSEC2TIMESPEC(lev->period[p], &lev->reactivation_time[p]);
//    TIMESPEC_ASSIGN(&lev->reactivation_time[p], &lev->cbs_dline[p]);
    lev->reactivation_timer[p] = kern_event_post(&lev->reactivation_time[p],
                                                 CBS_timer_reactivate,
                                                 (void *)p);
    #ifdef CBS_COUNTER
      if (p==5) cbs_counter2++;
    #endif
  }
//  kern_printf("act : %d %d |",lev->cbs_dline[p].tv_nsec/1000,p);
}

static void CBS_task_insert(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);
  struct timespec acttime;

  ll_gettime(TIME_EXACT, &acttime);

  CBS_activation(lev,p,&acttime);
}

static void CBS_task_extract(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  /* check if the wcet is finished... */
  CBS_avail_time_check(lev, p);

  level_table[ lev->scheduling_level ]->
    guest_end(lev->scheduling_level,p);
}

static void CBS_task_endcycle(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  /* check if the wcet is finished... */
  CBS_avail_time_check(lev, p);

  if (lev->nact[p]) {
    /* continue!!!! */
    ll_gettime(TIME_EXACT, &proc_table[p].request_time);
    lev->nact[p]--;
    level_table[ lev->scheduling_level ]->
      guest_epilogue(lev->scheduling_level,p);
  }
  else {
    level_table[ lev->scheduling_level ]->
      guest_end(lev->scheduling_level,p);

    if (lev->flag[p] & CBS_APERIODIC)
      proc_table[p].status = SLEEP;
    else  /* the task is soft_periodic */
      proc_table[p].status = CBS_IDLE;

  }
}

static void CBS_task_end(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  /* check if the wcet is finished... */
  CBS_avail_time_check(lev, p);

  level_table[ lev->scheduling_level ]->
    guest_end(lev->scheduling_level,p);

  /* we delete the reactivation timer */
  if (!(lev->flag[p] & CBS_APERIODIC)) {
    event_delete(lev->reactivation_timer[p]);
    lev->reactivation_timer[p] = -1;
  }

  /* Finally, we post the zombie event. when the end period is reached,
     the task descriptor and banwidth are freed */

  proc_table[p].status = CBS_ZOMBIE;
  lev->reactivation_timer[p] = kern_event_post(&lev->cbs_dline[p],
                                               CBS_timer_zombie,
                                               (void *)p);
}

static void CBS_task_sleep(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  /* check if the wcet is finished... */
  CBS_avail_time_check(lev, p);

  /* a task activation is finished, but we are using a JOB_TASK_MODEL
     that implements a single activation, so we have to call
     the guest_end, that representsa single activation... */

  level_table[ lev->scheduling_level ]->
    guest_end(lev->scheduling_level,p);

  /* we delete the reactivation timer */
  if (!(lev->flag[p] & CBS_APERIODIC)) {
    event_delete(lev->reactivation_timer[p]);
    lev->reactivation_timer[p] = -1;
  }

  proc_table[p].status = SLEEP;

  /* the sleep forgets pending activations... */
  lev->nact[p] = 0;
}

static void CBS_task_delay(LEVEL l, PID p, TIME usdelay)
{
  struct timespec wakeuptime;
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  /* check if the wcet is finished... */
  CBS_avail_time_check(lev, p);

  level_table[ lev->scheduling_level ]->
    guest_end(lev->scheduling_level,p);

  proc_table[p].status = CBS_DELAY;

  /* we need to delete this event if we kill the task while it is sleeping */
  ll_gettime(TIME_EXACT, &wakeuptime);
  ADDUSEC2TIMESPEC(usdelay, &wakeuptime);

  /* the timespec_priority field is used to store the time at witch the delay
     timer raises */

  TIMESPEC_ASSIGN(&proc_table[p].timespec_priority, &wakeuptime);
  proc_table[p].delay_timer = kern_event_post(&wakeuptime,
                                              CBS_timer_delay,
                                              (void *)p);
}


static int CBS_guest_create(LEVEL l, PID p, TASK_MODEL *m)
{ kern_raise(XINVALID_GUEST,exec_shadow); return 0; }

static void CBS_guest_detach(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_dispatch(LEVEL l, PID p, int nostop)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_epilogue(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_activate(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_insert(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_extract(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_endcycle(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_end(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_sleep(LEVEL l, PID p)
{ kern_raise(XINVALID_GUEST,exec_shadow); }

static void CBS_guest_delay(LEVEL l, PID p,DWORD tickdelay)
{ kern_raise(XINVALID_GUEST,exec_shadow); }




/* Registration functions */

/*+ Registration function:
    int flags                 the init flags ... see CBS.h +*/

void CBS_register_level(int flags, LEVEL master)
{
  LEVEL l;            /* the level that we register */
  CBS_level_des *lev;  /* for readableness only */
  PID i;              /* a counter */

  printk("CBS_register_level\n");

  /* request an entry in the level_table */
  l = level_alloc_descriptor();

  printk("    alloco descrittore %d %d\n",l,(int)sizeof(CBS_level_des));

  /* alloc the space needed for the CBS_level_des */
  lev = (CBS_level_des *)kern_alloc(sizeof(CBS_level_des));

  printk("    lev=%d\n",(int)lev);

  /* update the level_table with the new entry */
  level_table[l] = (level_des *)lev;

  /* fill the standard descriptor */
  strncpy(lev->l.level_name,  CBS_LEVELNAME, MAX_LEVELNAME);
  lev->l.level_code               = CBS_LEVEL_CODE;
  lev->l.level_version            = CBS_LEVEL_VERSION;

  lev->l.level_accept_task_model  = CBS_level_accept_task_model;
  lev->l.level_accept_guest_model = CBS_level_accept_guest_model;
  lev->l.level_status             = CBS_level_status;
  lev->l.level_scheduler          = CBS_level_scheduler;

  if (flags & CBS_ENABLE_GUARANTEE)
    lev->l.level_guarantee        = CBS_level_guarantee;
  else
    lev->l.level_guarantee        = NULL;

  lev->l.task_create              = CBS_task_create;
  lev->l.task_detach              = CBS_task_detach;
  lev->l.task_eligible            = CBS_task_eligible;
  lev->l.task_dispatch            = CBS_task_dispatch;
  lev->l.task_epilogue            = CBS_task_epilogue;
  lev->l.task_activate            = CBS_task_activate;
  lev->l.task_insert              = CBS_task_insert;
  lev->l.task_extract             = CBS_task_extract;
  lev->l.task_endcycle            = CBS_task_endcycle;
  lev->l.task_end                 = CBS_task_end;
  lev->l.task_sleep               = CBS_task_sleep;
  lev->l.task_delay               = CBS_task_delay;

  lev->l.guest_create             = CBS_guest_create;
  lev->l.guest_detach             = CBS_guest_detach;
  lev->l.guest_dispatch           = CBS_guest_dispatch;
  lev->l.guest_epilogue           = CBS_guest_epilogue;
  lev->l.guest_activate           = CBS_guest_activate;
  lev->l.guest_insert             = CBS_guest_insert;
  lev->l.guest_extract            = CBS_guest_extract;
  lev->l.guest_endcycle           = CBS_guest_endcycle;
  lev->l.guest_end                = CBS_guest_end;
  lev->l.guest_sleep              = CBS_guest_sleep;
  lev->l.guest_delay              = CBS_guest_delay;

  /* fill the CBS descriptor part */
  for (i=0; i<MAX_PROC; i++) {
     NULL_TIMESPEC(&lev->cbs_dline[i]);
     lev->period[i] = 0;
     NULL_TIMESPEC(&lev->reactivation_time[i]);
     lev->reactivation_timer[i] = -1;
     lev->nact[i] = 0;
     lev->flag[i] = 0;
  }


  lev->U = 0;

  lev->scheduling_level = master;

  lev->flags = flags & 0x01;
}

bandwidth_t CBS_usedbandwidth(LEVEL l)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);
  if (lev->l.level_code    == CBS_LEVEL_CODE &&
      lev->l.level_version == CBS_LEVEL_VERSION)
    return lev->U;
  else
    return 0;
}

int CBS_get_nact(LEVEL l, PID p)
{
  CBS_level_des *lev = (CBS_level_des *)(level_table[l]);

  return lev->nact[p];
}