Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Set level COORD_HOST_LEVEL and COORD_ASR_LEVEL if there are used.
[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   if(!strcmp(A_surfxml_prop_id,"coordinates")){
487           if(!strcmp(A_surfxml_prop_value,"yes") && !COORD_HOST_LEVEL)
488           {
489                     XBT_INFO("Configuration change: Set '%s' to '%s'", A_surfxml_prop_id, A_surfxml_prop_value);
490                         COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
491                         COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
492           }
493           if(strcmp(A_surfxml_prop_value,"yes"))
494                   xbt_die("Setting XML prop coordinates must be \"yes\"");
495   }
496   else{
497           value = xbt_strdup(A_surfxml_prop_value);
498           xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
499   }
500 }
501
502 /* Trace management functions */
503
504 static double trace_periodicity = -1.0;
505 static char *trace_file = NULL;
506 static char *trace_id = NULL;
507
508 static void parse_Stag_trace(void)
509 {
510   trace_id = xbt_strdup(A_surfxml_trace_id);
511   trace_file = xbt_strdup(A_surfxml_trace_file);
512   surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
513 }
514
515 static void parse_Etag_trace(void)
516 {
517   tmgr_trace_t trace;
518   if (!trace_file || strcmp(trace_file, "") != 0) {
519     trace = tmgr_trace_new(trace_file);
520   } else {
521     if (strcmp(surfxml_pcdata, "") == 0)
522       trace = NULL;
523     else
524       trace =
525           tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
526                                      trace_periodicity);
527   }
528   xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
529   xbt_free(trace_file);
530   trace_file = NULL;
531   xbt_free(trace_id);
532   trace_id = NULL;
533 }
534
535 static void parse_Stag_trace_connect(void)
536 {
537   xbt_assert2(xbt_dict_get_or_null
538               (traces_set_list, A_surfxml_trace_connect_trace),
539               "Cannot connect trace %s to %s: trace unknown",
540               A_surfxml_trace_connect_trace,
541               A_surfxml_trace_connect_element);
542
543   switch (A_surfxml_trace_connect_kind) {
544   case A_surfxml_trace_connect_kind_HOST_AVAIL:
545     xbt_dict_set(trace_connect_list_host_avail,
546                  A_surfxml_trace_connect_trace,
547                  xbt_strdup(A_surfxml_trace_connect_element), free);
548     break;
549   case A_surfxml_trace_connect_kind_POWER:
550     xbt_dict_set(trace_connect_list_power, A_surfxml_trace_connect_trace,
551                  xbt_strdup(A_surfxml_trace_connect_element), free);
552     break;
553   case A_surfxml_trace_connect_kind_LINK_AVAIL:
554     xbt_dict_set(trace_connect_list_link_avail,
555                  A_surfxml_trace_connect_trace,
556                  xbt_strdup(A_surfxml_trace_connect_element), free);
557     break;
558   case A_surfxml_trace_connect_kind_BANDWIDTH:
559     xbt_dict_set(trace_connect_list_bandwidth,
560                  A_surfxml_trace_connect_trace,
561                  xbt_strdup(A_surfxml_trace_connect_element), free);
562     break;
563   case A_surfxml_trace_connect_kind_LATENCY:
564     xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_connect_trace,
565                  xbt_strdup(A_surfxml_trace_connect_element), free);
566     break;
567   default:
568     xbt_die("Cannot connect trace %s to %s: kind of trace unknown",
569             A_surfxml_trace_connect_trace, A_surfxml_trace_connect_element);
570   }
571 }
572
573 /* Random tag functions */
574
575 double get_cpu_power(const char *power)
576 {
577   double power_scale = 0.0;
578   const char *p, *q;
579   char *generator;
580   random_data_t random = NULL;
581   /* randomness is inserted like this: power="$rand(my_random)" */
582   if (((p = strstr(power, "$rand(")) != NULL)
583       && ((q = strstr(power, ")")) != NULL)) {
584     if (p < q) {
585       generator = xbt_malloc(q - (p + 6) + 1);
586       memcpy(generator, p + 6, q - (p + 6));
587       generator[q - (p + 6)] = '\0';
588       random = xbt_dict_get_or_null(random_data_list, generator);
589       xbt_assert1(random, "Random generator %s undefined", generator);
590       power_scale = random_generate(random);
591     }
592   } else {
593     surf_parse_get_double(&power_scale, power);
594   }
595   return power_scale;
596 }
597
598 double random_min, random_max, random_mean, random_std_deviation,
599     random_generator;
600 char *random_id;
601
602 static void init_randomness(void)
603 {
604   random_id = A_surfxml_random_id;
605   surf_parse_get_double(&random_min, A_surfxml_random_min);
606   surf_parse_get_double(&random_max, A_surfxml_random_max);
607   surf_parse_get_double(&random_mean, A_surfxml_random_mean);
608   surf_parse_get_double(&random_std_deviation,
609                         A_surfxml_random_std_deviation);
610   random_generator = A_surfxml_random_generator;
611 }
612
613 static void add_randomness(void)
614 {
615   /* If needed aditional properties can be added by using the prop tag */
616   random_data_t random =
617       random_new(random_generator, 0, random_min, random_max, random_mean,
618                  random_std_deviation);
619   xbt_dict_set(random_data_list, random_id, (void *) random,
620                &xbt_free_ref);
621 }
622
623 /**
624  * create CPU resource via CPU Model
625  */
626 void surf_host_create_resource(char *name, double power_peak,
627                                double power_scale,
628                                tmgr_trace_t power_trace, int core,
629                                e_surf_resource_state_t state_initial,
630                                tmgr_trace_t state_trace,
631                                xbt_dict_t cpu_properties)
632 {
633   return surf_cpu_model->extension.cpu.create_resource(name, power_peak,
634                                                        power_scale,
635                                                        power_trace,
636                                                        core,
637                                                        state_initial,
638                                                        state_trace,
639                                                        cpu_properties);
640 }
641
642 /**
643  * create CPU resource via worsktation_ptask_L07 model
644  */
645
646 void surf_wsL07_host_create_resource(char *name, double power_peak,
647                                      double power_scale,
648                                      tmgr_trace_t power_trace,
649                                      e_surf_resource_state_t state_initial,
650                                      tmgr_trace_t state_trace,
651                                      xbt_dict_t cpu_properties)
652 {
653   surf_workstation_model->extension.workstation.cpu_create_resource(name,
654                                                                     power_peak,
655                                                                     power_scale,
656                                                                     power_trace,
657                                                                     state_initial,
658                                                                     state_trace,
659                                                                     cpu_properties);
660 }
661
662 /**
663  * create link resource via network Model
664  */
665 void surf_link_create_resource(char *name,
666                                double bw_initial,
667                                tmgr_trace_t bw_trace,
668                                double lat_initial,
669                                tmgr_trace_t lat_trace,
670                                e_surf_resource_state_t
671                                state_initial,
672                                tmgr_trace_t state_trace,
673                                e_surf_link_sharing_policy_t policy,
674                                xbt_dict_t properties)
675 {
676   return surf_network_model->extension.network.create_resource(name,
677                                                                bw_initial,
678                                                                bw_trace,
679                                                                lat_initial,
680                                                                lat_trace,
681                                                                state_initial,
682                                                                state_trace,
683                                                                policy,
684                                                                properties);
685 }
686
687 /**
688  * create link resource via workstation_ptask_L07 model
689  */
690
691 void surf_wsL07_link_create_resource(char *name,
692                                      double bw_initial,
693                                      tmgr_trace_t bw_trace,
694                                      double lat_initial,
695                                      tmgr_trace_t lat_trace,
696                                      e_surf_resource_state_t
697                                      state_initial,
698                                      tmgr_trace_t state_trace,
699                                      e_surf_link_sharing_policy_t
700                                      policy, xbt_dict_t properties)
701 {
702   return surf_workstation_model->extension.workstation.
703       link_create_resource(name, bw_initial, bw_trace, lat_initial,
704                            lat_trace, state_initial, state_trace, policy,
705                            properties);
706 }
707
708 /**
709  *
710  *init new routing model component
711  */
712
713 void surf_AS_new(const char *AS_id, const char *AS_mode)
714 {
715   routing_AS_init(AS_id, AS_mode);
716 }
717
718 void surf_AS_finalize(const char *AS_id)
719 {
720   routing_AS_end(AS_id);
721 }
722
723 /*
724  * add host to the network element list
725  */
726 void surf_route_add_host(const char *host_id)
727 {
728   routing_add_host(host_id);
729 }
730
731 /**
732  * set route
733  */
734 void surf_routing_add_route(const char *src_id, const char *dst_id,
735                             xbt_dynar_t links_id)
736 {
737   unsigned int i;
738   const char *link_id;
739   routing_set_route(src_id, dst_id);
740   xbt_dynar_foreach(links_id, i, link_id) {
741     routing_add_link(link_id);
742   }
743
744   //store the route
745   routing_store_route();
746 }
747
748 /**
749  * Add Traces
750  */
751 void surf_add_host_traces(void)
752 {
753   return surf_cpu_model->extension.cpu.add_traces();
754 }
755
756 void surf_add_link_traces(void)
757 {
758   return surf_network_model->extension.network.add_traces();
759 }
760
761 void surf_wsL07_add_traces(void)
762 {
763   return surf_workstation_model->extension.workstation.add_traces();
764 }