Rev 1269 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1270 | giacomo | 1 | //package first_filter; |
1269 | trimarchi | 2 | |
3 | import java.io.*; |
||
4 | import java.net.*; |
||
5 | import java.util.List; |
||
6 | import java.util.ArrayList; |
||
7 | import java.util.Iterator; |
||
8 | import org.jdom.*; |
||
9 | import org.jdom.input.SAXBuilder; |
||
10 | import org.jdom.output.XMLOutputter; |
||
11 | import java.util.regex.*; |
||
1270 | giacomo | 12 | import java.util.random; |
13 | |||
1269 | trimarchi | 14 | /** |
15 | * <p>Title: First XML filter</p> |
||
16 | * <p>Description: </p> |
||
17 | * <p>Copyright: Copyright (c) 2003</p> |
||
18 | * <p>Company: Retis Lab</p> |
||
19 | * @author not attributable |
||
20 | * @version 1.0 |
||
21 | */ |
||
22 | |||
23 | public class Applicazione { |
||
24 | private int numElements = 0; |
||
25 | final String LOADFILE_DIR="../loadfile/"; |
||
26 | final String EVENT_DEFINE="event.c"; |
||
27 | final String ACT_LIST="event.c"; |
||
28 | private int servernumber=0; |
||
29 | private timespec simulation_time=new timespec(); |
||
30 | ArrayList local_scheduler=new ArrayList(); |
||
31 | ArrayList Task_Section=new ArrayList(); |
||
32 | private int task_group=0; |
||
33 | private int task_server=0; |
||
34 | private int current_scheduler=0; |
||
35 | |||
36 | |||
37 | |||
38 | //Construct the frame |
||
39 | public Applicazione() { |
||
40 | try { |
||
41 | jbInit(); |
||
42 | } |
||
43 | catch(Exception e) { |
||
44 | e.printStackTrace(); |
||
45 | } |
||
46 | } |
||
47 | //Component initialization |
||
48 | private void jbInit() throws Exception { |
||
49 | } |
||
50 | |||
51 | public void process (String url) throws MalformedURLException { |
||
52 | try { |
||
53 | // Use SAXBuilder |
||
54 | SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true); |
||
55 | builder.setFeature("http://apache.org/xml/features/validation/schema", true); |
||
56 | |||
57 | |||
58 | Document doc = builder.build(url); |
||
59 | Element root = doc.getRootElement(); |
||
60 | |||
61 | try { |
||
62 | // Funzione per il salvataggio del file XML |
||
63 | OutputStream fout = new FileOutputStream(EVENT_DEFINE); |
||
64 | OutputStream bout = new BufferedOutputStream(fout); |
||
65 | OutputStreamWriter out = new OutputStreamWriter(bout, "8859_1"); |
||
66 | out.write("\n#include \"func.h\"\n"); |
||
67 | |||
68 | processElement (root,out); |
||
69 | out.flush(); |
||
70 | out.close(); |
||
71 | } catch (UnsupportedEncodingException e) { |
||
72 | System.out.println("Non sono supportati i caratteri latini"); |
||
73 | System.exit(1); |
||
74 | |||
75 | } catch (IOException e){ |
||
76 | System.out.println("Salvatagio fallito"); |
||
77 | System.exit(1); |
||
78 | } |
||
79 | |||
80 | |||
81 | |||
82 | System.out.println ("Total Number of Elements Processed: " |
||
83 | +numElements); |
||
84 | } catch (JDOMException e) { |
||
85 | System.out.println ("JDOM Exception: "+e.getMessage()); |
||
86 | } catch (java.io.IOException e) { |
||
87 | System.out.println ("File Exception: "+e.getMessage()); |
||
88 | |||
89 | } |
||
90 | } |
||
91 | |||
92 | // Recursive Function to Process Elements |
||
93 | // Prints the Element Name and keeps a running count |
||
94 | // out total number of elements. |
||
95 | private void processElement(Element element, OutputStreamWriter out) { |
||
96 | numElements++; |
||
97 | String elementName = element.getName(); |
||
98 | |||
99 | System.out.println(elementName); |
||
100 | List servers = element.getChildren(); |
||
101 | Iterator iterator = servers.iterator(); |
||
102 | /* get simulation parameter */ |
||
103 | Element SimulationInfo = (Element) iterator.next(); |
||
104 | |||
105 | Pattern pattern = Pattern.compile("[us\\s]+"); |
||
106 | try { |
||
107 | /*simulation time */ |
||
108 | |||
109 | String[] stime = pattern.split(SimulationInfo.getChild("time").getText()); |
||
110 | out.write("struct timespec total_time={" + stime[0] + "," + stime[1] + |
||
111 | "};\n\n"); |
||
112 | simulation_time.tv_sec = Long.valueOf(stime[0].trim()).longValue(); |
||
113 | simulation_time.tv_nsec = Long.valueOf(stime[1].trim()).longValue() / |
||
114 | 1000; |
||
115 | //float f = Float.valueOf(s.trim()).floatValue(); |
||
116 | /* server section start */ |
||
117 | out.write("struct loader_contract loader_contract_list[] = {\n"); |
||
118 | int total_server=0; |
||
119 | while (iterator.hasNext()) { |
||
120 | /* get server */ |
||
121 | Element server = (Element) iterator.next(); |
||
122 | process_server_section(server, out); |
||
123 | total_server++; |
||
124 | } |
||
125 | |||
126 | out.write("};\n\n"); |
||
127 | |||
128 | out.write("int total_loader_contract="+total_server+";\n\n"); |
||
129 | iterator = local_scheduler.iterator(); |
||
130 | while (iterator.hasNext()) { |
||
131 | /* get server */ |
||
132 | Element loc_sched = (Element) iterator.next(); |
||
133 | process_scheduler_section(loc_sched, out); |
||
134 | task_server++; |
||
135 | } |
||
136 | |||
137 | iterator = Task_Section.iterator(); |
||
138 | int total_task_section=0; |
||
139 | out.write("\nstruct loader_task loader_task_list[] = {\n"); |
||
140 | while (iterator.hasNext()) { |
||
141 | /* get server */ |
||
142 | task_class tsk_sec = (task_class) iterator.next(); |
||
143 | out.write(" {\"" + tsk_sec.name + "\"," + tsk_sec.task_type + "," + |
||
144 | tsk_sec.contract + |
||
145 | "," + tsk_sec.localscheduler + "," + tsk_sec.number + "," |
||
146 | + tsk_sec.group + ",{" + tsk_sec.deadline.tv_sec + |
||
147 | "," + |
||
148 | tsk_sec.deadline.tv_nsec * 1000 + "}," + "{" + |
||
149 | tsk_sec.wcet.tv_sec + "," + |
||
150 | tsk_sec.wcet.tv_nsec * 1000 + "}," + tsk_sec.act_number + |
||
151 | ",0,act_" + tsk_sec.name + |
||
152 | ",exec_" + tsk_sec.name + "},\n"); |
||
153 | total_task_section++; |
||
154 | } |
||
155 | out.write("};\n\n"); |
||
156 | out.write("int total_loader_task="+total_task_section+";\n\n"); |
||
157 | |||
158 | |||
159 | } |
||
160 | catch (java.io.IOException e) { |
||
161 | System.out.println("File Exception: " + e.getMessage()); |
||
162 | |||
163 | } |
||
164 | |||
165 | } |
||
166 | |||
167 | void process_scheduler_section(Element e, OutputStreamWriter out) { |
||
168 | Attribute t; |
||
169 | t=e.getAttribute("type"); |
||
170 | |||
171 | List localpars = e.getChildren(); |
||
172 | Iterator iterator = localpars.iterator(); |
||
173 | while (iterator.hasNext()) { |
||
174 | /* get task section */ |
||
175 | Element loc_task = (Element) iterator.next(); |
||
176 | process_task_section(loc_task, out, t); |
||
177 | |||
178 | } |
||
179 | |||
180 | } |
||
181 | |||
182 | void process_task_section(Element e, OutputStreamWriter out, Attribute loc) { |
||
183 | Attribute t; |
||
184 | int act; |
||
185 | act=1; |
||
186 | int task_type=0; |
||
187 | int localscheduler=0; |
||
188 | timespec time = new timespec(); |
||
189 | Pattern pattern = Pattern.compile("[us\\s]+"); |
||
190 | String scheduler=loc.getValue(); |
||
191 | if (scheduler.equals("POSIX")) { |
||
192 | localscheduler=30; |
||
193 | } |
||
194 | else if (scheduler.equals("EDF")) { |
||
195 | localscheduler=31; |
||
196 | } |
||
197 | else if (scheduler.equals("RM")) { |
||
198 | localscheduler=32; |
||
199 | } |
||
200 | |||
201 | /* get task section */ |
||
202 | t=e.getAttribute("type"); |
||
203 | |||
204 | if (t.getValue().equals("BackTask")) { |
||
205 | process_back_task(e, out); |
||
206 | task_type=23; |
||
207 | } else if (t.getValue().equals("OneShot")) { |
||
208 | process_oneshot_task(e, out); |
||
209 | task_type=21; |
||
1270 | giacomo | 210 | } else if (t.getValue().equals("CyclicalTask")) { |
1269 | trimarchi | 211 | String[] dline=pattern.split(e.getChild("dline").getText()); |
212 | task_type=22; |
||
213 | time.tv_sec = Long.valueOf(dline[0]).longValue(); |
||
214 | time.tv_nsec = Long.valueOf(dline[1]).longValue() / 1000; |
||
215 | |||
216 | |||
217 | act=process_periodic_task(e, out); |
||
218 | } |
||
219 | |||
220 | task_class section=new task_class(); |
||
221 | section.deadline=new timespec(); |
||
222 | section.deadline.tv_sec=time.tv_sec; |
||
223 | section.deadline.tv_nsec=time.tv_nsec; |
||
224 | section.name="task"+task_group; |
||
225 | section.contract=task_server; |
||
226 | section.group=task_group; |
||
227 | section.act_number=act; |
||
228 | section.task_type=task_type; |
||
229 | section.localscheduler=localscheduler; |
||
230 | section.number=Integer.valueOf(e.getChild("number").getText()).intValue(); |
||
231 | |||
232 | |||
233 | String[] wcet=pattern.split(e.getChild("wcet").getText()); |
||
234 | time.tv_sec = Long.valueOf(wcet[0]).longValue(); |
||
235 | time.tv_nsec = Long.valueOf(wcet[1]).longValue() / 1000; |
||
236 | section.wcet=new timespec(); |
||
237 | section.wcet.tv_sec=time.tv_sec; |
||
238 | section.wcet.tv_nsec=time.tv_nsec; |
||
239 | |||
240 | |||
241 | |||
242 | Task_Section.add(section); |
||
243 | task_group++; |
||
244 | |||
245 | } |
||
246 | |||
247 | void process_back_task(Element e, OutputStreamWriter out) { |
||
248 | |||
249 | Pattern pattern = Pattern.compile("[us\\s]+"); |
||
1270 | giacomo | 250 | |
251 | Element act_section=e.getChild("act_section"); |
||
252 | String[] start_time=pattern.split(act_section.getChild("start_time").getText()); |
||
253 | Element exec_section=e.getChild("exec_section"); |
||
254 | |||
255 | String[] exec_const=pattern.split(exec_section.getChild("exec_const").getText()); |
||
1269 | trimarchi | 256 | try { |
257 | out.write("struct timespec act_task"+task_group+"[]={{"+start_time[0]+","+start_time[1]+"},};\n\n"); |
||
1270 | giacomo | 258 | out.write("struct timespec exec_task"+task_group+"[]={{"+exec_const[0]+","+exec_const[1]+"},};\n\n"); |
1269 | trimarchi | 259 | |
260 | } |
||
261 | catch (java.io.IOException ex) { |
||
262 | System.out.println("File Exception: " + ex.getMessage()); |
||
263 | |||
264 | } |
||
265 | } |
||
266 | void process_oneshot_task(Element e, OutputStreamWriter out) { |
||
267 | Pattern pattern = Pattern.compile("[us\\s]+"); |
||
1270 | giacomo | 268 | |
269 | Element act_section=e.getChild("act_section"); |
||
270 | String[] start_time=pattern.split(act_section.getChild("start_time").getText()); |
||
271 | |||
272 | Element exec_section=e.getChild("exec_section"); |
||
273 | String[] exec_const=pattern.split(exec_section.getChild("exec_const").getText()); |
||
274 | |||
1269 | trimarchi | 275 | try { |
276 | out.write("struct timespec act_task"+task_group+"[]={{"+start_time[0]+","+start_time[1]+"},};\n\n"); |
||
1270 | giacomo | 277 | out.write("struct timespec exec_task"+task_group+"[]={{"+exec_const[0]+","+exec_const[1]+"},};\n\n"); |
1269 | trimarchi | 278 | } |
279 | catch (java.io.IOException ex) { |
||
280 | System.out.println("File Exception: " + ex.getMessage()); |
||
281 | } |
||
282 | } |
||
283 | |||
1270 | giacomo | 284 | int process_cyclical_task(Element e, OutputStreamWriter out) { |
285 | |||
1269 | trimarchi | 286 | int activated=1; |
287 | Pattern pattern = Pattern.compile("[us\\s]+"); |
||
1270 | giacomo | 288 | Element act_section=e.getChild("act_section"); |
289 | String[] start_time=pattern.split(act_section.getChild("start_time").getText()); |
||
290 | String[] period=pattern.split(act_section.getChild("period_time").getText()); |
||
291 | Element deltatime=act_section.getChild("delta_time"); |
||
292 | String[] delta_time; |
||
293 | |||
294 | long act_delta=0; |
||
295 | timespec delta_act_ts = new timespec(); |
||
296 | |||
297 | if (deltatime.exists()) { |
||
298 | delta_time = pattern.split(deltatime.getText()); |
||
299 | delta_act_ts.tv_sec = Long.valueOf(delta_time[0]).longValue(); |
||
300 | delta_act_ts.tv_nsec = Long.valueOf(delta_time[1]).longValue() / 1000; |
||
301 | act_delta = delta_act_ts.TIMESPEC2USEC(); |
||
302 | } |
||
303 | |||
304 | Element exec_section=e.getChild("exec_section"); |
||
305 | String[] exec_const=pattern.split(exec_section.getChild("exec_const").getText()); |
||
306 | Element execdelta=exec_section.getChild("exec_delta"); |
||
307 | String[] exec_delta; |
||
308 | |||
309 | long exec_delta_long = 0; |
||
310 | timespec delta_act_ts = new timespec(); |
||
311 | |||
312 | if (execdelta.exists()) { |
||
313 | exec_delta=pattern.split(execdelta.getText()); |
||
314 | exec_act_ts.tv_sec = Long.valueOf(exec_delta[0]).longValue(); |
||
315 | exec_act_ts.tv_nsec = Long.valueOf(exec_delta[1]).longValue() / 1000; |
||
316 | exec_delta_long = exec_act_ts.TIMESPEC2USEC(); |
||
317 | } |
||
318 | |||
1269 | trimarchi | 319 | try { |
320 | out.write("struct timespec act_task"+task_group+"[]={{"+ |
||
321 | start_time[0]+","+start_time[1]+"},\n"); |
||
322 | |||
323 | timespec time = new timespec(); |
||
324 | time.tv_sec = Long.valueOf(period[0]).longValue(); |
||
325 | time.tv_nsec = Long.valueOf(period[1]).longValue() / 1000; |
||
326 | |||
1270 | giacomo | 327 | long usecperiod = time.TIMESPEC2USEC(); |
328 | timespec nextact = new timespec(); |
||
1269 | trimarchi | 329 | |
330 | time.tv_sec = Long.valueOf(start_time[0]).longValue(); |
||
331 | time.tv_nsec = Long.valueOf(start_time[1]).longValue() / 1000; |
||
1270 | giacomo | 332 | |
333 | Random rd = new Random(); |
||
334 | |||
1269 | trimarchi | 335 | while (simulation_time.TIMESPEC_A_GT_B(time)) { |
1270 | giacomo | 336 | |
337 | nextact.tv_sec = Long.valueOf(period[0]).longValue(); |
||
338 | nextact.tv_nsec = Long.valueOf(period[1]).longValue() / 1000; |
||
339 | |||
340 | nextact.ADDUSEC2TIMESPEC((rd.nextInt() & Integer.MAX_INT) % act_delta - act_delta/2); |
||
341 | |||
342 | time.ADDUSEC2TIMESPEC(usecperiod); |
||
343 | out.write(" {"+nextact.tv_sec+","+nextact.tv_nsec/1000+"},\n"); |
||
1269 | trimarchi | 344 | activated++; |
345 | } |
||
346 | out.write("};\n"); |
||
347 | |||
1270 | giacomo | 348 | out.write("struct timespec exec_task"+task_group+"[]={\n"); |
1269 | trimarchi | 349 | |
1270 | giacomo | 350 | timespec nextexec = new timespec(); |
1269 | trimarchi | 351 | |
1270 | giacomo | 352 | for (int i=0; i<activated; i++) { |
353 | |||
354 | nextexec.tv_sec = Long.valueOf(exec_const[0]).longValue(); |
||
355 | nextexec.tv_nsec = Long.valueOf(exec_const[1]).longValue() / 1000; |
||
356 | |||
357 | nextexec.ADDUSEC2TIMESPEC((rd.nextInt() & Integer.MAX_INT) % exec_delta_long - exec_delta_long/2); |
||
358 | |||
359 | out.write(" {"+nextexec.tv_sec+","+nextexec.tv_nsec/1000+"},\n"); |
||
360 | |||
1269 | trimarchi | 361 | } |
362 | out.write("};\n"); |
||
363 | } |
||
364 | catch (java.io.IOException ex) { |
||
365 | System.out.println("File Exception: " + ex.getMessage()); |
||
366 | } |
||
367 | |||
368 | return activated; |
||
369 | } |
||
370 | |||
371 | void process_server_section(Element e, OutputStreamWriter out) { |
||
372 | |||
373 | |||
374 | List serverpars = e.getChildren(); |
||
375 | Iterator iterator = serverpars.iterator(); |
||
376 | Element serverpar = (Element) iterator.next(); |
||
377 | String current_value; |
||
378 | current_value=serverpar.getText(); |
||
379 | try { |
||
380 | out.write("{" + servernumber + ",{"); |
||
381 | servernumber++; |
||
382 | |||
383 | Pattern pattern = Pattern.compile("[us\\s]+"); |
||
384 | /* cmin */ |
||
385 | String[] cmin = pattern.split(current_value); |
||
386 | out.write(cmin[0] + "," + cmin[1]+"},{"); |
||
387 | |||
388 | /* tmax */ |
||
389 | serverpar = (Element) iterator.next(); |
||
390 | current_value=serverpar.getText(); |
||
391 | String[] tmax = pattern.split(current_value); |
||
392 | out.write(tmax[0] + "," + tmax[1]+"},{"); |
||
393 | |||
394 | /* cmax */ |
||
395 | serverpar = (Element) iterator.next(); |
||
396 | current_value=serverpar.getText(); |
||
397 | String[] cmax = pattern.split(current_value); |
||
398 | out.write(cmax[0] + "," + cmax[1]+"},{"); |
||
399 | |||
400 | /* tmin */ |
||
401 | serverpar = (Element) iterator.next(); |
||
402 | current_value=serverpar.getText(); |
||
403 | String[] tmin = pattern.split(current_value); |
||
404 | out.write(tmin[0] + "," + tmin[1]+"},"); |
||
405 | |||
406 | /*work load */ |
||
407 | serverpar = (Element) iterator.next(); |
||
408 | current_value=serverpar.getText(); |
||
409 | out.write(current_value+","); |
||
410 | |||
411 | serverpar = (Element) iterator.next(); |
||
412 | current_value=serverpar.getName(); |
||
413 | Attribute loc_type=serverpar.getAttribute("type"); |
||
414 | String scheduler=loc_type.getValue(); |
||
415 | if (scheduler.equals("POSIX")) { |
||
416 | out.write("30"); |
||
417 | } |
||
418 | else if (scheduler.equals("EDF")) { |
||
419 | out.write("31"); |
||
420 | } |
||
421 | else if (scheduler.equals("RM")) { |
||
422 | out.write("32"); |
||
423 | } |
||
424 | out.write(",-1},\n"); |
||
425 | local_scheduler.add(servernumber-1,serverpar); |
||
426 | |||
427 | } catch (java.io.IOException ex) { |
||
428 | System.out.println ("File Exception: "+ex.getMessage()); |
||
429 | } |
||
430 | |||
431 | /* |
||
432 | fprintf(file_event_header, " {%d,{%d,%d},{%d,%d},{%d,%d},{%d,%d},%d,%d,-1},\n", |
||
433 | (int)c->number,(int)c->cmin.tv_sec,(int)c->cmin.tv_nsec, |
||
434 | (int)c->tmax.tv_sec,(int)c->tmax.tv_nsec, |
||
435 | (int)c->cmax.tv_sec,(int)c->cmax.tv_nsec, |
||
436 | (int)c->tmin.tv_sec,(int)c->tmin.tv_nsec, |
||
437 | (int)c->workload,(int)c->local_scheduler); |
||
438 | |||
439 | */ |
||
440 | //processElement (kid); |
||
441 | } |
||
442 | |||
443 | |||
444 | |||
445 | public static void main(String[] args) throws Exception { |
||
446 | System.out.println("Parser Versione 1.0"); |
||
447 | Applicazione app = new Applicazione(); |
||
448 | if (args.length > 0) |
||
449 | app.process(args[0]); |
||
450 | } |
||
451 | |||
452 | /* Event Generator |
||
453 | * |
||
454 | * Giacomo Guidi |
||
455 | */ |
||
456 | |||
457 | |||
458 | |||
459 | |||
460 | |||
461 | /* |
||
462 | int write_basic_par_start(void) |
||
463 | { |
||
464 | |||
465 | FILE *file_event_header; |
||
466 | |||
467 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
468 | if (file_event_header == NULL) return 1; |
||
469 | |||
470 | fprintf(file_event_header, "struct loader_task loader_task_list[] = {\n"); |
||
471 | |||
472 | fclose(file_event_header); |
||
473 | |||
474 | return 0; |
||
475 | |||
476 | } |
||
477 | |||
478 | |||
479 | int write_basic_par(struct loader_task *c) |
||
480 | { |
||
481 | |||
482 | FILE *file_event_header; |
||
483 | |||
484 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
485 | if (file_event_header == NULL) return 1; |
||
486 | |||
487 | fprintf(file_event_header, " {\"%s\",%d,%d,%d,%d,%d,{%d,%d},{%d,%d},%d,0,act_%s,exec_%s},\n", |
||
488 | c->name,(int)c->task_type,(int)c->server,(int)c->local_scheduler,(int)c->number,(int)c->group, |
||
489 | (int)c->deadline.tv_sec, (int)c->deadline.tv_nsec, |
||
490 | (int)c->wcet.tv_sec, (int)c->wcet.tv_nsec, |
||
491 | (int)c->act_number, c->name, c->name); |
||
492 | |||
493 | fclose(file_event_header); |
||
494 | |||
495 | return 0; |
||
496 | |||
497 | } |
||
498 | |||
499 | int write_contract(struct loader_contract *c) |
||
500 | { |
||
501 | |||
502 | FILE *file_event_header; |
||
503 | |||
504 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
505 | if (file_event_header == NULL) return 1; |
||
506 | |||
507 | fprintf(file_event_header, " {%d,{%d,%d},{%d,%d},{%d,%d},{%d,%d},%d,%d,-1},\n", |
||
508 | (int)c->number,(int)c->cmin.tv_sec,(int)c->cmin.tv_nsec, |
||
509 | (int)c->tmax.tv_sec,(int)c->tmax.tv_nsec, |
||
510 | (int)c->cmax.tv_sec,(int)c->cmax.tv_nsec, |
||
511 | (int)c->tmin.tv_sec,(int)c->tmin.tv_nsec, |
||
512 | (int)c->workload,(int)c->local_scheduler); |
||
513 | |||
514 | fclose(file_event_header); |
||
515 | |||
516 | return 0; |
||
517 | |||
518 | } |
||
519 | |||
520 | int close_loader_task(int total_task_number) |
||
521 | { |
||
522 | |||
523 | FILE *file_event_header; |
||
524 | |||
525 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
526 | if (file_event_header == NULL) return 1; |
||
527 | |||
528 | fprintf(file_event_header,"};\n\n"); |
||
529 | |||
530 | fprintf(file_event_header,"int total_loader_task = %d;\n\n",total_task_number); |
||
531 | |||
532 | fclose(file_event_header); |
||
533 | |||
534 | return 0; |
||
535 | |||
536 | } |
||
537 | |||
538 | int close_loader_contract(int total_contract_number) |
||
539 | { |
||
540 | |||
541 | FILE *file_event_header; |
||
542 | |||
543 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
544 | if (file_event_header == NULL) return 1; |
||
545 | |||
546 | fprintf(file_event_header,"};\n\n"); |
||
547 | |||
548 | fprintf(file_event_header,"int total_loader_contract = %d;\n\n",total_contract_number); |
||
549 | |||
550 | fclose(file_event_header); |
||
551 | |||
552 | return 0; |
||
553 | |||
554 | } |
||
555 | |||
556 | int write_simulation_time(struct timespec *total) |
||
557 | { |
||
558 | |||
559 | FILE *file_event_header; |
||
560 | |||
561 | file_event_header = fopen(EVENT_DEFINE,"a+"); |
||
562 | if (file_event_header == NULL) return 1; |
||
563 | |||
564 | fprintf(file_event_header,"struct timespec total_time = {%d,%d};\n\n",(int)total->tv_sec,(int)total->tv_nsec); |
||
565 | |||
566 | fclose(file_event_header); |
||
567 | |||
568 | return 0; |
||
569 | |||
570 | |||
571 | } |
||
572 | |||
573 | int write_single_act(struct timespec *t, struct loader_task *c) |
||
574 | { |
||
575 | |||
576 | FILE *file_act_header; |
||
577 | |||
578 | file_act_header = fopen(ACT_LIST,"a+"); |
||
579 | if (file_act_header == NULL) return 1; |
||
580 | |||
581 | if (TIMESPEC_A_GT_B(t,&c->act_par_1)) { |
||
582 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d}};\n\n",c->name, |
||
583 | (int)c->act_par_1.tv_sec,(int)c->act_par_1.tv_nsec); |
||
584 | c->act_number = 1; |
||
585 | } else { |
||
586 | fprintf(file_act_header,"struct timespec act_%s[] = {{0,0}};\n\n",c->name); |
||
587 | c->act_number = 0; |
||
588 | } |
||
589 | |||
590 | fclose(file_act_header); |
||
591 | |||
592 | return 0; |
||
593 | |||
594 | } |
||
595 | |||
596 | int write_periodic_act(struct timespec *t, struct loader_task *c) |
||
597 | { |
||
598 | |||
599 | FILE *file_act_header; |
||
600 | struct timespec tot_time; |
||
601 | int period; |
||
602 | |||
603 | file_act_header = fopen(ACT_LIST,"a+"); |
||
604 | if (file_act_header == NULL) return 1; |
||
605 | |||
606 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
||
607 | (int)c->act_par_1.tv_sec,(int)c->act_par_1.tv_nsec); |
||
608 | |||
609 | c->act_number = 1; |
||
610 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
611 | period = TIMESPEC2USEC(&c->act_par_2); |
||
612 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
613 | c->act_number++; |
||
614 | ADDUSEC2TIMESPEC(period,&tot_time); |
||
615 | fprintf(file_act_header," {%d,%d},\n", |
||
616 | (int)c->act_par_2.tv_sec,(int)c->act_par_2.tv_nsec); |
||
617 | } |
||
618 | |||
619 | fprintf(file_act_header," };\n\n"); |
||
620 | |||
621 | fclose(file_act_header); |
||
622 | |||
623 | return 0; |
||
624 | |||
625 | } |
||
626 | |||
627 | int write_mean_act(struct timespec *t,struct loader_task *c) |
||
628 | { |
||
629 | |||
630 | FILE *file_act_header; |
||
631 | struct timespec tot_time; |
||
632 | int next_act; |
||
633 | |||
634 | file_act_header = fopen(ACT_LIST,"a+"); |
||
635 | if (file_act_header == NULL) return 1; |
||
636 | |||
637 | fprintf(file_act_header,"struct timespec act_%s[] = {{%d,%d},\n",c->name, |
||
638 | (int)c->act_par_1.tv_sec,(int)c->act_par_1.tv_nsec); |
||
639 | |||
640 | c->act_number = 1; |
||
641 | TIMESPEC_ASSIGN(&tot_time,&c->act_par_1); |
||
642 | while (TIMESPEC_A_GT_B(t, &tot_time)) { |
||
643 | c->act_number++; |
||
644 | next_act = TIMESPEC2USEC(&c->act_par_2) + random() % TIMESPEC2USEC(&c->act_par_3) - TIMESPEC2USEC(&c->act_par_3) / 2; |
||
645 | ADDUSEC2TIMESPEC(next_act,&tot_time); |
||
646 | fprintf(file_act_header," {%d,%d},\n", |
||
647 | next_act / 1000000, next_act % 1000000 * 1000); |
||
648 | } |
||
649 | |||
650 | fprintf(file_act_header," };\n\n"); |
||
651 | |||
652 | fclose(file_act_header); |
||
653 | |||
654 | return 0; |
||
655 | |||
656 | } |
||
657 | |||
658 | int write_exec_const(struct loader_task *c) |
||
659 | { |
||
660 | |||
661 | FILE *file_exec_header; |
||
662 | int i; |
||
663 | |||
664 | file_exec_header = fopen(ACT_LIST,"a+"); |
||
665 | if (file_exec_header == NULL) return 1; |
||
666 | |||
667 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",c->name, |
||
668 | (int)c->exec_par_1.tv_sec,(int)c->exec_par_1.tv_nsec); |
||
669 | |||
670 | for (i=0; i< c->act_number-1; i++) |
||
671 | fprintf(file_exec_header," {%d,%d},\n", |
||
672 | (int)c->exec_par_1.tv_sec,(int)c->exec_par_1.tv_nsec); |
||
673 | |||
674 | fprintf(file_exec_header," };\n\n"); |
||
675 | |||
676 | fclose(file_exec_header); |
||
677 | |||
678 | return 0; |
||
679 | |||
680 | } |
||
681 | |||
682 | int write_exec_mean(struct loader_task *c) |
||
683 | { |
||
684 | |||
685 | FILE *file_exec_header; |
||
686 | int exec_time_usec; |
||
687 | int i; |
||
688 | |||
689 | file_exec_header = fopen(ACT_LIST,"a+"); |
||
690 | if (file_exec_header == NULL) return 1; |
||
691 | |||
692 | exec_time_usec = TIMESPEC2USEC(&c->exec_par_1) |
||
693 | + random() % TIMESPEC2USEC(&c->exec_par_2) - TIMESPEC2USEC(&c->exec_par_2) / 2; |
||
694 | fprintf(file_exec_header,"struct timespec exec_%s[] = {{%d,%d},\n",c->name, |
||
695 | exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000); |
||
696 | |||
697 | for (i=0; i< c->act_number-1; i++) { |
||
698 | exec_time_usec = TIMESPEC2USEC(&c->exec_par_1) |
||
699 | + random() % TIMESPEC2USEC(&c->exec_par_2) - TIMESPEC2USEC(&c->exec_par_2) / 2; |
||
700 | fprintf(file_exec_header," {%d,%d},\n", |
||
701 | exec_time_usec / 1000000, exec_time_usec % 1000000 * 1000); |
||
702 | } |
||
703 | |||
704 | fprintf(file_exec_header," };\n\n"); |
||
705 | |||
706 | fclose(file_exec_header); |
||
707 | |||
708 | return 0; |
||
709 | |||
710 | } |
||
711 | |||
712 | void *start; |
||
713 | void *end; |
||
714 | |||
715 | int main(int argc, char **argv) { |
||
716 | |||
717 | char loadfile[100]; |
||
718 | struct timespec total_time; |
||
719 | struct loader_task *start_loader_task = NULL, *current_t; |
||
720 | struct loader_contract *start_loader_contract = NULL, *current_c; |
||
721 | int err,ldnum; |
||
722 | int total_task_number; |
||
723 | int total_contract_number; |
||
724 | |||
725 | printf("\nEvent Generator\n"); |
||
726 | |||
727 | if (argc < 2) { |
||
728 | printf("Error: event_gen loadfile.fsf\n"); |
||
729 | exit(1); |
||
730 | } |
||
731 | |||
732 | printf("Read loader file %s\n",argv[1]); |
||
733 | |||
734 | sprintf(loadfile,"%s%s",LOADFILE_DIR,argv[1]); |
||
735 | err = dos_preload(loadfile,100000,&start,&end); |
||
736 | |||
737 | if (err != 0) { |
||
738 | printf("Error: File not found\n"); |
||
739 | exit(1); |
||
740 | } |
||
741 | |||
742 | printf("Parsing file\n"); |
||
743 | |||
744 | line_reader(start, end, &total_time, &start_loader_task, &start_loader_contract); |
||
745 | |||
746 | srandom(time(NULL)); |
||
747 | |||
748 | write_struct(); |
||
749 | |||
750 | current_t = start_loader_task; |
||
751 | ldnum = 1; |
||
752 | |||
753 | while(current_t != NULL) { |
||
754 | |||
755 | sprintf(current_t->name,"ltask%d",ldnum); |
||
756 | current_t->group = ldnum; |
||
757 | ldnum++; |
||
758 | |||
759 | switch (current_t->act_type) { |
||
760 | case PAR_ACT_SINGLE: |
||
761 | err = write_single_act(&total_time,current_t); |
||
762 | if (err != 0) { |
||
763 | printf("Error writing activation header\n"); |
||
764 | exit(1); |
||
765 | } |
||
766 | break; |
||
767 | case PAR_ACT_PERIODIC: |
||
768 | err = write_periodic_act(&total_time,current_t); |
||
769 | if (err != 0) { |
||
770 | printf("Error writing activation header\n"); |
||
771 | exit(1); |
||
772 | } |
||
773 | break; |
||
774 | case PAR_ACT_MEAN: |
||
775 | err = write_mean_act(&total_time,current_t); |
||
776 | if (err != 0) { |
||
777 | printf("Error writing activation header\n"); |
||
778 | exit(1); |
||
779 | } |
||
780 | break; |
||
781 | } |
||
782 | |||
783 | switch (current_t->exec_type) { |
||
784 | case PAR_EXEC_CONST: |
||
785 | err = write_exec_const(current_t); |
||
786 | if (err != 0) { |
||
787 | printf("Error writing exec header\n"); |
||
788 | exit(1); |
||
789 | } |
||
790 | break; |
||
791 | case PAR_EXEC_MEAN: |
||
792 | err = write_exec_mean(current_t); |
||
793 | if (err != 0) { |
||
794 | printf("Error writing exec header\n"); |
||
795 | exit(1); |
||
796 | } |
||
797 | break; |
||
798 | } |
||
799 | |||
800 | current_t = current_t->next; |
||
801 | |||
802 | } |
||
803 | |||
804 | write_basic_par_start(); |
||
805 | |||
806 | total_task_number = 0; |
||
807 | current_t = start_loader_task; |
||
808 | while(current_t != NULL) { |
||
809 | |||
810 | write_basic_par(current_t); |
||
811 | |||
812 | current_t = current_t->next; |
||
813 | |||
814 | total_task_number++; |
||
815 | |||
816 | } |
||
817 | |||
818 | close_loader_task(total_task_number); |
||
819 | |||
820 | write_contract_start(); |
||
821 | |||
822 | total_contract_number = 0; |
||
823 | current_c = start_loader_contract; |
||
824 | while(current_c != NULL) { |
||
825 | |||
826 | write_contract(current_c); |
||
827 | |||
828 | current_c = current_c->next; |
||
829 | |||
830 | total_contract_number++; |
||
831 | |||
832 | } |
||
833 | |||
834 | close_loader_contract(total_contract_number); |
||
835 | |||
836 | write_simulation_time(&total_time); |
||
837 | |||
838 | return 0; |
||
839 | |||
840 | } |
||
841 | |||
842 | */ |
||
843 | } |