Subversion Repositories shark

Compare Revisions

Ignore whitespace Rev 1180 → Rev 1181

/demos/trunk/fsf/test1.c
0,0 → 1,406
/*
* Project: S.Ha.R.K.
*
* Coordinators:
* Giorgio Buttazzo <giorgio@sssup.it>
* Paolo Gai <pj@gandalf.sssup.it>
*
* Authors :
* Giacomo Guidi <giacomo@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) 2002 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 "pthread.h"
#include "time.h"
#include "posix.h"
 
#include "fsf_contract.h"
 
#include "cbsstar.h"
#include "posixstar.h"
#include "modules/rr.h"
#include "modules/dummy.h"
 
#include "modules/sem.h"
#include "modules/pi.h"
#include "modules/pc.h"
 
#include "modules/hartport.h"
#include "modules/cabs.h"
 
#include "drivers/keyb.h"
#include <stdlib.h>
 
// --------------------------------------------------
// --------------------------------------------------
// Init Part
// --------------------------------------------------
// --------------------------------------------------
 
/*+ sysyem tick in us +*/
#define TICK 0
 
/*+ RR tick in us +*/
#define RRTICK 10000
 
extern int cbsstar_level;
extern int posix_level;
 
TIME __kernel_register_levels__(void *arg)
{
struct multiboot_info *mb = (struct multiboot_info *)arg;
 
EDF_register_level(EDF_ENABLE_ALL);
posix_level = POSIX_register_level(RRTICK, RR_MAIN_YES, mb, 32);
dummy_register_level();
cbsstar_level = CBSSTAR_register_level(4, 0);
 
// for the keyboard...
CBS_register_level(CBS_ENABLE_ALL, 0);
 
PI_register_module();
PC_register_module();
 
SEM_register_module();
 
PTHREAD_register_module(1, 0, 1);
 
return TICK;
}
 
 
TASK __init__(void *arg)
{
struct multiboot_info *mb = (struct multiboot_info *)arg;
HARTPORT_init();
 
KEYB_init(NULL);
__call_main__(mb);
 
return (void *)0;
 
}
 
// --------------------------------------------------
// --------------------------------------------------
// The Test
// --------------------------------------------------
// --------------------------------------------------
 
pthread_t j1 = -1;
pthread_t j2 = -1;
pthread_t j3 = -1;
pthread_t j4 = -1;
fsf_server_id_t server1 = -1;
fsf_server_id_t server2 = -1;
fsf_server_id_t server3 = -1;
fsf_server_id_t server4 = -1;
fsf_contract_parameters_t contract1, contract2;
 
pthread_mutex_t mux;
 
#define TASK_PERIOD 1000000
 
extern struct timespec maxdiffint;
 
void *periodic_star(void *arg)
{
struct timespec actual,end,next_time;
int actpersecond,act,cycle;
int mean,nmean;
bool was_deadline_missed, was_budget_overran;
 
act = 0;
actpersecond = 0;
mean = 0;
nmean = 0;
cycle = 0;
for (;;) {
kern_gettime(&actual);
cycle++;
 
if (act == 0) {
TIMESPEC_ASSIGN(&end,&actual);
end.tv_sec++;
}
 
if (TIMESPEC_A_LT_B(&actual,&end)) {
act++;
} else {
actpersecond = act;
act = 0;
mean = (mean * nmean + actpersecond) / (nmean+1);
nmean++;
}
 
pthread_mutex_lock(&mux);
printf_xy(0,exec_shadow,WHITE,"Thread %3d Act_per_Second = %8d Mean = %8d Cycle = %8d",
exec_shadow,actpersecond,mean,cycle);
printf_xy(50,20,RED,"<DISTIME Ts%ld:Tns%ld>",maxdiffint.tv_sec,maxdiffint.tv_nsec);
pthread_mutex_unlock(&mux);
 
kern_gettime(&next_time);
ADDUSEC2TIMESPEC(TASK_PERIOD, &next_time);
fsf_schedule_next_timed_job(NULL, NULL, NULL, &was_deadline_missed, &was_budget_overran);
 
}
 
return NULL;
 
}
 
void *star(void *arg)
{
struct timespec actual,end;
int actpersecond,act;
int mean,nmean,cycle;
 
act = 0;
actpersecond = 0;
mean = 0;
nmean = 0;
cycle = 0;
for (;;) {
cycle++;
kern_gettime(&actual);
 
if (act == 0) {
TIMESPEC_ASSIGN(&end,&actual);
end.tv_sec++;
}
 
if (TIMESPEC_A_LT_B(&actual,&end)) {
act++;
} else {
actpersecond = act;
act = 0;
mean = (mean * nmean + actpersecond) / (nmean+1);
nmean++;
}
 
pthread_mutex_lock(&mux);
printf_xy(0,exec_shadow,WHITE,"Thread %3d Act_per_Second = %8d Mean = %8d Cycle = %8d",
exec_shadow,actpersecond,mean,cycle);
pthread_mutex_unlock(&mux);
 
}
 
return NULL;
 
}
 
void *edftask(void *arg)
{
int i,j;
while(1) {
for (i=0;i<5; i++) {
for (j=0; j<10; j++);
//cputc('#');
//cputs((char *)(arg));
}
 
task_endcycle();
}
 
return NULL;
}
 
 
void create()
{
HARD_TASK_MODEL mhard;
 
struct timespec period1 = {0,100000000};
struct timespec period2 = {0,100000000};
struct timespec budget1 = {0,30000000};
struct timespec budget2 = {0,30000000};
PID t1, t2;
int err;
 
kern_printf("(Start Create)");
 
hard_task_default_model(mhard);
hard_task_def_ctrl_jet(mhard);
hard_task_def_mit(mhard,32000);
hard_task_def_wcet(mhard,3000);
hard_task_def_arg(mhard,(void *)"X");
hard_task_def_group(mhard,1);
hard_task_def_periodic(mhard);
t1 = task_create("X", edftask, &mhard, NULL);
if (t1 == NIL) {
perror("Could not create task X ...");
sys_end();
}
 
hard_task_def_mit(mhard,32000);
hard_task_def_wcet(mhard,3000);
hard_task_def_arg(mhard,(void *)"Y");
t2 = task_create("Y", edftask, &mhard, NULL);
if (t2 == NIL) {
perror("Could not create task Y ...");
sys_end();
}
 
group_activate(1);
 
err = pthread_create(&j1, NULL, star, (void *)"A");
if (err) {
perror("Could not create task A ...");
sys_end();
}
 
err = pthread_create(&j2, NULL, periodic_star, (void *)"B");
if (err) {
perror("Could not create task B ...");
sys_end();
}
 
err = pthread_create(&j3, NULL, star, (void *)"C");
if (err) {
perror("Could not create task C ...");
sys_end();
}
 
err = pthread_create(&j4, NULL, periodic_star, (void *)"D");
if (err) {
perror("Could not create task D ...");
sys_end();
}
fsf_initialize_contract(&contract1);
fsf_set_contract_basic_parameters(&contract1,&budget1,&period1,NULL,NULL,FSF_DEFAULT_WORKLOAD);
fsf_initialize_contract(&contract2);
fsf_set_contract_basic_parameters(&contract2,&budget2,&period2,NULL,NULL,FSF_DEFAULT_WORKLOAD);
fsf_set_local_scheduler_parameter(&contract2,FSF_SCHEDULER_EDF);
 
kern_printf("(End Create)");
 
}
 
int main(int argc, char **argv)
{
 
char ch = 0;
int err;
NRT_TASK_MODEL nrt;
HARD_TASK_MODEL ht;
 
pthread_mutex_init(&mux,NULL);
 
create();
 
nrt_task_default_model(nrt);
nrt_task_def_save_arrivals(nrt);
 
hard_task_default_model(ht);
hard_task_def_mit(ht,1000000);
hard_task_def_wcet(ht,10000);
 
do {
ch = keyb_getch(BLOCK);
 
switch(ch) {
case '1':
err = fsf_bind_thread_to_server(server1,j1,&nrt);
kern_printf("(%d)",err);
break;
case '2':
err = fsf_bind_thread_to_server(server2,j2,&ht);
kern_printf("(%d)",err);
break;
case '3':
err = fsf_bind_thread_to_server(server1,j3,&nrt);
kern_printf("(%d)",err);
break;
case '4':
err = fsf_bind_thread_to_server(server2,j4,&ht);
kern_printf("(%d)",err);
break;
case '5':
err = fsf_unbind_thread_from_server(j1);
kern_printf("(%d)",err);
break;
case '6':
err = fsf_unbind_thread_from_server(j2);
kern_printf("(%d)",err);
break;
case '7':
err = fsf_unbind_thread_from_server(j3);
kern_printf("(%d)",err);
break;
case '8':
err = fsf_unbind_thread_from_server(j4);
kern_printf("(%d)",err);
break;
case 'q':
err = fsf_negotiate_contract(&contract1,&server1);
cprintf("(%d)",err);
break;
case 'w':
err = fsf_negotiate_contract(&contract2,&server2);
kern_printf("(%d)",err);
break;
case 'e':
err = fsf_negotiate_contract(&contract1,&server3);
kern_printf("(%d)",err);
break;
case 'r':
err = fsf_cancel_contract(&server1);
kern_printf("(%d)",err);
break;
case 't':
err = fsf_cancel_contract(&server2);
kern_printf("(%d)",err);
break;
case 'y':
err = fsf_cancel_contract(&server3);
kern_printf("(%d)",err);
break;
 
}
 
} while(ch != ESC);
 
sys_end();
 
return 0;
 
}
 
/demos/trunk/fsf/demos.txt
0,0 → 1,48
FSF demo for S.Ha.R.K
 
test1.c:
 
This demo shows the main feature of service contract implementation.
 
A set of 4 threads is created using the pthread standard functions.
At the beginning, these threads run without temporal restrictions
and they don't respect any deadline.
 
Two service contracts are initialized and set
 
fsf_initialize_contract(&contract1);
fsf_initialize_contract(&contract2);
fsf_set_contract_basic_parameters(&contract1,&budget1,&period1,NULL,NULL,FSF_DEFAULT_WORKLOAD);
fsf_set_contract_basic_parameters(&contract2,&budget2,&period2,NULL,NULL,FSF_DEFAULT_WORKLOAD);
 
whit the button 'q','w','e' you can respectively
negotiate contract for a server
 
[err = fsf_negotiate_contract(&contractX,&serverY);]
 
'q' -> negotiate contract1 for server1
'w' -> negotiate contract2 for server2
'e' -> negotiate contract1 for server3
 
now with button '1','2','3','4' you can bind the thread to a server
 
[err = fsf_bind_thread_to_server(serverY,jZ);]
 
'1' -> bind thread1 to server1
'2' -> bind thread2 to server2
'3' -> bind thread3 to server3
'4' -> bind thread4 to server2
 
threads will start to respect the assigned budget. It's possible
to bind more threads to one server. The local scheduler specified in the
contract define how the threads will get the server resources.
 
With '5','6','7','8' the thread is unbind from the server
 
[err = fsf_unbind_thread_from_server(jZ);]
 
With 'r','t','y' a server is removed
 
[err = fsf_cancel_contract(&server2);]
 
 
/demos/trunk/fsf/makefile
0,0 → 1,13
#
#
 
BASE=../..
include $(BASE)/config/config.mk
 
PROGS= test1
 
include $(BASE)/config/example.mk
 
test1:
make -f $(SUBMAKE) APP=test1 INIT= OTHEROBJS= OTHERINCL= SHARKOPT="__OLDCHAR__ __FIRST__"