Logo AND Algorithmique Numérique Distribuée

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