Logo AND Algorithmique Numérique Distribuée

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