Logo AND Algorithmique Numérique Distribuée

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