Logo AND Algorithmique Numérique Distribuée

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