Blame |
Last modification |
View Log
| RSS feed
/* Event Generator
*
* Giacomo Guidi
*/
#include <stdio.h>
#include <stdlib.h>
#include "lparser.h"
#include "lread.h"
#include "time.h"
#include "common/time.h"
#define LOADFILE_DIR "../loadfile/"
#define EVENT_DEFINE "event.c"
#define ACT_LIST "event.c"
int write_struct
(void)
{
FILE
*file_event_header
;
file_event_header
= fopen(EVENT_DEFINE
,"w");
if (file_event_header
== NULL
) return 1;
fprintf(file_event_header
, "\n#include \"func.h\"\n");
fclose(file_event_header
);
return 0;
}
int write_basic_par_start
(void)
{
FILE
*file_event_header
;
file_event_header
= fopen(EVENT_DEFINE
,"a+");
if (file_event_header
== NULL
) return 1;
fprintf(file_event_header
, "struct loader_task loader_task_list[] = {\n");
fclose(file_event_header
);
return 0;
}
int write_contract_start
(void)
{
FILE
*file_event_header
;
file_event_header
= fopen(EVENT_DEFINE
,"a+");
if (file_event_header
== NULL
) return 1;
fprintf(file_event_header
, "struct loader_contract loader_contract_list[] = {\n");
fclose(file_event_header
);
return 0;
}
int write_basic_par
(struct loader_task
*c
)
{
FILE
*file_event_header
;
int muxpres
;
char muxtemp
[30];
file_event_header
= fopen(EVENT_DEFINE
,"a+");
if (file_event_header
== NULL
) return 1;
if (c
->crit_type
== PAR_CRIT
) {
muxpres
= 1;
sprintf(muxtemp
,"block_%s",c
->name
);
} else {
muxpres
= 0;
sprintf(muxtemp
,"NULL");
}
fprintf(file_event_header
, " {\"%s\",%d,%d,%d,%d,%d,{%d,%d},{%d,%d},%d,0,%d,%d,act_%s,exec_%s,%s},\n",
c
->name
,(int)c
->task_type
,(int)c
->server
,(int)c
->local_scheduler
,(int)c
->number
,(int)c
->group
,
(int)c
->deadline.
tv_sec, (int)c
->deadline.
tv_nsec,
(int)c
->wcet.
tv_sec, (int)c
->wcet.
tv_nsec,
(int)c
->act_number
, (int)c
->resource
, muxpres
, c
->name
, c
->name
, muxtemp
);
fclose(file_event_header
);
return 0;
}
int write_contract
(struct loader_contract
*c
)
{
FILE
*file_event_header
;
file_event_header
= fopen(EVENT_DEFINE
,"a+");
if (file_event_header
== NULL
) return 1;
fprintf(file_event_header
, " {%d,{%d,%d},{%d,%d},{%d,%d},{%d,%d},%d,{%d,%d},%d,-1},\n",
(int)c
->number
,(int)c
->cmin.
tv_sec,(int)c
->cmin.
tv_nsec,
(int)c
->tmax.
tv_sec,(int)c
->tmax.
tv_nsec,
(int)c
->cmax.
tv_sec,(int)c
->cmax.
tv_nsec,
(int)c
->tmin.
tv_sec,(int)c
->tmin.
tv_nsec,
(int)c
->workload
,
(int)c
->deadline.
tv_sec,(int)c
->deadline.
tv_nsec,
(int)c
->local_scheduler
);
fclose(file_event_header
);
return 0;
}
int close_loader_task
(int total_task_number
)
{
FILE
*file_event_header
;
file_event_header
= fopen(EVENT_DEFINE
,"a+");
if (file_event_header
== NULL
) return 1;
fprintf(file_event_header
,"};\n\n");
fprintf(file_event_header
,"int total_loader_task = %d;\n\n",total_task_number
);
fclose(file_event_header
);
return 0;
}
int close_loader_contract
(int total_contract_number
)
{
FILE
*file_event_header
;
file_event_header
= fopen(EVENT_DEFINE
,"a+");
if (file_event_header
== NULL
) return 1;
fprintf(file_event_header
,"};\n\n");
fprintf(file_event_header
,"int total_loader_contract = %d;\n\n",total_contract_number
);
fclose(file_event_header
);
return 0;
}
int write_simulation_time
(struct timespec
*total
)
{
FILE
*file_event_header
;
file_event_header
= fopen(EVENT_DEFINE
,"a+");
if (file_event_header
== NULL
) return 1;
fprintf(file_event_header
,"struct timespec total_time = {%d,%d};\n\n",(int)total
->tv_sec
,(int)total
->tv_nsec
);
fclose(file_event_header
);
return 0;
}
int write_single_act
(struct timespec
*t
, struct loader_task
*c
)
{
FILE
*file_act_header
;
file_act_header
= fopen(ACT_LIST
,"a+");
if (file_act_header
== NULL
) return 1;
if (TIMESPEC_A_GT_B
(t
,&c
->act_par_1
)) {
fprintf(file_act_header
,"struct timespec act_%s[] = {{%d,%d}};\n\n",c
->name
,
(int)c
->act_par_1.
tv_sec,(int)c
->act_par_1.
tv_nsec);
c
->act_number
= 1;
} else {
fprintf(file_act_header
,"struct timespec act_%s[] = {{0,0}};\n\n",c
->name
);
c
->act_number
= 0;
}
fclose(file_act_header
);
return 0;
}
int write_periodic_act
(struct timespec
*t
, struct loader_task
*c
)
{
FILE
*file_act_header
;
struct timespec tot_time
;
int period
;
file_act_header
= fopen(ACT_LIST
,"a+");
if (file_act_header
== NULL
) return 1;
fprintf(file_act_header
,"struct timespec act_%s[] = {{%d,%d},\n",c
->name
,
(int)c
->act_par_1.
tv_sec,(int)c
->act_par_1.
tv_nsec);
c
->act_number
= 1;
TIMESPEC_ASSIGN
(&tot_time
,&c
->act_par_1
);
period
= TIMESPEC2USEC
(&c
->act_par_2
);
while (TIMESPEC_A_GT_B
(t
, &tot_time
)) {
c
->act_number
++;
ADDUSEC2TIMESPEC
(period
,&tot_time
);
fprintf(file_act_header
," {%d,%d},\n",
(int)c
->act_par_2.
tv_sec,(int)c
->act_par_2.
tv_nsec);
}
fprintf(file_act_header
," };\n\n");
fclose(file_act_header
);
return 0;
}
int write_mean_act
(struct timespec
*t
,struct loader_task
*c
)
{
FILE
*file_act_header
;
struct timespec tot_time
;
int next_act
;
file_act_header
= fopen(ACT_LIST
,"a+");
if (file_act_header
== NULL
) return 1;
fprintf(file_act_header
,"struct timespec act_%s[] = {{%d,%d},\n",c
->name
,
(int)c
->act_par_1.
tv_sec,(int)c
->act_par_1.
tv_nsec);
c
->act_number
= 1;
TIMESPEC_ASSIGN
(&tot_time
,&c
->act_par_1
);
while (TIMESPEC_A_GT_B
(t
, &tot_time
)) {
c
->act_number
++;
next_act
= TIMESPEC2USEC
(&c
->act_par_2
) + random
() % TIMESPEC2USEC
(&c
->act_par_3
) - TIMESPEC2USEC
(&c
->act_par_3
) / 2;
ADDUSEC2TIMESPEC
(next_act
,&tot_time
);
fprintf(file_act_header
," {%d,%d},\n",
next_act
/ 1000000, next_act
% 1000000 * 1000);
}
fprintf(file_act_header
," };\n\n");
fclose(file_act_header
);
return 0;
}
int write_block_const
(struct loader_task
*c
)
{
FILE
*file_block_header
;
int i
;
file_block_header
= fopen(ACT_LIST
,"a+");
if (file_block_header
== NULL
) return 1;
fprintf(file_block_header
,"struct timespec block_%s[] = {{%d,%d},\n",c
->name
,
(int)c
->crit_par.
tv_sec,(int)c
->crit_par.
tv_nsec);
for (i
=0; i
< c
->act_number
-1; i
++)
fprintf(file_block_header
," {%d,%d},\n",
(int)c
->crit_par.
tv_sec,(int)c
->crit_par.
tv_nsec);
fprintf(file_block_header
," };\n\n");
fclose(file_block_header
);
return 0;
}
int write_exec_const
(struct loader_task
*c
)
{
FILE
*file_exec_header
;
int i
;
file_exec_header
= fopen(ACT_LIST
,"a+");
if (file_exec_header
== NULL
) return 1;
fprintf(file_exec_header
,"struct timespec exec_%s[] = {{%d,%d},\n",c
->name
,
(int)c
->exec_par_1.
tv_sec,(int)c
->exec_par_1.
tv_nsec);
for (i
=0; i
< c
->act_number
-1; i
++)
fprintf(file_exec_header
," {%d,%d},\n",
(int)c
->exec_par_1.
tv_sec,(int)c
->exec_par_1.
tv_nsec);
fprintf(file_exec_header
," };\n\n");
fclose(file_exec_header
);
return 0;
}
int write_exec_mean
(struct loader_task
*c
)
{
FILE
*file_exec_header
;
int exec_time_usec
;
int i
;
file_exec_header
= fopen(ACT_LIST
,"a+");
if (file_exec_header
== NULL
) return 1;
exec_time_usec
= TIMESPEC2USEC
(&c
->exec_par_1
)
+ random
() % TIMESPEC2USEC
(&c
->exec_par_2
) - TIMESPEC2USEC
(&c
->exec_par_2
) / 2;
fprintf(file_exec_header
,"struct timespec exec_%s[] = {{%d,%d},\n",c
->name
,
exec_time_usec
/ 1000000, exec_time_usec
% 1000000 * 1000);
for (i
=0; i
< c
->act_number
-1; i
++) {
exec_time_usec
= TIMESPEC2USEC
(&c
->exec_par_1
)
+ random
() % TIMESPEC2USEC
(&c
->exec_par_2
) - TIMESPEC2USEC
(&c
->exec_par_2
) / 2;
fprintf(file_exec_header
," {%d,%d},\n",
exec_time_usec
/ 1000000, exec_time_usec
% 1000000 * 1000);
}
fprintf(file_exec_header
," };\n\n");
fclose(file_exec_header
);
return 0;
}
void *start
;
void *end
;
int main
(int argc
, char **argv
) {
char loadfile
[100];
struct timespec total_time
;
struct loader_task
*start_loader_task
= NULL
, *current_t
;
struct loader_contract
*start_loader_contract
= NULL
, *current_c
;
int err
,ldnum
;
int total_task_number
;
int total_contract_number
;
printf("\nEvent Generator\n");
if (argc
< 2) {
printf("Error: event_gen loadfile.fsf\n");
exit(1);
}
printf("Read loader file %s\n",argv
[1]);
sprintf(loadfile
,"%s%s",LOADFILE_DIR
,argv
[1]);
err
= dos_preload
(loadfile
,100000,&start
,&end
);
if (err
!= 0) {
printf("Error: File not found\n");
exit(1);
}
printf("Parsing file\n");
line_reader
(start
, end
, &total_time
, &start_loader_task
, &start_loader_contract
);
srandom
(time(NULL
));
write_struct
();
current_t
= start_loader_task
;
ldnum
= 1;
while(current_t
!= NULL
) {
sprintf(current_t
->name
,"ltask%d",ldnum
);
current_t
->group
= ldnum
;
ldnum
++;
switch (current_t
->act_type
) {
case PAR_ACT_SINGLE
:
err
= write_single_act
(&total_time
,current_t
);
if (err
!= 0) {
printf("Error writing activation header\n");
exit(1);
}
break;
case PAR_ACT_PERIODIC
:
err
= write_periodic_act
(&total_time
,current_t
);
if (err
!= 0) {
printf("Error writing activation header\n");
exit(1);
}
break;
case PAR_ACT_MEAN
:
err
= write_mean_act
(&total_time
,current_t
);
if (err
!= 0) {
printf("Error writing activation header\n");
exit(1);
}
break;
}
switch (current_t
->exec_type
) {
case PAR_EXEC_CONST
:
err
= write_exec_const
(current_t
);
if (err
!= 0) {
printf("Error writing exec header\n");
exit(1);
}
break;
case PAR_EXEC_MEAN
:
err
= write_exec_mean
(current_t
);
if (err
!= 0) {
printf("Error writing exec header\n");
exit(1);
}
break;
}
switch (current_t
->crit_type
) {
case PAR_CRIT
:
err
= write_block_const
(current_t
);
if (err
!= 0) {
printf("Error writing block header\n");
exit(1);
}
break;
}
current_t
= current_t
->next
;
}
write_basic_par_start
();
total_task_number
= 0;
current_t
= start_loader_task
;
while(current_t
!= NULL
) {
write_basic_par
(current_t
);
current_t
= current_t
->next
;
total_task_number
++;
}
close_loader_task
(total_task_number
);
write_contract_start
();
total_contract_number
= 0;
current_c
= start_loader_contract
;
while(current_c
!= NULL
) {
write_contract
(current_c
);
current_c
= current_c
->next
;
total_contract_number
++;
}
close_loader_contract
(total_contract_number
);
write_simulation_time
(&total_time
);
return 0;
}