Logo AND Algorithmique Numérique Distribuée

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