Rev 1234 | Rev 1238 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1225 | giacomo | 1 | /* Event Generator |
2 | * |
||
3 | * Giacomo Guidi |
||
4 | */ |
||
5 | |||
6 | #include <stdio.h> |
||
7 | #include <stdlib.h> |
||
8 | |||
1237 | giacomo | 9 | #include "parser.h" |
10 | #include "time.h" |
||
11 | |||
12 | #define EVENT_HEADER "eventh.h" |
||
13 | #define EVENT_DEFINE "eventc.c" |
||
1228 | giacomo | 14 | #define ACT_LIST "act_list.h" |
1225 | giacomo | 15 | |
16 | |||
1227 | giacomo | 17 | int act_number; |
18 | |||
1228 | giacomo | 19 | int write_struct(void) |
20 | { |
||
21 | |||
22 | FILE *file_event_header; |
||
23 | |||
1237 | giacomo | 24 | file_event_header = fopen(EVENT_DEFINE,"w"); |
1228 | giacomo | 25 | if (file_event_header == NULL) return 1; |
26 | |||
1237 | giacomo | 27 | fprintf(file_event_header, "\n#include \"event_gen.h\"\n"); |
28 | fprintf(file_event_header, "#include \"%s\"\n",ACT_LIST); |
||
1228 | giacomo | 29 | |
30 | fprintf(file_event_header, "struct loader_task loader_task_list[] = {\n"); |
||
31 | |||
32 | fclose(file_event_header); |
||
33 | |||
34 | return 0; |
||
35 | |||
36 | } |
||
37 | |||
1237 | giacomo | 38 | int write_basic_par(struct loader_task *c) |
1228 | giacomo | 39 | { |
40 | |||
41 | FILE *file_event_header; |
||
42 | |||
1237 | giacomo | 43 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
1228 | giacomo | 44 | if (file_event_header == NULL) return 1; |
45 | |||
1237 | giacomo | 46 | fprintf(file_event_header, " {\"%s\",%d,%d,%d,%d,%d,{%d,%d},{%d,%d},%d,0,act_%s,exec_%s},\n", |
47 | c->name,c->task_type,c->server,c->local_scheduler,c->number,c->group, |
||
48 | c->deadline.tv_sec, c->deadline.tv_nsec,c->act_number, |
||
49 | c->wcet.tv_sec, c->wcet.tv_nsec, c->name, c->name, c->name); |
||
1228 | giacomo | 50 | |
51 | fclose(file_event_header); |
||
52 | |||
53 | return 0; |
||
54 | |||
55 | } |
||
56 | |||
57 | int close_loader() |
||
58 | { |
||
59 | |||
60 | FILE *file_event_header; |
||
61 | |||
1237 | giacomo | 62 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
1228 | giacomo | 63 | if (file_event_header == NULL) return 1; |
64 | |||
65 | fprintf(file_event_header,"};\n\n"); |
||
66 | |||
67 | fclose(file_event_header); |
||
68 | |||
69 | return 0; |
||
70 | |||
71 | } |
||
72 | |||
1237 | giacomo | 73 | int write_single_act(struct timespec *t, struct loader_task *c) |
1225 | giacomo | 74 | { |
75 | |||
76 | FILE *file_act_header; |
||
77 | |||
1228 | giacomo | 78 | file_act_header = fopen(ACT_LIST,"a+"); |
1225 | giacomo | 79 | if (file_act_header == NULL) return 1; |
80 | |||
1237 | giacomo | 81 | if (TIMESPEC_A_GT_B(t,&c->act_par_1)) { |
82 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name, |
||
83 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
84 | c->act_number = 1; |
||
85 | } else { |
||
86 | fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name); |
||
87 | c->act_number = 0; |
||
88 | } |
||
1227 | giacomo | 89 | |
1225 | giacomo | 90 | fclose(file_act_header); |
91 | |||
92 | return 0; |
||
93 | |||
94 | } |
||
95 | |||
1237 | giacomo | 96 | int write_periodic_act(struct timespec *t, struct loader_task *c) |
1225 | giacomo | 97 | { |
98 | |||
99 | FILE *file_act_header; |
||
1237 | giacomo | 100 | int i; |
101 | struct timespec tot_time; |
||
102 | int period; |
||
1228 | giacomo | 103 | |
104 | file_act_header = fopen(ACT_LIST,"a+"); |
||
1225 | giacomo | 105 | if (file_act_header == NULL) return 1; |
106 | |||
1237 | giacomo | 107 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
108 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
1225 | giacomo | 109 | |
1237 | giacomo | 110 | c->act_number = 1; |
111 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
112 | period = TIMESPEC2USEC(&c->act_par_2); |
||
113 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
114 | c->act_number++; |
||
115 | ADDUSEC2TIMESPEC(period,&tot_time); |
||
1228 | giacomo | 116 | fprintf(file_act_header," {%d,%d},\n", |
1237 | giacomo | 117 | c->act_par_2.tv_sec,c->act_par_2.tv_nsec); |
1225 | giacomo | 118 | } |
119 | |||
1228 | giacomo | 120 | fprintf(file_act_header," };\n\n"); |
1225 | giacomo | 121 | |
122 | fclose(file_act_header); |
||
123 | |||
124 | return 0; |
||
125 | |||
126 | } |
||
127 | |||
1237 | giacomo | 128 | int write_mean_act(struct timespec *t,struct loader_task *c) |
1225 | giacomo | 129 | { |
130 | |||
131 | FILE *file_act_header; |
||
1227 | giacomo | 132 | int i,first_time_usec,mean_time_usec,delta_time_usec; |
1237 | giacomo | 133 | struct timespec tot_time; |
134 | int next_act; |
||
135 | |||
1228 | giacomo | 136 | file_act_header = fopen(ACT_LIST,"a+"); |
1225 | giacomo | 137 | if (file_act_header == NULL) return 1; |
138 | |||
1237 | giacomo | 139 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
140 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
141 | |||
142 | c->act_number = 1; |
||
143 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
144 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
145 | c->act_number++; |
||
146 | next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2; |
||
147 | ADDUSEC2TIMESPEC(next_act,&tot_time); |
||
1232 | giacomo | 148 | fprintf(file_act_header," {%d,%d},\n", |
1237 | giacomo | 149 | next_act / 1000000, next_act % 1000000 * 1000); |
1225 | giacomo | 150 | } |
151 | |||
1228 | giacomo | 152 | fprintf(file_act_header," };\n\n"); |
1225 | giacomo | 153 | |
154 | fclose(file_act_header); |
||
155 | |||
156 | return 0; |
||
157 | |||
158 | } |
||
159 | |||
1237 | giacomo | 160 | int write_exec_const(struct loader_task *c) |
1226 | giacomo | 161 | { |
162 | |||
1227 | giacomo | 163 | FILE *file_exec_header; |
164 | int exec_time_usec,i; |
||
165 | |||
1228 | giacomo | 166 | file_exec_header = fopen(ACT_LIST,"a+"); |
1227 | giacomo | 167 | if (file_exec_header == NULL) return 1; |
168 | |||
1237 | giacomo | 169 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",c->name, |
170 | c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec); |
||
1227 | giacomo | 171 | |
172 | for (i=0; i< act_number-1; i++) |
||
1228 | giacomo | 173 | fprintf(file_exec_header," {%d,%d},\n", |
1237 | giacomo | 174 | c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec); |
1227 | giacomo | 175 | |
1228 | giacomo | 176 | fprintf(file_exec_header," };\n\n"); |
1227 | giacomo | 177 | |
178 | fclose(file_exec_header); |
||
179 | |||
1226 | giacomo | 180 | return 0; |
181 | |||
182 | } |
||
183 | |||
1237 | giacomo | 184 | int write_exec_mean(struct loader_task *c) |
1226 | giacomo | 185 | { |
186 | |||
1227 | giacomo | 187 | FILE *file_exec_header; |
1237 | giacomo | 188 | struct timespec exec_time; |
189 | int i; |
||
1227 | giacomo | 190 | |
1228 | giacomo | 191 | file_exec_header = fopen(ACT_LIST,"a+"); |
1227 | giacomo | 192 | if (file_exec_header == NULL) return 1; |
193 | |||
1237 | giacomo | 194 | //exec_time_usec = mean_time_usec + random() % delta_time_usec - delta_time_usec / 2; |
1227 | giacomo | 195 | |
1228 | giacomo | 196 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n", |
1237 | giacomo | 197 | c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec); |
198 | |||
199 | TIMESPEC_ASSIGN(&exec_time,&c->exec_par_1); for (i=0; i< act_number-1; i++) { |
||
200 | //exec_time_usec = mean_time_usec + random() % delta_time_usec - delta_time_usec / 2; |
||
1228 | giacomo | 201 | fprintf(file_exec_header," {%d,%d},\n", |
1237 | giacomo | 202 | exec_time.tv_sec,exec_time.tv_nsec); |
1227 | giacomo | 203 | } |
204 | |||
1228 | giacomo | 205 | fprintf(file_exec_header," };\n\n"); |
1227 | giacomo | 206 | |
207 | fclose(file_exec_header); |
||
208 | |||
1226 | giacomo | 209 | return 0; |
210 | |||
211 | } |
||
212 | |||
1237 | giacomo | 213 | void *start; |
214 | void *end; |
||
215 | |||
1225 | giacomo | 216 | int main(void) { |
217 | |||
218 | char task_name[100]; |
||
1226 | giacomo | 219 | char act_type,exec_type; |
1237 | giacomo | 220 | struct timespec total_time; |
221 | struct loader_task *start_loader_task, *current; |
||
222 | int err,ldnum; |
||
1225 | giacomo | 223 | |
1237 | giacomo | 224 | printf("\nEvent Generator\n"); |
1225 | giacomo | 225 | |
1237 | giacomo | 226 | printf("Read loader file\n"); |
227 | |||
228 | dos_preload("loadfile.txt",100000,&start,&end); |
||
229 | |||
230 | printf("Parsing file\n"); |
||
231 | |||
232 | line_reader(start, end, &total_time, &start_loader_task); |
||
233 | |||
1225 | giacomo | 234 | srandom(12354132); |
235 | |||
1228 | giacomo | 236 | write_struct(); |
237 | |||
1237 | giacomo | 238 | current = start_loader_task; |
239 | ldnum = 0; |
||
1225 | giacomo | 240 | |
1237 | giacomo | 241 | while(current != NULL) { |
1225 | giacomo | 242 | |
1237 | giacomo | 243 | sprintf(current->name,"ltask%d",ldnum); |
244 | current->group = ldnum; |
||
1226 | giacomo | 245 | |
1237 | giacomo | 246 | write_basic_par(current); |
1228 | giacomo | 247 | |
1237 | giacomo | 248 | switch (current->act_type) { |
249 | case PAR_ACT_SINGLE: |
||
250 | err = write_single_act(&total_time,current); |
||
1226 | giacomo | 251 | if (err != 0) { |
252 | printf("Error writing activation header\n"); |
||
253 | exit(1); |
||
254 | } |
||
255 | break; |
||
1237 | giacomo | 256 | case PAR_ACT_PERIODIC: |
257 | err = write_periodic_act(&total_time,current); |
||
1226 | giacomo | 258 | if (err != 0) { |
259 | printf("Error writing activation header\n"); |
||
260 | exit(1); |
||
261 | } |
||
262 | break; |
||
1237 | giacomo | 263 | case PAR_ACT_MEAN: |
264 | err = write_mean_act(&total_time,current); |
||
1226 | giacomo | 265 | if (err != 0) { |
266 | printf("Error writing activation header\n"); |
||
267 | exit(1); |
||
268 | } |
||
269 | break; |
||
270 | } |
||
271 | |||
1237 | giacomo | 272 | switch (current->exec_type) { |
273 | case PAR_EXEC_CONST: |
||
274 | err = write_exec_const(current); |
||
1226 | giacomo | 275 | if (err != 0) { |
276 | printf("Error writing exec header\n"); |
||
277 | exit(1); |
||
278 | } |
||
279 | break; |
||
1237 | giacomo | 280 | case PAR_EXEC_MEAN: |
281 | err = write_exec_mean(current); |
||
1226 | giacomo | 282 | if (err != 0) { |
283 | printf("Error writing exec header\n"); |
||
284 | exit(1); |
||
285 | } |
||
286 | break; |
||
287 | } |
||
288 | |||
1237 | giacomo | 289 | current = current->next; |
290 | |||
1225 | giacomo | 291 | } |
292 | |||
1237 | giacomo | 293 | close_loader(); |
294 | |||
1225 | giacomo | 295 | return 0; |
296 | |||
297 | } |