Logo AND Algorithmique Numérique Distribuée

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