Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * 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 "edf.h"
#include "valmodel.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>

//#define edf_printf kern_printf
#define edf_printf printk

/*+ Status used in the level +*/
#define EDF_READY         MODULE_STATUS_BASE    /*+ - Ready status        +*/
#define EDF_DELAY         MODULE_STATUS_BASE+1  /*+ - Delay status        +*/
#define EDF_WCET_VIOLATED MODULE_STATUS_BASE+2  /*+ when wcet is finished +*/
#define EDF_WAIT          MODULE_STATUS_BASE+3  /*+ to wait the deadline  +*/
#define EDF_IDLE          MODULE_STATUS_BASE+4  /*+ to wait the deadline  +*/
#define EDF_ZOMBIE        MODULE_STATUS_BASE+5  /*+ to wait the free time +*/


/* For group create */
#define EDF_NOTYET_GUARANTEED 1
#define EDF_GUARANTEED 2
#define EDF_REJECTED 3

/*+ flags +*/
#define EDF_FLAG_SPORADIC    1
#define EDF_FLAG_NORAISEEXC  2
#define EDF_FLAG_APERIODIC   3

static LEVEL ModuleLevel;  //Ugling to save the level we r registered at.

/*+ the level redefinition for the Earliest Deadline First level +*/
typedef struct{
  level_des l;     /*+ the standard level descriptor          +*/
       
  TIME period[MAX_PROC]; /*+ The task periods; the deadlines are
                       stored in the priority field           +*/

  int deadline_timer[MAX_PROC];
                   /*+ The task deadline timers               +*/

  int flag[MAX_PROC];  /*+ used to manage the JOB_TASK_MODEL and the
                       periodicity                            +*/


  QUEUE ready;     /*+ the ready queue                        +*/

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

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

  int guarantee_status[MAX_PROC]; /* for the group create */

  QUEUE arrived;                  /* for all the tasks that arrive */


  /* VALUE_TASK specific data */
  long accumulatedValue;                     /* The accumulated system value */

  TIME dline[MAX_PROC];

  int value[MAX_PROC];
  int penalty[MAX_PROC];
       
} EDF_level_des;


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

  switch (status) {
    case EDF_READY        : return "EDF_Ready";
    case EDF_DELAY        : return "EDF_Delay";
    case EDF_WCET_VIOLATED: return "EDF_Wcet_Violated";
    case EDF_WAIT         : return "EDF_Sporadic_Wait";
    case EDF_IDLE         : return "EDF_Idle";
    case EDF_ZOMBIE       : return "EDF_Zombie";
    default               : return "EDF_Unknown";
  }
}

static void EDF_timer_deadline(void *par)
{
  PID p = (PID) par;
  EDF_level_des *lev;

  edf_printf("$");

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

  switch (proc_table[p].status) {
    case EDF_ZOMBIE:
      /* we finally put the task in the ready queue */
      proc_table[p].status = FREE;
      q_insertfirst(p,&freedesc);
      /* and free the allocated bandwidth */
      if(lev->flag[p] == EDF_FLAG_APERIODIC){
          //!!!!!  FRIG™R den allokerade bandbredden h„r!!!!
          // kanske n†ge mera
      }else{
            /* SPORADIC, could be removed. (we do not use them)*/

        lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet;
      }
      break;

    case EDF_IDLE:
      /* tracer stuff */
      trc_logevent(TRC_INTACTIVATION,&p);
      /* similar to EDF_task_activate */
      TIMESPEC_ASSIGN(&proc_table[p].request_time,
                      &proc_table[p].timespec_priority);
      ADDUSEC2TIMESPEC(lev->period[p], &proc_table[p].timespec_priority);
      proc_table[p].status = EDF_READY;
      q_timespec_insert(p,&lev->ready);
      lev->deadline_timer[p] = kern_event_post(&proc_table[p].timespec_priority,
                                               EDF_timer_deadline,
                                               (void *)p);
      edf_printf("(dline p%d ev%d %d.%d)",(int)p,(int)lev->deadline_timer[p],(int)proc_table[p].timespec_priority.tv_sec,(int)proc_table[p].timespec_priority.tv_nsec/1000);
      //printk("(d%d idle priority set to %d)",p,proc_table[p].priority );
      event_need_reschedule();
      printk("el%d|",p);
      break;

    case EDF_WAIT:
      /* Without this, the task cannot be reactivated!!! */
      proc_table[p].status = SLEEP;
      break;

    default:
      /* else, a deadline miss occurred!!! */
      edf_printf("\nstatus %d\n", (int)proc_table[p].status);
      edf_printf("timer_deadline:AAARRRGGGHHH!!!");
      kern_raise(XDEADLINE_MISS,p);
  }
}


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

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

  proc_table[p].status = EDF_READY;
  q_timespec_insert(p,&lev->ready);

  proc_table[p].delay_timer = NIL;  /* Paranoia */

  event_need_reschedule();
}


static int EDF_level_accept_task_model(LEVEL l, TASK_MODEL *m)
{
  if(m->pclass == VALUE_PCLASS){
    /* VALUE_TASK_MODEL */
    VALUE_TASK_MODEL *v = (VALUE_TASK_MODEL *)m;

    if(v->wcet && v->dline && v->value && v->penalty){
      if(v->dline >= v->wcet && v->value <= 100 && v->penalty <= 100)
         return 0;
    }
  }else if (m->pclass == HARD_PCLASS || m->pclass == (HARD_PCLASS | l)) {
    /* HARD_TASK_MODEL */

    HARD_TASK_MODEL *h = (HARD_TASK_MODEL *)m;

    if (h->wcet && h->mit)
      return 0;
  }


  return -1;
}

/* We do not accept any guest calls and models */
static int EDF_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 EDF_level_status(LEVEL l)
{
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
  PID p = lev->ready;

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

  while (p != NIL) {
    if ((proc_table[p].pclass) == JOB_PCLASS)
      kern_printf("Pid: %2d (GUEST)\n", p);
    else
      kern_printf("Pid: %2d Name: %10s %s: %9ld Dline: %9ld.%6ld Stat: %s\n",
              p,
              proc_table[p].name,
              lev->flag[p] & EDF_FLAG_SPORADIC ? "MinITime" : "Period  ",
              lev->period[p],
              proc_table[p].timespec_priority.tv_sec,
              proc_table[p].timespec_priority.tv_nsec/1000,
              EDF_status_to_a(proc_table[p].status));
    p = proc_table[p].next;
  }

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

/* The scheduler only gets the first task in the queue */
static PID EDF_level_scheduler(LEVEL l)
{
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);

/*  {  // print 4 dbg the ready queue
    PID p= lev->ready;
    kern_printf("(s");
    while (p != NIL) {
      kern_printf("%d ",p);
      p = proc_table[p].next;
    }
    kern_printf(") ");
  }
  */

  return (PID)lev->ready;
}

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


  //om flaggan sattes i task_create,
  //kanske inte ska vara h„r men det fungerar just nu i alla fall.
  if (lev->flags & EDF_FAILED_GUARANTEE) {
    *freebandwidth = 0;
    return 0;
  }


  /*check all the arrived tasks, guarantee them or not */
  while(lev->arrived != NIL){
    p = q_getfirst(&lev->arrived);

    if(lev->flag[p] == EDF_FLAG_APERIODIC){

        //!!!! h„r ska v†rat acceptans test f”r value task ligga

        lev->guarantee_status[p] = EDF_GUARANTEED;
    }else{
        /* Accept all the periodic tasks */
        lev->guarantee_status[p] = EDF_GUARANTEED;
    }
  }


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

}

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


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



  if(m->pclass == VALUE_PCLASS){
    /* VALUE_TASK_MODEL */

        //!!!!!!!!!!!! Kan vara smart att sortera value task i kön efter priority fältet !!!!!!!!!


    VALUE_TASK_MODEL *v = (VALUE_TASK_MODEL *)m;


    lev->value[p] = v->value;
    lev->penalty[p] = v->penalty;
    lev->dline[p] = v->dline;


    /* Mark that this is a value task (aperiodic)*/
    lev->flag[p] = EDF_FLAG_APERIODIC;

    /* Enable wcet check */
    if (lev->flags & EDF_ENABLE_WCET_CHECK) {
       proc_table[p].avail_time = v->wcet;
       proc_table[p].wcet       = v->wcet;
       proc_table[p].control |= CONTROL_CAP;
    }
  }else{
    /* HARD_TASK_MODEL */

    HARD_TASK_MODEL *h = (HARD_TASK_MODEL *)m;

    lev->period[p] = h->mit;

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

    lev->deadline_timer[p] = -1;


    /* Enable wcet check */
    if (lev->flags & EDF_ENABLE_WCET_CHECK) {
       proc_table[p].avail_time = h->wcet;
       proc_table[p].wcet       = h->wcet;
       proc_table[p].control |= CONTROL_CAP;
    }
  }


  //detta test kan/SKA man g”ra i level_guarantee, kanske tom i v†rat
  //acceptans test. H„r m†ste man ju „nd† alltid returnera ok (0)

  /* update the bandwidth... */
//  if (lev->flags & EDF_ENABLE_GUARANTEE) {
//    bandwidth_t b;
//    b = (MAX_BANDWIDTH / h->mit) * h->wcet;

    /* 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)...
         in this case, we don't raise an exception... in fact, after the
         EDF_task_create the task_create will call level_guarantee that return
         -1... return -1 in EDF_task_create isn't correct, because:
           . generally, the guarantee must be done when also the resources
             are registered
           . returning -1 will cause the task_create to return with an errno
             ETASK_CREATE instead of ENO_GUARANTEE!!!

         Why I use the flag??? because if the lev->U overflows, if i.e. I set
         it to MAX_BANDWIDTH, I lose the correct allocated bandwidth...
      */

//      lev->flags |= EDF_FAILED_GUARANTEE;
//  }



  /* Insert the tasks in the arrived QUEUE to enable group_create */
  q_insert(p, &lev->arrived);
  lev->guarantee_status[p] = EDF_NOTYET_GUARANTEED;



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

/* Called if task creation did'nt work */
static void EDF_task_detach(LEVEL l, PID p)
{
  /* the EDF level doesn't introduce any dinamic allocated new field.
     we have only to reset the NO_GUARANTEE FIELD and decrement the allocated
     bandwidth */


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

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


  //extract the arrived task from the Q
  q_extract(p, &lev->arrived);
  lev->guarantee_status[p] = EDF_NOTYET_GUARANTEED;

}

static int EDF_task_eligible(LEVEL l, PID p)
{
  return 0; /* if the task p is chosen, it is always eligible */
}

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

static void EDF_task_dispatch(LEVEL l, PID p, int nostop)
{
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);

  edf_printf("(disp p%d %d.%d)",(int)p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);

  /* the task state is set EXE by the scheduler()
     we extract the task from the ready queue
     NB: we can't assume that p is the first task in the queue!!! */

  q_extract(p, &lev->ready);

  #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 EDF_task_epilogue(LEVEL l, PID p)
{
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);

  edf_printf("(epil p%d %d.%d)",p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);

  /* check if the wcet is finished... */
  if ((lev->flags & EDF_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) {
    /* if it is, raise a XWCET_VIOLATION exception */
    kern_raise(XWCET_VIOLATION,p);
    proc_table[p].status = EDF_WCET_VIOLATED;
  }
  else {
    /* the task has been preempted. it returns into the ready queue... */
    q_timespec_insert(p,&lev->ready);
    proc_table[p].status = EDF_READY;
  }
}

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

  if (proc_table[p].status == EDF_WAIT) {
    kern_raise(XACTIVATION,p);
    return;
  }

  /* Test if we are trying to activate a non sleeping task    */
  /* Ignore this; the task is already active                  */
  if (proc_table[p].status != SLEEP &&
      proc_table[p].status != EDF_WCET_VIOLATED)
    return;


  /* Set the deadline for the task*/
  if(lev->flag[p] == EDF_FLAG_APERIODIC ){
        /* VALUE_TASK */

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

        TIMESPEC_ASSIGN(&proc_table[p].timespec_priority,
                  &proc_table[p].request_time);
        ADDUSEC2TIMESPEC(lev->dline[p], &proc_table[p].timespec_priority);
  }else{
        /* HARD_TASK */

        /* see also EDF_timer_deadline */
        /* e.g. set a new deadline for the hard periodic task */
        ll_gettime(TIME_EXACT, &proc_table[p].request_time);

        TIMESPEC_ASSIGN(&proc_table[p].timespec_priority,
                  &proc_table[p].request_time);
        ADDUSEC2TIMESPEC(lev->period[p], &proc_table[p].timespec_priority);
  }

  /* Insert task in the correct position */
  proc_table[p].status = EDF_READY;
  q_timespec_insert(p,&lev->ready);

  /* Set the deadline timer */
  lev->deadline_timer[p] = kern_event_post(&proc_table[p].timespec_priority,
                                           EDF_timer_deadline,
                                           (void *)p);
  edf_printf("(dline p%d ev%d %d.%d)",p,(int)lev->deadline_timer[p],(int)proc_table[p].timespec_priority.tv_sec,(int)proc_table[p].timespec_priority.tv_nsec/1000);

}

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

  /* Similar to EDF_task_activate, but we don't check in what state
     the task is and we don't set the request_time*/


  /* Insert task in the coEDFect position */
  proc_table[p].status = EDF_READY;
  q_timespec_insert(p,&lev->ready);
}

static void EDF_task_extract(LEVEL l, PID p)
{
  /* Extract the running task from the level
     . we have already extract it from the ready queue at the dispatch time.
     . the capacity event have to be removed by the generic kernel
     . the wcet don't need modification...
     . the state of the task is set by the calling function
     . the deadline must remain...

     So, we do nothing!!!
  */

}

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

  edf_printf("(ecyc p%d %d.%d)",p,(int)schedule_time.tv_sec,(int)schedule_time.tv_nsec/1000);

  /* the task has terminated his job before it consume the wcet. All OK! */
  if (lev->flag[p] & EDF_FLAG_SPORADIC)
    proc_table[p].status = EDF_WAIT;
  else /* pclass = sporadic_pclass */
    proc_table[p].status = EDF_IDLE;

  /* we reset the capacity counters... */
  if (lev->flags & EDF_ENABLE_WCET_CHECK)
    proc_table[p].avail_time = proc_table[p].wcet;

  /* when the deadline timer fire, it recognize the situation and set
     correctly all the stuffs (like reactivation, request_time, etc... ) */

}

static void EDF_task_end(LEVEL l, PID p)
{
//  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);

  kern_printf("EDF_task_end: %s\n", proc_table[p].name);

  proc_table[p].status = EDF_ZOMBIE;

  /* When the deadline timer fire, it put the task descriptor in
     the free queue, and free the allocated bandwidth... */

}

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

  /* the task has terminated his job before it consume the wcet. All OK! */
  proc_table[p].status = EDF_WAIT;

  /* we reset the capacity counters... */
  if (lev->flags & EDF_ENABLE_WCET_CHECK)
    proc_table[p].avail_time = proc_table[p].wcet;

  /* when the deadline timer fire, it recognize the situation and set
     correctly the task state to sleep... */

}

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

  /* equal to EDF_task_endcycle */
  proc_table[p].status = EDF_DELAY;

  /* we need to delete this event if we kill the task while it is sleeping */
  ll_gettime(TIME_EXACT, &wakeuptime);
  ADDUSEC2TIMESPEC(usdelay, &wakeuptime);
  proc_table[p].delay_timer = kern_event_post(&wakeuptime,
                                              EDF_timer_delay,
                                              (void *)p);
}

/* Guest Functions
   These functions manages a JOB_TASK_MODEL, that is used to put
   a guest task in the EDF ready queue. */



static void EDF_timer_guest_deadline(void *par)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

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

static void EDF_guest_detach(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

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

static void EDF_guest_epilogue(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

static void EDF_guest_activate(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

static void EDF_guest_insert(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

static void EDF_guest_extract(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

static void EDF_guest_endcycle(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}
static void EDF_guest_end(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

static void EDF_guest_sleep(LEVEL l, PID p)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

static void EDF_guest_delay(LEVEL l, PID p, TIME usdelay)
{
  kern_raise(XUNVALID_GUEST, exec_shadow);
}

/* Registration functions */

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

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

  printk("EDF_register_level\n");

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

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

  /* alloc the space needed for the EDF_level_des */
  lev = (EDF_level_des *)kern_alloc(sizeof(EDF_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,  EDF_LEVELNAME, MAX_LEVELNAME);
  lev->l.level_code               = EDF_LEVEL_CODE;
  lev->l.level_version            = EDF_LEVEL_VERSION;

  lev->l.level_accept_task_model  = EDF_level_accept_task_model;
  lev->l.level_accept_guest_model = EDF_level_accept_guest_model;
  lev->l.level_status             = EDF_level_status;
  lev->l.level_scheduler          = EDF_level_scheduler;

  if (flags & EDF_ENABLE_GUARANTEE)
    lev->l.level_guarantee        = EDF_level_guarantee;
  else
    lev->l.level_guarantee        = NULL;

  lev->l.task_create              = EDF_task_create;
  lev->l.task_detach              = EDF_task_detach;
  lev->l.task_eligible            = EDF_task_eligible;
  lev->l.task_dispatch            = EDF_task_dispatch;
  lev->l.task_epilogue            = EDF_task_epilogue;
  lev->l.task_activate            = EDF_task_activate;
  lev->l.task_insert              = EDF_task_insert;
  lev->l.task_extract             = EDF_task_extract;
  lev->l.task_endcycle            = EDF_task_endcycle;
  lev->l.task_end                 = EDF_task_end;
  lev->l.task_sleep               = EDF_task_sleep;
  lev->l.task_delay               = EDF_task_delay;

  lev->l.guest_create             = EDF_guest_create;
  lev->l.guest_detach             = EDF_guest_detach;
  lev->l.guest_dispatch           = EDF_guest_dispatch;
  lev->l.guest_epilogue           = EDF_guest_epilogue;
  lev->l.guest_activate           = EDF_guest_activate;
  lev->l.guest_insert             = EDF_guest_insert;
  lev->l.guest_extract            = EDF_guest_extract;
  lev->l.guest_endcycle           = EDF_guest_endcycle;
  lev->l.guest_end                = EDF_guest_end;
  lev->l.guest_sleep              = EDF_guest_sleep;
  lev->l.guest_delay              = EDF_guest_delay;

  /* fill the EDF descriptor part */
  for(i=0; i<MAX_PROC; i++) {
    lev->period[i]         = 0;
    lev->deadline_timer[i] = -1;
    lev->flag[i]          = 0;
    lev->value[i] = -1;
    lev->penalty[i] = -1;
    lev->dline[i] = -1;
  }

  lev->ready = NIL;
  lev->flags = flags & 0x07;
  lev->U     = 0;
  lev->accumulatedValue = 0;
}


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

    if(lev->guarantee_status[p] == EDF_GUARANTEED)
       return 1;

    return 0;
}


bandwidth_t EDF_usedbandwidth(LEVEL l)
{
  EDF_level_des *lev = (EDF_level_des *)(level_table[l]);
  if (lev->l.level_code    == EDF_LEVEL_CODE &&
      lev->l.level_version == EDF_LEVEL_VERSION)
    return lev->U;
  else
    return 0;
}

/*+ returns the current total value +*/
int GROUP5_getvalue(LEVEL l){
    return ((EDF_level_des *)(level_table[l]))->accumulatedValue;
}

/* Interface with the crunch application */

void crunch_register_models(struct multiboot_info *mb){
  // here you can register your scheduling modules
  EDF_register_level(EDF_ENABLE_ALL);           // Level 0
}

int crunch_taskaccepted(PID p){
  return is_accepted(proc_table[p].task_level, p );
}

int crunch_getvalue(/*LEVEL l*/){
  // here just return the accumulated value by the modules you used.
  // Should call the GROUP5_getvalue(LEVEL l) function.
  return GROUP5_getvalue(ModuleLevel);
}