Logo AND Algorithmique Numérique Distribuée

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