Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak.
[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_foreach(set_list, cursor, key, data) {
556     xbt_dynar_t set = (xbt_dynar_t)data;
557     char *name;
558     unsigned int cpt = 0;
559
560     xbt_dynar_foreach (set, cpt, name)  free(name);
561     xbt_dynar_free(&set);
562   }
563   xbt_dict_free(&set_list);
564
565   xbt_dict_free(&trace_connect_list_host_avail);
566   xbt_dict_free(&trace_connect_list_power);
567   xbt_dict_free(&trace_connect_list_link_avail);
568   xbt_dict_free(&trace_connect_list_bandwidth);
569   xbt_dict_free(&trace_connect_list_latency);
570   xbt_dict_free(&traces_set_list);
571 }
572
573 void parse_platform_file(const char* file)
574 {
575   surf_parse_open(file);
576   init_data();
577   xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
578   free_data();
579   surf_parse_close();
580 }
581
582 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
583
584 static void parse_make_temporary_route(const char *src, const char *dst, int action)
585 {
586   int AX_ptr = 0;
587   surfxml_bufferstack = xbt_new0(char, 2048);
588   
589   A_surfxml_route_action = action;
590   SURFXML_BUFFER_SET(route_src,                     src);
591   SURFXML_BUFFER_SET(route_dst,                     dst);
592 }
593
594 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
595                                         const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
596 {
597   int AX_ptr = 0;
598   surfxml_bufferstack = xbt_new0(char, 2048);
599  
600   SURFXML_BUFFER_SET(host_id,                     hostName);
601   SURFXML_BUFFER_SET(host_power,                  surfxml_host_power /*hostPower*/);
602   SURFXML_BUFFER_SET(host_availability,           surfxml_host_availability);
603   SURFXML_BUFFER_SET(host_availability_file,      surfxml_host_availability_file);
604   SURFXML_BUFFER_SET(host_state_file,             surfxml_host_state_file);
605 }
606
607 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
608                                         const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
609 {
610   int AX_ptr = 0;
611   surfxml_bufferstack = xbt_new0(char, 2048);
612  
613   SURFXML_BUFFER_SET(link_id,                linkName);
614   SURFXML_BUFFER_SET(link_bandwidth,         surfxml_link_bandwidth);
615   SURFXML_BUFFER_SET(link_bandwidth_file,    surfxml_link_bandwidth_file);
616   SURFXML_BUFFER_SET(link_latency,           surfxml_link_latency);
617   SURFXML_BUFFER_SET(link_latency_file,      surfxml_link_latency_file);
618   SURFXML_BUFFER_SET(link_state_file,        surfxml_link_state_file);
619 }
620
621 /**
622 * \brief Restores the original surfxml buffer
623 */
624 static void parse_restore_original_buffer(void)
625 {
626   free(surfxml_bufferstack);
627   surfxml_bufferstack = old_buff;
628 }
629
630 /* Functions for the sets and foreach tags */
631
632 static void parse_sets(void)
633 {
634   char *id, *suffix, *prefix, *radical;
635   int start, end;
636   xbt_dynar_t radical_ends;
637   xbt_dynar_t current_set;
638   char *value;
639   int i;
640
641   id = xbt_strdup(A_surfxml_set_id);
642   prefix = xbt_strdup(A_surfxml_set_prefix);
643   suffix = xbt_strdup(A_surfxml_set_suffix);
644   radical = xbt_strdup(A_surfxml_set_radical);
645   
646   xbt_assert1(!xbt_dict_get_or_null(set_list, id),
647               "Set '%s' declared several times in the platform file.",id);  
648   radical_ends = xbt_str_split(radical, "-");
649   xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
650
651   surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
652   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
653
654   current_set = xbt_dynar_new(sizeof(char*), NULL);
655
656   
657   for (i=start; i<=end; i++) {
658      value = bprintf("%s%d%s", prefix, i, suffix);
659      xbt_dynar_push(current_set, &value);
660   } 
661   
662   xbt_dict_set(set_list, id, current_set, NULL);
663
664   free(radical);
665   free(suffix);
666   free(prefix);
667   free(id);
668 }
669
670 static const char* surfxml_host_power;
671 static const char* surfxml_host_availability;
672 static const char* surfxml_host_availability_file;
673 static const char* surfxml_host_state_file;
674
675 static void parse_host_foreach(void)
676 {
677   surfxml_host_power = A_surfxml_host_power;
678   surfxml_host_availability = A_surfxml_host_availability;
679   surfxml_host_availability_file = A_surfxml_host_availability_file;
680   surfxml_host_state_file = A_surfxml_host_state_file;
681 }
682
683 static void finalize_host_foreach(void)
684 {
685   xbt_dynar_t names = NULL;
686   unsigned int cpt = 0;
687   char *name;
688   xbt_dict_cursor_t cursor = NULL;
689   char *key,*data; 
690
691   xbt_dict_t cluster_host_props = current_property_set;
692   
693   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
694               "Set name '%s' reffered by foreach tag not found.", foreach_set_name);  
695
696   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);
697
698         
699   /* foreach name in set call the main host callback */
700   xbt_dynar_foreach (names, cpt, name) {
701     parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
702                                         surfxml_host_availability_file, surfxml_host_state_file);
703     surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
704
705     xbt_dict_foreach(cluster_host_props,cursor,key,data) {
706            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
707     }
708
709     surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
710     free(surfxml_bufferstack);
711   }
712
713   current_property_set = xbt_dict_new();
714
715   surfxml_bufferstack = old_buff;
716   
717 }
718
719 static const char* surfxml_link_bandwidth;
720 static const char* surfxml_link_bandwidth_file;
721 static const char* surfxml_link_latency;
722 static const char* surfxml_link_latency_file;
723 static const char* surfxml_link_state_file;
724
725 static void parse_link_foreach(void)
726 {
727   surfxml_link_bandwidth = A_surfxml_link_bandwidth;
728   surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
729   surfxml_link_latency = A_surfxml_link_latency;
730   surfxml_link_latency_file = A_surfxml_link_latency_file;
731   surfxml_link_state_file = A_surfxml_link_state_file;
732 }
733
734 static void finalize_link_foreach(void)
735 {
736   xbt_dynar_t names = NULL;
737   unsigned int cpt = 0;
738   char *name;
739   xbt_dict_cursor_t cursor = NULL;
740   char *key,*data; 
741
742   xbt_dict_t cluster_link_props = current_property_set;
743
744   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
745               "Set name '%s' reffered by foreach tag not found.", foreach_set_name); 
746
747   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);
748
749
750   /* for each name in set call the main link callback */
751   xbt_dynar_foreach (names, cpt, name) {
752     parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
753                                         surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
754     surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
755
756     xbt_dict_foreach(cluster_link_props,cursor,key,data) {
757            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
758     }
759
760     surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
761    free(surfxml_bufferstack);
762
763   }
764
765   current_property_set = xbt_dict_new();
766
767   surfxml_bufferstack = old_buff;
768 }
769
770 static void parse_foreach(void)
771 {
772   /* save the host & link callbacks */
773   main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
774   main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
775   main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
776   main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
777
778   /* define host & link callbacks to be used only by the foreach tag */
779   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
780   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
781   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
782   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
783
784   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
785   surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
786   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
787   surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
788
789   /* get set name */
790   foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id); 
791 }
792
793 /* Route:multi functions */
794
795 static int route_multi_size=0;
796 static char* src_name, *dst_name;
797 static int is_symmetric_route;
798
799 static void parse_route_elem(void)
800 {
801   char *val;
802
803   val = xbt_strdup(A_surfxml_link_c_ctn_id);
804   
805   xbt_dynar_push(route_link_list, &val);
806 }
807
808 static void parse_route_multi_set_endpoints(void)
809 {
810   src_name = xbt_strdup(A_surfxml_route_c_multi_src); 
811   dst_name = xbt_strdup(A_surfxml_route_c_multi_dst); 
812   route_action = A_surfxml_route_c_multi_action;
813   is_symmetric_route = A_surfxml_route_c_multi_symmetric;
814   route_multi_size++;
815
816   route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
817 }
818
819 static int contains(xbt_dynar_t list, const char* value)
820 {
821  unsigned int cpt;
822  char * val;
823  xbt_dynar_foreach(list, cpt, val) {
824    if (strcmp(val, value) == 0)
825       return 1;
826   }
827   return 0;
828 }
829
830 /* 
831    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.
832    The decision is based upon the value of action specified in the xml route:multi attribute action
833  */
834 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
835 {
836   unsigned int cpt;
837   xbt_dynar_t links;
838   char *value;
839
840   /* get already existing list if it exists */
841   links = xbt_dict_get_or_null(routing_table, route_name);
842   DEBUG1("ROUTE: %s", route_name);
843   if (links != NULL) {
844      switch (action) {
845         case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
846                                     xbt_dynar_foreach(links, cpt, value) {      
847                                        xbt_dynar_push(route_link_list,&value);
848                                     }
849                                     xbt_dynar_free(&links);
850                                     break;
851         case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */ 
852                                     xbt_dynar_foreach(route_link_list, cpt, value) {
853                                        xbt_dynar_push(links,&value);
854                                     }
855                                     xbt_dynar_free(&route_link_list);
856                                     route_link_list = links;
857                                     break;
858         case A_surfxml_route_action_OVERRIDE:
859                                     xbt_dynar_free(&links);
860                                     break;
861         default:break;
862      }
863   }
864   /* this is the final route; do not add if name is a set; add only if name is in set list */
865   if (!isMultiRoute){    
866     xbt_dict_set(routing_table, route_name, route_link_list, NULL);
867   }
868 }
869
870 static void parse_route_multi_set_route(void)
871 {
872   char* route_name;
873
874   route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
875
876   xbt_dynar_push(route_multi_elements, &route_name);
877
878   /* Add route */
879   xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
880   /* add symmetric if it is the case */ 
881   if (is_symmetric_route == 1) {
882     char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
883   
884     xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
885     xbt_dynar_push(route_multi_elements, &symmetric_name);
886     is_symmetric_route = 0;
887   }
888   free(src_name);
889   free(dst_name);
890 }
891
892 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
893 {
894   unsigned int cpt;
895   char* value, *val;
896    parse_make_temporary_route(src_name, dst_name, route_action);
897    surfxml_call_cb_functions(STag_surfxml_route_cb_list);
898    DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
899    /* Build link list */ 
900    xbt_dynar_foreach(links, cpt, value) {
901      if (strcmp(value, src) == 0)
902        val =  xbt_strdup(src_name);
903      else if (strcmp(value, dst) == 0)
904        val = xbt_strdup(dst_name);
905      else if (strcmp(value, "$dst") == 0)
906        val = xbt_strdup(dst_name);
907      else if (strcmp(value, "$src") == 0)
908        val = xbt_strdup(src_name);
909      else
910        val = xbt_strdup(value);
911      DEBUG1("\t\tELEMENT: %s", val);
912      xbt_dynar_push(route_link_list, &val);
913    }    
914    surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
915    free(surfxml_bufferstack);
916 }
917
918 static void convert_route_multi_to_routes(void)
919 {
920   xbt_dict_cursor_t cursor_w;
921   int symmetric;
922   unsigned int cpt, cpt2, cursor;
923   char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w; 
924   const char* sep="#";
925   xbt_dict_t set;
926   xbt_dynar_t src_names = NULL, dst_names = NULL, links;
927
928   if (!route_multi_elements) return;
929
930   set = cpu_set;
931   DEBUG1("%d", xbt_dict_length(workstation_set));                               
932   if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
933      set = workstation_set;
934   
935
936   old_buff = surfxml_bufferstack;
937   /* Get all routes in the exact order they were entered in the platform file */
938   xbt_dynar_foreach(route_multi_elements, cursor, key) {
939      /* Get links for the route */     
940      links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
941      keys = xbt_str_split_str(key, sep);
942      /* Get route ends */
943      src = xbt_dynar_get_as(keys, 0, char*);
944      dst = xbt_dynar_get_as(keys, 1, char*);
945      route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
946      symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
947
948     /* Create the dynar of src and dst hosts for the new routes */ 
949     /* NOTE: src and dst can be either set names or simple host names */
950     src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
951     dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
952     /* Add to dynar even if they are simple names */
953     if (src_names == NULL) {
954        src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
955        val = xbt_strdup(src);
956        xbt_dynar_push(src_names, &val);
957        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
958          THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
959     }
960     if (dst_names == NULL) {
961        dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
962        val = xbt_strdup(dst);
963        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
964          THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
965        xbt_dynar_push(dst_names, &val);
966     }
967
968     /* Build the routes */
969     DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
970     xbt_dynar_foreach(src_names, cpt, src_host_name) {
971       xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
972         /* If dst is $* then set this route to have its dst point to all hosts */
973         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
974                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
975                           //int n = xbt_dynar_member(src_names, (char*)key_w);
976                             add_multi_links(src, dst, links, src_host_name, key_w);               
977                   }
978         }
979         /* If src is $* then set this route to have its dst point to all hosts */
980         if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
981                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
982                      // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
983                         add_multi_links(src, dst, links, key_w, dst_host_name);               
984                 }
985         }
986         /* if none of them are equal to $* */
987         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
988                         add_multi_links(src, dst, links, src_host_name, dst_host_name);               
989         }     
990       }
991     }
992   }  
993   surfxml_bufferstack = old_buff;
994   xbt_dict_free(&route_multi_table);
995   xbt_dynar_free(&route_multi_elements);
996 }
997
998 /* Cluster tag functions */
999
1000 static void parse_cluster(void)
1001 {  
1002    static int AX_ptr = 0;
1003    static int surfxml_bufferstack_size = 2048;
1004  
1005    char* cluster_id = A_surfxml_cluster_id;
1006    char* cluster_prefix = A_surfxml_cluster_prefix;
1007    char* cluster_suffix = A_surfxml_cluster_suffix;
1008    char* cluster_radical = A_surfxml_cluster_radical;
1009    char* cluster_power = A_surfxml_cluster_power;
1010    char* cluster_bw = A_surfxml_cluster_bw;
1011    char* cluster_lat = A_surfxml_cluster_lat;
1012    char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
1013    char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
1014  
1015    char* saved_buff = surfxml_bufferstack;
1016
1017    char * backbone_name;
1018
1019    surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
1020
1021    /* Make set */
1022    SURFXML_BUFFER_SET(set_id, cluster_id);
1023    SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1024    SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1025    SURFXML_BUFFER_SET(set_radical, cluster_radical);
1026   
1027    SURFXML_START_TAG(set);
1028    SURFXML_END_TAG(set);
1029
1030    /* Make foreach */
1031    SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1032
1033    SURFXML_START_TAG(foreach);
1034
1035      /* Make host for the foreach */
1036      parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1037      A_surfxml_host_state = A_surfxml_host_state_ON;
1038
1039      SURFXML_START_TAG(host);
1040      SURFXML_END_TAG(host);
1041
1042      /* Make link for the foreach */
1043      parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
1044      A_surfxml_link_state = A_surfxml_link_state_ON;
1045      A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1046  
1047      SURFXML_START_TAG(link);
1048      SURFXML_END_TAG(link);
1049
1050    SURFXML_END_TAG(foreach);
1051
1052    /* Make backbone link */
1053    backbone_name = bprintf("%s_bb", cluster_id);
1054    parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
1055    A_surfxml_link_state = A_surfxml_link_state_ON;
1056    A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1057  
1058    SURFXML_START_TAG(link);
1059    SURFXML_END_TAG(link);
1060
1061    /* Make route multi with the outside world, i.e. cluster->$* */
1062    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1063    SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1064    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1065    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1066    
1067    SURFXML_START_TAG(route_c_multi);
1068
1069      SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
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    /* Make route multi between cluster hosts, i.e. cluster->cluster */
1077    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1078    SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1079    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1080    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1081    
1082    SURFXML_START_TAG(route_c_multi);
1083     
1084      SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1085     
1086      SURFXML_START_TAG(link_c_ctn);
1087      SURFXML_END_TAG(link_c_ctn);
1088     
1089    SURFXML_END_TAG(route_c_multi);
1090
1091
1092    /* Restore buff */
1093    free(surfxml_bufferstack);
1094    surfxml_bufferstack = saved_buff;
1095 }
1096
1097 /* Trace management functions */
1098
1099 static double trace_periodicity = -1.0;
1100 static char* trace_file = NULL;
1101 static char* trace_id;
1102
1103 static void parse_trace_init(void)
1104 {
1105    trace_id = strdup(A_surfxml_trace_id);
1106    trace_file = strdup(A_surfxml_trace_file);
1107    surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1108 }
1109
1110 static void parse_trace_finalize(void)
1111 {
1112   tmgr_trace_t trace;
1113   if (!trace_file || strcmp(trace_file,"") != 0) {
1114     surf_parse_get_trace(&trace, trace_file);
1115   }
1116   else {
1117     if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1118     else
1119       trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);  
1120   }
1121   xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1122 }
1123
1124 static void parse_trace_c_connect(void)
1125 {
1126    xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1127               "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1128    
1129    switch (A_surfxml_trace_c_connect_kind) {
1130     case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1131       xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1132       break;
1133     case A_surfxml_trace_c_connect_kind_POWER:
1134       xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1135       break;
1136     case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1137       xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1138       break;
1139     case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1140       xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1141       break;
1142     case A_surfxml_trace_c_connect_kind_LATENCY:
1143       xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1144       break;
1145     default:
1146       xbt_die("kind of trace unknown");
1147    }   
1148 }
1149
1150 /* Random tag functions */
1151
1152 double get_cpu_power(const char *power)
1153
1154   double power_scale = 0.0;
1155   const char *p, *q;
1156   char *generator;
1157   random_data_t random = NULL; 
1158   /* randomness is inserted like this: power="$rand(my_random)" */
1159   if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1160      if (p < q) {
1161        generator = xbt_malloc(q - (p + 6) + 1);
1162        memcpy(generator, p + 6, q - (p + 6)); 
1163        generator[q - (p + 6)] = '\0';
1164        xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1165               "Random generator %s undefined", generator);
1166        power_scale = random_generate(random);
1167      }
1168   }
1169   else {
1170     surf_parse_get_double(&power_scale, power);
1171   }
1172   return power_scale;
1173 }
1174
1175 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1176 char *random_id;
1177
1178 static void init_randomness(void)
1179 {
1180   random_id = A_surfxml_random_id;
1181   surf_parse_get_int(&random_min, A_surfxml_random_min);
1182   surf_parse_get_int(&random_max, A_surfxml_random_max);
1183   surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1184   surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1185   random_generator = A_surfxml_random_generator;
1186 }
1187
1188 static void add_randomness(void)
1189 {
1190    /* If needed aditional properties can be added by using the prop tag */
1191   random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1192    xbt_dict_set(random_data_list, random_id, (void *)random, NULL);
1193 }
1194