Subversion Repositories shark

Compare Revisions

Ignore whitespace Rev 1548 → Rev 1549

/demos/trunk/shutdown/readme.txt
0,0 → 1,15
This demo shows how the SHaRK shutdown sequence works.
 
There are two ways to run the demo:
 
- the first one shows a (hopefully) correct shutdown sequence
 
- the second one shows an incorrect shutdows sequence, where the
system task does not have CPU time to execute and correctly
terminate. In this case the initializtion file posted a timeout that
forces system shutdown.
 
Enjoy,
 
PJ
 
/demos/trunk/shutdown/shutdown.c
0,0 → 1,321
/*
* 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
*/
 
/*
* 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
*
*
* CVS : $Id: shutdown.c,v 1.1 2005-01-08 14:38:27 pj Exp $
 
Shutdown demo, used to test system shutdown.
 
- derived from demo aster4
- the aper_asteroid task simulates some system tasks that have to
finish prior system shutdown.
 
- note that if you remove the nanosleep in aper_asteroid
the system does not shut down correctly because the system
tasks does not receive any CPU time!!!
*/
 
/*
* 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 "kernel/kern.h"
#include "modules/edf.h"
#include "modules/cbs.h"
 
#include <time.h>
 
#include <drivers/shark_linuxc26.h>
#include <drivers/shark_input26.h>
#include <drivers/shark_keyb26.h>
 
int num_aster = 0;
#define ASTER_LIM 60
#define DISPLAY_MAX 10
#define ASTER_MAX 70
#define STAT_Y 24
 
#define PER_MAX 5
#define APER_MAX 8
 
// These numbers works on a Pentium 133 */
#define PER_WCET 25000
#define APER_WCET 53000
#define CLOCK_WCET 1000
#define ASTER_WCET 1000
#define SOFT_MET 6300
 
#define APER_REP 22000
 
PID aper_table[APER_MAX];
 
int shutting_down = 0;
int nobandwidth;
 
TASK asteroide(void *arg)
{
int i;
 
for (;;) {
i = 1;
for (i=1; i < ASTER_LIM; i++) {
puts_xy(i,20,WHITE,"*");
task_endcycle();
puts_xy(i,20,WHITE," ");
}
}
}
 
 
TASK system_asteroide(void *arg)
{
int i;
struct timespec t;
t.tv_sec = 0;
 
for (;;) {
i = 1;
for (i=1; i < ASTER_LIM; i++) {
puts_xy(i,21,WHITE,"s");
 
if (shutting_down) {
cprintf("Ending System Task %d\n",(int)arg);
return 0;
}
t.tv_nsec = 1000000*(100+rand()%200);
nanosleep(&t,NULL);
 
puts_xy(i,21,WHITE," ");
}
}
}
 
 
TASK aster()
{
PID p;
 
SOFT_TASK_MODEL m_soft;
int r;
int x; // adaptive bandwidth...
 
 
soft_task_default_model(m_soft);
soft_task_def_met(m_soft,SOFT_MET);
soft_task_def_ctrl_jet(m_soft);
 
x = 64;
 
while (1) {
if (num_aster < ASTER_MAX) {
r = (rand() % 200);
 
soft_task_def_period(m_soft, (x+r)*1000);
p = task_create("aaa",asteroide,&m_soft,NULL);
if (p == -1)
{
if (x < 500 && errno != ENO_AVAIL_TASK) x += 1;
}
else {
num_aster++;
task_activate(p);
x /= 2;
if (x<50) x = 50;
}
}
task_endcycle();
}
}
 
 
/* we consider the first ASTER_MAX + 2 tasks from the PID 2
and plot on the screen the elapsed times... */
TASK jetcontrol()
{
int i; /* a counter */
TIME curr;
PID p;
struct timespec t;
t.tv_sec=0;
t.tv_nsec=100000;
 
 
kern_cli();
printf_xy(0,STAT_Y,WHITE,"PID ³ Curr. ³ status ³ name");
kern_sti();
 
for (;;) {
for (i=0,p=0; i<DISPLAY_MAX+5 && p<MAX_PROC; p++) {
if (proc_table[p].status==FREE) continue;
 
curr = 0;
jet_getstat(p, NULL, NULL, NULL, &curr);
 
kern_cli();
printf_xy(0,STAT_Y+i+1,WHITE,"%-3d ³ %-7d ³ %-6d ³ %s ",
p, (int)curr, proc_table[p].status, proc_table[p].name);
kern_sti();
i++;
}
 
for (; i<DISPLAY_MAX+5;i++) {
printf_xy(0,STAT_Y+i+1,WHITE," ³ ³ ³ ");
}
 
if (!nobandwidth) nanosleep(&t, NULL);
}
}
 
void endfun(KEY_EVT *k)
{
exit(0);
}
 
void exiting(void *arg)
{
cprintf("System shut down...\n");
shutting_down = 1;
}
 
int main(int argc, char **argv)
{
KEY_EVT k;
 
PID p1,p3;
HARD_TASK_MODEL m;
SOFT_TASK_MODEL m_soft;
NRT_TASK_MODEL m_nrt;
int i;
 
srand(7);
 
clear();
cprintf("REMEMBER: RUN THIS DEMO IN 80x50 TEXT MODE!!!\n");
cprintf("use \"mode con lines=50\" to put the screen in 80x50.\n\n");
cprintf("Press SPACE for a typical WRONG shutdown sequence\n");
cprintf(" where the shutdown task does not receive bandwidth\n\n");
cprintf("Press another key for a clear shutdown\n");
nobandwidth = !(keyb_getch(BLOCK) - ' ');
 
 
k.flag = 0;
k.scan = KEY_ENT;
k.ascii = 13;
k.status = KEY_PRESSED;
keyb_hook(k, endfun, FALSE);
 
 
 
clear();
if (nobandwidth)
cprintf("ENTER to end the demo WITH A TIMEOUT...\n");
else
cprintf("ENTER to end the demo SHUTTING DOWN THE SYSTEM TASKS...\n");
 
sys_atrunlevel(exiting, NULL, RUNLEVEL_SHUTDOWN);
 
hard_task_default_model(m);
hard_task_def_wcet(m,ASTER_WCET);
hard_task_def_mit(m,10000);
hard_task_def_group(m,1);
hard_task_def_ctrl_jet(m);
p1 = task_create("Aster",aster,&m,NULL);
if (p1 == -1) {
sys_shutdown_message("(main): Could not create task <aster> ...");
exit(0);
}
 
 
soft_task_default_model(m_soft);
soft_task_def_met(m_soft,1000);
soft_task_def_period(m_soft,100000);
soft_task_def_group(m_soft,1);
soft_task_def_aperiodic(m_soft);
soft_task_def_system(m_soft);
soft_task_def_ctrl_jet(m_soft);
soft_task_def_nokill(m_soft);
p3 = task_create("JetControl",jetcontrol,&m_soft,NULL);
if (p3 == -1) {
sys_shutdown_message("(main): Could not create task <JetControl> ...");
exit(0);
}
 
 
/* These are the tasks that simulate the system tasks */
 
nrt_task_default_model(m_nrt);
nrt_task_def_ctrl_jet(m_nrt);
nrt_task_def_system(m_nrt);
nrt_task_def_group(m_nrt,1);
 
cprintf("creating system tasks: ");
for (i=0; i<APER_MAX; i++) {
char systemtaskname[]="mysystemtask0";
nrt_task_def_arg(m_nrt,(void *)i);
systemtaskname[12]='0'+i;
aper_table[i] = task_create(systemtaskname,system_asteroide,&m_nrt,NULL);
if (aper_table[i] == -1) {
sys_shutdown_message("(main): Could not create task <mysystemtask> ..."); exit(0);
}
else
cprintf(" %d",i);
}
cprintf("\n");
 
 
 
group_activate(1);
return 0;
}
 
/demos/trunk/shutdown/ishut.c
0,0 → 1,183
/*
* 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
*/
 
/*
* 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 "kernel/kern.h"
#include "modules/intdrive.h"
#include "modules/edf.h"
#include "modules/rr.h"
#include "modules/tbs.h"
#include "modules/cbs.h"
#include "modules/rrsoft.h"
#include "modules/dummy.h"
#include "modules/sem.h"
#include "modules/hartport.h"
 
/*+ sysyem tick in us +*/
#define TICK 0
 
#define RRTICK 5000
 
/*+ Interrupt Server +*/
#define INTDRIVE_Q 1000
#define INTDRIVE_T 10000
#define INTDRIVE_FLAG 0
 
#include <drivers/shark_linuxc26.h>
#include <drivers/shark_input26.h>
#include <drivers/shark_keyb26.h>
 
#define FRAME_BUFFER_DEVICE 0
 
void call_shutdown_task(void *arg);
int device_drivers_init();
int device_drivers_close();
void set_shutdown_task();
TASK shutdown_task_body(void *arg);
 
PID shutdown_task_PID = -1;
 
TIME __kernel_register_levels__(void *arg)
{
struct multiboot_info *mb = (struct multiboot_info *)arg;
 
INTDRIVE_register_level(INTDRIVE_Q,INTDRIVE_T,INTDRIVE_FLAG);
EDF_register_level(EDF_ENABLE_ALL);
RR_register_level(RRTICK, RR_MAIN_YES, mb);
CBS_register_level(CBS_ENABLE_ALL, 1);
 
dummy_register_level();
 
SEM_register_module();
 
return TICK;
}
 
TASK __init__(void *arg)
{
struct multiboot_info *mb = (struct multiboot_info *)arg;
 
HARTPORT_init();
 
/* Create the shutdown task. It will be activated at RUNLEVEL
SHUTDOWN */
set_shutdown_task();
 
/* Init the drivers */
device_drivers_init();
 
/* Set the shutdown task activation */
sys_atrunlevel(call_shutdown_task, NULL, RUNLEVEL_SHUTDOWN);
 
__call_main__(mb);
 
return (void *)0;
}
 
void set_shutdown_task() {
 
/* WARNING: the shutdown task is a background thread. It cannot execute
if the system is overloaded */
NRT_TASK_MODEL nrt;
 
nrt_task_default_model(nrt);
nrt_task_def_system(nrt);
 
shutdown_task_PID = task_create("Shutdown Task",shutdown_task_body,&nrt,NULL);
if (shutdown_task_PID == NIL) {
sys_shutdown_message("Error: Cannot create shutdown task\n");
exit(1);
}
 
}
 
int device_drivers_init() {
 
KEYB_PARMS kparms = BASE_KEYB;
LINUXC26_register_module();
 
INPUT26_init();
 
keyb_def_ctrlC(kparms, NULL);
 
KEYB26_init(&kparms);
 
return 0;
 
}
 
int device_drivers_close() {
KEYB26_close();
INPUT26_close();
return 0;
}
 
#define SHUTDOWN_TIMEOUT_SEC 5
 
void mytimeout(void *x)
{
kern_printf("\nTIMEOUT!!!!!\n");
sys_abort_shutdown(1);
}
 
void call_shutdown_task(void *arg)
{
struct timespec t;
 
kern_printf("\ncall_shutdown_task\n");
sys_gettime(&t);
t.tv_sec += SHUTDOWN_TIMEOUT_SEC;
 
/* Emergency timeout to exit from RUNLEVEL_SHUTDOWN */
kern_event_post(&t,mytimeout,(void *)0);
 
task_activate(shutdown_task_PID);
}
 
TASK shutdown_task_body(void *arg) {
 
device_drivers_close();
 
sys_shutdown_message("-- S.Ha.R.K. Closed --\n");
 
return NULL;
 
}
/demos/trunk/shutdown/makefile
0,0 → 1,11
ifndef BASE
BASE=../..
endif
include $(BASE)/config/config.mk
 
PROGS += shutdown
 
include $(BASE)/config/example.mk
 
shutdown:
make -f $(SUBMAKE) APP=shutdown INIT= OTHEROBJS="ishut.o" OTHERINCL= SHARKOPT="__LINUXC26__ __INPUT__"