Logo AND Algorithmique Numérique Distribuée

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