Blame | 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>
* (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: EDFACT.C,v 1.1 2004-06-04 10:48:28 giacomo Exp $
File: $File$
Revision: $Revision: 1.1 $
Last update: $Date: 2004-06-04 10:48:28 $
------------
**/
/*
* Copyright (C) 2001 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 "edfact.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 edfact_printf kern_printf
#define edfact_printf printk
/*+ Status used in the level +*/
#define EDFACT_READY MODULE_STATUS_BASE /*+ - Ready status +*/
#define EDFACT_IDLE MODULE_STATUS_BASE+4 /*+ to wait the deadline +*/
/*+ flags +*/
#define EDFACT_FLAG_NORAISEEXC 2
/*+ 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 +*/
struct timespec deadline_timespec[MAX_PROC];
int dline_miss[MAX_PROC]; /*+ Deadline miss counter +*/
int wcet_miss[MAX_PROC]; /*+ Wcet miss counter +*/
int nact[MAX_PROC]; /*+ Wcet miss counter +*/
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 +*/
} EDFACT_level_des;
static void EDFACT_timer_deadline(void *par);
static void EDFACT_internal_activate(EDFACT_level_des *lev, PID p)
{
TIMESPEC_ASSIGN(&proc_table[p].timespec_priority,
&proc_table[p].request_time);
ADDUSEC2TIMESPEC(lev->period[p], &proc_table[p].timespec_priority);
TIMESPEC_ASSIGN(&lev->deadline_timespec[p],
&proc_table[p].timespec_priority);
/* Insert task in the correct position */
proc_table[p].status = EDFACT_READY;
q_timespec_insert(p,&lev->ready);
/* needed because when there is a wcet miss I disable CONTROL_CAP */
proc_table[p].control |= CONTROL_CAP;
}
static char *EDFACT_status_to_a(WORD status)
{
if (status < MODULE_STATUS_BASE)
return status_to_a(status);
switch (status) {
case EDFACT_READY : return "EDFACT_Ready";
case EDFACT_IDLE : return "EDFACT_Idle";
default : return "EDFACT_Unknown";
}
}
static void EDFACT_timer_deadline(void *par)
{
PID p = (PID) par;
EDFACT_level_des *lev;
lev = (EDFACT_level_des *)level_table[proc_table[p].task_level];
switch (proc_table[p].status) {
case EDFACT_IDLE:
edfact_printf("I%d",p);
TIMESPEC_ASSIGN(&proc_table[p].request_time,
&proc_table[p].timespec_priority);
EDFACT_internal_activate(lev,p);
event_need_reschedule();
break;
default:
edfact_printf("D%d",p );
/* else, a deadline miss occurred!!! */
lev->dline_miss[p]++;
/* the task is into another state */
lev->nact[p]++;
/* Set the deadline timer */
ADDUSEC2TIMESPEC(lev->period[p], &lev->deadline_timespec[p]);
}
/* Set the deadline timer */
lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
EDFACT_timer_deadline,
(void *)p);
}
static void EDFACT_timer_guest_deadline(void *par)
{
PID p = (PID) par;
edfact_printf("AAARRRGGGHHH!!!");
kern_raise(XDEADLINE_MISS,p);
}
static int EDFACT_level_accept_task_model(LEVEL l, TASK_MODEL *m)
{
if (m->pclass == HARD_PCLASS || m->pclass == (HARD_PCLASS | l)) {
HARD_TASK_MODEL *h = (HARD_TASK_MODEL *)m;
if (h->wcet && h->mit && h->periodicity == PERIODIC)
return 0;
}
return -1;
}
static int EDFACT_level_accept_guest_model(LEVEL l, TASK_MODEL *m)
{
if (m->pclass == JOB_PCLASS || m->pclass == (JOB_PCLASS | l))
return 0;
else
return -1;
}
static char *onoff(int i)
{
if (i)
return "On ";
else
return "Off";
}
static void EDFACT_level_status(LEVEL l)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
PID p = lev->ready;
kern_printf("On-line guarantee : %s\n",
onoff(lev->flags & EDFACT_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: %9d Dline: %9d.%6d Stat: %s\n",
p,
proc_table[p].name,
"Period ",
lev->period[p],
proc_table[p].timespec_priority.tv_sec,
proc_table[p].timespec_priority.tv_nsec/1000,
EDFACT_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 != EDFACT_READY
&& proc_table[p].status != FREE )
kern_printf("Pid: %2d Name: %10s %s: %9d Dline: %9d.%6d Stat: %s\n",
p,
proc_table[p].name,
"Period ",
lev->period[p],
proc_table[p].timespec_priority.tv_sec,
proc_table[p].timespec_priority.tv_nsec/1000,
EDFACT_status_to_a(proc_table[p].status));
}
/* The scheduler only gets the first task in the queue */
static PID EDFACT_level_scheduler(LEVEL l)
{
EDFACT_level_des *lev = (EDFACT_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 EDFACT_level_guarantee(LEVEL l, bandwidth_t *freebandwidth)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->flags & EDFACT_FAILED_GUARANTEE) {
*freebandwidth = 0;
return 0;
}
else
if (*freebandwidth >= lev->U) {
*freebandwidth -= lev->U;
return 1;
}
else
return 0;
}
static int EDFACT_task_create(LEVEL l, PID p, TASK_MODEL *m)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* if the EDFACT_task_create is called, then the pclass must be a
valid pclass. */
HARD_TASK_MODEL *h = (HARD_TASK_MODEL *)m;
lev->period[p] = h->mit;
lev->flag[p] = 0;
lev->deadline_timer[p] = -1;
lev->dline_miss[p] = 0;
lev->wcet_miss[p] = 0;
lev->nact[p] = 0;
/* Enable wcet check */
proc_table[p].avail_time = h->wcet;
proc_table[p].wcet = h->wcet;
proc_table[p].control |= CONTROL_CAP;
/* update the bandwidth... */
if (lev->flags & EDFACT_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
EDFACT_task_create the task_create will call level_guarantee that return
-1... return -1 in EDFACT_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 |= EDFACT_FAILED_GUARANTEE;
}
return 0; /* OK, also if the task cannot be guaranteed... */
}
static void EDFACT_task_detach(LEVEL l, PID p)
{
/* the EDFACT level doesn't introduce any dinamic allocated new field.
we have only to reset the NO_GUARANTEE FIELD and decrement the allocated
bandwidth */
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->flags & EDFACT_FAILED_GUARANTEE)
lev->flags &= ~EDFACT_FAILED_GUARANTEE;
else
lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet;
}
static int EDFACT_task_eligible(LEVEL l, PID p)
{
return 0; /* if the task p is chosen, it is always eligible */
}
static void EDFACT_task_dispatch(LEVEL l, PID p, int nostop)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* 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);
}
static void EDFACT_task_epilogue(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* check if the wcet is finished... */
if (proc_table[p].avail_time <= 0 && proc_table[p].control&CONTROL_CAP) {
/* wcet finished: disable wcet event and count wcet miss */
edfact_printf("W%d",p);
proc_table[p].control &= ~CONTROL_CAP;
lev->wcet_miss[p]++;
}
/* the task it returns into the ready queue... */
q_timespec_insert(p,&lev->ready);
proc_table[p].status = EDFACT_READY;
}
static void EDFACT_task_activate(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* Test if we are trying to activate a non sleeping task */
/* save activation (only if needed... */
if (proc_table[p].status != SLEEP) {
/* a periodic task cannot be activated when it is already active */
kern_raise(XACTIVATION,p);
return;
}
ll_gettime(TIME_EXACT, &proc_table[p].request_time);
EDFACT_internal_activate(lev,p);
/* Set the deadline timer */
lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
EDFACT_timer_deadline,
(void *)p);
}
static void EDFACT_task_insert(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* Insert task in the coEDFect position */
proc_table[p].status = EDFACT_READY;
q_timespec_insert(p,&lev->ready);
}
static void EDFACT_task_extract(LEVEL l, PID p)
{
}
static void EDFACT_task_endcycle(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* we reset the capacity counters... */
proc_table[p].avail_time = proc_table[p].wcet;
if (lev->nact[p] > 0) {
edfact_printf("E%d",p);
/* Pending activation: reactivate the thread!!! */
lev->nact[p]--;
/* see also EDFACT_timer_deadline */
ll_gettime(TIME_EXACT, &proc_table[p].request_time);
EDFACT_internal_activate(lev,p);
/* check if the deadline has already expired */
if (TIMESPEC_A_LT_B(&proc_table[p].timespec_priority, &schedule_time)) {
/* count the deadline miss */
lev->dline_miss[p]++;
event_delete(lev->deadline_timer[p]);
}
}
else {
edfact_printf("e%d",p);
/* the task has terminated his job before it consume the wcet. All OK! */
proc_table[p].status = EDFACT_IDLE;
/* when the deadline timer fire, it recognize the situation and set
correctly all the stuffs (like reactivation, request_time, etc... ) */
}
}
static void EDFACT_task_end(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
edfact_printf("Û%d",p);
/* 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;
if (lev->deadline_timer[p] != -1) {
edfact_printf("²%d",p);
event_delete(lev->deadline_timer[p]);
}
}
static void EDFACT_task_sleep(LEVEL l, PID p)
{ kern_raise(XUNVALID_TASK,exec_shadow); }
static void EDFACT_task_delay(LEVEL l, PID p, TIME usdelay)
{ kern_raise(XUNVALID_TASK,exec_shadow); }
/* Guest Functions
These functions manages a JOB_TASK_MODEL, that is used to put
a guest task in the EDFACT ready queue. */
static int EDFACT_guest_create(LEVEL l, PID p, TASK_MODEL *m)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
JOB_TASK_MODEL *job = (JOB_TASK_MODEL *)m;
/* if the EDFACT_guest_create is called, then the pclass must be a
valid pclass. */
TIMESPEC_ASSIGN(&proc_table[p].timespec_priority, &job->deadline);
lev->deadline_timer[p] = -1;
lev->dline_miss[p] = 0;
lev->wcet_miss[p] = 0;
lev->nact[p] = 0;
if (job->noraiseexc)
lev->flag[p] = EDFACT_FLAG_NORAISEEXC;
else
lev->flag[p] = 0;
lev->period[p] = job->period;
/* there is no bandwidth guarantee at this level, it is performed
by the level that inserts guest tasks... */
return 0; /* OK, also if the task cannot be guaranteed... */
}
static void EDFACT_guest_detach(LEVEL l, PID p)
{
/* the EDFACT level doesn't introduce any dinamic allocated new field.
No guarantee is performed on guest tasks... so we don't have to reset
the NO_GUARANTEE FIELD */
}
static void EDFACT_guest_dispatch(LEVEL l, PID p, int nostop)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* the task state is set to 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);
}
static void EDFACT_guest_epilogue(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* the task has been preempted. it returns into the ready queue... */
q_timespec_insert(p,&lev->ready);
proc_table[p].status = EDFACT_READY;
}
static void EDFACT_guest_activate(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* Insert task in the correct position */
q_timespec_insert(p,&lev->ready);
proc_table[p].status = EDFACT_READY;
/* Set the deadline timer */
if (!(lev->flag[p] & EDFACT_FLAG_NORAISEEXC))
lev->deadline_timer[p] = kern_event_post(&proc_table[p].timespec_priority,
EDFACT_timer_guest_deadline,
(void *)p);
}
static void EDFACT_guest_insert(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
/* Insert task in the correct position */
q_timespec_insert(p,&lev->ready);
proc_table[p].status = EDFACT_READY;
}
static void EDFACT_guest_extract(LEVEL l, PID p)
{
}
static void EDFACT_guest_endcycle(LEVEL l, PID p)
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
static void EDFACT_guest_end(LEVEL l, PID p)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
//kern_printf("EDFACT_guest_end: dline timer %d\n",lev->deadline_timer[p]);
if (proc_table[p].status == EDFACT_READY)
{
q_extract(p, &lev->ready);
//kern_printf("(g_end rdy extr)");
}
/* we remove the deadline timer, because the slice is finished */
if (lev->deadline_timer[p] != NIL) {
// kern_printf("EDFACT_guest_end: dline timer %d\n",lev->deadline_timer[p]);
event_delete(lev->deadline_timer[p]);
lev->deadline_timer[p] = NIL;
}
}
static void EDFACT_guest_sleep(LEVEL l, PID p)
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
static void EDFACT_guest_delay(LEVEL l, PID p, TIME usdelay)
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
/* Registration functions */
/*+ Registration function:
int flags the init flags ... see EDFACT.h +*/
void EDFACT_register_level(int flags)
{
LEVEL l; /* the level that we register */
EDFACT_level_des *lev; /* for readableness only */
PID i; /* a counter */
printk("EDFACT_register_level\n");
/* request an entry in the level_table */
l = level_alloc_descriptor();
printk(" alloco descrittore %d %d\n",l,(int)sizeof(EDFACT_level_des));
/* alloc the space needed for the EDFACT_level_des */
lev = (EDFACT_level_des *)kern_alloc(sizeof(EDFACT_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, EDFACT_LEVELNAME, MAX_LEVELNAME);
lev->l.level_code = EDFACT_LEVEL_CODE;
lev->l.level_version = EDFACT_LEVEL_VERSION;
lev->l.level_accept_task_model = EDFACT_level_accept_task_model;
lev->l.level_accept_guest_model = EDFACT_level_accept_guest_model;
lev->l.level_status = EDFACT_level_status;
lev->l.level_scheduler = EDFACT_level_scheduler;
if (flags & EDFACT_ENABLE_GUARANTEE)
lev->l.level_guarantee = EDFACT_level_guarantee;
else
lev->l.level_guarantee = NULL;
lev->l.task_create = EDFACT_task_create;
lev->l.task_detach = EDFACT_task_detach;
lev->l.task_eligible = EDFACT_task_eligible;
lev->l.task_dispatch = EDFACT_task_dispatch;
lev->l.task_epilogue = EDFACT_task_epilogue;
lev->l.task_activate = EDFACT_task_activate;
lev->l.task_insert = EDFACT_task_insert;
lev->l.task_extract = EDFACT_task_extract;
lev->l.task_endcycle = EDFACT_task_endcycle;
lev->l.task_end = EDFACT_task_end;
lev->l.task_sleep = EDFACT_task_sleep;
lev->l.task_delay = EDFACT_task_delay;
lev->l.guest_create = EDFACT_guest_create;
lev->l.guest_detach = EDFACT_guest_detach;
lev->l.guest_dispatch = EDFACT_guest_dispatch;
lev->l.guest_epilogue = EDFACT_guest_epilogue;
lev->l.guest_activate = EDFACT_guest_activate;
lev->l.guest_insert = EDFACT_guest_insert;
lev->l.guest_extract = EDFACT_guest_extract;
lev->l.guest_endcycle = EDFACT_guest_endcycle;
lev->l.guest_end = EDFACT_guest_end;
lev->l.guest_sleep = EDFACT_guest_sleep;
lev->l.guest_delay = EDFACT_guest_delay;
/* fill the EDFACT descriptor part */
for(i=0; i<MAX_PROC; i++) {
lev->period[i] = 0;
lev->deadline_timer[i] = -1;
lev->flag[i] = 0;
lev->dline_miss[i] = 0;
lev->wcet_miss[i] = 0;
lev->nact[i] = 0;
}
lev->ready = NIL;
lev->flags = flags & 0x07;
lev->U = 0;
}
bandwidth_t EDFACT_usedbandwidth(LEVEL l)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->l.level_code == EDFACT_LEVEL_CODE &&
lev->l.level_version == EDFACT_LEVEL_VERSION)
return lev->U;
else
return 0;
}
int EDFACT_get_dline_miss(PID p)
{
LEVEL l = proc_table[p].task_level;
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->l.level_code == EDFACT_LEVEL_CODE &&
lev->l.level_version == EDFACT_LEVEL_VERSION)
return lev->dline_miss[p];
else
return -1;
}
int EDFACT_get_wcet_miss(PID p)
{
LEVEL l = proc_table[p].task_level;
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->l.level_code == EDFACT_LEVEL_CODE &&
lev->l.level_version == EDFACT_LEVEL_VERSION)
return lev->wcet_miss[p];
else
return -1;
}
int EDFACT_get_nact(PID p)
{
LEVEL l = proc_table[p].task_level;
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->l.level_code == EDFACT_LEVEL_CODE &&
lev->l.level_version == EDFACT_LEVEL_VERSION)
return lev->nact[p];
else
return -1;
}
int EDFACT_reset_dline_miss(PID p)
{
LEVEL l = proc_table[p].task_level;
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->l.level_code == EDFACT_LEVEL_CODE &&
lev->l.level_version == EDFACT_LEVEL_VERSION)
{
lev->dline_miss[p] = 0;
return 0;
}
else
return -1;
}
int EDFACT_reset_wcet_miss(PID p)
{
LEVEL l = proc_table[p].task_level;
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[l]);
if (lev->l.level_code == EDFACT_LEVEL_CODE &&
lev->l.level_version == EDFACT_LEVEL_VERSION)
{
lev->wcet_miss[p] = 0;
return 0;
}
else
return -1;
}
void EDFACT_modify_dline(PID p, TIME period, int wcet)
{
EDFACT_level_des *lev = (EDFACT_level_des *)(level_table[proc_table[p].task_level]);
lev->period[p] = period;
// lev->wcet[p] = wcet;
proc_table[p].wcet =wcet;
}