Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use the function sg_platf_new_linkctn when parsing platform
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. 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 <stdarg.h> /* va_arg */
8
9 #include "xbt/misc.h"
10 #include "xbt/log.h"
11 #include "xbt/str.h"
12 #include "xbt/dict.h"
13 #include "surf/surfxml_parse.h"
14 #include "surf/surf_private.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
17                                 "Logging specific to the SURF parsing module");
18 #undef CLEANUP
19 int ETag_surfxml_include_state(void);
20
21 #include "simgrid_dtd.c"
22
23 char* surf_parsed_filename = NULL; // to locate parse error messages
24
25 /*
26  * Helping functions
27  */
28 void surf_parse_error(const char *fmt, ...) {
29   va_list va;
30   va_start(va,fmt);
31   char *msg = bvprintf(fmt,va);
32   va_end(va);
33   xbt_die("Parse error at %s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
34 }
35 void surf_parse_warn(const char *fmt, ...) {
36   va_list va;
37   va_start(va,fmt);
38   char *msg = bvprintf(fmt,va);
39   va_end(va);
40     XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
41     free(msg);
42 }
43
44 double surf_parse_get_double(const char *string) {
45   double res;
46   int ret = sscanf(string, "%lg", &res);
47   if (ret != 1)
48     surf_parse_error("%s is not a double", string);
49   return res;
50 }
51
52 int surf_parse_get_int(const char *string) {
53   int res;
54   int ret = sscanf(string, "%d", &res);
55   if (ret != 1)
56     surf_parse_error("%s is not an integer", string);
57   return res;
58 }
59
60
61 /*
62  * All the callback lists that can be overridden anywhere.
63  * (this list should probably be reduced to the bare minimum to allow the models to work)
64  */
65
66 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
67 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
69 xbt_dynar_t ETag_surfxml_link_ctn_cb_list = NULL;
70 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
71 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
72 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
73 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
74 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
75 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
76 xbt_dynar_t STag_surfxml_peer_cb_list = NULL;
77 xbt_dynar_t ETag_surfxml_peer_cb_list = NULL;
78 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
79 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
80 xbt_dynar_t STag_surfxml_trace_connect_cb_list = NULL;
81 xbt_dynar_t ETag_surfxml_trace_connect_cb_list = NULL;
82 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
83 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
84 xbt_dynar_t STag_surfxml_ASroute_cb_list = NULL;
85 xbt_dynar_t ETag_surfxml_ASroute_cb_list = NULL;
86 xbt_dynar_t STag_surfxml_bypassRoute_cb_list = NULL;
87 xbt_dynar_t ETag_surfxml_bypassRoute_cb_list = NULL;
88 xbt_dynar_t STag_surfxml_bypassASroute_cb_list = NULL;
89 xbt_dynar_t ETag_surfxml_bypassASroute_cb_list = NULL;
90 xbt_dynar_t STag_surfxml_include_cb_list = NULL;
91 xbt_dynar_t ETag_surfxml_include_cb_list = NULL;
92
93 xbt_dynar_t STag_surfxml_storage_cb_list = NULL;
94 xbt_dynar_t ETag_surfxml_storage_cb_list = NULL;
95 xbt_dynar_t STag_surfxml_storage_type_cb_list = NULL;
96 xbt_dynar_t ETag_surfxml_storage_type_cb_list = NULL;
97 xbt_dynar_t STag_surfxml_mount_cb_list = NULL;
98 xbt_dynar_t ETag_surfxml_mount_cb_list = NULL;
99 xbt_dynar_t STag_surfxml_mstorage_cb_list = NULL;
100 xbt_dynar_t ETag_surfxml_mstorage_cb_list = NULL;
101
102 /* The default current property receiver. Setup in the corresponding opening callbacks. */
103 xbt_dict_t current_property_set = NULL;
104 /* dictionary of random generator data */
105 xbt_dict_t random_data_list = NULL;
106
107 /* Call all the callbacks of a specific SAX event */
108 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
109
110 YY_BUFFER_STATE surf_input_buffer;
111 FILE *surf_file_to_parse = NULL;
112
113 static void init_randomness(void);
114 static void add_randomness(void);
115
116 /*
117  * Stuff relative to storage
118  */
119 void STag_surfxml_storage(void)
120 {
121   XBT_DEBUG("STag_surfxml_storage");
122 }
123 void ETag_surfxml_storage(void)
124 {
125   s_sg_platf_storage_cbarg_t storage;
126   memset(&storage,0,sizeof(storage));
127
128   storage.id = A_surfxml_storage_id;
129   storage.type_id = A_surfxml_storage_typeId;
130   storage.content = A_surfxml_storage_content;
131   sg_platf_new_storage(&storage);
132 }
133 void STag_surfxml_storage_type(void)
134 {
135   XBT_DEBUG("STag_surfxml_storage_type");
136   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
137 }
138 void ETag_surfxml_storage_type(void)
139 {
140   s_sg_platf_storage_type_cbarg_t storage_type;
141   memset(&storage_type,0,sizeof(storage_type));
142
143   storage_type.content = A_surfxml_storage_type_content;
144   storage_type.id = A_surfxml_storage_type_id;
145   storage_type.model = A_surfxml_storage_type_model;
146   storage_type.properties = current_property_set;
147   storage_type.size = surf_parse_get_int(A_surfxml_storage_type_size);
148   sg_platf_new_storage_type(&storage_type);
149   current_property_set = NULL;
150 }
151 void STag_surfxml_mstorage(void)
152 {
153   XBT_DEBUG("STag_surfxml_mstorage");
154 }
155 void ETag_surfxml_mstorage(void)
156 {
157   s_sg_platf_mstorage_cbarg_t mstorage;
158   memset(&mstorage,0,sizeof(mstorage));
159
160   mstorage.name = A_surfxml_mstorage_name;
161   mstorage.type_id = A_surfxml_mstorage_typeId;
162   sg_platf_new_mstorage(&mstorage);
163 }
164 void STag_surfxml_mount(void)
165 {
166   XBT_DEBUG("STag_surfxml_mount");
167 }
168 void ETag_surfxml_mount(void)
169 {
170   s_sg_platf_mount_cbarg_t mount;
171   memset(&mount,0,sizeof(mount));
172
173   mount.name = A_surfxml_mount_name;
174   mount.id = A_surfxml_mount_id;
175   sg_platf_new_mount(&mount);
176 }
177
178 /*
179  * Stuff relative to the <include> tag
180  */
181 static xbt_dynar_t surf_input_buffer_stack = NULL;
182 static xbt_dynar_t surf_file_to_parse_stack = NULL;
183 static xbt_dynar_t surf_parsed_filename_stack = NULL;
184
185 void STag_surfxml_include(void)
186 {
187   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
188   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
189   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
190
191   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
192
193   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
194   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
195               A_surfxml_include_file);
196   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
197   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
198   surf_parse_push_buffer_state(surf_input_buffer);
199
200   fflush(NULL);
201 }
202
203 void ETag_surfxml_include(void) {
204 /* Nothing to do when done with reading the include tag.
205  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
206 }
207
208 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
209  *
210  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
211  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
212  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
213  * error message in that case.
214  *
215  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
216  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
217  * on a line. One day maybe, if the maya allow it.
218  */
219 int ETag_surfxml_include_state(void)
220 {
221   fflush(NULL);
222   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
223
224   if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
225     return 0;
226
227   // Yeah, we were in an <include> Restore state and proceed.
228   fclose(surf_file_to_parse);
229   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
230   surf_parse_pop_buffer_state();
231   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
232
233   // Restore the filename for error messages
234   free(surf_parsed_filename);
235   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
236
237   return 1;
238 }
239
240
241 void surf_parse_init_callbacks(void)
242 {
243     sg_platf_init(); // FIXME: move to a proper place?
244
245     STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
246     ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
247     STag_surfxml_process_cb_list =
248         xbt_dynar_new(sizeof(void_f_void_t), NULL);
249     ETag_surfxml_process_cb_list =
250         xbt_dynar_new(sizeof(void_f_void_t), NULL);
251     STag_surfxml_argument_cb_list =
252         xbt_dynar_new(sizeof(void_f_void_t), NULL);
253     ETag_surfxml_argument_cb_list =
254         xbt_dynar_new(sizeof(void_f_void_t), NULL);
255     STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
256     ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
257     STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
258     ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
259     STag_surfxml_trace_connect_cb_list =
260         xbt_dynar_new(sizeof(void_f_void_t), NULL);
261     ETag_surfxml_trace_connect_cb_list =
262         xbt_dynar_new(sizeof(void_f_void_t), NULL);
263     STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
264     ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
265     STag_surfxml_ASroute_cb_list =
266         xbt_dynar_new(sizeof(void_f_void_t), NULL);
267     ETag_surfxml_ASroute_cb_list =
268         xbt_dynar_new(sizeof(void_f_void_t), NULL);
269     STag_surfxml_bypassRoute_cb_list =
270         xbt_dynar_new(sizeof(void_f_void_t), NULL);
271     ETag_surfxml_bypassRoute_cb_list =
272         xbt_dynar_new(sizeof(void_f_void_t), NULL);
273       STag_surfxml_bypassASroute_cb_list =
274           xbt_dynar_new(sizeof(void_f_void_t), NULL);
275       ETag_surfxml_bypassASroute_cb_list =
276           xbt_dynar_new(sizeof(void_f_void_t), NULL);
277     STag_surfxml_peer_cb_list =
278         xbt_dynar_new(sizeof(void_f_void_t), NULL);
279     ETag_surfxml_peer_cb_list =
280         xbt_dynar_new(sizeof(void_f_void_t), NULL);
281     STag_surfxml_include_cb_list =
282         xbt_dynar_new(sizeof(void_f_void_t), NULL);
283     ETag_surfxml_include_cb_list =
284         xbt_dynar_new(sizeof(void_f_void_t), NULL);
285
286     STag_surfxml_storage_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
287     ETag_surfxml_storage_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
288       STag_surfxml_storage_type_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
289       ETag_surfxml_storage_type_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
290       STag_surfxml_mount_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
291       ETag_surfxml_mount_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
292       STag_surfxml_mstorage_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
293       ETag_surfxml_mstorage_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
294 }
295
296 void surf_parse_reset_callbacks(void)
297 {
298   surf_parse_free_callbacks();
299   surf_parse_init_callbacks();
300 }
301
302 void surf_parse_free_callbacks(void)
303 {
304   sg_platf_exit(); // FIXME: better place?
305
306   xbt_dynar_free(&STag_surfxml_route_cb_list);
307   xbt_dynar_free(&ETag_surfxml_route_cb_list);
308   xbt_dynar_free(&STag_surfxml_process_cb_list);
309   xbt_dynar_free(&ETag_surfxml_process_cb_list);
310   xbt_dynar_free(&STag_surfxml_argument_cb_list);
311   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
312   xbt_dynar_free(&STag_surfxml_prop_cb_list);
313   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
314   xbt_dynar_free(&STag_surfxml_trace_cb_list);
315   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
316   xbt_dynar_free(&STag_surfxml_trace_connect_cb_list);
317   xbt_dynar_free(&ETag_surfxml_trace_connect_cb_list);
318   xbt_dynar_free(&STag_surfxml_random_cb_list);
319   xbt_dynar_free(&ETag_surfxml_random_cb_list);
320   xbt_dynar_free(&STag_surfxml_ASroute_cb_list);
321   xbt_dynar_free(&ETag_surfxml_ASroute_cb_list);
322   xbt_dynar_free(&STag_surfxml_bypassRoute_cb_list);
323   xbt_dynar_free(&ETag_surfxml_bypassRoute_cb_list);
324   xbt_dynar_free(&STag_surfxml_bypassASroute_cb_list);
325   xbt_dynar_free(&ETag_surfxml_bypassASroute_cb_list);
326   xbt_dynar_free(&STag_surfxml_peer_cb_list);
327   xbt_dynar_free(&ETag_surfxml_peer_cb_list);
328   xbt_dynar_free(&STag_surfxml_include_cb_list);
329   xbt_dynar_free(&ETag_surfxml_include_cb_list);
330
331   xbt_dynar_free(&STag_surfxml_storage_cb_list);
332   xbt_dynar_free(&ETag_surfxml_storage_cb_list);
333   xbt_dynar_free(&STag_surfxml_mstorage_cb_list);
334   xbt_dynar_free(&ETag_surfxml_mstorage_cb_list);
335   xbt_dynar_free(&STag_surfxml_mount_cb_list);
336   xbt_dynar_free(&ETag_surfxml_mount_cb_list);
337   xbt_dynar_free(&STag_surfxml_storage_type_cb_list);
338   xbt_dynar_free(&ETag_surfxml_storage_type_cb_list);
339 }
340
341 /* Stag and Etag parse functions */
342 void ETag_surfxml_router(void)  { /* ignored -- do not add content here */ }
343
344 void STag_surfxml_platform(void) {
345   _XBT_GNUC_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
346
347   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
348       "You're using an ancient XML file.\n"
349       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
350       "instead of MBytes, MFlops and seconds.\n"
351
352       "Use simgrid_update_xml to update your file automatically. "
353       "This program is installed automatically with SimGrid, or "
354       "available in the tools/ directory of the source archive.\n"
355
356       "Please check also out the SURF section of the ChangeLog for "
357       "the 3.1 version for more information. \n"
358
359       "Last, do not forget to also update your values for "
360       "the calls to MSG_task_create (if any).");
361   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
362       "You're using an old XML file.\n"
363       "Use simgrid_update_xml to update your file automatically. "
364       "This program is installed automatically with SimGrid, or "
365       "available in the tools/ directory of the source archive.");
366
367   sg_platf_begin();
368 }
369 void ETag_surfxml_platform(void){
370   sg_platf_end();
371 }
372
373 void STag_surfxml_host(void){
374   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
375 }
376
377 void ETag_surfxml_host(void)    {
378   s_sg_platf_host_cbarg_t host;
379   memset(&host,0,sizeof(host));
380
381   host.properties = current_property_set;
382
383   host.id = A_surfxml_host_id;
384   host.power_peak = get_cpu_power(A_surfxml_host_power);
385   host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
386   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
387   host.power_trace = tmgr_trace_new_from_file(A_surfxml_host_availability_file);
388   host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state_file);
389   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
390         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
391   if (A_surfxml_host_state == A_surfxml_host_state_ON)
392     host.initial_state = SURF_RESOURCE_ON;
393   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
394     host.initial_state = SURF_RESOURCE_OFF;
395   host.coord = A_surfxml_host_coordinates;
396
397   sg_platf_new_host(&host);
398   current_property_set = NULL;
399 }
400
401 void STag_surfxml_host_link(void){
402   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host_link_id);
403   s_sg_platf_host_link_cbarg_t host_link;
404   memset(&host_link,0,sizeof(host_link));
405
406   host_link.id = A_surfxml_host_link_id;
407   host_link.link_up = A_surfxml_host_link_up;
408   host_link.link_down = A_surfxml_host_link_down;
409   sg_platf_new_host_link(&host_link);
410 }
411
412 void ETag_surfxml_host_link(void){
413   XBT_DEBUG("End create a Host_link for %s",A_surfxml_host_link_id);
414 }
415
416 void STag_surfxml_router(void){
417   s_sg_platf_router_cbarg_t router;
418   memset(&router, 0, sizeof(router));
419
420   router.id = A_surfxml_router_id;
421   router.coord = A_surfxml_router_coordinates;
422
423   sg_platf_new_router(&router);
424 }
425
426 void STag_surfxml_cluster(void){
427   s_sg_platf_cluster_cbarg_t cluster;
428   memset(&cluster,0,sizeof(cluster));
429   cluster.id = A_surfxml_cluster_id;
430   cluster.prefix = A_surfxml_cluster_prefix;
431   cluster.suffix = A_surfxml_cluster_suffix;
432   cluster.radical = A_surfxml_cluster_radical;
433   cluster.power= surf_parse_get_double(A_surfxml_cluster_power);
434   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
435   cluster.bw =   surf_parse_get_double(A_surfxml_cluster_bw);
436   cluster.lat =  surf_parse_get_double(A_surfxml_cluster_lat);
437   if(strcmp(A_surfxml_cluster_bb_bw,""))
438     cluster.bb_bw = surf_parse_get_double(A_surfxml_cluster_bb_bw);
439   if(strcmp(A_surfxml_cluster_bb_lat,""))
440     cluster.bb_lat = surf_parse_get_double(A_surfxml_cluster_bb_lat);
441   cluster.router_id = A_surfxml_cluster_router_id;
442
443   switch (AX_surfxml_cluster_sharing_policy) {
444   case A_surfxml_cluster_sharing_policy_SHARED:
445     cluster.sharing_policy = SURF_LINK_SHARED;
446     break;
447   case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
448     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
449     break;
450   case A_surfxml_cluster_sharing_policy_FATPIPE:
451     cluster.sharing_policy = SURF_LINK_FATPIPE;
452     break;
453   default:
454     surf_parse_error("Invalid cluster sharing policy for cluster %s",
455                      cluster.id);
456     break;
457   }
458   switch (AX_surfxml_cluster_bb_sharing_policy) {
459   case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
460     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
461     break;
462   case A_surfxml_cluster_bb_sharing_policy_SHARED:
463     cluster.bb_sharing_policy = SURF_LINK_SHARED;
464     break;
465   default:
466     surf_parse_error("Invalid bb sharing policy in cluster %s",
467                      cluster.id);
468     break;
469   }
470
471   cluster.availability_trace = A_surfxml_cluster_availability_file;
472   cluster.state_trace = A_surfxml_cluster_state_file;
473   sg_platf_new_cluster(&cluster);
474 }
475 void ETag_surfxml_cluster(void){
476   /* nothing I can think of */
477 }
478
479 void STag_surfxml_cabinet(void){
480   s_sg_platf_cabinet_cbarg_t cabinet;
481   memset(&cabinet,0,sizeof(cabinet));
482   cabinet.id = A_surfxml_cabinet_id;
483   cabinet.prefix = A_surfxml_cabinet_prefix;
484   cabinet.suffix = A_surfxml_cabinet_suffix;
485   cabinet.power = surf_parse_get_double(A_surfxml_cabinet_power);
486   cabinet.bw = surf_parse_get_double(A_surfxml_cabinet_bw);
487   cabinet.lat = surf_parse_get_double(A_surfxml_cabinet_lat);
488   cabinet.radical = A_surfxml_cabinet_radical;
489
490   sg_platf_new_cabinet(&cabinet);
491 }
492 void ETag_surfxml_cabinet(void){
493   /* nothing I can think of */
494 }
495
496 void STag_surfxml_peer(void){
497   s_sg_platf_peer_cbarg_t peer;
498   memset(&peer,0,sizeof(peer));
499   peer.id = A_surfxml_peer_id;
500   peer.power = surf_parse_get_double(A_surfxml_peer_power);
501   peer.bw_in = surf_parse_get_double(A_surfxml_peer_bw_in);
502   peer.bw_out = surf_parse_get_double(A_surfxml_peer_bw_out);
503   peer.lat = surf_parse_get_double(A_surfxml_peer_lat);
504   peer.coord = A_surfxml_peer_coordinates;
505   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability_file);
506   peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state_file);
507
508   sg_platf_new_peer(&peer);
509 }
510 void ETag_surfxml_peer(void){
511   /* nothing to do here */
512 }
513 void STag_surfxml_link(void){
514   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
515 }
516
517 void STag_surfxml_backbone(void){
518   /* nothing to do here */
519 }
520
521 void ETag_surfxml_link(void){
522   s_sg_platf_link_cbarg_t link;
523   memset(&link,0,sizeof(link));
524
525   link.properties = current_property_set;
526
527   link.id = A_surfxml_link_id;
528   link.bandwidth = surf_parse_get_double(A_surfxml_link_bandwidth);
529   link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth_file);
530   link.latency = surf_parse_get_double(A_surfxml_link_latency);
531   link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency_file);
532
533   switch (A_surfxml_link_state) {
534   case A_surfxml_link_state_ON:
535     link.state = SURF_RESOURCE_ON;
536     break;
537   case A_surfxml_link_state_OFF:
538     link.state = SURF_RESOURCE_OFF;
539     break;
540   default:
541     surf_parse_error("invalid state for link %s", link.id);
542     break;
543   }
544   link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state_file);
545
546   switch (A_surfxml_link_sharing_policy) {
547   case A_surfxml_link_sharing_policy_SHARED:
548     link.policy = SURF_LINK_SHARED;
549     break;
550   case A_surfxml_link_sharing_policy_FATPIPE:
551      link.policy = SURF_LINK_FATPIPE;
552      break;
553   case A_surfxml_link_sharing_policy_FULLDUPLEX:
554      link.policy = SURF_LINK_FULLDUPLEX;
555      break;
556   default:
557     surf_parse_error("Invalid sharing policy in link %s", link.id);
558     break;
559   }
560
561   sg_platf_new_link(&link);
562
563   current_property_set = NULL;
564 }
565
566 void STag_surfxml_link_ctn(void){
567   s_sg_platf_linkctn_cbarg_t linkctn;
568   memset(&linkctn,0,sizeof(linkctn));
569
570   linkctn.id = A_surfxml_link_ctn_id;
571
572   switch (A_surfxml_link_ctn_direction) {
573   case AU_surfxml_link_ctn_direction:
574   case A_surfxml_link_ctn_direction_NONE:
575     linkctn.direction = SURF_LINK_DIRECTION_NONE;
576     break;
577   case A_surfxml_link_ctn_direction_UP:
578     linkctn.direction = SURF_LINK_DIRECTION_UP;
579     break;
580   case A_surfxml_link_ctn_direction_DOWN:
581     linkctn.direction = SURF_LINK_DIRECTION_DOWN;
582     break;
583   }
584   sg_platf_new_linkctn(&linkctn);
585
586 }
587
588 void ETag_surfxml_link_ctn(void){
589  // NOTHING TO DO
590 }
591 void ETag_surfxml_backbone(void){
592   s_sg_platf_link_cbarg_t link;
593   memset(&link,0,sizeof(link));
594
595   link.properties = NULL;
596
597   link.id = A_surfxml_backbone_id;
598   link.bandwidth = surf_parse_get_double(A_surfxml_backbone_bandwidth);
599   link.latency = surf_parse_get_double(A_surfxml_backbone_latency);
600   link.state = SURF_RESOURCE_ON;
601   link.policy = SURF_LINK_SHARED;
602
603   sg_platf_new_link(&link);
604   routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, A_surfxml_backbone_id, SURF_LINK_LEVEL));
605   current_property_set = NULL;
606 }
607
608 void STag_surfxml_route(void){
609   surfxml_call_cb_functions(STag_surfxml_route_cb_list);
610 }
611 void STag_surfxml_process(void){
612   surfxml_call_cb_functions(STag_surfxml_process_cb_list);
613 }
614 void STag_surfxml_argument(void){
615   surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
616 }
617 void STag_surfxml_prop(void){
618   surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
619 }
620 void STag_surfxml_trace(void){
621   surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
622 }
623 void STag_surfxml_trace_connect(void){
624   surfxml_call_cb_functions(STag_surfxml_trace_connect_cb_list);
625 }
626 void STag_surfxml_AS(void){
627   sg_platf_new_AS_begin(A_surfxml_AS_id, (int)A_surfxml_AS_routing);
628 }
629 void ETag_surfxml_AS(void){
630   sg_platf_new_AS_end();
631 }
632 void STag_surfxml_ASroute(void){
633   surfxml_call_cb_functions(STag_surfxml_ASroute_cb_list);
634 }
635 void STag_surfxml_bypassRoute(void){
636   surfxml_call_cb_functions(STag_surfxml_bypassRoute_cb_list);
637 }
638 void STag_surfxml_bypassASroute(void){
639   surfxml_call_cb_functions(STag_surfxml_bypassASroute_cb_list);
640 }
641 void STag_surfxml_config(void){
642   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
643   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
644 }
645 void ETag_surfxml_config(void){
646   xbt_dict_cursor_t cursor = NULL;
647   char *key;
648   char *elem;
649   char *cfg;
650   xbt_dict_foreach(current_property_set, cursor, key, elem) {
651     cfg = bprintf("%s:%s",key,elem);
652     if(xbt_cfg_is_default_value(_surf_cfg_set, key))
653       xbt_cfg_set_parse(_surf_cfg_set, cfg);
654     else
655       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
656     free(cfg);
657   }
658   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
659   xbt_dict_free(&current_property_set);
660 }
661 void STag_surfxml_random(void){
662   surfxml_call_cb_functions(STag_surfxml_random_cb_list);
663 }
664
665 #define parse_method(type,name)                                         \
666   void type##Tag_surfxml_##name(void)                                   \
667   { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }    \
668   void type##Tag_surfxml_##name(void)
669
670 parse_method(E, route);
671 parse_method(E, process);
672 parse_method(E, argument);
673 parse_method(E, prop);
674 parse_method(E, trace);
675 parse_method(E, trace_connect);
676 parse_method(E, random);
677 parse_method(E, ASroute);
678 parse_method(E, bypassRoute);
679 parse_method(E, bypassASroute);
680
681 /* Open and Close parse file */
682
683 void surf_parse_open(const char *file)
684 {
685   static int warned = 0;        /* warn only once */
686   if (!file) {
687     if (!warned) {
688       XBT_WARN
689           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
690               "If it is not what you want, go fix your code.");
691       warned = 1;
692     }
693     return;
694   }
695
696   if (!surf_input_buffer_stack)
697     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
698   if (!surf_file_to_parse_stack)
699     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
700
701   if (!surf_parsed_filename_stack)
702     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
703   surf_parsed_filename = xbt_strdup(file);
704
705   surf_file_to_parse = surf_fopen(file, "r");
706   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
707   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
708   surf_parse__switch_to_buffer(surf_input_buffer);
709   surf_parse_lineno = 1;
710 }
711
712 void surf_parse_close(void)
713 {
714   xbt_dynar_free(&surf_input_buffer_stack);
715   xbt_dynar_free(&surf_file_to_parse_stack);
716   xbt_dynar_free(&surf_parsed_filename_stack);
717
718   free(surf_parsed_filename);
719
720   if (surf_file_to_parse) {
721     surf_parse__delete_buffer(surf_input_buffer);
722     fclose(surf_file_to_parse);
723     surf_file_to_parse = NULL; //Must be reset for Bypass
724   }
725 }
726
727 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
728
729 static int _surf_parse(void) {
730   return surf_parse_lex();
731 }
732 int_f_void_t surf_parse = _surf_parse;
733
734
735 /* Aux parse functions */
736
737 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
738 {
739   xbt_dynar_push(cb_list, &function);
740 }
741
742 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
743 {
744   xbt_ex_t e;
745   unsigned int it=0;
746   void_f_void_t null_f=NULL;
747
748   TRY {
749     it = xbt_dynar_search(cb_list,&function);
750   }
751   CATCH(e) {
752     if (e.category == not_found_error) {
753       xbt_ex_free(e);
754       xbt_die("Trying to remove a callback that is not here! This should not happen");
755     }
756     RETHROW;
757   }
758
759   xbt_dynar_replace(cb_list, it,&null_f);
760 }
761
762 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
763 {
764   unsigned int iterator;
765   void_f_void_t fun;
766   xbt_dynar_foreach(cb_list, iterator, fun) {
767     if (fun) fun();
768   }
769 }
770
771
772 /* Prop tag functions */
773
774 /**
775  * With XML parser
776  */
777 void parse_properties(void)
778 {
779   if (!current_property_set)
780     current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
781
782   xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
783 }
784
785
786 /* Random tag functions */
787
788 double get_cpu_power(const char *power)
789 {
790   double power_scale = 0.0;
791   const char *p, *q;
792   char *generator;
793   random_data_t random = NULL;
794   /* randomness is inserted like this: power="$rand(my_random)" */
795   if (((p = strstr(power, "$rand(")) != NULL)
796       && ((q = strstr(power, ")")) != NULL)) {
797     if (p < q) {
798       generator = xbt_malloc(q - (p + 6) + 1);
799       memcpy(generator, p + 6, q - (p + 6));
800       generator[q - (p + 6)] = '\0';
801       random = xbt_dict_get_or_null(random_data_list, generator);
802       xbt_assert(random, "Random generator %s undefined", generator);
803       power_scale = random_generate(random);
804     }
805   } else {
806     power_scale = surf_parse_get_double(power);
807   }
808   return power_scale;
809 }
810
811 double random_min, random_max, random_mean, random_std_deviation;
812 e_random_generator_t random_generator;
813 char *random_id;
814
815 static void init_randomness(void)
816 {
817   random_id = A_surfxml_random_id;
818   random_min = surf_parse_get_double(A_surfxml_random_min);
819   random_max = surf_parse_get_double(A_surfxml_random_max);
820   random_mean = surf_parse_get_double(A_surfxml_random_mean);
821   random_std_deviation = surf_parse_get_double(A_surfxml_random_std_deviation);
822   switch (A_surfxml_random_generator) {
823   case AU_surfxml_random_generator:
824   case A_surfxml_random_generator_NONE:
825     random_generator = NONE;
826     break;
827   case A_surfxml_random_generator_DRAND48:
828     random_generator = DRAND48;
829     break;
830   case A_surfxml_random_generator_RAND:
831     random_generator = RAND;
832     break;
833   case A_surfxml_random_generator_RNGSTREAM:
834     random_generator = RNGSTREAM;
835     break;
836   default:
837     surf_parse_error("Invalid random generator");
838     break;
839   }
840 }
841
842 static void add_randomness(void)
843 {
844   /* If needed aditional properties can be added by using the prop tag */
845   random_data_t random =
846       random_new(random_generator, 0, random_min, random_max, random_mean,
847                  random_std_deviation);
848   xbt_dict_set(random_data_list, random_id, (void *) random,
849                &xbt_free_ref);
850 }
851