Logo AND Algorithmique Numérique Distribuée

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