Logo AND Algorithmique Numérique Distribuée

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