Rev 1250 | 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 | |||
1238 | giacomo | 9 | #include "lparser.h" |
1237 | giacomo | 10 | #include "time.h" |
11 | |||
1250 | giacomo | 12 | #define EVENT_HEADER "event.h" |
13 | #define EVENT_DEFINE "event.c" |
||
14 | #define ACT_LIST "event.c" |
||
1225 | giacomo | 15 | |
1228 | giacomo | 16 | int write_struct(void) |
17 | { |
||
18 | |||
19 | FILE *file_event_header; |
||
20 | |||
1237 | giacomo | 21 | file_event_header = fopen(EVENT_DEFINE,"w"); |
1228 | giacomo | 22 | if (file_event_header == NULL) return 1; |
23 | |||
1240 | giacomo | 24 | fprintf(file_event_header, "\n#include \"func.h\"\n"); |
1228 | giacomo | 25 | |
26 | fclose(file_event_header); |
||
27 | |||
28 | return 0; |
||
29 | |||
30 | } |
||
31 | |||
1238 | giacomo | 32 | int write_basic_par_start(void) |
33 | { |
||
34 | |||
35 | FILE *file_event_header; |
||
36 | |||
37 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
38 | if (file_event_header == NULL) return 1; |
||
39 | |||
40 | fprintf(file_event_header, "struct loader_task loader_task_list[] = {\n"); |
||
41 | |||
42 | fclose(file_event_header); |
||
43 | |||
44 | } |
||
45 | |||
1245 | giacomo | 46 | int write_contract_start(void) |
47 | { |
||
48 | |||
49 | FILE *file_event_header; |
||
50 | |||
51 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
52 | if (file_event_header == NULL) return 1; |
||
53 | |||
54 | fprintf(file_event_header, "struct loader_contract loader_contract_list[] = {\n"); |
||
55 | |||
56 | fclose(file_event_header); |
||
57 | |||
58 | } |
||
59 | |||
1237 | giacomo | 60 | int write_basic_par(struct loader_task *c) |
1228 | giacomo | 61 | { |
62 | |||
63 | FILE *file_event_header; |
||
64 | |||
1237 | giacomo | 65 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
1228 | giacomo | 66 | if (file_event_header == NULL) return 1; |
67 | |||
1237 | giacomo | 68 | fprintf(file_event_header, " {\"%s\",%d,%d,%d,%d,%d,{%d,%d},{%d,%d},%d,0,act_%s,exec_%s},\n", |
69 | c->name,c->task_type,c->server,c->local_scheduler,c->number,c->group, |
||
1240 | giacomo | 70 | c->deadline.tv_sec, c->deadline.tv_nsec, |
71 | c->wcet.tv_sec, c->wcet.tv_nsec, |
||
72 | c->act_number, c->name, c->name, c->name); |
||
1228 | giacomo | 73 | |
74 | fclose(file_event_header); |
||
75 | |||
76 | return 0; |
||
77 | |||
78 | } |
||
79 | |||
1245 | giacomo | 80 | int write_contract(struct loader_contract *c) |
1228 | giacomo | 81 | { |
1245 | giacomo | 82 | |
83 | FILE *file_event_header; |
||
84 | |||
85 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
86 | if (file_event_header == NULL) return 1; |
||
87 | |||
88 | fprintf(file_event_header, " {%d,{%d,%d},{%d,%d},{%d,%d},{%d,%d},%d,%d,-1},\n", |
||
89 | c->number,c->cmin.tv_sec,c->cmin.tv_nsec, |
||
90 | c->tmax.tv_sec,c->tmax.tv_nsec, |
||
91 | c->cmax.tv_sec,c->cmax.tv_nsec, |
||
92 | c->tmin.tv_sec,c->tmin.tv_nsec, |
||
93 | c->workload,c->local_scheduler); |
||
1228 | giacomo | 94 | |
1245 | giacomo | 95 | fclose(file_event_header); |
96 | |||
97 | return 0; |
||
98 | |||
99 | } |
||
100 | |||
101 | int close_loader_task(int total_task_number) |
||
102 | { |
||
103 | |||
1228 | giacomo | 104 | FILE *file_event_header; |
105 | |||
1237 | giacomo | 106 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
1228 | giacomo | 107 | if (file_event_header == NULL) return 1; |
108 | |||
109 | fprintf(file_event_header,"};\n\n"); |
||
110 | |||
1241 | giacomo | 111 | fprintf(file_event_header,"int total_loader_task = %d;\n\n",total_task_number); |
112 | |||
1228 | giacomo | 113 | fclose(file_event_header); |
114 | |||
115 | return 0; |
||
116 | |||
117 | } |
||
118 | |||
1245 | giacomo | 119 | int close_loader_contract(int total_contract_number) |
120 | { |
||
121 | |||
122 | FILE *file_event_header; |
||
123 | |||
124 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
125 | if (file_event_header == NULL) return 1; |
||
126 | |||
127 | fprintf(file_event_header,"};\n\n"); |
||
128 | |||
129 | fprintf(file_event_header,"int total_loader_contract = %d;\n\n",total_contract_number); |
||
1248 | giacomo | 130 | |
1245 | giacomo | 131 | fclose(file_event_header); |
132 | |||
133 | return 0; |
||
134 | |||
135 | } |
||
136 | |||
1251 | giacomo | 137 | int write_header() |
138 | { |
||
139 | |||
140 | FILE *file_event_header; |
||
141 | |||
142 | file_event_header = fopen(EVENT_HEADER,"w"); |
||
143 | if (file_event_header == NULL) return 1; |
||
144 | |||
145 | fprintf(file_event_header,"\n#define CALIBRATION_DELTA 100000\n"); |
||
146 | |||
147 | fclose(file_event_header); |
||
148 | |||
149 | return 0; |
||
150 | |||
151 | } |
||
152 | |||
1248 | giacomo | 153 | int write_simulation_time(struct timespec *total) |
154 | { |
||
155 | |||
156 | FILE *file_event_header; |
||
157 | |||
158 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
159 | if (file_event_header == NULL) return 1; |
||
160 | |||
161 | fprintf(file_event_header,"struct timespec total_time = {%d,%d};\n\n",total->tv_sec,total->tv_nsec); |
||
162 | |||
163 | fclose(file_event_header); |
||
164 | |||
165 | return 0; |
||
166 | |||
167 | |||
168 | } |
||
169 | |||
1237 | giacomo | 170 | int write_single_act(struct timespec *t, struct loader_task *c) |
1225 | giacomo | 171 | { |
172 | |||
173 | FILE *file_act_header; |
||
174 | |||
1228 | giacomo | 175 | file_act_header = fopen(ACT_LIST,"a+"); |
1225 | giacomo | 176 | if (file_act_header == NULL) return 1; |
177 | |||
1237 | giacomo | 178 | if (TIMESPEC_A_GT_B(t,&c->act_par_1)) { |
179 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name, |
||
180 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
181 | c->act_number = 1; |
||
182 | } else { |
||
183 | fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name); |
||
184 | c->act_number = 0; |
||
185 | } |
||
1227 | giacomo | 186 | |
1225 | giacomo | 187 | fclose(file_act_header); |
188 | |||
189 | return 0; |
||
190 | |||
191 | } |
||
192 | |||
1237 | giacomo | 193 | int write_periodic_act(struct timespec *t, struct loader_task *c) |
1225 | giacomo | 194 | { |
195 | |||
196 | FILE *file_act_header; |
||
1237 | giacomo | 197 | int i; |
198 | struct timespec tot_time; |
||
199 | int period; |
||
1228 | giacomo | 200 | |
201 | file_act_header = fopen(ACT_LIST,"a+"); |
||
1225 | giacomo | 202 | if (file_act_header == NULL) return 1; |
203 | |||
1237 | giacomo | 204 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
205 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
1225 | giacomo | 206 | |
1237 | giacomo | 207 | c->act_number = 1; |
208 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
209 | period = TIMESPEC2USEC(&c->act_par_2); |
||
210 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
211 | c->act_number++; |
||
212 | ADDUSEC2TIMESPEC(period,&tot_time); |
||
1228 | giacomo | 213 | fprintf(file_act_header," {%d,%d},\n", |
1237 | giacomo | 214 | c->act_par_2.tv_sec,c->act_par_2.tv_nsec); |
1225 | giacomo | 215 | } |
216 | |||
1228 | giacomo | 217 | fprintf(file_act_header," };\n\n"); |
1225 | giacomo | 218 | |
219 | fclose(file_act_header); |
||
220 | |||
221 | return 0; |
||
222 | |||
223 | } |
||
224 | |||
1237 | giacomo | 225 | int write_mean_act(struct timespec *t,struct loader_task *c) |
1225 | giacomo | 226 | { |
227 | |||
228 | FILE *file_act_header; |
||
1227 | giacomo | 229 | int i,first_time_usec,mean_time_usec,delta_time_usec; |
1237 | giacomo | 230 | struct timespec tot_time; |
231 | int next_act; |
||
232 | |||
1228 | giacomo | 233 | file_act_header = fopen(ACT_LIST,"a+"); |
1225 | giacomo | 234 | if (file_act_header == NULL) return 1; |
235 | |||
1237 | giacomo | 236 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
237 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
238 | |||
239 | c->act_number = 1; |
||
240 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
241 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
242 | c->act_number++; |
||
243 | next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2; |
||
244 | ADDUSEC2TIMESPEC(next_act,&tot_time); |
||
1232 | giacomo | 245 | fprintf(file_act_header," {%d,%d},\n", |
1237 | giacomo | 246 | next_act / 1000000, next_act % 1000000 * 1000); |
1225 | giacomo | 247 | } |
248 | |||
1228 | giacomo | 249 | fprintf(file_act_header," };\n\n"); |
1225 | giacomo | 250 | |
251 | fclose(file_act_header); |
||
252 | |||
253 | return 0; |
||
254 | |||
255 | } |
||
256 | |||
1237 | giacomo | 257 | int write_exec_const(struct loader_task *c) |
1226 | giacomo | 258 | { |
259 | |||
1227 | giacomo | 260 | FILE *file_exec_header; |
261 | int exec_time_usec,i; |
||
262 | |||
1228 | giacomo | 263 | file_exec_header = fopen(ACT_LIST,"a+"); |
1227 | giacomo | 264 | if (file_exec_header == NULL) return 1; |
265 | |||
1237 | giacomo | 266 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",c->name, |
267 | c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec); |
||
1227 | giacomo | 268 | |
1241 | giacomo | 269 | for (i=0; i< c->act_number-1; i++) |
1228 | giacomo | 270 | fprintf(file_exec_header," {%d,%d},\n", |
1237 | giacomo | 271 | c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec); |
1227 | giacomo | 272 | |
1228 | giacomo | 273 | fprintf(file_exec_header," };\n\n"); |
1227 | giacomo | 274 | |
275 | fclose(file_exec_header); |
||
276 | |||
1226 | giacomo | 277 | return 0; |
278 | |||
279 | } |
||
280 | |||
1237 | giacomo | 281 | int write_exec_mean(struct loader_task *c) |
1226 | giacomo | 282 | { |
283 | |||
1227 | giacomo | 284 | FILE *file_exec_header; |
1240 | giacomo | 285 | int exec_time_usec; |
1237 | giacomo | 286 | int i; |
1227 | giacomo | 287 | |
1228 | giacomo | 288 | file_exec_header = fopen(ACT_LIST,"a+"); |
1227 | giacomo | 289 | if (file_exec_header == NULL) return 1; |
290 | |||
1240 | giacomo | 291 | exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2; |
1228 | giacomo | 292 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n", |
1240 | giacomo | 293 | exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000); |
1237 | giacomo | 294 | |
1241 | giacomo | 295 | for (i=0; i< c->act_number-1; i++) { |
1240 | giacomo | 296 | exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2; |
1228 | giacomo | 297 | fprintf(file_exec_header," {%d,%d},\n", |
1240 | giacomo | 298 | exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000); |
1227 | giacomo | 299 | } |
300 | |||
1228 | giacomo | 301 | fprintf(file_exec_header," };\n\n"); |
1227 | giacomo | 302 | |
303 | fclose(file_exec_header); |
||
304 | |||
1226 | giacomo | 305 | return 0; |
306 | |||
307 | } |
||
308 | |||
1237 | giacomo | 309 | void *start; |
310 | void *end; |
||
311 | |||
1225 | giacomo | 312 | int main(void) { |
313 | |||
314 | char task_name[100]; |
||
1226 | giacomo | 315 | char act_type,exec_type; |
1237 | giacomo | 316 | struct timespec total_time; |
1244 | giacomo | 317 | struct loader_task *start_loader_task = NULL, *current_t; |
318 | struct loader_contract *start_loader_contract = NULL, *current_c; |
||
1237 | giacomo | 319 | int err,ldnum; |
1241 | giacomo | 320 | int total_task_number; |
1245 | giacomo | 321 | int total_contract_number; |
1225 | giacomo | 322 | |
1237 | giacomo | 323 | printf("\nEvent Generator\n"); |
1225 | giacomo | 324 | |
1237 | giacomo | 325 | printf("Read loader file\n"); |
326 | |||
1238 | giacomo | 327 | dos_preload("load.txt",100000,&start,&end); |
1237 | giacomo | 328 | |
329 | printf("Parsing file\n"); |
||
330 | |||
1244 | giacomo | 331 | line_reader(start, end, &total_time, &start_loader_task, &start_loader_contract); |
1237 | giacomo | 332 | |
1251 | giacomo | 333 | srandom(123234132); |
1225 | giacomo | 334 | |
1251 | giacomo | 335 | write_header(); |
336 | |||
1228 | giacomo | 337 | write_struct(); |
338 | |||
1244 | giacomo | 339 | current_t = start_loader_task; |
1243 | giacomo | 340 | ldnum = 1; |
1225 | giacomo | 341 | |
1244 | giacomo | 342 | while(current_t != NULL) { |
1225 | giacomo | 343 | |
1244 | giacomo | 344 | sprintf(current_t->name,"ltask%d",ldnum); |
345 | current_t->group = ldnum; |
||
346 | ldnum++; |
||
1226 | giacomo | 347 | |
1244 | giacomo | 348 | switch (current_t->act_type) { |
1237 | giacomo | 349 | case PAR_ACT_SINGLE: |
1244 | giacomo | 350 | err = write_single_act(&total_time,current_t); |
1226 | giacomo | 351 | if (err != 0) { |
352 | printf("Error writing activation header\n"); |
||
353 | exit(1); |
||
354 | } |
||
355 | break; |
||
1237 | giacomo | 356 | case PAR_ACT_PERIODIC: |
1244 | giacomo | 357 | err = write_periodic_act(&total_time,current_t); |
1226 | giacomo | 358 | if (err != 0) { |
359 | printf("Error writing activation header\n"); |
||
360 | exit(1); |
||
361 | } |
||
362 | break; |
||
1237 | giacomo | 363 | case PAR_ACT_MEAN: |
1244 | giacomo | 364 | err = write_mean_act(&total_time,current_t); |
1226 | giacomo | 365 | if (err != 0) { |
366 | printf("Error writing activation header\n"); |
||
367 | exit(1); |
||
368 | } |
||
369 | break; |
||
370 | } |
||
371 | |||
1244 | giacomo | 372 | switch (current_t->exec_type) { |
1237 | giacomo | 373 | case PAR_EXEC_CONST: |
1244 | giacomo | 374 | err = write_exec_const(current_t); |
1226 | giacomo | 375 | if (err != 0) { |
376 | printf("Error writing exec header\n"); |
||
377 | exit(1); |
||
378 | } |
||
379 | break; |
||
1237 | giacomo | 380 | case PAR_EXEC_MEAN: |
1244 | giacomo | 381 | err = write_exec_mean(current_t); |
1226 | giacomo | 382 | if (err != 0) { |
383 | printf("Error writing exec header\n"); |
||
384 | exit(1); |
||
385 | } |
||
386 | break; |
||
387 | } |
||
388 | |||
1244 | giacomo | 389 | current_t = current_t->next; |
1237 | giacomo | 390 | |
1225 | giacomo | 391 | } |
392 | |||
1244 | giacomo | 393 | write_basic_par_start(); |
394 | |||
1241 | giacomo | 395 | total_task_number = 0; |
1244 | giacomo | 396 | current_t = start_loader_task; |
397 | while(current_t != NULL) { |
||
1238 | giacomo | 398 | |
1244 | giacomo | 399 | write_basic_par(current_t); |
1238 | giacomo | 400 | |
1244 | giacomo | 401 | current_t = current_t->next; |
1238 | giacomo | 402 | |
1241 | giacomo | 403 | total_task_number++; |
404 | |||
1238 | giacomo | 405 | } |
406 | |||
1245 | giacomo | 407 | close_loader_task(total_task_number); |
1237 | giacomo | 408 | |
1245 | giacomo | 409 | write_contract_start(); |
410 | |||
411 | total_contract_number = 0; |
||
412 | current_c = start_loader_contract; |
||
413 | while(current_c != NULL) { |
||
414 | |||
415 | write_contract(current_c); |
||
416 | |||
417 | current_c = current_c->next; |
||
418 | |||
419 | total_contract_number++; |
||
420 | |||
421 | } |
||
422 | |||
423 | close_loader_contract(total_contract_number); |
||
424 | |||
1248 | giacomo | 425 | write_simulation_time(&total_time); |
426 | |||
1225 | giacomo | 427 | return 0; |
428 | |||
429 | } |