Logo AND Algorithmique Numérique Distribuée

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