Logo AND Algorithmique Numérique Distribuée

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