Logo AND Algorithmique Numérique Distribuée

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