Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix Network Constant
[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 <errno.h>
8 #include <math.h>
9 #include <stdarg.h> /* va_arg */
10
11 #include "xbt/misc.h"
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14 #include "xbt/dict.h"
15 #include "surf/surfxml_parse.h"
16 #include "surf/surf_private.h"
17 #include "surf/random_mgr.h"
18 #include "simgrid/sg_config.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
21                                 "Logging specific to the SURF parsing module");
22 #undef CLEANUP
23 int ETag_surfxml_include_state(void);
24
25 #include "simgrid_dtd.c"
26
27 char* surf_parsed_filename = NULL; // to locate parse error messages
28
29 xbt_dynar_t parsed_link_list = NULL;   /* temporary store of current list link of a route */
30 extern AS_t current_routing;
31 /*
32  * Helping functions
33  */
34 void surf_parse_error(const char *fmt, ...) {
35   va_list va;
36   va_start(va,fmt);
37   char *msg = bvprintf(fmt,va);
38   va_end(va);
39   xbt_die("Parse error at %s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
40 }
41 void surf_parse_warn(const char *fmt, ...) {
42   va_list va;
43   va_start(va,fmt);
44   char *msg = bvprintf(fmt,va);
45   va_end(va);
46     XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
47     free(msg);
48 }
49
50 double surf_parse_get_double(const char *string) {
51   double res;
52   int ret = sscanf(string, "%lg", &res);
53   if (ret != 1)
54     surf_parse_error("%s is not a double", string);
55   //printf("Parsed double [%lg] %s\n", res, string);  
56   return res;
57 }
58
59 int surf_parse_get_int(const char *string) {
60   int res;
61   int ret = sscanf(string, "%d", &res);
62   if (ret != 1)
63     surf_parse_error("%s is not an integer", string);
64   return res;
65 }
66
67 struct unit_scale {
68   const char *unit;
69   double scale;
70 };
71
72 /* Note: field `unit' for the last element of parametre `units' should be
73  * NULL. */
74 static double surf_parse_get_value_with_unit(const char *string,
75                                              const struct unit_scale *units)
76 {
77   char* ptr;
78   double res;
79   int i;
80   errno = 0;
81   res = strtod(string, &ptr);
82   if (errno == ERANGE)
83     surf_parse_error("value out of range: %s", string);
84   if (ptr == string)
85     surf_parse_error("cannot parse number: %s", string);
86   for (i = 0; units[i].unit != NULL && strcmp(ptr, units[i].unit) != 0; i++) {
87   }
88   if (units[i].unit != NULL)
89     res *= units[i].scale;
90   else
91     surf_parse_error("unknown unit: %s", ptr);
92   return res;
93 }
94
95 double surf_parse_get_time(const char *string)
96 {
97   const struct unit_scale units[] = {
98     { "w",  7 * 24 * 60 * 60 },
99     { "d",  24 * 60 * 60 },
100     { "h",  60 * 60 },
101     { "m",  60 },
102     { "s",  1.0 },
103     { "",   1.0 },              /* default unit is seconds */
104     { "ms", 1e-3 },
105     { "us", 1e-6 },
106     { "ns", 1e-9 },
107     { "ps", 1e-12 },
108     { NULL, 0 }
109   };
110   return surf_parse_get_value_with_unit(string, units);
111 }
112
113 double surf_parse_get_bandwidth(const char *string)
114 {
115   const struct unit_scale units[] = {
116     { "TiBps", pow(1024, 4) },
117     { "GiBps", pow(1024, 3) },
118     { "MiBps", pow(1024, 2) },
119     { "KiBps", 1024 },
120     { "TBps",  1e12 },
121     { "GBps",  1e9 },
122     { "MBps",  1e6 },
123     { "kBps",  1e3 },
124     { "Bps",   1.0 },
125     { "",      1.0 },           /* default unit is bytes per second */
126     { "Tibps", 0.125 * pow(1024, 4) },
127     { "Gibps", 0.125 * pow(1024, 3) },
128     { "Mibps", 0.125 * pow(1024, 2) },
129     { "Kibps", 0.125 * 1024 },
130     { "Tbps",  0.125 * 1e12 },
131     { "Gbps",  0.125 * 1e9 },
132     { "Mbps",  0.125 * 1e6 },
133     { "kbps",  0.125 * 1e3 },
134     { "bps",   0.125 },
135     { NULL,    0 }
136   };
137   return surf_parse_get_value_with_unit(string, units);
138 }
139
140 double surf_parse_get_power(const char *string)
141 {
142   const struct unit_scale units[] = {
143     { "yottaflops", 1e24 },
144     { "Yf",         1e24 },
145     { "zettaflops", 1e21 },
146     { "Zf",         1e21 },
147     { "exaflops",   1e18 },
148     { "Ef",         1e18 },
149     { "petaflops",  1e15 },
150     { "Pf",         1e15 },
151     { "teraflops",  1e12 },
152     { "Tf",         1e12 },
153     { "gigaflops",  1e9 },
154     { "Gf",         1e9 },
155     { "megaflops",  1e6 },
156     { "Mf",         1e6 },
157     { "kiloflops",  1e3 },
158     { "kf",         1e3 },
159     { "flops",      1.0 },
160     { "f",          1.0 },
161     { "",           1.0 },      /* default unit is flops */
162     { NULL,         0 }
163   };
164   return surf_parse_get_value_with_unit(string, units);
165 }
166
167 /*
168  * All the callback lists that can be overridden anywhere.
169  * (this list should probably be reduced to the bare minimum to allow the models to work)
170  */
171
172 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
173
174 /* The default current property receiver. Setup in the corresponding opening callbacks. */
175 xbt_dict_t current_property_set = NULL;
176 xbt_dict_t as_current_property_set = NULL;
177 int AS_TAG = 0;
178 char* as_name_tab[1024];
179 void* as_dict_tab[1024];
180 int as_prop_nb = 0;
181
182
183 /* dictionary of random generator data */
184 xbt_dict_t random_data_list = NULL;
185
186 YY_BUFFER_STATE surf_input_buffer;
187 FILE *surf_file_to_parse = NULL;
188
189 static void init_randomness(void);
190 static void add_randomness(void);
191
192 /*
193  * Stuff relative to storage
194  */
195 void STag_surfxml_storage(void)
196 {
197   AS_TAG = 0;
198   XBT_DEBUG("STag_surfxml_storage");
199   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
200 }
201 void ETag_surfxml_storage(void)
202 {
203   s_sg_platf_storage_cbarg_t storage;
204   memset(&storage,0,sizeof(storage));
205
206   storage.id = A_surfxml_storage_id;
207   storage.type_id = A_surfxml_storage_typeId;
208   storage.content = A_surfxml_storage_content;
209   storage.properties = current_property_set;
210   sg_platf_new_storage(&storage);
211   current_property_set = NULL;
212 }
213 void STag_surfxml_storage___type(void)
214 {
215   AS_TAG = 0;
216   XBT_DEBUG("STag_surfxml_storage___type");
217   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
218 }
219 void ETag_surfxml_storage___type(void)
220 {
221   s_sg_platf_storage_type_cbarg_t storage_type;
222   memset(&storage_type,0,sizeof(storage_type));
223
224   storage_type.content = A_surfxml_storage___type_content;
225   storage_type.id = A_surfxml_storage___type_id;
226   storage_type.model = A_surfxml_storage___type_model;
227   storage_type.properties = current_property_set;
228   storage_type.size = surf_parse_get_int(A_surfxml_storage___type_size);
229   sg_platf_new_storage_type(&storage_type);
230   current_property_set = NULL;
231 }
232 void STag_surfxml_mstorage(void)
233 {
234   XBT_DEBUG("STag_surfxml_mstorage");
235 }
236 void ETag_surfxml_mstorage(void)
237 {
238   s_sg_platf_mstorage_cbarg_t mstorage;
239   memset(&mstorage,0,sizeof(mstorage));
240
241   mstorage.name = A_surfxml_mstorage_name;
242   mstorage.type_id = A_surfxml_mstorage_typeId;
243   sg_platf_new_mstorage(&mstorage);
244 }
245 void STag_surfxml_mount(void)
246 {
247   XBT_DEBUG("STag_surfxml_mount");
248 }
249 void ETag_surfxml_mount(void)
250 {
251   s_sg_platf_mount_cbarg_t mount;
252   memset(&mount,0,sizeof(mount));
253
254   mount.name = A_surfxml_mount_name;
255   mount.id = A_surfxml_mount_id;
256   sg_platf_new_mount(&mount);
257 }
258
259 /*
260  * Stuff relative to the <include> tag
261  */
262 static xbt_dynar_t surf_input_buffer_stack = NULL;
263 static xbt_dynar_t surf_file_to_parse_stack = NULL;
264 static xbt_dynar_t surf_parsed_filename_stack = NULL;
265
266 void STag_surfxml_include(void)
267 {
268   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
269   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
270   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
271
272   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
273
274   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
275   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
276               A_surfxml_include_file);
277   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
278   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
279   surf_parse_push_buffer_state(surf_input_buffer);
280
281   fflush(NULL);
282 }
283
284 void ETag_surfxml_include(void) {
285 /* Nothing to do when done with reading the include tag.
286  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
287 }
288
289 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
290  *
291  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
292  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
293  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
294  * error message in that case.
295  *
296  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
297  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
298  * on a line. One day maybe, if the maya allow it.
299  */
300 int ETag_surfxml_include_state(void)
301 {
302   fflush(NULL);
303   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
304
305   if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
306     return 0;
307
308   // Yeah, we were in an <include> Restore state and proceed.
309   fclose(surf_file_to_parse);
310   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
311   surf_parse_pop_buffer_state();
312   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
313
314   // Restore the filename for error messages
315   free(surf_parsed_filename);
316   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
317
318   return 1;
319 }
320
321
322 void surf_parse_init_callbacks(void)
323 {
324     sg_platf_init();
325 }
326
327 void surf_parse_reset_callbacks(void)
328 {
329   surf_parse_free_callbacks();
330   surf_parse_init_callbacks();
331 }
332
333 void surf_parse_free_callbacks(void)
334 {
335   sg_platf_exit();
336 }
337
338 /* Stag and Etag parse functions */
339
340 void STag_surfxml_platform(void) {
341   _XBT_GNUC_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
342
343   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
344       "You're using an ancient XML file.\n"
345       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
346       "instead of MBytes, MFlops and seconds.\n"
347
348       "Use simgrid_update_xml to update your file automatically. "
349       "This program is installed automatically with SimGrid, or "
350       "available in the tools/ directory of the source archive.\n"
351
352       "Please check also out the SURF section of the ChangeLog for "
353       "the 3.1 version for more information. \n"
354
355       "Last, do not forget to also update your values for "
356       "the calls to MSG_task_create (if any).");
357   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
358       "You're using an old XML file.\n"
359       "Use simgrid_update_xml to update your file automatically. "
360       "This program is installed automatically with SimGrid, or "
361       "available in the tools/ directory of the source archive.");
362
363   sg_platf_begin();
364 }
365 void ETag_surfxml_platform(void){
366   sg_platf_end();
367 }
368
369 void STag_surfxml_host(void){
370   AS_TAG = 0;
371   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
372 }
373
374 void STag_surfxml_prop(void)
375 {
376   if(AS_TAG){
377     if (!as_current_property_set){
378       xbt_assert(as_prop_nb < 1024, "Number of AS property reach the limit!!!");
379       as_current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
380       as_name_tab[as_prop_nb] = xbt_strdup(A_surfxml_AS_id);
381       as_dict_tab[as_prop_nb] = as_current_property_set;
382       XBT_DEBUG("PUSH prop %p for AS '%s'",as_dict_tab[as_prop_nb],as_name_tab[as_prop_nb]);
383       as_prop_nb++;
384     }
385     xbt_dict_set(as_current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
386   }
387   else{
388     if (!current_property_set)
389       current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
390     xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
391   }
392 }
393
394 void ETag_surfxml_host(void)    {
395   s_sg_platf_host_cbarg_t host;
396   memset(&host,0,sizeof(host));
397
398   host.properties = current_property_set;
399
400   host.id = A_surfxml_host_id;
401   host.power_peak = get_cpu_power(A_surfxml_host_power);
402   host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
403   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
404   host.power_trace = tmgr_trace_new_from_file(A_surfxml_host_availability___file);
405   host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state___file);
406   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
407         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
408   if (A_surfxml_host_state == A_surfxml_host_state_ON)
409     host.initial_state = SURF_RESOURCE_ON;
410   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
411     host.initial_state = SURF_RESOURCE_OFF;
412   host.coord = A_surfxml_host_coordinates;
413
414   sg_platf_new_host(&host);
415   current_property_set = NULL;
416 }
417
418 void STag_surfxml_host___link(void){
419   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
420   s_sg_platf_host_link_cbarg_t host_link;
421   memset(&host_link,0,sizeof(host_link));
422
423   host_link.id = A_surfxml_host___link_id;
424   host_link.link_up = A_surfxml_host___link_up;
425   host_link.link_down = A_surfxml_host___link_down;
426   sg_platf_new_host_link(&host_link);
427 }
428
429 void STag_surfxml_router(void){
430   s_sg_platf_router_cbarg_t router;
431   memset(&router, 0, sizeof(router));
432
433   router.id = A_surfxml_router_id;
434   router.coord = A_surfxml_router_coordinates;
435
436   sg_platf_new_router(&router);
437 }
438
439 void ETag_surfxml_cluster(void){
440   s_sg_platf_cluster_cbarg_t cluster;
441   memset(&cluster,0,sizeof(cluster));
442   cluster.properties = current_property_set;
443
444   cluster.id = A_surfxml_cluster_id;
445   cluster.prefix = A_surfxml_cluster_prefix;
446   cluster.suffix = A_surfxml_cluster_suffix;
447   cluster.radical = A_surfxml_cluster_radical;
448   cluster.power = surf_parse_get_power(A_surfxml_cluster_power);
449   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
450   cluster.bw =   surf_parse_get_bandwidth(A_surfxml_cluster_bw);
451   cluster.lat =  surf_parse_get_time(A_surfxml_cluster_lat);
452   if(strcmp(A_surfxml_cluster_bb___bw,""))
453     cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw);
454   if(strcmp(A_surfxml_cluster_bb___lat,""))
455     cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat);
456   if(strcmp(A_surfxml_cluster_limiter___link,""))
457     cluster.limiter_link = surf_parse_get_double(A_surfxml_cluster_limiter___link);
458   if(strcmp(A_surfxml_cluster_loopback___bw,""))
459     cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw);
460   if(strcmp(A_surfxml_cluster_loopback___lat,""))
461     cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat);
462   cluster.router_id = A_surfxml_cluster_router___id;
463
464   switch (AX_surfxml_cluster_sharing___policy) {
465   case A_surfxml_cluster_sharing___policy_SHARED:
466     cluster.sharing_policy = SURF_LINK_SHARED;
467     break;
468   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
469     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
470     break;
471   case A_surfxml_cluster_sharing___policy_FATPIPE:
472     cluster.sharing_policy = SURF_LINK_FATPIPE;
473     break;
474   default:
475     surf_parse_error("Invalid cluster sharing policy for cluster %s",
476                      cluster.id);
477     break;
478   }
479   switch (AX_surfxml_cluster_bb___sharing___policy) {
480   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
481     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
482     break;
483   case A_surfxml_cluster_bb___sharing___policy_SHARED:
484     cluster.bb_sharing_policy = SURF_LINK_SHARED;
485     break;
486   default:
487     surf_parse_error("Invalid bb sharing policy in cluster %s",
488                      cluster.id);
489     break;
490   }
491
492   cluster.availability_trace = A_surfxml_cluster_availability___file;
493   cluster.state_trace = A_surfxml_cluster_state___file;
494   sg_platf_new_cluster(&cluster);
495   
496   current_property_set = NULL;
497 }
498
499 void STag_surfxml_cluster(void){ 
500   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
501 }
502
503 void STag_surfxml_cabinet(void){
504   s_sg_platf_cabinet_cbarg_t cabinet;
505   memset(&cabinet,0,sizeof(cabinet));
506   cabinet.id = A_surfxml_cabinet_id;
507   cabinet.prefix = A_surfxml_cabinet_prefix;
508   cabinet.suffix = A_surfxml_cabinet_suffix;
509   cabinet.power = surf_parse_get_power(A_surfxml_cabinet_power);
510   cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw);
511   cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat);
512   cabinet.radical = A_surfxml_cabinet_radical;
513
514   sg_platf_new_cabinet(&cabinet);
515 }
516
517 void STag_surfxml_peer(void){
518   s_sg_platf_peer_cbarg_t peer;
519   memset(&peer,0,sizeof(peer));
520   peer.id = A_surfxml_peer_id;
521   peer.power = surf_parse_get_power(A_surfxml_peer_power);
522   peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in);
523   peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out);
524   peer.lat = surf_parse_get_time(A_surfxml_peer_lat);
525   peer.coord = A_surfxml_peer_coordinates;
526   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file);
527   peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state___file);
528
529   sg_platf_new_peer(&peer);
530 }
531
532 void STag_surfxml_link(void){
533   AS_TAG = 0;
534   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
535 }
536
537 void ETag_surfxml_link(void){
538   s_sg_platf_link_cbarg_t link;
539   memset(&link,0,sizeof(link));
540
541   link.properties = current_property_set;
542
543   link.id = A_surfxml_link_id;
544   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth);
545   //printf("Link bandwidth [%lg]\n", link.bandwidth);  
546   link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file);
547   link.latency = surf_parse_get_time(A_surfxml_link_latency);
548   //printf("Link latency [%lg]\n", link.latency);  
549   link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency___file);
550
551   switch (A_surfxml_link_state) {
552   case A_surfxml_link_state_ON:
553     link.state = SURF_RESOURCE_ON;
554     break;
555   case A_surfxml_link_state_OFF:
556     link.state = SURF_RESOURCE_OFF;
557     break;
558   default:
559     surf_parse_error("invalid state for link %s", link.id);
560     break;
561   }
562   link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state___file);
563
564   switch (A_surfxml_link_sharing___policy) {
565   case A_surfxml_link_sharing___policy_SHARED:
566     link.policy = SURF_LINK_SHARED;
567     break;
568   case A_surfxml_link_sharing___policy_FATPIPE:
569      link.policy = SURF_LINK_FATPIPE;
570      break;
571   case A_surfxml_link_sharing___policy_FULLDUPLEX:
572      link.policy = SURF_LINK_FULLDUPLEX;
573      break;
574   default:
575     surf_parse_error("Invalid sharing policy in link %s", link.id);
576     break;
577   }
578
579   sg_platf_new_link(&link);
580
581   current_property_set = NULL;
582 }
583
584 void STag_surfxml_link___ctn(void){
585
586   char *link_id;
587   switch (A_surfxml_link___ctn_direction) {
588   case AU_surfxml_link___ctn_direction:
589   case A_surfxml_link___ctn_direction_NONE:
590     link_id = xbt_strdup(A_surfxml_link___ctn_id);
591     break;
592   case A_surfxml_link___ctn_direction_UP:
593     link_id = bprintf("%s_UP", A_surfxml_link___ctn_id);
594     break;
595   case A_surfxml_link___ctn_direction_DOWN:
596     link_id = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
597     break;
598   }
599
600   // FIXME we should push the surf link object but it don't
601   // work because of model rulebased
602   xbt_dynar_push(parsed_link_list, &link_id);
603 }
604
605 void ETag_surfxml_backbone(void){
606   s_sg_platf_link_cbarg_t link;
607   memset(&link,0,sizeof(link));
608
609   link.properties = NULL;
610
611   link.id = A_surfxml_backbone_id;
612   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth);
613   link.latency = surf_parse_get_time(A_surfxml_backbone_latency);
614   link.state = SURF_RESOURCE_ON;
615   link.policy = SURF_LINK_SHARED;
616
617   sg_platf_new_link(&link);
618   routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, A_surfxml_backbone_id, SURF_LINK_LEVEL));
619 }
620
621 void STag_surfxml_route(void){
622   xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
623       "Missing end-points while defining route \"%s\"->\"%s\"",
624       A_surfxml_route_src, A_surfxml_route_dst);
625   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
626 }
627
628 void STag_surfxml_ASroute(void){
629   xbt_assert(strlen(A_surfxml_ASroute_src) > 0
630              || strlen(A_surfxml_ASroute_dst) > 0
631              || strlen(A_surfxml_ASroute_gw___src) > 0
632              || strlen(A_surfxml_ASroute_gw___dst) > 0,
633              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
634              A_surfxml_ASroute_src, A_surfxml_ASroute_dst,
635              A_surfxml_ASroute_gw___src, A_surfxml_ASroute_gw___dst);
636   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
637 }
638
639 void STag_surfxml_bypassRoute(void){
640   xbt_assert(strlen(A_surfxml_bypassRoute_src) > 0
641              || strlen(A_surfxml_bypassRoute_dst) > 0,
642              "Missing end-points while defining bupass route \"%s\"->\"%s\"",
643              A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
644   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
645 }
646
647 void STag_surfxml_bypassASroute(void){
648   xbt_assert(strlen(A_surfxml_bypassASroute_src) > 0
649              || strlen(A_surfxml_bypassASroute_dst) > 0
650              || strlen(A_surfxml_bypassASroute_gw___src) > 0
651              || strlen(A_surfxml_bypassASroute_gw___dst) > 0,
652              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
653              A_surfxml_bypassASroute_src, A_surfxml_bypassASroute_dst,
654              A_surfxml_bypassASroute_gw___src,A_surfxml_bypassASroute_gw___dst);
655   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
656 }
657
658 void ETag_surfxml_route(void){
659   s_sg_platf_route_cbarg_t route;
660   memset(&route,0,sizeof(route));
661
662   route.src = A_surfxml_route_src;
663   route.dst = A_surfxml_route_dst;
664   route.gw_src = NULL;
665   route.gw_dst = NULL;
666   route.link_list = parsed_link_list;
667
668   switch (A_surfxml_route_symmetrical) {
669   case AU_surfxml_route_symmetrical:
670   case A_surfxml_route_symmetrical_YES:
671     route.symmetrical = TRUE;
672     break;
673   case A_surfxml_route_symmetrical_NO:
674     route.symmetrical = FALSE;;
675     break;
676   }
677
678   sg_platf_new_route(&route);
679   parsed_link_list = NULL;
680 }
681
682 void ETag_surfxml_ASroute(void){
683   s_sg_platf_route_cbarg_t ASroute;
684   memset(&ASroute,0,sizeof(ASroute));
685
686   ASroute.src = A_surfxml_ASroute_src;
687   ASroute.dst = A_surfxml_ASroute_dst;
688
689   ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___src);
690   ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___dst);
691
692   ASroute.link_list = parsed_link_list;
693
694   switch (A_surfxml_ASroute_symmetrical) {
695   case AU_surfxml_ASroute_symmetrical:
696   case A_surfxml_ASroute_symmetrical_YES:
697     ASroute.symmetrical = TRUE;
698     break;
699   case A_surfxml_ASroute_symmetrical_NO:
700     ASroute.symmetrical = FALSE;
701     break;
702   }
703
704   sg_platf_new_ASroute(&ASroute);
705   parsed_link_list = NULL;
706 }
707
708 void ETag_surfxml_bypassRoute(void){
709   s_sg_platf_route_cbarg_t route;
710   memset(&route,0,sizeof(route));
711
712   route.src = A_surfxml_bypassRoute_src;
713   route.dst = A_surfxml_bypassRoute_dst;
714   route.gw_src = NULL;
715   route.gw_dst = NULL;
716   route.link_list = parsed_link_list;
717   route.symmetrical = FALSE;
718
719   sg_platf_new_bypassRoute(&route);
720   parsed_link_list = NULL;
721 }
722
723 void ETag_surfxml_bypassASroute(void){
724   s_sg_platf_route_cbarg_t ASroute;
725   memset(&ASroute,0,sizeof(ASroute));
726
727   ASroute.src = A_surfxml_bypassASroute_src;
728   ASroute.dst = A_surfxml_bypassASroute_dst;
729   ASroute.link_list = parsed_link_list;
730   ASroute.symmetrical = FALSE;
731
732   ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___src);
733   ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
734
735   sg_platf_new_bypassASroute(&ASroute);
736   parsed_link_list = NULL;
737 }
738
739 void ETag_surfxml_trace(void){
740   s_sg_platf_trace_cbarg_t trace;
741   memset(&trace,0,sizeof(trace));
742
743   trace.id = A_surfxml_trace_id;
744   trace.file = A_surfxml_trace_file;
745   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
746   trace.pc_data = surfxml_pcdata;
747
748   sg_platf_new_trace(&trace);
749 }
750
751 void STag_surfxml_trace___connect(void){
752   s_sg_platf_trace_connect_cbarg_t trace_connect;
753   memset(&trace_connect,0,sizeof(trace_connect));
754
755   trace_connect.element = A_surfxml_trace___connect_element;
756   trace_connect.trace = A_surfxml_trace___connect_trace;
757
758   switch (A_surfxml_trace___connect_kind) {
759   case AU_surfxml_trace___connect_kind:
760   case A_surfxml_trace___connect_kind_POWER:
761     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_POWER;
762     break;
763   case A_surfxml_trace___connect_kind_BANDWIDTH:
764     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_BANDWIDTH;
765     break;
766   case A_surfxml_trace___connect_kind_HOST___AVAIL:
767     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
768     break;
769   case A_surfxml_trace___connect_kind_LATENCY:
770     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LATENCY;
771     break;
772   case A_surfxml_trace___connect_kind_LINK___AVAIL:
773     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
774     break;
775   }
776   sg_platf_trace_connect(&trace_connect);
777 }
778
779 void STag_surfxml_AS(void){
780   AS_TAG = 1;
781   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
782   AS.id = A_surfxml_AS_id;
783   AS.routing = (int)A_surfxml_AS_routing;
784
785   as_current_property_set = NULL;
786
787   sg_platf_new_AS_begin(&AS);
788 }
789 void ETag_surfxml_AS(void){
790   if(as_prop_nb){
791     char *name = as_name_tab[as_prop_nb-1];
792     xbt_dict_t dict = as_dict_tab[as_prop_nb-1];
793     as_prop_nb--;
794     XBT_DEBUG("POP prop %p for AS '%s'",dict,name);
795     xbt_lib_set(as_router_lib,
796         name,
797       ROUTING_PROP_ASR_LEVEL,
798       dict);
799     xbt_free(name);
800   }
801   sg_platf_new_AS_end();
802 }
803
804 extern int _sg_init_status; /* FIXME: find a proper way to export this at some point */
805
806 void STag_surfxml_config(void){
807   AS_TAG = 0;
808   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
809   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
810   if (_sg_init_status == 2) {
811     surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
812   }
813 }
814 void ETag_surfxml_config(void){
815   xbt_dict_cursor_t cursor = NULL;
816   char *key;
817   char *elem;
818   char *cfg;
819   xbt_dict_foreach(current_property_set, cursor, key, elem) {
820     cfg = bprintf("%s:%s",key,elem);
821     if(xbt_cfg_is_default_value(_sg_cfg_set, key))
822       xbt_cfg_set_parse(_sg_cfg_set, cfg);
823     else
824       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
825     free(cfg);
826   }
827   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
828   xbt_dict_free(&current_property_set);
829   current_property_set = NULL;
830 }
831
832 static int argc;
833 static char **argv;
834
835 void STag_surfxml_process(void){
836   AS_TAG = 0;
837   argc = 1;
838   argv = xbt_new(char *, 1);
839   argv[0] = xbt_strdup(A_surfxml_process_function);
840   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
841 }
842
843 void ETag_surfxml_process(void){
844   s_sg_platf_process_cbarg_t process;
845   memset(&process,0,sizeof(process));
846
847   process.argc = argc;
848   process.argv = (const char **)argv;
849   process.properties = current_property_set;
850   process.host = A_surfxml_process_host;
851   process.function = A_surfxml_process_function;
852   process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
853   process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
854
855   switch (A_surfxml_process_on___failure) {
856   case AU_surfxml_process_on___failure:
857   case A_surfxml_process_on___failure_DIE:
858     process.on_failure =  SURF_PROCESS_ON_FAILURE_DIE;
859     break;
860   case A_surfxml_process_on___failure_RESTART:
861     process.on_failure =  SURF_PROCESS_ON_FAILURE_RESTART;
862     break;
863   }
864
865   sg_platf_new_process(&process);
866   current_property_set = NULL;
867 }
868
869 void STag_surfxml_argument(void){
870   argc++;
871   argv = xbt_realloc(argv, (argc) * sizeof(char *));
872   argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
873 }
874
875 /* ***************************************** */
876 /* TUTORIAL: New TAG                         */
877 void STag_surfxml_gpu(void)
878 {
879   XBT_DEBUG("STag_surfxml_gpu");
880 }
881 void ETag_surfxml_gpu(void)
882 {
883   s_sg_platf_gpu_cbarg_t gpu;
884   memset(&gpu,0,sizeof(gpu));
885
886   gpu.name = A_surfxml_gpu_name;
887
888   sg_platf_new_gpu(&gpu);
889 }
890 /* ***************************************** */
891
892 /* nothing to do in those functions */
893 void ETag_surfxml_prop(void){}
894 void STag_surfxml_random(void){}
895 void ETag_surfxml_random(void){}
896 void ETag_surfxml_trace___connect(void){}
897 void STag_surfxml_trace(void){}
898 void ETag_surfxml_router(void){}
899 void ETag_surfxml_host___link(void){}
900 void ETag_surfxml_cabinet(void){}
901 void ETag_surfxml_peer(void){}
902 void STag_surfxml_backbone(void){}
903 void ETag_surfxml_link___ctn(void){}
904 void ETag_surfxml_argument(void){}
905
906 /* Open and Close parse file */
907
908 void surf_parse_open(const char *file)
909 {
910   static int warned = 0;        /* warn only once */
911   if (!file) {
912     if (!warned) {
913       XBT_WARN
914           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
915               "If it is not what you want, go fix your code.");
916       warned = 1;
917     }
918     return;
919   }
920
921   if (!surf_input_buffer_stack)
922     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
923   if (!surf_file_to_parse_stack)
924     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
925
926   if (!surf_parsed_filename_stack)
927     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
928   surf_parsed_filename = xbt_strdup(file);
929
930   surf_file_to_parse = surf_fopen(file, "r");
931   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
932   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
933   surf_parse__switch_to_buffer(surf_input_buffer);
934   surf_parse_lineno = 1;
935 }
936
937 void surf_parse_close(void)
938 {
939   xbt_dynar_free(&surf_input_buffer_stack);
940   xbt_dynar_free(&surf_file_to_parse_stack);
941   xbt_dynar_free(&surf_parsed_filename_stack);
942
943   free(surf_parsed_filename);
944   surf_parsed_filename = NULL;
945
946   if (surf_file_to_parse) {
947     surf_parse__delete_buffer(surf_input_buffer);
948     fclose(surf_file_to_parse);
949     surf_file_to_parse = NULL; //Must be reset for Bypass
950   }
951 }
952
953 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
954
955 static int _surf_parse(void) {
956   return surf_parse_lex();
957 }
958 int_f_void_t surf_parse = _surf_parse;
959
960
961 /* Prop tag functions */
962
963 /**
964  * With XML parser
965  */
966
967 /* Random tag functions */
968
969 double get_cpu_power(const char *power)
970 {
971   double power_scale = 0.0;
972   const char *p, *q;
973   char *generator;
974   random_data_t random = NULL;
975   /* randomness is inserted like this: power="$rand(my_random)" */
976   if (((p = strstr(power, "$rand(")) != NULL)
977       && ((q = strstr(power, ")")) != NULL)) {
978     if (p < q) {
979       generator = xbt_malloc(q - (p + 6) + 1);
980       memcpy(generator, p + 6, q - (p + 6));
981       generator[q - (p + 6)] = '\0';
982       random = xbt_dict_get_or_null(random_data_list, generator);
983       xbt_assert(random, "Random generator %s undefined", generator);
984       power_scale = random_generate(random);
985     }
986   } else {
987     power_scale = surf_parse_get_power(power);
988   }
989   return power_scale;
990 }
991
992 double random_min, random_max, random_mean, random_std_deviation;
993 e_random_generator_t random_generator;
994 char *random_id;
995
996 static void init_randomness(void)
997 {
998   random_id = A_surfxml_random_id;
999   random_min = surf_parse_get_double(A_surfxml_random_min);
1000   random_max = surf_parse_get_double(A_surfxml_random_max);
1001   random_mean = surf_parse_get_double(A_surfxml_random_mean);
1002   random_std_deviation = surf_parse_get_double(A_surfxml_random_std___deviation);
1003   switch (A_surfxml_random_generator) {
1004   case AU_surfxml_random_generator:
1005   case A_surfxml_random_generator_NONE:
1006     random_generator = NONE;
1007     break;
1008   case A_surfxml_random_generator_DRAND48:
1009     random_generator = DRAND48;
1010     break;
1011   case A_surfxml_random_generator_RAND:
1012     random_generator = RAND;
1013     break;
1014   case A_surfxml_random_generator_RNGSTREAM:
1015     random_generator = RNGSTREAM;
1016     break;
1017   default:
1018     surf_parse_error("Invalid random generator");
1019     break;
1020   }
1021 }
1022
1023 static void add_randomness(void)
1024 {
1025   /* If needed aditional properties can be added by using the prop tag */
1026   random_data_t random =
1027       random_new(random_generator, 0, random_min, random_max, random_mean,
1028                  random_std_deviation);
1029   xbt_dict_set(random_data_list, random_id, (void *) random,
1030                &xbt_free_ref);
1031 }
1032
1033
1034 xbt_dict_t get_as_router_properties(const char* name)
1035 {
1036   return xbt_lib_get_or_null(as_router_lib, name, ROUTING_PROP_ASR_LEVEL);
1037 }
1038