Rev 1251 | 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); |
||
1252 | giacomo | 162 | |
163 | fprintf(file_event_header,"int cal_cycles = 0;\n\n"); |
||
164 | |||
1248 | giacomo | 165 | fclose(file_event_header); |
166 | |||
167 | return 0; |
||
168 | |||
169 | |||
170 | } |
||
171 | |||
1237 | giacomo | 172 | int write_single_act(struct timespec *t, struct loader_task *c) |
1225 | giacomo | 173 | { |
174 | |||
175 | FILE *file_act_header; |
||
176 | |||
1228 | giacomo | 177 | file_act_header = fopen(ACT_LIST,"a+"); |
1225 | giacomo | 178 | if (file_act_header == NULL) return 1; |
179 | |||
1237 | giacomo | 180 | if (TIMESPEC_A_GT_B(t,&c->act_par_1)) { |
181 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name, |
||
182 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
183 | c->act_number = 1; |
||
184 | } else { |
||
185 | fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name); |
||
186 | c->act_number = 0; |
||
187 | } |
||
1227 | giacomo | 188 | |
1225 | giacomo | 189 | fclose(file_act_header); |
190 | |||
191 | return 0; |
||
192 | |||
193 | } |
||
194 | |||
1237 | giacomo | 195 | int write_periodic_act(struct timespec *t, struct loader_task *c) |
1225 | giacomo | 196 | { |
197 | |||
198 | FILE *file_act_header; |
||
1237 | giacomo | 199 | int i; |
200 | struct timespec tot_time; |
||
201 | int period; |
||
1228 | giacomo | 202 | |
203 | file_act_header = fopen(ACT_LIST,"a+"); |
||
1225 | giacomo | 204 | if (file_act_header == NULL) return 1; |
205 | |||
1237 | giacomo | 206 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
207 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
1225 | giacomo | 208 | |
1237 | giacomo | 209 | c->act_number = 1; |
210 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
211 | period = TIMESPEC2USEC(&c->act_par_2); |
||
212 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
213 | c->act_number++; |
||
214 | ADDUSEC2TIMESPEC(period,&tot_time); |
||
1228 | giacomo | 215 | fprintf(file_act_header," {%d,%d},\n", |
1237 | giacomo | 216 | c->act_par_2.tv_sec,c->act_par_2.tv_nsec); |
1225 | giacomo | 217 | } |
218 | |||
1228 | giacomo | 219 | fprintf(file_act_header," };\n\n"); |
1225 | giacomo | 220 | |
221 | fclose(file_act_header); |
||
222 | |||
223 | return 0; |
||
224 | |||
225 | } |
||
226 | |||
1237 | giacomo | 227 | int write_mean_act(struct timespec *t,struct loader_task *c) |
1225 | giacomo | 228 | { |
229 | |||
230 | FILE *file_act_header; |
||
1227 | giacomo | 231 | int i,first_time_usec,mean_time_usec,delta_time_usec; |
1237 | giacomo | 232 | struct timespec tot_time; |
233 | int next_act; |
||
234 | |||
1228 | giacomo | 235 | file_act_header = fopen(ACT_LIST,"a+"); |
1225 | giacomo | 236 | if (file_act_header == NULL) return 1; |
237 | |||
1237 | giacomo | 238 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
239 | c->act_par_1.tv_sec,c->act_par_1.tv_nsec); |
||
240 | |||
241 | c->act_number = 1; |
||
242 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
243 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
244 | c->act_number++; |
||
245 | next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2; |
||
246 | ADDUSEC2TIMESPEC(next_act,&tot_time); |
||
1232 | giacomo | 247 | fprintf(file_act_header," {%d,%d},\n", |
1237 | giacomo | 248 | next_act / 1000000, next_act % 1000000 * 1000); |
1225 | giacomo | 249 | } |
250 | |||
1228 | giacomo | 251 | fprintf(file_act_header," };\n\n"); |
1225 | giacomo | 252 | |
253 | fclose(file_act_header); |
||
254 | |||
255 | return 0; |
||
256 | |||
257 | } |
||
258 | |||
1237 | giacomo | 259 | int write_exec_const(struct loader_task *c) |
1226 | giacomo | 260 | { |
261 | |||
1227 | giacomo | 262 | FILE *file_exec_header; |
263 | int exec_time_usec,i; |
||
264 | |||
1228 | giacomo | 265 | file_exec_header = fopen(ACT_LIST,"a+"); |
1227 | giacomo | 266 | if (file_exec_header == NULL) return 1; |
267 | |||
1237 | giacomo | 268 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",c->name, |
269 | c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec); |
||
1227 | giacomo | 270 | |
1241 | giacomo | 271 | for (i=0; i< c->act_number-1; i++) |
1228 | giacomo | 272 | fprintf(file_exec_header," {%d,%d},\n", |
1237 | giacomo | 273 | c->exec_par_1.tv_sec,c->exec_par_1.tv_nsec); |
1227 | giacomo | 274 | |
1228 | giacomo | 275 | fprintf(file_exec_header," };\n\n"); |
1227 | giacomo | 276 | |
277 | fclose(file_exec_header); |
||
278 | |||
1226 | giacomo | 279 | return 0; |
280 | |||
281 | } |
||
282 | |||
1237 | giacomo | 283 | int write_exec_mean(struct loader_task *c) |
1226 | giacomo | 284 | { |
285 | |||
1227 | giacomo | 286 | FILE *file_exec_header; |
1240 | giacomo | 287 | int exec_time_usec; |
1237 | giacomo | 288 | int i; |
1227 | giacomo | 289 | |
1228 | giacomo | 290 | file_exec_header = fopen(ACT_LIST,"a+"); |
1227 | giacomo | 291 | if (file_exec_header == NULL) return 1; |
292 | |||
1240 | giacomo | 293 | exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2; |
1228 | giacomo | 294 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n", |
1240 | giacomo | 295 | exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000); |
1237 | giacomo | 296 | |
1241 | giacomo | 297 | for (i=0; i< c->act_number-1; i++) { |
1240 | giacomo | 298 | exec_time_usec = TIMESPEC2USEC(&c->exec_par_2) + random() % TIMESPEC2USEC(&c->exec_par_3) - TIMESPEC2USEC(&c->exec_par_3) / 2; |
1228 | giacomo | 299 | fprintf(file_exec_header," {%d,%d},\n", |
1240 | giacomo | 300 | exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000); |
1227 | giacomo | 301 | } |
302 | |||
1228 | giacomo | 303 | fprintf(file_exec_header," };\n\n"); |
1227 | giacomo | 304 | |
305 | fclose(file_exec_header); |
||
306 | |||
1226 | giacomo | 307 | return 0; |
308 | |||
309 | } |
||
310 | |||
1237 | giacomo | 311 | void *start; |
312 | void *end; |
||
313 | |||
1225 | giacomo | 314 | int main(void) { |
315 | |||
316 | char task_name[100]; |
||
1226 | giacomo | 317 | char act_type,exec_type; |
1237 | giacomo | 318 | struct timespec total_time; |
1244 | giacomo | 319 | struct loader_task *start_loader_task = NULL, *current_t; |
320 | struct loader_contract *start_loader_contract = NULL, *current_c; |
||
1237 | giacomo | 321 | int err,ldnum; |
1241 | giacomo | 322 | int total_task_number; |
1245 | giacomo | 323 | int total_contract_number; |
1225 | giacomo | 324 | |
1237 | giacomo | 325 | printf("\nEvent Generator\n"); |
1225 | giacomo | 326 | |
1237 | giacomo | 327 | printf("Read loader file\n"); |
328 | |||
1238 | giacomo | 329 | dos_preload("load.txt",100000,&start,&end); |
1237 | giacomo | 330 | |
331 | printf("Parsing file\n"); |
||
332 | |||
1244 | giacomo | 333 | line_reader(start, end, &total_time, &start_loader_task, &start_loader_contract); |
1237 | giacomo | 334 | |
1251 | giacomo | 335 | srandom(123234132); |
1225 | giacomo | 336 | |
1251 | giacomo | 337 | write_header(); |
338 | |||
1228 | giacomo | 339 | write_struct(); |
340 | |||
1244 | giacomo | 341 | current_t = start_loader_task; |
1243 | giacomo | 342 | ldnum = 1; |
1225 | giacomo | 343 | |
1244 | giacomo | 344 | while(current_t != NULL) { |
1225 | giacomo | 345 | |
1244 | giacomo | 346 | sprintf(current_t->name,"ltask%d",ldnum); |
347 | current_t->group = ldnum; |
||
348 | ldnum++; |
||
1226 | giacomo | 349 | |
1244 | giacomo | 350 | switch (current_t->act_type) { |
1237 | giacomo | 351 | case PAR_ACT_SINGLE: |
1244 | giacomo | 352 | err = write_single_act(&total_time,current_t); |
1226 | giacomo | 353 | if (err != 0) { |
354 | printf("Error writing activation header\n"); |
||
355 | exit(1); |
||
356 | } |
||
357 | break; |
||
1237 | giacomo | 358 | case PAR_ACT_PERIODIC: |
1244 | giacomo | 359 | err = write_periodic_act(&total_time,current_t); |
1226 | giacomo | 360 | if (err != 0) { |
361 | printf("Error writing activation header\n"); |
||
362 | exit(1); |
||
363 | } |
||
364 | break; |
||
1237 | giacomo | 365 | case PAR_ACT_MEAN: |
1244 | giacomo | 366 | err = write_mean_act(&total_time,current_t); |
1226 | giacomo | 367 | if (err != 0) { |
368 | printf("Error writing activation header\n"); |
||
369 | exit(1); |
||
370 | } |
||
371 | break; |
||
372 | } |
||
373 | |||
1244 | giacomo | 374 | switch (current_t->exec_type) { |
1237 | giacomo | 375 | case PAR_EXEC_CONST: |
1244 | giacomo | 376 | err = write_exec_const(current_t); |
1226 | giacomo | 377 | if (err != 0) { |
378 | printf("Error writing exec header\n"); |
||
379 | exit(1); |
||
380 | } |
||
381 | break; |
||
1237 | giacomo | 382 | case PAR_EXEC_MEAN: |
1244 | giacomo | 383 | err = write_exec_mean(current_t); |
1226 | giacomo | 384 | if (err != 0) { |
385 | printf("Error writing exec header\n"); |
||
386 | exit(1); |
||
387 | } |
||
388 | break; |
||
389 | } |
||
390 | |||
1244 | giacomo | 391 | current_t = current_t->next; |
1237 | giacomo | 392 | |
1225 | giacomo | 393 | } |
394 | |||
1244 | giacomo | 395 | write_basic_par_start(); |
396 | |||
1241 | giacomo | 397 | total_task_number = 0; |
1244 | giacomo | 398 | current_t = start_loader_task; |
399 | while(current_t != NULL) { |
||
1238 | giacomo | 400 | |
1244 | giacomo | 401 | write_basic_par(current_t); |
1238 | giacomo | 402 | |
1244 | giacomo | 403 | current_t = current_t->next; |
1238 | giacomo | 404 | |
1241 | giacomo | 405 | total_task_number++; |
406 | |||
1238 | giacomo | 407 | } |
408 | |||
1245 | giacomo | 409 | close_loader_task(total_task_number); |
1237 | giacomo | 410 | |
1245 | giacomo | 411 | write_contract_start(); |
412 | |||
413 | total_contract_number = 0; |
||
414 | current_c = start_loader_contract; |
||
415 | while(current_c != NULL) { |
||
416 | |||
417 | write_contract(current_c); |
||
418 | |||
419 | current_c = current_c->next; |
||
420 | |||
421 | total_contract_number++; |
||
422 | |||
423 | } |
||
424 | |||
425 | close_loader_contract(total_contract_number); |
||
426 | |||
1248 | giacomo | 427 | write_simulation_time(&total_time); |
428 | |||
1225 | giacomo | 429 | return 0; |
430 | |||
431 | } |