Logo AND Algorithmique Numérique Distribuée

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