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