/*
* 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 "modules/posix.h"
#include "fsf_contract.h"
#include "fsf_server.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
int grubstar_level
;
TIME __kernel_register_levels__
(void *arg
)
{
struct multiboot_info
*mb
= (struct multiboot_info
*)arg
;
EDF_register_level
(EDF_ENABLE_ALL
);
POSIX_register_level
(RRTICK
, 1, mb
, 32);
grubstar_level
= GRUBSTAR_register_level
(5, 0);
FSF_register_module
(grubstar_level
, (int)(MAX_BANDWIDTH
* 0.9));
dummy_register_level
();
// 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 jposix
= -1;
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 contract
;
pthread_mutex_t mux
;
void *posix
(void *arg
)
{
struct timespec actual
,end
;
int actpersecond
,act
;
int cycle
;
act
= 0;
actpersecond
= 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;
}
pthread_mutex_lock
(&mux
);
printf_xy
(0,exec_shadow
,GREEN
,"Thread %3d Act_per_Second = %8d cycle = %8d",
exec_shadow
,actpersecond
,cycle
);
pthread_mutex_unlock
(&mux
);
}
return NULL
;
}
void *star
(void *arg
)
{
struct timespec actual
,end
;
int actpersecond
,act
;
int cycle
,rec
;
act
= 0;
actpersecond
= 0;
cycle
= 0;
rec
= 0;
for (;;) {
cycle
++;
kern_gettime
(&actual
);
rec
= SERVER_get_last_reclaiming
(grubstar_level
,exec_shadow
);
if (act
== 0) {
TIMESPEC_ASSIGN
(&end
,&actual
);
end.
tv_sec++;
}
if (TIMESPEC_A_LT_B
(&actual
,&end
)) {
act
++;
} else {
actpersecond
= act
;
act
= 0;
}
pthread_mutex_lock
(&mux
);
printf_xy
(0,exec_shadow
,WHITE
,"Thread %3d Act_per_Second = %8d cycle = %8d rec = %8d",
exec_shadow
,actpersecond
,cycle
,rec
);
pthread_mutex_unlock
(&mux
);
}
return NULL
;
}
void *periodic_star
(void *arg
)
{
struct timespec actual
,end
;
int actpersecond
,act
;
int cycle
,rec
;
act
= 0;
actpersecond
= 0;
cycle
= 0;
rec
= 0;
for (;;) {
cycle
++;
kern_gettime
(&actual
);
rec
= SERVER_get_last_reclaiming
(grubstar_level
,exec_shadow
);
if (act
== 0) {
TIMESPEC_ASSIGN
(&end
,&actual
);
end.
tv_sec++;
}
if (TIMESPEC_A_LT_B
(&actual
,&end
)) {
act
++;
} else {
actpersecond
= act
;
act
= 0;
}
pthread_mutex_lock
(&mux
);
printf_xy
(0,exec_shadow
,RED
,"Thread %3d Act_per_Second = %8d cycle = %8d rec = %8d",
exec_shadow
,actpersecond
,cycle
,rec
);
pthread_mutex_unlock
(&mux
);
task_endcycle
();
}
return NULL
;
}
int keyinc
= 5;
void *edftask
(void *arg
)
{
long long j
;
while(1) {
for (j
=0; j
<10000*keyinc
; j
++);
task_endcycle
();
}
return NULL
;
}
void create
()
{
HARD_TASK_MODEL mhard
;
int err
;
struct timespec period1
= {0,90000000}; //30%
struct timespec period2
= {0,300000000}; //20%
struct timespec period3
= {0,300000000}; //10%
struct timespec budget1
= {0,30000000};
struct timespec budget2
= {0,60000000};
struct timespec budget3
= {0,30000000};
PID t1
, t2
;
kern_printf
("(Start Create)");
hard_task_default_model
(mhard
);
hard_task_def_ctrl_jet
(mhard
);
hard_task_def_mit
(mhard
,30000); //5%
hard_task_def_wcet
(mhard
,1500);
hard_task_def_arg
(mhard
,(void *)"X");
hard_task_def_group
(mhard
,1);
hard_task_def_periodic
(mhard
);
hard_task_def_level
(mhard
,0);
t1
= task_create
("X", edftask
, &mhard
, NULL
);
if (t1
== NIL
) {
perror("Could not create task X ...");
sys_end
();
}
hard_task_def_mit
(mhard
,30000); //5%
hard_task_def_wcet
(mhard
,1500);
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);
fsf_initialize_contract
(&contract
);
fsf_set_contract_basic_parameters
(&contract
,&budget1
,&period1
,NULL
,NULL
,FSF_DEFAULT_WORKLOAD
);
fsf_negotiate_contract
(&contract
,&server1
);
fsf_set_contract_basic_parameters
(&contract
,&budget2
,&period2
,NULL
,NULL
,FSF_DEFAULT_WORKLOAD
);
fsf_negotiate_contract
(&contract
,&server2
);
fsf_set_contract_basic_parameters
(&contract
,&budget3
,&period3
,NULL
,NULL
,FSF_DEFAULT_WORKLOAD
);
fsf_negotiate_contract
(&contract
,&server3
);
fsf_set_contract_basic_parameters
(&contract
,&budget2
,&period2
,NULL
,NULL
,FSF_DEFAULT_WORKLOAD
);
fsf_set_local_scheduler_parameter
(&contract
, FSF_SCHEDULER_EDF
);
fsf_negotiate_contract
(&contract
,&server4
);
err
= pthread_create
(&jposix
, NULL
, posix
, NULL
);
if (err
) kern_printf
("(Error creating posix task)");
kern_printf
("(End Create)");
}
int main
(int argc
, char **argv
)
{
int err
;
HARD_TASK_MODEL ht
;
NRT_TASK_MODEL nrt
;
struct timespec endtimer
;
pthread_mutex_init
(&mux
,NULL
);
srand(kern_gettime
(NULL
));
create
();
nrt_task_default_model
(nrt
);
kern_gettime
(&endtimer
);
endtimer.
tv_sec += 20;
kern_event_post
(&endtimer
, (void (*)(void *))(sys_end
), NULL
);
err
= fsf_create_thread
(server1
,&j1
,NULL
,star
,NULL
,&nrt
);
kern_printf
("(%d)",err
);
err
= fsf_create_thread
(server2
,&j2
,NULL
,star
,NULL
,&nrt
);
kern_printf
("(%d)",err
);
err
= fsf_create_thread
(server3
,&j3
,NULL
,star
,NULL
,&nrt
);
kern_printf
("(%d)",err
);
hard_task_default_model
(ht
);
hard_task_def_mit
(ht
,200000);
hard_task_def_wcet
(ht
,50000);
err
= fsf_create_thread
(server4
,&j4
,NULL
,periodic_star
,NULL
,&ht
);
kern_printf
("(%d)",err
);
/* hard_task_default_model(ht);
hard_task_def_mit(ht,159000);
hard_task_def_wcet(ht,50000);
err = fsf_create_thread(server4,&j4,NULL,periodic_star,NULL,&ht);
kern_printf("(%d)",err);
*/
err
= fsf_create_thread
(server1
,&j1
,NULL
,star
,NULL
,&nrt
);
kern_printf
("(%d)",err
);
err
= fsf_create_thread
(server2
,&j2
,NULL
,star
,NULL
,&nrt
);
kern_printf
("(%d)",err
);
err
= fsf_create_thread
(server3
,&j3
,NULL
,star
,NULL
,&nrt
);
kern_printf
("(%d)",err
);
return 0;
}