Logo AND Algorithmique Numérique Distribuée

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