Logo AND Algorithmique Numérique Distribuée

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