Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace xbt_die(bprintf(...)) with xbt_die(...).
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
15                                 "Logging specific to the SURF parsing module");
16 #undef CLEANUP
17 #include "simgrid_dtd.c"
18
19 /* Initialize the parsing globals */
20 xbt_dict_t traces_set_list = NULL;
21 xbt_dict_t trace_connect_list_host_avail = NULL;
22 xbt_dict_t trace_connect_list_power = NULL;
23 xbt_dict_t trace_connect_list_link_avail = NULL;
24 xbt_dict_t trace_connect_list_bandwidth = NULL;
25 xbt_dict_t trace_connect_list_latency = NULL;
26
27 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the foreach tag */
28 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
29 int surfxml_bufferstack_size = 2048;
30
31 static char *old_buff = NULL;
32 static void surf_parse_error(char *msg);
33
34 unsigned int surfxml_buffer_stack_stack_ptr;
35 unsigned int surfxml_buffer_stack_stack[1024];
36
37
38 void surfxml_bufferstack_push(int new)
39 {
40   if (!new)
41     old_buff = surfxml_bufferstack;
42   else {
43     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
44     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
45   }
46 }
47
48 void surfxml_bufferstack_pop(int new)
49 {
50   if (!new)
51     surfxml_bufferstack = old_buff;
52   else {
53     free(surfxml_bufferstack);
54     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
55   }
56 }
57
58 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
59 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_link_ctn_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_link_ctn_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
73 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
74 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
75 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
76 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
77 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
78 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
79 xbt_dynar_t STag_surfxml_peer_cb_list = NULL;
80 xbt_dynar_t ETag_surfxml_peer_cb_list = NULL;
81 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
82 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
83 xbt_dynar_t STag_surfxml_trace_connect_cb_list = NULL;
84 xbt_dynar_t ETag_surfxml_trace_connect_cb_list = NULL;
85 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
86 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
87 xbt_dynar_t STag_surfxml_AS_cb_list = NULL;
88 xbt_dynar_t ETag_surfxml_AS_cb_list = NULL;
89 xbt_dynar_t STag_surfxml_ASroute_cb_list = NULL;
90 xbt_dynar_t ETag_surfxml_ASroute_cb_list = NULL;
91 xbt_dynar_t STag_surfxml_bypassRoute_cb_list = NULL;
92 xbt_dynar_t ETag_surfxml_bypassRoute_cb_list = NULL;
93 xbt_dynar_t STag_surfxml_config_cb_list = NULL;
94 xbt_dynar_t ETag_surfxml_config_cb_list = NULL;
95
96 /* store the current property set for any tag */
97 xbt_dict_t current_property_set = NULL;
98 /* dictionary of random generator data */
99 xbt_dict_t random_data_list = NULL;
100
101 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
102
103 YY_BUFFER_STATE surf_input_buffer;
104 FILE *surf_file_to_parse = NULL;
105
106 static void surf_parse_error(char *msg);
107
108 static void parse_Stag_trace(void);
109 static void parse_Etag_trace(void);
110 static void parse_Stag_trace_connect(void);
111
112 static void init_randomness(void);
113 static void add_randomness(void);
114
115 /*
116  * This function return 0 or 1 if callbacks are empty or not.
117  */
118 int surf_parse_no_callbacks(void)
119 {
120         return  xbt_dynar_is_empty(STag_surfxml_platform_cb_list) &&
121           xbt_dynar_is_empty(ETag_surfxml_platform_cb_list) &&
122           xbt_dynar_is_empty(STag_surfxml_host_cb_list) &&
123           xbt_dynar_is_empty(ETag_surfxml_host_cb_list) &&
124           xbt_dynar_is_empty(STag_surfxml_router_cb_list) &&
125           xbt_dynar_is_empty(ETag_surfxml_router_cb_list) &&
126           xbt_dynar_is_empty(STag_surfxml_link_cb_list) &&
127           xbt_dynar_is_empty(ETag_surfxml_link_cb_list) &&
128           xbt_dynar_is_empty(STag_surfxml_route_cb_list) &&
129           xbt_dynar_is_empty(ETag_surfxml_route_cb_list) &&
130           xbt_dynar_is_empty(STag_surfxml_link_ctn_cb_list) &&
131           xbt_dynar_is_empty(ETag_surfxml_link_ctn_cb_list) &&
132           xbt_dynar_is_empty(STag_surfxml_process_cb_list) &&
133           xbt_dynar_is_empty(ETag_surfxml_process_cb_list) &&
134           xbt_dynar_is_empty(STag_surfxml_argument_cb_list) &&
135           xbt_dynar_is_empty(ETag_surfxml_argument_cb_list) &&
136           xbt_dynar_is_empty(STag_surfxml_prop_cb_list) &&
137           xbt_dynar_is_empty(ETag_surfxml_prop_cb_list) &&
138           xbt_dynar_is_empty(STag_surfxml_trace_cb_list) &&
139           xbt_dynar_is_empty(ETag_surfxml_trace_cb_list) &&
140           xbt_dynar_is_empty(STag_surfxml_trace_connect_cb_list) &&
141           xbt_dynar_is_empty(ETag_surfxml_trace_connect_cb_list) &&
142           xbt_dynar_is_empty(STag_surfxml_random_cb_list) &&
143           xbt_dynar_is_empty(ETag_surfxml_random_cb_list) &&
144           xbt_dynar_is_empty(STag_surfxml_AS_cb_list) &&
145           xbt_dynar_is_empty(ETag_surfxml_AS_cb_list) &&
146           xbt_dynar_is_empty(STag_surfxml_ASroute_cb_list) &&
147           xbt_dynar_is_empty(ETag_surfxml_ASroute_cb_list) &&
148           xbt_dynar_is_empty(STag_surfxml_bypassRoute_cb_list) &&
149           xbt_dynar_is_empty(ETag_surfxml_bypassRoute_cb_list) &&
150           xbt_dynar_is_empty(STag_surfxml_cluster_cb_list) &&
151           xbt_dynar_is_empty(ETag_surfxml_cluster_cb_list) &&
152           xbt_dynar_is_empty(STag_surfxml_peer_cb_list) &&
153           xbt_dynar_is_empty(ETag_surfxml_peer_cb_list) &&
154           xbt_dynar_is_empty(STag_surfxml_config_cb_list) &&
155           xbt_dynar_is_empty(ETag_surfxml_config_cb_list);
156 }
157
158 void surf_parse_init_callbacks(void)
159 {
160           STag_surfxml_platform_cb_list =
161               xbt_dynar_new(sizeof(void_f_void_t), NULL);
162           ETag_surfxml_platform_cb_list =
163               xbt_dynar_new(sizeof(void_f_void_t), NULL);
164           STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
165           ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
166           STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
167           ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
168           STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
169           ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
170           STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
171           ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
172           STag_surfxml_link_ctn_cb_list =
173               xbt_dynar_new(sizeof(void_f_void_t), NULL);
174           ETag_surfxml_link_ctn_cb_list =
175               xbt_dynar_new(sizeof(void_f_void_t), NULL);
176           STag_surfxml_process_cb_list =
177               xbt_dynar_new(sizeof(void_f_void_t), NULL);
178           ETag_surfxml_process_cb_list =
179               xbt_dynar_new(sizeof(void_f_void_t), NULL);
180           STag_surfxml_argument_cb_list =
181               xbt_dynar_new(sizeof(void_f_void_t), NULL);
182           ETag_surfxml_argument_cb_list =
183               xbt_dynar_new(sizeof(void_f_void_t), NULL);
184           STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
185           ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
186           STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
187           ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
188           STag_surfxml_trace_connect_cb_list =
189               xbt_dynar_new(sizeof(void_f_void_t), NULL);
190           ETag_surfxml_trace_connect_cb_list =
191               xbt_dynar_new(sizeof(void_f_void_t), NULL);
192           STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
193           ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
194           STag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
195           ETag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
196           STag_surfxml_ASroute_cb_list =
197               xbt_dynar_new(sizeof(void_f_void_t), NULL);
198           ETag_surfxml_ASroute_cb_list =
199               xbt_dynar_new(sizeof(void_f_void_t), NULL);
200           STag_surfxml_bypassRoute_cb_list =
201               xbt_dynar_new(sizeof(void_f_void_t), NULL);
202           ETag_surfxml_bypassRoute_cb_list =
203               xbt_dynar_new(sizeof(void_f_void_t), NULL);
204           STag_surfxml_cluster_cb_list =
205               xbt_dynar_new(sizeof(void_f_void_t), NULL);
206           ETag_surfxml_cluster_cb_list =
207               xbt_dynar_new(sizeof(void_f_void_t), NULL);
208           STag_surfxml_peer_cb_list =
209               xbt_dynar_new(sizeof(void_f_void_t), NULL);
210           ETag_surfxml_peer_cb_list =
211               xbt_dynar_new(sizeof(void_f_void_t), NULL);
212           STag_surfxml_config_cb_list =
213                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
214           ETag_surfxml_config_cb_list =
215                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
216 }
217
218 void surf_parse_reset_callbacks(void)
219 {
220         surf_parse_free_callbacks();
221         surf_parse_init_callbacks();
222 }
223
224 void surf_parse_free_callbacks(void)
225 {
226   xbt_dynar_free(&STag_surfxml_platform_cb_list);
227   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
228   xbt_dynar_free(&STag_surfxml_host_cb_list);
229   xbt_dynar_free(&ETag_surfxml_host_cb_list);
230   xbt_dynar_free(&STag_surfxml_router_cb_list);
231   xbt_dynar_free(&ETag_surfxml_router_cb_list);
232   xbt_dynar_free(&STag_surfxml_link_cb_list);
233   xbt_dynar_free(&ETag_surfxml_link_cb_list);
234   xbt_dynar_free(&STag_surfxml_route_cb_list);
235   xbt_dynar_free(&ETag_surfxml_route_cb_list);
236   xbt_dynar_free(&STag_surfxml_link_ctn_cb_list);
237   xbt_dynar_free(&ETag_surfxml_link_ctn_cb_list);
238   xbt_dynar_free(&STag_surfxml_process_cb_list);
239   xbt_dynar_free(&ETag_surfxml_process_cb_list);
240   xbt_dynar_free(&STag_surfxml_argument_cb_list);
241   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
242   xbt_dynar_free(&STag_surfxml_prop_cb_list);
243   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
244   xbt_dynar_free(&STag_surfxml_trace_cb_list);
245   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
246   xbt_dynar_free(&STag_surfxml_trace_connect_cb_list);
247   xbt_dynar_free(&ETag_surfxml_trace_connect_cb_list);
248   xbt_dynar_free(&STag_surfxml_random_cb_list);
249   xbt_dynar_free(&ETag_surfxml_random_cb_list);
250   xbt_dynar_free(&STag_surfxml_AS_cb_list);
251   xbt_dynar_free(&ETag_surfxml_AS_cb_list);
252   xbt_dynar_free(&STag_surfxml_ASroute_cb_list);
253   xbt_dynar_free(&ETag_surfxml_ASroute_cb_list);
254   xbt_dynar_free(&STag_surfxml_bypassRoute_cb_list);
255   xbt_dynar_free(&ETag_surfxml_bypassRoute_cb_list);
256   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
257   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
258   xbt_dynar_free(&STag_surfxml_peer_cb_list);
259   xbt_dynar_free(&ETag_surfxml_peer_cb_list);
260   xbt_dynar_free(&STag_surfxml_config_cb_list);
261   xbt_dynar_free(&ETag_surfxml_config_cb_list);
262 }
263
264 /* Stag and Etag parse functions */
265
266 void STag_surfxml_platform(void)
267 {
268   double version = 0.0;
269
270   sscanf(A_surfxml_platform_version, "%lg", &version);
271
272   xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
273       "You're using an ancient XML file.\n"
274       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
275       "instead of MBytes, MFlops and seconds.\n"
276
277       "Use simgrid_update_xml to update your file automatically. "
278       "This program is installed automatically with SimGrid, or "
279       "available in the tools/ directory of the source archive.\n"
280
281       "Please check also out the SURF section of the ChangeLog for "
282       "the 3.1 version for more information. \n"
283
284       "Last, do not forget to also update your values for "
285       "the calls to MSG_task_create (if any).");
286   xbt_assert0((version >= 3.0), "******* BIG FAT WARNING *********\n "
287       "You're using an old XML file.\n"
288       "Use simgrid_update_xml to update your file automatically. "
289       "This program is installed automatically with SimGrid, or "
290       "available in the tools/ directory of the source archive.");
291
292   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
293
294 }
295
296 #define parse_method(type,name) \
297 void type##Tag_surfxml_##name(void) \
298 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
299
300 parse_method(E, platform);
301 parse_method(S, host);
302 parse_method(E, host);
303 parse_method(S, router);
304 parse_method(E, router);
305 parse_method(S, link);
306 parse_method(E, link);
307 parse_method(S, route);
308 parse_method(E, route);
309 parse_method(S, link_ctn);
310 parse_method(E, link_ctn);
311 parse_method(S, process);
312 parse_method(E, process);
313 parse_method(S, argument);
314 parse_method(E, argument);
315 parse_method(S, prop);
316 parse_method(E, prop);
317 parse_method(S, trace);
318 parse_method(E, trace);
319 parse_method(S, trace_connect);
320 parse_method(E, trace_connect);
321 parse_method(S, random);
322 parse_method(E, random);
323 parse_method(S, AS);
324 parse_method(E, AS);
325 parse_method(S, ASroute);
326 parse_method(E, ASroute);
327 parse_method(S, bypassRoute);
328 parse_method(E, bypassRoute);
329 parse_method(S, cluster);
330 parse_method(E, cluster);
331 parse_method(S, peer);
332 parse_method(E, peer);
333 parse_method(S, config);
334 parse_method(E, config);
335
336 /* Open and Close parse file */
337
338 void surf_parse_open(const char *file)
339 {
340   static int warned = 0;        /* warn only once */
341   if (!file) {
342     if (!warned) {
343       XBT_WARN
344           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
345       warned = 1;
346     }
347     return;
348   }
349   surf_file_to_parse = surf_fopen(file, "r");
350   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
351   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
352   surf_parse__switch_to_buffer(surf_input_buffer);
353   surf_parse_lineno = 1;
354 }
355
356 void surf_parse_close(void)
357 {
358   if (surf_file_to_parse) {
359     surf_parse__delete_buffer(surf_input_buffer);
360     fclose(surf_file_to_parse);
361     surf_file_to_parse = NULL; //Must be reset for Bypass
362   }
363 }
364
365 /* Parse Function */
366
367 static int _surf_parse(void)
368 {
369   return surf_parse_lex();
370 }
371
372 int_f_void_t surf_parse = _surf_parse;
373
374 void surf_parse_error(char *msg)
375 {
376   fprintf(stderr, "Parse error on line %d: %s\n", surf_parse_lineno, msg);
377   abort();
378 }
379
380 void surf_parse_get_double(double *value, const char *string)
381 {
382   int ret = 0;
383   ret = sscanf(string, "%lg", value);
384   if (ret != 1)
385     surf_parse_error(bprintf("%s is not a double", string));
386 }
387
388 void surf_parse_get_int(int *value, const char *string)
389 {
390   int ret = 0;
391   ret = sscanf(string, "%d", value);
392   if (ret != 1)
393     surf_parse_error(bprintf("%s is not an integer", string));
394 }
395
396 /* Aux parse functions */
397
398 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
399 {
400   xbt_dynar_push(cb_list, &function);
401 }
402
403 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
404 {
405   xbt_ex_t e;
406   unsigned int it=0;
407   void_f_void_t null_f=NULL;
408
409   TRY {
410     it = xbt_dynar_search(cb_list,&function);
411   } CATCH(e) {
412     if (e.category == not_found_error) {
413       xbt_ex_free(e);
414       xbt_die("Trying to remove a callback that is not here! This should not happen");
415     }
416     RETHROW;
417   }
418
419   xbt_dynar_replace(cb_list, it,&null_f);
420 }
421
422 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
423 {
424   unsigned int iterator;
425   void_f_void_t fun;
426   xbt_dynar_foreach(cb_list, iterator, fun) {
427     if (fun) (*fun) ();
428   }
429 }
430
431 /* Init and free parse data */
432
433 static void init_data(void)
434 {
435   if (!surfxml_bufferstack_stack)
436     surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
437
438   traces_set_list = xbt_dict_new();
439   trace_connect_list_host_avail = xbt_dict_new();
440   trace_connect_list_power = xbt_dict_new();
441   trace_connect_list_link_avail = xbt_dict_new();
442   trace_connect_list_bandwidth = xbt_dict_new();
443   trace_connect_list_latency = xbt_dict_new();
444
445   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_Stag_trace);
446   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_Etag_trace);
447   surfxml_add_callback(STag_surfxml_trace_connect_cb_list,
448                                            &parse_Stag_trace_connect);
449 }
450
451 static void free_data(void)
452 {
453   xbt_dict_free(&trace_connect_list_host_avail);
454   xbt_dict_free(&trace_connect_list_power);
455   xbt_dict_free(&trace_connect_list_link_avail);
456   xbt_dict_free(&trace_connect_list_bandwidth);
457   xbt_dict_free(&trace_connect_list_latency);
458   xbt_dict_free(&traces_set_list);
459   xbt_dict_free(&random_data_list);
460   xbt_dynar_free(&surfxml_bufferstack_stack);
461 }
462
463 /* Here start parse */
464 void parse_platform_file(const char *file)
465 {
466   int parse_status;
467
468   surfxml_buffer_stack_stack_ptr = 1;
469   surfxml_buffer_stack_stack[0] = 0;
470
471   surf_parse_open(file);
472   init_data();
473   parse_status = surf_parse();
474   free_data();
475   surf_parse_close();
476   xbt_assert1(!parse_status, "Parse error in %s", file);
477 }
478
479 /* Prop tag functions */
480
481 void parse_properties(void)
482 {
483   char *value = NULL;
484   if (!current_property_set)
485     current_property_set = xbt_dict_new();      // Maybe, it should be make a error
486   value = xbt_strdup(A_surfxml_prop_value);
487   xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
488 }
489
490 /* Trace management functions */
491
492 static double trace_periodicity = -1.0;
493 static char *trace_file = NULL;
494 static char *trace_id = NULL;
495
496 static void parse_Stag_trace(void)
497 {
498   trace_id = xbt_strdup(A_surfxml_trace_id);
499   trace_file = xbt_strdup(A_surfxml_trace_file);
500   surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
501 }
502
503 static void parse_Etag_trace(void)
504 {
505   tmgr_trace_t trace;
506   if (!trace_file || strcmp(trace_file, "") != 0) {
507     trace = tmgr_trace_new(trace_file);
508   } else {
509     if (strcmp(surfxml_pcdata, "") == 0)
510       trace = NULL;
511     else
512       trace =
513           tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
514                                      trace_periodicity);
515   }
516   xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
517   xbt_free(trace_file);
518   trace_file = NULL;
519   xbt_free(trace_id);
520   trace_id = NULL;
521 }
522
523 static void parse_Stag_trace_connect(void)
524 {
525   xbt_assert2(xbt_dict_get_or_null
526               (traces_set_list, A_surfxml_trace_connect_trace),
527               "Cannot connect trace %s to %s: trace unknown",
528               A_surfxml_trace_connect_trace,
529               A_surfxml_trace_connect_element);
530
531   switch (A_surfxml_trace_connect_kind) {
532   case A_surfxml_trace_connect_kind_HOST_AVAIL:
533     xbt_dict_set(trace_connect_list_host_avail,
534                  A_surfxml_trace_connect_trace,
535                  xbt_strdup(A_surfxml_trace_connect_element), free);
536     break;
537   case A_surfxml_trace_connect_kind_POWER:
538     xbt_dict_set(trace_connect_list_power, A_surfxml_trace_connect_trace,
539                  xbt_strdup(A_surfxml_trace_connect_element), free);
540     break;
541   case A_surfxml_trace_connect_kind_LINK_AVAIL:
542     xbt_dict_set(trace_connect_list_link_avail,
543                  A_surfxml_trace_connect_trace,
544                  xbt_strdup(A_surfxml_trace_connect_element), free);
545     break;
546   case A_surfxml_trace_connect_kind_BANDWIDTH:
547     xbt_dict_set(trace_connect_list_bandwidth,
548                  A_surfxml_trace_connect_trace,
549                  xbt_strdup(A_surfxml_trace_connect_element), free);
550     break;
551   case A_surfxml_trace_connect_kind_LATENCY:
552     xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_connect_trace,
553                  xbt_strdup(A_surfxml_trace_connect_element), free);
554     break;
555   default:
556     xbt_die("Cannot connect trace %s to %s: kind of trace unknown",
557             A_surfxml_trace_connect_trace, A_surfxml_trace_connect_element);
558   }
559 }
560
561 /* Random tag functions */
562
563 double get_cpu_power(const char *power)
564 {
565   double power_scale = 0.0;
566   const char *p, *q;
567   char *generator;
568   random_data_t random = NULL;
569   /* randomness is inserted like this: power="$rand(my_random)" */
570   if (((p = strstr(power, "$rand(")) != NULL)
571       && ((q = strstr(power, ")")) != NULL)) {
572     if (p < q) {
573       generator = xbt_malloc(q - (p + 6) + 1);
574       memcpy(generator, p + 6, q - (p + 6));
575       generator[q - (p + 6)] = '\0';
576       xbt_assert1((random =
577                    xbt_dict_get_or_null(random_data_list, generator)),
578                   "Random generator %s undefined", generator);
579       power_scale = random_generate(random);
580     }
581   } else {
582     surf_parse_get_double(&power_scale, power);
583   }
584   return power_scale;
585 }
586
587 double random_min, random_max, random_mean, random_std_deviation,
588     random_generator;
589 char *random_id;
590
591 static void init_randomness(void)
592 {
593   random_id = A_surfxml_random_id;
594   surf_parse_get_double(&random_min, A_surfxml_random_min);
595   surf_parse_get_double(&random_max, A_surfxml_random_max);
596   surf_parse_get_double(&random_mean, A_surfxml_random_mean);
597   surf_parse_get_double(&random_std_deviation,
598                         A_surfxml_random_std_deviation);
599   random_generator = A_surfxml_random_generator;
600 }
601
602 static void add_randomness(void)
603 {
604   /* If needed aditional properties can be added by using the prop tag */
605   random_data_t random =
606       random_new(random_generator, 0, random_min, random_max, random_mean,
607                  random_std_deviation);
608   xbt_dict_set(random_data_list, random_id, (void *) random,
609                &xbt_free_ref);
610 }
611
612 /**
613  * create CPU resource via CPU Model
614  */
615 void surf_host_create_resource(char *name, double power_peak,
616                                double power_scale,
617                                tmgr_trace_t power_trace, int core,
618                                e_surf_resource_state_t state_initial,
619                                tmgr_trace_t state_trace,
620                                xbt_dict_t cpu_properties)
621 {
622   return surf_cpu_model->extension.cpu.create_resource(name, power_peak,
623                                                        power_scale,
624                                                        power_trace,
625                                                        core,
626                                                        state_initial,
627                                                        state_trace,
628                                                        cpu_properties);
629 }
630
631 /**
632  * create CPU resource via worsktation_ptask_L07 model
633  */
634
635 void surf_wsL07_host_create_resource(char *name, double power_peak,
636                                      double power_scale,
637                                      tmgr_trace_t power_trace,
638                                      e_surf_resource_state_t state_initial,
639                                      tmgr_trace_t state_trace,
640                                      xbt_dict_t cpu_properties)
641 {
642   surf_workstation_model->extension.workstation.cpu_create_resource(name,
643                                                                     power_peak,
644                                                                     power_scale,
645                                                                     power_trace,
646                                                                     state_initial,
647                                                                     state_trace,
648                                                                     cpu_properties);
649 }
650
651 /**
652  * create link resource via network Model
653  */
654 void surf_link_create_resource(char *name,
655                                double bw_initial,
656                                tmgr_trace_t bw_trace,
657                                double lat_initial,
658                                tmgr_trace_t lat_trace,
659                                e_surf_resource_state_t
660                                state_initial,
661                                tmgr_trace_t state_trace,
662                                e_surf_link_sharing_policy_t policy,
663                                xbt_dict_t properties)
664 {
665   return surf_network_model->extension.network.create_resource(name,
666                                                                bw_initial,
667                                                                bw_trace,
668                                                                lat_initial,
669                                                                lat_trace,
670                                                                state_initial,
671                                                                state_trace,
672                                                                policy,
673                                                                properties);
674 }
675
676 /**
677  * create link resource via workstation_ptask_L07 model
678  */
679
680 void surf_wsL07_link_create_resource(char *name,
681                                      double bw_initial,
682                                      tmgr_trace_t bw_trace,
683                                      double lat_initial,
684                                      tmgr_trace_t lat_trace,
685                                      e_surf_resource_state_t
686                                      state_initial,
687                                      tmgr_trace_t state_trace,
688                                      e_surf_link_sharing_policy_t
689                                      policy, xbt_dict_t properties)
690 {
691   return surf_workstation_model->extension.workstation.
692       link_create_resource(name, bw_initial, bw_trace, lat_initial,
693                            lat_trace, state_initial, state_trace, policy,
694                            properties);
695 }
696
697 /**
698  *
699  *init new routing model component
700  */
701
702 void surf_AS_new(const char *AS_id, const char *AS_mode)
703 {
704   routing_AS_init(AS_id, AS_mode);
705 }
706
707 void surf_AS_finalize(const char *AS_id)
708 {
709   routing_AS_end(AS_id);
710 }
711
712 /*
713  * add host to the network element list
714  */
715 void surf_route_add_host(const char *host_id)
716 {
717   routing_add_host(host_id);
718 }
719
720 /**
721  * set route
722  */
723 void surf_routing_add_route(const char *src_id, const char *dst_id,
724                             xbt_dynar_t links_id)
725 {
726   unsigned int i;
727   const char *link_id;
728   routing_set_route(src_id, dst_id);
729   xbt_dynar_foreach(links_id, i, link_id) {
730     routing_add_link(link_id);
731   }
732
733   //store the route
734   routing_store_route();
735 }
736
737 /**
738  * Add Traces
739  */
740 void surf_add_host_traces(void)
741 {
742   return surf_cpu_model->extension.cpu.add_traces();
743 }
744
745 void surf_add_link_traces(void)
746 {
747   return surf_network_model->extension.network.add_traces();
748 }
749
750 void surf_wsL07_add_traces(void)
751 {
752   return surf_workstation_model->extension.workstation.add_traces();
753 }