Logo AND Algorithmique Numérique Distribuée

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