Logo AND Algorithmique Numérique Distribuée

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