Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2186a8602ae674f43757c2f4a3ce4b0fbed6c4b6
[simgrid.git] / src / surf / surfxml_parse.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/misc.h"
9 #include "xbt/log.h"
10 #include "xbt/str.h"
11 #include "xbt/dict.h"
12 #include "surf/surfxml_parse_private.h"
13 #include "surf/surf_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
16                                 "Logging specific to the SURF parsing module");
17
18 #undef CLEANUP
19 #include "simgrid_dtd.c"
20
21 /* Initialize the parsing globals */
22 int route_action = 0;
23 xbt_dict_t traces_set_list = NULL;
24 //xbt_dynar_t traces_connect_list = NULL;
25 xbt_dict_t trace_connect_list_host_avail = NULL;
26 xbt_dict_t trace_connect_list_power = NULL;
27 xbt_dict_t trace_connect_list_link_avail = NULL;
28 xbt_dict_t trace_connect_list_bandwidth = NULL;
29 xbt_dict_t trace_connect_list_latency = NULL;
30
31 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
32 char* old_buff;
33 /* Stores the set name reffered to by the foreach tag */
34 static const char* foreach_set_name;
35 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
36 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
37 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
38 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
39
40 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
41 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
42 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
43 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
44 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
45 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
46 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
47 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
73
74 /* Stores the sets defined in the XML */
75 xbt_dict_t set_list = NULL;
76
77 xbt_dict_t current_property_set = NULL;
78
79 /* For the route:multi tag */
80 xbt_dict_t route_table = NULL;
81 xbt_dict_t route_multi_table = NULL;
82 xbt_dynar_t route_multi_elements = NULL;
83 static xbt_dynar_t route_link_list = NULL;
84 xbt_dynar_t links = NULL;
85 xbt_dynar_t keys = NULL;
86
87 xbt_dict_t random_data_list = NULL;
88
89 static xbt_dynar_t surf_input_buffer_stack = NULL;
90 static xbt_dynar_t surf_file_to_parse_stack = NULL;
91
92 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
93
94 YY_BUFFER_STATE surf_input_buffer;
95 FILE *surf_file_to_parse = NULL;
96
97 static void convert_route_multi_to_routes(void);
98 static void parse_route_elem(void);
99 static void parse_foreach(void);
100 static void parse_sets(void);
101 static void parse_route_multi_set_endpoints(void);
102 static void parse_route_multi_set_route(void);
103 static void parse_cluster(void);
104 static void parse_trace_init(void);
105 static void parse_trace_finalize(void);
106 static void parse_trace_c_connect(void);
107 static void init_randomness(void);
108 static void add_randomness(void);
109
110 void surf_parse_free_callbacks(void)
111 {
112   xbt_dynar_free(&STag_surfxml_platform_cb_list);
113   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
114   xbt_dynar_free(&STag_surfxml_host_cb_list);
115   xbt_dynar_free(&ETag_surfxml_host_cb_list);
116   xbt_dynar_free(&STag_surfxml_router_cb_list);
117   xbt_dynar_free(&ETag_surfxml_router_cb_list);
118   xbt_dynar_free(&STag_surfxml_link_cb_list);
119   xbt_dynar_free(&ETag_surfxml_link_cb_list);
120   xbt_dynar_free(&STag_surfxml_route_cb_list);
121   xbt_dynar_free(&ETag_surfxml_route_cb_list);
122   xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
123   xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
124   xbt_dynar_free(&STag_surfxml_process_cb_list);
125   xbt_dynar_free(&ETag_surfxml_process_cb_list);
126   xbt_dynar_free(&STag_surfxml_argument_cb_list);
127   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
128   xbt_dynar_free(&STag_surfxml_prop_cb_list);
129   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
130   xbt_dynar_free(&STag_surfxml_set_cb_list);
131   xbt_dynar_free(&ETag_surfxml_set_cb_list);
132   xbt_dynar_free(&STag_surfxml_foreach_cb_list);
133   xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
134   xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
135   xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
136   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
137   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
138   xbt_dynar_free(&STag_surfxml_trace_cb_list);
139   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
140   xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
141   xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
142   xbt_dynar_free(&STag_surfxml_random_cb_list);
143   xbt_dynar_free(&ETag_surfxml_random_cb_list);
144 }
145
146 void surf_parse_reset_parser(void)
147 {
148   surf_parse_free_callbacks();
149   STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150   ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153   STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154   ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157   STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158   ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159   STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160   ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161   STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162   ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163   STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
164   ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
165   STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
166   ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
167   STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
168   ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
169   STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
170   ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
171   STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
172   ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
173   STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
174   ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
175   STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
176   ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
177   STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
178   ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
179   STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
180   ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
181 }
182
183 void STag_surfxml_include(void)
184 {
185   xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
186   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
187
188   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
189   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
190               A_surfxml_include_file);
191   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
192   surf_parse__switch_to_buffer(surf_input_buffer);
193   printf("STAG\n");
194   fflush(NULL);
195 }
196
197 void ETag_surfxml_include(void)
198 {
199   printf("ETAG\n");
200   fflush(NULL);
201   surf_parse__delete_buffer(surf_input_buffer);
202   fclose(surf_file_to_parse);
203   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
204   xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
205 }
206
207 void STag_surfxml_platform(void)
208 {
209   double version = 0.0;
210
211   sscanf(A_surfxml_platform_version, "%lg", &version);
212
213   xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
214               "You're using an ancient XML file. "
215               "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
216               "instead of MBytes, MFlops and seconds. "
217               "A script (surfxml_update.pl) to help you convert your old "
218               "platform files "
219               "is available in the contrib/platform_generation directory "
220               "of the simgrid repository. Please check also out the "
221               "SURF section of the ChangeLog for the 3.1 version. "
222               "Last, do not forget to also update your values for "
223               "the calls to MSG_task_create (if any).");
224   xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
225               "You're using an old XML file. "
226               "A script (surfxml_update.pl) to help you convert your old "
227               "platform files "
228               "is available in the contrib/platform_generation directory "
229               "of the simgrid repository.");
230
231   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
232
233 }
234
235 void ETag_surfxml_platform(void)
236 {
237   convert_route_multi_to_routes();
238
239   surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
240
241   xbt_dict_free(&random_data_list);
242   xbt_dict_free(&set_list);
243 }
244
245 void STag_surfxml_host(void)
246 {
247   surfxml_call_cb_functions(STag_surfxml_host_cb_list);
248 }
249
250 void ETag_surfxml_host(void)
251 {
252   surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
253 }
254
255 void STag_surfxml_router(void)
256 {
257   surfxml_call_cb_functions(STag_surfxml_router_cb_list);
258 }
259
260 void ETag_surfxml_router(void)
261 {
262   surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
263 }
264
265 void STag_surfxml_link(void)
266 {
267   surfxml_call_cb_functions(STag_surfxml_link_cb_list);
268 }
269
270 void ETag_surfxml_link(void)
271 {
272   surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
273 }
274
275 void STag_surfxml_route(void)
276 {
277   surfxml_call_cb_functions(STag_surfxml_route_cb_list);
278 }
279
280 void ETag_surfxml_route(void)
281 {
282   surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
283 }
284
285 void STag_surfxml_link_c_ctn(void)
286 {
287   surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
288 }
289
290 void ETag_surfxml_link_c_ctn(void)
291 {
292   surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
293 }
294
295 void STag_surfxml_process(void)
296 {
297   surfxml_call_cb_functions(STag_surfxml_process_cb_list);
298 }
299
300 void ETag_surfxml_process(void)
301 {
302   surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
303 }
304
305 void STag_surfxml_argument(void)
306 {
307   surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
308 }
309
310 void ETag_surfxml_argument(void)
311 {
312   surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
313 }
314
315 void STag_surfxml_prop(void)
316 {
317   surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
318 }
319 void ETag_surfxml_prop(void)
320 {
321   surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
322 }
323
324 void STag_surfxml_set(void)
325 {
326   surfxml_call_cb_functions(STag_surfxml_set_cb_list);
327 }
328
329 void ETag_surfxml_set(void)
330 {
331   surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
332 }
333
334 void STag_surfxml_foreach(void)
335 {
336   /* Save the current buffer */
337   old_buff = surfxml_bufferstack;
338   surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
339 }
340
341 void ETag_surfxml_foreach(void)
342
343   surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
344
345   /* free the temporary dynar and restore original */
346   xbt_dynar_free(&STag_surfxml_host_cb_list);
347   xbt_dynar_free(&ETag_surfxml_host_cb_list);
348
349   STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
350   ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
351
352   /* free the temporary dynar and restore original */
353   xbt_dynar_free(&STag_surfxml_link_cb_list);
354   xbt_dynar_free(&ETag_surfxml_link_cb_list);
355
356   STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
357   ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
358
359 }
360
361 void STag_surfxml_route_c_multi(void)
362 {
363   surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
364 }
365
366 void ETag_surfxml_route_c_multi(void)
367 {
368   surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
369 }
370
371 void STag_surfxml_cluster(void)
372 {
373   surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
374 }
375
376 void ETag_surfxml_cluster(void)
377 {
378   surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
379 }
380
381 void STag_surfxml_trace(void)
382 {
383   surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
384 }
385
386 void ETag_surfxml_trace(void)
387 {
388   surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
389 }
390
391 void STag_surfxml_trace_c_connect(void)
392 {
393   surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
394 }
395
396 void ETag_surfxml_trace_c_connect(void)
397 {
398   surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
399 }
400
401 void STag_surfxml_random(void)
402 {
403   surfxml_call_cb_functions(STag_surfxml_random_cb_list);
404 }
405
406 void ETag_surfxml_random(void)
407 {
408   surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
409 }
410
411 void surf_parse_open(const char *file)
412 {
413   static int warned = 0;        /* warn only once */
414   if (!file) {
415     if (!warned) {
416       WARN0
417           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
418       warned = 1;
419     }
420     return;
421   }
422   if (!surf_input_buffer_stack)
423     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
424   if (!surf_file_to_parse_stack)
425     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
426
427   surf_file_to_parse = surf_fopen(file, "r");
428   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
429   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
430   surf_parse__switch_to_buffer(surf_input_buffer);
431   surf_parse_lineno = 1;
432 }
433
434 void surf_parse_close(void)
435 {
436   if (surf_input_buffer_stack)
437     xbt_dynar_free(&surf_input_buffer_stack);
438   if (surf_file_to_parse_stack)
439     xbt_dynar_free(&surf_file_to_parse_stack);
440
441   if (surf_file_to_parse) {
442     surf_parse__delete_buffer(surf_input_buffer);
443     fclose(surf_file_to_parse);
444   }
445 }
446
447
448 static int _surf_parse(void)
449 {
450   return surf_parse_lex();
451 }
452
453 int_f_void_t surf_parse = _surf_parse;
454
455 void surf_parse_get_double(double *value, const char *string)
456 {
457   int ret = 0;
458
459   ret = sscanf(string, "%lg", value);
460   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
461               surf_parse_lineno, string);
462 }
463
464 void surf_parse_get_int(int *value, const char *string)
465 {
466   int ret = 0;
467
468   ret = sscanf(string, "%d", value);
469   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
470               surf_parse_lineno, string);
471 }
472
473 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
474 {
475   if ((!string) || (strcmp(string, "") == 0))
476     *trace = NULL;
477   else
478     *trace = tmgr_trace_new(string);
479 }
480
481 void parse_properties(void)
482 {
483   char *value = NULL;
484
485   if(!current_property_set) current_property_set = xbt_dict_new();
486
487    value = xbt_strdup(A_surfxml_prop_value);  
488    xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
489 }
490
491 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
492 {
493   xbt_dynar_push(cb_list, &function);
494 }
495
496 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
497 {
498   unsigned int iterator;
499   void_f_void_t fun;
500   xbt_dynar_foreach(cb_list, iterator, fun){
501        DEBUG2("call %p %p",fun,*fun);
502        (*fun)();
503     }
504 }
505
506 static void parse_route_set_endpoints(void) {
507   route_link_list = xbt_dynar_new(sizeof(char *), NULL);
508 }
509
510 static void init_data(void)
511 {
512   xbt_dict_free(&route_table);
513   xbt_dynar_free(&route_link_list);
514   route_table = xbt_dict_new();
515
516   route_multi_table = xbt_dict_new();
517   route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
518   traces_set_list = xbt_dict_new();
519   if (set_list == NULL) set_list = xbt_dict_new();
520
521   trace_connect_list_host_avail = xbt_dict_new();
522   trace_connect_list_power = xbt_dict_new();
523   trace_connect_list_link_avail = xbt_dict_new();
524   trace_connect_list_bandwidth = xbt_dict_new();
525   trace_connect_list_latency = xbt_dict_new();
526
527   random_data_list = xbt_dict_new();
528   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
529   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
530   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
531   surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
532   surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
533   surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
534   surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
535   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
536   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
537   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
538   surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
539   surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
540   surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
541 }
542
543 static void free_data(void) 
544 {
545   char *key,*data;
546   xbt_dict_cursor_t cursor = NULL;
547
548   xbt_dict_foreach(route_table, cursor, key, data) {
549     xbt_dynar_t links = (xbt_dynar_t)data;
550     xbt_dynar_free(&links);
551   }
552   xbt_dict_free(&route_table);
553   route_link_list = NULL;
554
555   xbt_dict_foreach(set_list, cursor, key, data) {
556     xbt_dynar_t set = (xbt_dynar_t)data;
557     char *name;
558     unsigned int cpt = 0;
559
560     xbt_dynar_foreach (set, cpt, name)  free(name);
561     xbt_dynar_free(&set);
562   }
563   xbt_dict_free(&set_list);
564
565   xbt_dict_free(&trace_connect_list_host_avail);
566   xbt_dict_free(&trace_connect_list_power);
567   xbt_dict_free(&trace_connect_list_link_avail);
568   xbt_dict_free(&trace_connect_list_bandwidth);
569   xbt_dict_free(&trace_connect_list_latency);
570   xbt_dict_free(&traces_set_list);
571 }
572
573 void parse_platform_file(const char* file)
574 {
575   surf_parse_open(file);
576   init_data();
577   xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
578   free_data();
579   surf_parse_close();
580 }
581
582 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
583
584 static void parse_make_temporary_route(const char *src, const char *dst, int action)
585 {
586   int AX_ptr = 0;
587   surfxml_bufferstack = xbt_new0(char, 2048);
588   
589   A_surfxml_route_action = action;
590   SURFXML_BUFFER_SET(route_src,                     src);
591   SURFXML_BUFFER_SET(route_dst,                     dst);
592 }
593
594 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
595                                         const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
596 {
597   int AX_ptr = 0;
598   surfxml_bufferstack = xbt_new0(char, 2048);
599  
600   SURFXML_BUFFER_SET(host_id,                     hostName);
601   SURFXML_BUFFER_SET(host_power,                  surfxml_host_power /*hostPower*/);
602   SURFXML_BUFFER_SET(host_availability,           surfxml_host_availability);
603   SURFXML_BUFFER_SET(host_availability_file,      surfxml_host_availability_file);
604   SURFXML_BUFFER_SET(host_state_file,             surfxml_host_state_file);
605 }
606
607 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
608                                         const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
609 {
610   int AX_ptr = 0;
611   surfxml_bufferstack = xbt_new0(char, 2048);
612  
613   SURFXML_BUFFER_SET(link_id,                linkName);
614   SURFXML_BUFFER_SET(link_bandwidth,         surfxml_link_bandwidth);
615   SURFXML_BUFFER_SET(link_bandwidth_file,    surfxml_link_bandwidth_file);
616   SURFXML_BUFFER_SET(link_latency,           surfxml_link_latency);
617   SURFXML_BUFFER_SET(link_latency_file,      surfxml_link_latency_file);
618   SURFXML_BUFFER_SET(link_state_file,        surfxml_link_state_file);
619 }
620
621 /**
622 * \brief Restores the original surfxml buffer
623 */
624 static void parse_restore_original_buffer(void)
625 {
626   free(surfxml_bufferstack);
627   surfxml_bufferstack = old_buff;
628 }
629
630 /* Functions for the sets and foreach tags */
631
632 static void parse_sets(void)
633 {
634   char *id, *suffix, *prefix, *radical;
635   int start, end;
636   xbt_dynar_t radical_ends;
637   xbt_dynar_t current_set;
638   char *value;
639   int i;
640
641   id = xbt_strdup(A_surfxml_set_id);
642   prefix = xbt_strdup(A_surfxml_set_prefix);
643   suffix = xbt_strdup(A_surfxml_set_suffix);
644   radical = xbt_strdup(A_surfxml_set_radical);
645   
646   xbt_assert1(!xbt_dict_get_or_null(set_list, id),
647               "Set '%s' declared several times in the platform file.",id);  
648   radical_ends = xbt_str_split(radical, "-");
649   xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
650
651   surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
652   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
653
654   current_set = xbt_dynar_new(sizeof(char*), NULL);
655
656   
657   for (i=start; i<=end; i++) {
658      value = bprintf("%s%d%s", prefix, i, suffix);
659      xbt_dynar_push(current_set, &value);
660   } 
661   
662   xbt_dict_set(set_list, id, current_set, NULL);
663 }
664
665 static const char* surfxml_host_power;
666 static const char* surfxml_host_availability;
667 static const char* surfxml_host_availability_file;
668 static const char* surfxml_host_state_file;
669
670 static void parse_host_foreach(void)
671 {
672   surfxml_host_power = A_surfxml_host_power;
673   surfxml_host_availability = A_surfxml_host_availability;
674   surfxml_host_availability_file = A_surfxml_host_availability_file;
675   surfxml_host_state_file = A_surfxml_host_state_file;
676 }
677
678 static void finalize_host_foreach(void)
679 {
680   xbt_dynar_t names = NULL;
681   unsigned int cpt = 0;
682   char *name;
683   xbt_dict_cursor_t cursor = NULL;
684   char *key,*data; 
685
686   xbt_dict_t cluster_host_props = current_property_set;
687   
688   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
689               "Set name '%s' reffered by foreach tag not found.", foreach_set_name);  
690
691   xbt_assert1((strcmp(A_surfxml_host_id, "$1") == 0), "The id of the host within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_host_id);
692
693         
694   /* foreach name in set call the main host callback */
695   xbt_dynar_foreach (names, cpt, name) {
696     parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
697                                         surfxml_host_availability_file, surfxml_host_state_file);
698     surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
699
700     xbt_dict_foreach(cluster_host_props,cursor,key,data) {
701            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
702     }
703
704     surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
705     free(surfxml_bufferstack);
706   }
707
708   current_property_set = xbt_dict_new();
709
710   surfxml_bufferstack = old_buff;
711   
712 }
713
714 static const char* surfxml_link_bandwidth;
715 static const char* surfxml_link_bandwidth_file;
716 static const char* surfxml_link_latency;
717 static const char* surfxml_link_latency_file;
718 static const char* surfxml_link_state_file;
719
720 static void parse_link_foreach(void)
721 {
722   surfxml_link_bandwidth = A_surfxml_link_bandwidth;
723   surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
724   surfxml_link_latency = A_surfxml_link_latency;
725   surfxml_link_latency_file = A_surfxml_link_latency_file;
726   surfxml_link_state_file = A_surfxml_link_state_file;
727 }
728
729 static void finalize_link_foreach(void)
730 {
731   xbt_dynar_t names = NULL;
732   unsigned int cpt = 0;
733   char *name;
734   xbt_dict_cursor_t cursor = NULL;
735   char *key,*data; 
736
737   xbt_dict_t cluster_link_props = current_property_set;
738
739   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
740               "Set name '%s' reffered by foreach tag not found.", foreach_set_name); 
741
742   xbt_assert1((strcmp(A_surfxml_link_id, "$1") == 0), "The id of the link within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_link_id);
743
744
745   /* for each name in set call the main link callback */
746   xbt_dynar_foreach (names, cpt, name) {
747     parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
748                                         surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
749     surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
750
751     xbt_dict_foreach(cluster_link_props,cursor,key,data) {
752            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
753     }
754
755     surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
756    free(surfxml_bufferstack);
757
758   }
759
760   current_property_set = xbt_dict_new();
761
762   surfxml_bufferstack = old_buff;
763 }
764
765 static void parse_foreach(void)
766 {
767   /* save the host & link callbacks */
768   main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
769   main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
770   main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
771   main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
772
773   /* define host & link callbacks to be used only by the foreach tag */
774   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
775   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
776   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
777   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
778
779   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
780   surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
781   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
782   surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
783
784   /* get set name */
785   foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id); 
786 }
787
788 /* Route:multi functions */
789
790 static int route_multi_size=0;
791 static char* src_name, *dst_name;
792 static int is_symmetric_route;
793
794 static void parse_route_elem(void)
795 {
796   char *val;
797
798   val = xbt_strdup(A_surfxml_link_c_ctn_id);
799   
800   xbt_dynar_push(route_link_list, &val);
801 }
802
803 static void parse_route_multi_set_endpoints(void)
804 {
805   src_name = xbt_strdup(A_surfxml_route_c_multi_src); 
806   dst_name = xbt_strdup(A_surfxml_route_c_multi_dst); 
807   route_action = A_surfxml_route_c_multi_action;
808   is_symmetric_route = A_surfxml_route_c_multi_symmetric;
809   route_multi_size++;
810
811   route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
812 }
813
814 static int contains(xbt_dynar_t list, const char* value)
815 {
816  unsigned int cpt;
817  char * val;
818  xbt_dynar_foreach(list, cpt, val) {
819    if (strcmp(val, value) == 0)
820       return 1;
821   }
822   return 0;
823 }
824
825 /* 
826    This function is used to append or override the contents of an already existing route in the case a new one with its name is found.
827    The decision is based upon the value of action specified in the xml route:multi attribute action
828  */
829 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
830 {
831   unsigned int cpt;
832   xbt_dynar_t links;
833   char *value;
834
835   /* get already existing list if it exists */
836   links = xbt_dict_get_or_null(routing_table, route_name);
837   DEBUG1("ROUTE: %s", route_name);
838   if (links != NULL) {
839      switch (action) {
840         case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
841                                     xbt_dynar_foreach(links, cpt, value) {      
842                                        xbt_dynar_push(route_link_list,&value);
843                                     }
844                                     xbt_dynar_free(&links);
845                                     break;
846         case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */ 
847                                     xbt_dynar_foreach(route_link_list, cpt, value) {
848                                        xbt_dynar_push(links,&value);
849                                     }
850                                     xbt_dynar_free(&route_link_list);
851                                     route_link_list = links;
852                                     break;
853         case A_surfxml_route_action_OVERRIDE:
854                                     xbt_dynar_free(&links);
855                                     break;
856         default:break;
857      }
858   }
859   /* this is the final route; do not add if name is a set; add only if name is in set list */
860   if (!isMultiRoute){    
861     xbt_dict_set(routing_table, route_name, route_link_list, NULL);
862   }
863 }
864
865 static void parse_route_multi_set_route(void)
866 {
867   char* route_name;
868
869   route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
870
871   xbt_dynar_push(route_multi_elements, &route_name);
872
873   /* Add route */
874   xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
875   /* add symmetric if it is the case */ 
876   if (is_symmetric_route == 1) {
877     char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
878   
879     xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
880     xbt_dynar_push(route_multi_elements, &symmetric_name);
881     is_symmetric_route = 0;
882   }
883   free(src_name);
884   free(dst_name);
885 }
886
887 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
888 {
889   unsigned int cpt;
890   char* value, *val;
891    parse_make_temporary_route(src_name, dst_name, route_action);
892    surfxml_call_cb_functions(STag_surfxml_route_cb_list);
893    DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
894    /* Build link list */ 
895    xbt_dynar_foreach(links, cpt, value) {
896      if (strcmp(value, src) == 0)
897        val =  xbt_strdup(src_name);
898      else if (strcmp(value, dst) == 0)
899        val = xbt_strdup(dst_name);
900      else if (strcmp(value, "$dst") == 0)
901        val = xbt_strdup(dst_name);
902      else if (strcmp(value, "$src") == 0)
903        val = xbt_strdup(src_name);
904      else
905        val = xbt_strdup(value);
906      DEBUG1("\t\tELEMENT: %s", val);
907      xbt_dynar_push(route_link_list, &val);
908    }    
909    surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
910    free(surfxml_bufferstack);
911 }
912
913 static void convert_route_multi_to_routes(void)
914 {
915   xbt_dict_cursor_t cursor_w;
916   int symmetric;
917   unsigned int cpt, cpt2, cursor;
918   char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w; 
919   const char* sep="#";
920   xbt_dict_t set;
921   xbt_dynar_t src_names = NULL, dst_names = NULL, links;
922
923   if (!route_multi_elements) return;
924
925   set = cpu_set;
926   DEBUG1("%d", xbt_dict_length(workstation_set));                               
927   if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
928      set = workstation_set;
929   
930
931   old_buff = surfxml_bufferstack;
932   /* Get all routes in the exact order they were entered in the platform file */
933   xbt_dynar_foreach(route_multi_elements, cursor, key) {
934      /* Get links for the route */     
935      links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
936      keys = xbt_str_split_str(key, sep);
937      /* Get route ends */
938      src = xbt_dynar_get_as(keys, 0, char*);
939      dst = xbt_dynar_get_as(keys, 1, char*);
940      route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
941      symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
942
943     /* Create the dynar of src and dst hosts for the new routes */ 
944     /* NOTE: src and dst can be either set names or simple host names */
945     src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
946     dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
947     /* Add to dynar even if they are simple names */
948     if (src_names == NULL) {
949        src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
950        val = xbt_strdup(src);
951        xbt_dynar_push(src_names, &val);
952        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
953          THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
954     }
955     if (dst_names == NULL) {
956        dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
957        val = xbt_strdup(dst);
958        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
959          THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
960        xbt_dynar_push(dst_names, &val);
961     }
962
963     /* Build the routes */
964     DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
965     xbt_dynar_foreach(src_names, cpt, src_host_name) {
966       xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
967         /* If dst is $* then set this route to have its dst point to all hosts */
968         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
969                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
970                           //int n = xbt_dynar_member(src_names, (char*)key_w);
971                             add_multi_links(src, dst, links, src_host_name, key_w);               
972                   }
973         }
974         /* If src is $* then set this route to have its dst point to all hosts */
975         if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
976                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
977                      // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
978                         add_multi_links(src, dst, links, key_w, dst_host_name);               
979                 }
980         }
981         /* if none of them are equal to $* */
982         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
983                         add_multi_links(src, dst, links, src_host_name, dst_host_name);               
984         }     
985       }
986     }
987   }  
988   surfxml_bufferstack = old_buff;
989   xbt_dict_free(&route_multi_table);
990   xbt_dynar_free(&route_multi_elements);
991 }
992
993 /* Cluster tag functions */
994
995 static void parse_cluster(void)
996 {  
997    static int AX_ptr = 0;
998    static int surfxml_bufferstack_size = 2048;
999  
1000    char* cluster_id = A_surfxml_cluster_id;
1001    char* cluster_prefix = A_surfxml_cluster_prefix;
1002    char* cluster_suffix = A_surfxml_cluster_suffix;
1003    char* cluster_radical = A_surfxml_cluster_radical;
1004    char* cluster_power = A_surfxml_cluster_power;
1005    char* cluster_bw = A_surfxml_cluster_bw;
1006    char* cluster_lat = A_surfxml_cluster_lat;
1007    char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
1008    char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
1009  
1010    char* saved_buff = surfxml_bufferstack;
1011
1012    char * backbone_name;
1013
1014    surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
1015
1016    /* Make set */
1017    SURFXML_BUFFER_SET(set_id, cluster_id);
1018    SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1019    SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1020    SURFXML_BUFFER_SET(set_radical, cluster_radical);
1021   
1022    SURFXML_START_TAG(set);
1023    SURFXML_END_TAG(set);
1024
1025    /* Make foreach */
1026    SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1027
1028    SURFXML_START_TAG(foreach);
1029
1030      /* Make host for the foreach */
1031      parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1032      A_surfxml_host_state = A_surfxml_host_state_ON;
1033
1034      SURFXML_START_TAG(host);
1035      SURFXML_END_TAG(host);
1036
1037      /* Make link for the foreach */
1038      parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
1039      A_surfxml_link_state = A_surfxml_link_state_ON;
1040      A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1041  
1042      SURFXML_START_TAG(link);
1043      SURFXML_END_TAG(link);
1044
1045    SURFXML_END_TAG(foreach);
1046
1047    /* Make backbone link */
1048    backbone_name = bprintf("%s_bb", cluster_id);
1049    parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
1050    A_surfxml_link_state = A_surfxml_link_state_ON;
1051    A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1052  
1053    SURFXML_START_TAG(link);
1054    SURFXML_END_TAG(link);
1055
1056    /* Make route multi with the outside world, i.e. cluster->$* */
1057    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1058    SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1059    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1060    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1061    
1062    SURFXML_START_TAG(route_c_multi);
1063
1064      SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1065     
1066      SURFXML_START_TAG(link_c_ctn);
1067      SURFXML_END_TAG(link_c_ctn);
1068
1069    SURFXML_END_TAG(route_c_multi);
1070
1071    /* Make route multi between cluster hosts, i.e. cluster->cluster */
1072    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1073    SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1074    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1075    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1076    
1077    SURFXML_START_TAG(route_c_multi);
1078     
1079      SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1080     
1081      SURFXML_START_TAG(link_c_ctn);
1082      SURFXML_END_TAG(link_c_ctn);
1083     
1084    SURFXML_END_TAG(route_c_multi);
1085
1086
1087    /* Restore buff */
1088    free(surfxml_bufferstack);
1089    surfxml_bufferstack = saved_buff;
1090 }
1091
1092 /* Trace management functions */
1093
1094 static double trace_periodicity = -1.0;
1095 static char* trace_file = NULL;
1096 static char* trace_id;
1097
1098 static void parse_trace_init(void)
1099 {
1100    trace_id = strdup(A_surfxml_trace_id);
1101    trace_file = strdup(A_surfxml_trace_file);
1102    surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1103 }
1104
1105 static void parse_trace_finalize(void)
1106 {
1107   tmgr_trace_t trace;
1108   if (!trace_file || strcmp(trace_file,"") != 0) {
1109     surf_parse_get_trace(&trace, trace_file);
1110   }
1111   else {
1112     if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1113     else
1114       trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);  
1115   }
1116   xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1117 }
1118
1119 static void parse_trace_c_connect(void)
1120 {
1121    xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1122               "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1123    
1124    switch (A_surfxml_trace_c_connect_kind) {
1125     case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1126       xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1127       break;
1128     case A_surfxml_trace_c_connect_kind_POWER:
1129       xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1130       break;
1131     case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1132       xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1133       break;
1134     case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1135       xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1136       break;
1137     case A_surfxml_trace_c_connect_kind_LATENCY:
1138       xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1139       break;
1140     default:
1141       xbt_die("kind of trace unknown");
1142    }   
1143 }
1144
1145 /* Random tag functions */
1146
1147 double get_cpu_power(const char *power)
1148
1149   double power_scale = 0.0;
1150   const char *p, *q;
1151   char *generator;
1152   random_data_t random = NULL; 
1153   /* randomness is inserted like this: power="$rand(my_random)" */
1154   if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1155      if (p < q) {
1156        generator = xbt_malloc(q - (p + 6) + 1);
1157        memcpy(generator, p + 6, q - (p + 6)); 
1158        generator[q - (p + 6)] = '\0';
1159        xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1160               "Random generator %s undefined", generator);
1161        power_scale = random_generate(random);
1162      }
1163   }
1164   else {
1165     surf_parse_get_double(&power_scale, power);
1166   }
1167   return power_scale;
1168 }
1169
1170 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1171 char *random_id;
1172
1173 static void init_randomness(void)
1174 {
1175   random_id = A_surfxml_random_id;
1176   surf_parse_get_int(&random_min, A_surfxml_random_min);
1177   surf_parse_get_int(&random_max, A_surfxml_random_max);
1178   surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1179   surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1180   random_generator = A_surfxml_random_generator;
1181 }
1182
1183 static void add_randomness(void)
1184 {
1185    /* If needed aditional properties can be added by using the prop tag */
1186   random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1187    xbt_dict_set(random_data_list, random_id, (void *)random, NULL);
1188 }
1189