Logo AND Algorithmique Numérique Distribuée

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