Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Useless casts.
[simgrid.git] / src / surf / xml / surfxml_sax_cb.cpp
1 /* Copyright (c) 2006-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/kernel/routing/NetPoint.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
10 #include "src/kernel/resource/profile/Profile.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "src/surf/surf_interface.hpp"
13 #include "src/surf/xml/platf_private.hpp"
14 #include "surf/surf.hpp"
15 #include "xbt/file.hpp"
16
17 #include <boost/algorithm/string.hpp>
18 #include <boost/algorithm/string/classification.hpp>
19 #include <boost/algorithm/string/split.hpp>
20 #include <string>
21 #include <tuple>
22 #include <unordered_map>
23 #include <vector>
24
25 #include "simgrid_dtd.c"
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
28
29 static std::string surf_parsed_filename; // Currently parsed file (for the error messages)
30 std::vector<simgrid::kernel::resource::LinkImpl*>
31     parsed_link_list; /* temporary store of current list link of a route */
32
33 /*
34  * Helping functions
35  */
36 void surf_parse_assert(bool cond, const std::string& msg)
37 {
38   if (not cond) {
39     int lineno = surf_parse_lineno;
40     cleanup();
41     XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename.c_str(), lineno, msg.c_str());
42     surf_exit();
43     xbt_die("Exiting now");
44   }
45 }
46
47 void surf_parse_error(const std::string& msg)
48 {
49   int lineno = surf_parse_lineno;
50   cleanup();
51   XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename.c_str(), lineno, msg.c_str());
52   surf_exit();
53   xbt_die("Exiting now");
54 }
55
56 void surf_parse_assert_netpoint(const std::string& hostname, const std::string& pre, const std::string& post)
57 {
58   if (simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(hostname) != nullptr) // found
59     return;
60
61   std::string msg = pre + hostname + post + " Existing netpoints: \n";
62
63   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
64       simgrid::s4u::Engine::get_instance()->get_all_netpoints();
65   std::sort(netpoints.begin(), netpoints.end(),
66             [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
67               return a->get_name() < b->get_name();
68             });
69   bool first = true;
70   for (auto const& np : netpoints) {
71     if (np->is_netzone())
72       continue;
73
74     if (not first)
75       msg += ",";
76     first = false;
77     msg += "'" + np->get_name() + "'";
78     if (msg.length() > 4096) {
79       msg.pop_back(); // remove trailing quote
80       msg += "...(list truncated)......";
81       break;
82     }
83   }
84   surf_parse_error(msg);
85 }
86
87 double surf_parse_get_double(const std::string& s)
88 {
89   try {
90     return std::stod(s);
91   } catch (const std::invalid_argument&) {
92     surf_parse_error(s + " is not a double");
93   }
94 }
95
96 int surf_parse_get_int(const std::string& s)
97 {
98   try {
99     return std::stoi(s);
100   } catch (const std::invalid_argument&) {
101     surf_parse_error(s + " is not a double");
102   }
103 }
104
105 namespace {
106
107 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
108 std::vector<int>* explodesRadical(const std::string& radicals)
109 {
110   std::vector<int>* exploded = new std::vector<int>();
111
112   // Make all hosts
113   std::vector<std::string> radical_elements;
114   boost::split(radical_elements, radicals, boost::is_any_of(","));
115   for (auto const& group : radical_elements) {
116     std::vector<std::string> radical_ends;
117     boost::split(radical_ends, group, boost::is_any_of("-"));
118     int start = surf_parse_get_int(radical_ends.front());
119     int end   = 0;
120
121     switch (radical_ends.size()) {
122       case 1:
123         end = start;
124         break;
125       case 2:
126         end = surf_parse_get_int(radical_ends.back());
127         break;
128       default:
129         surf_parse_error(std::string("Malformed radical: ") + group);
130     }
131     for (int i = start; i <= end; i++)
132       exploded->push_back(i);
133   }
134
135   return exploded;
136 }
137
138 class unit_scale : public std::unordered_map<std::string, double> {
139 public:
140   using std::unordered_map<std::string, double>::unordered_map;
141   // tuples are : <unit, value for unit, base (2 or 10), true if abbreviated>
142   explicit unit_scale(std::initializer_list<std::tuple<const std::string, double, int, bool>> generators);
143 };
144
145 unit_scale::unit_scale(std::initializer_list<std::tuple<const std::string, double, int, bool>> generators)
146 {
147   for (const auto& gen : generators) {
148     const std::string& unit = std::get<0>(gen);
149     double value            = std::get<1>(gen);
150     const int base          = std::get<2>(gen);
151     const bool abbrev       = std::get<3>(gen);
152     double mult;
153     std::vector<std::string> prefixes;
154     switch (base) {
155       case 2:
156         mult     = 1024.0;
157         prefixes = abbrev ? std::vector<std::string>{"Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
158                           : std::vector<std::string>{"kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"};
159         break;
160       case 10:
161         mult     = 1000.0;
162         prefixes = abbrev ? std::vector<std::string>{"k", "M", "G", "T", "P", "E", "Z", "Y"}
163                           : std::vector<std::string>{"kilo", "mega", "giga", "tera", "peta", "exa", "zeta", "yotta"};
164         break;
165       default:
166         THROW_IMPOSSIBLE;
167     }
168     emplace(unit, value);
169     for (const auto& prefix : prefixes) {
170       value *= mult;
171       emplace(prefix + unit, value);
172     }
173   }
174 }
175
176 /* Note: no warning is issued for unit-less values when `name' is empty. */
177 double surf_parse_get_value_with_unit(const char* string, const unit_scale& units, const char* entity_kind,
178                                       const std::string& name, const char* error_msg, const char* default_unit)
179 {
180   char* endptr;
181   errno = 0;
182   double res      = strtod(string, &endptr);
183   const char* ptr = endptr; // for const-correctness
184   if (errno == ERANGE)
185     surf_parse_error(std::string("value out of range: ") + string);
186   if (ptr == string)
187     surf_parse_error(std::string("cannot parse number:") + string);
188   if (ptr[0] == '\0') {
189     // Ok, 0 can be unit-less
190     if (res != 0 && not name.empty())
191       XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s", string, entity_kind, name.c_str(), error_msg);
192     ptr = default_unit;
193   }
194   auto u = units.find(ptr);
195   if (u == units.end())
196     surf_parse_error(std::string("unknown unit: ") + ptr);
197   return res * u->second;
198 }
199 }
200
201 double surf_parse_get_time(const char* string, const char* entity_kind, const std::string& name)
202 {
203   static const unit_scale units{std::make_pair("w", 7 * 24 * 60 * 60),
204                                 std::make_pair("d", 24 * 60 * 60),
205                                 std::make_pair("h", 60 * 60),
206                                 std::make_pair("m", 60),
207                                 std::make_pair("s", 1.0),
208                                 std::make_pair("ms", 1e-3),
209                                 std::make_pair("us", 1e-6),
210                                 std::make_pair("ns", 1e-9),
211                                 std::make_pair("ps", 1e-12)};
212   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
213       "Append 's' to your time to get seconds", "s");
214 }
215
216 double surf_parse_get_size(const char* string, const char* entity_kind, const std::string& name)
217 {
218   static const unit_scale units{std::make_tuple("b", 0.125, 2, true), std::make_tuple("b", 0.125, 10, true),
219                                 std::make_tuple("B", 1.0, 2, true), std::make_tuple("B", 1.0, 10, true)};
220   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
221       "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
222 }
223
224 double surf_parse_get_bandwidth(const char* string, const char* entity_kind, const std::string& name)
225 {
226   static const unit_scale units{std::make_tuple("bps", 0.125, 2, true), std::make_tuple("bps", 0.125, 10, true),
227                                 std::make_tuple("Bps", 1.0, 2, true), std::make_tuple("Bps", 1.0, 10, true)};
228   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
229       "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
230 }
231
232 double surf_parse_get_speed(const char* string, const char* entity_kind, const std::string& name)
233 {
234   static const unit_scale units{std::make_tuple("f", 1.0, 10, true), std::make_tuple("flops", 1.0, 10, false)};
235   return surf_parse_get_value_with_unit(string, units, entity_kind, name,
236       "Append 'f' or 'flops' to your speed to get flop per second", "f");
237 }
238
239 static std::vector<double> surf_parse_get_all_speeds(char* speeds, const char* entity_kind, const std::string& id)
240 {
241
242   std::vector<double> speed_per_pstate;
243
244   if (strchr(speeds, ',') == nullptr){
245     double speed = surf_parse_get_speed(speeds, entity_kind, id);
246     speed_per_pstate.push_back(speed);
247   } else {
248     std::vector<std::string> pstate_list;
249     boost::split(pstate_list, speeds, boost::is_any_of(","));
250     for (auto speed_str : pstate_list) {
251       boost::trim(speed_str);
252       double speed = surf_parse_get_speed(speed_str.c_str(), entity_kind, id);
253       speed_per_pstate.push_back(speed);
254       XBT_DEBUG("Speed value: %f", speed);
255     }
256   }
257   return speed_per_pstate;
258 }
259
260 /*
261  * All the callback lists that can be overridden anywhere.
262  * (this list should probably be reduced to the bare minimum to allow the models to work)
263  */
264
265 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
266
267 /* The default current property receiver. Setup in the corresponding opening callbacks. */
268 std::unordered_map<std::string, std::string>* current_property_set       = nullptr;
269 std::unordered_map<std::string, std::string>* current_model_property_set = nullptr;
270 int ZONE_TAG                            = 0; // Whether we just opened a zone tag (to see what to do with the properties)
271
272 FILE *surf_file_to_parse = nullptr;
273
274 /* Stuff relative to storage */
275 void STag_surfxml_storage()
276 {
277   ZONE_TAG = 0;
278   XBT_DEBUG("STag_surfxml_storage");
279   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
280 }
281
282 void ETag_surfxml_storage()
283 {
284   simgrid::kernel::routing::StorageCreationArgs storage;
285
286   storage.properties   = current_property_set;
287   current_property_set = nullptr;
288
289   storage.id           = A_surfxml_storage_id;
290   storage.type_id      = A_surfxml_storage_typeId;
291   storage.content      = A_surfxml_storage_content;
292   storage.attach       = A_surfxml_storage_attach;
293
294   sg_platf_new_storage(&storage);
295 }
296 void STag_surfxml_storage___type()
297 {
298   ZONE_TAG = 0;
299   XBT_DEBUG("STag_surfxml_storage___type");
300   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
301   xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
302 }
303 void ETag_surfxml_storage___type()
304 {
305   simgrid::kernel::routing::StorageTypeCreationArgs storage_type;
306
307   storage_type.properties = current_property_set;
308   current_property_set    = nullptr;
309
310   storage_type.model_properties = current_model_property_set;
311   current_model_property_set    = nullptr;
312
313   storage_type.content = A_surfxml_storage___type_content;
314   storage_type.id      = A_surfxml_storage___type_id;
315   storage_type.model   = A_surfxml_storage___type_model;
316   storage_type.size =
317       surf_parse_get_size(A_surfxml_storage___type_size, "size of storage type", storage_type.id.c_str());
318   sg_platf_new_storage_type(&storage_type);
319 }
320
321 void STag_surfxml_mount()
322 {
323   XBT_DEBUG("STag_surfxml_mount");
324 }
325
326 void ETag_surfxml_mount()
327 {
328   simgrid::kernel::routing::MountCreationArgs mount;
329
330   mount.name      = A_surfxml_mount_name;
331   mount.storageId = A_surfxml_mount_storageId;
332   sg_platf_new_mount(&mount);
333 }
334
335 void STag_surfxml_include()
336 {
337   xbt_die("<include> tag was removed in SimGrid v3.18. Please stop using it now.");
338 }
339
340 void ETag_surfxml_include()
341 {
342   /* Won't happen since <include> is now removed since v3.18. */
343 }
344
345 /* Stag and Etag parse functions */
346 void STag_surfxml_platform() {
347   XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
348
349   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
350       "You're using an ancient XML file.\n"
351       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
352       "instead of MBytes, MFlops and seconds.\n"
353
354       "Use simgrid_update_xml to update your file automatically. "
355       "This program is installed automatically with SimGrid, or "
356       "available in the tools/ directory of the source archive.\n"
357
358       "Please check also out the SURF section of the ChangeLog for "
359       "the 3.1 version for more information. \n"
360
361       "Last, do not forget to also update your values for "
362       "the calls to MSG_task_create (if any).");
363   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
364       "You're using an old XML file.\n"
365       "Use simgrid_update_xml to update your file automatically. "
366       "This program is installed automatically with SimGrid, or "
367       "available in the tools/ directory of the source archive.");
368   xbt_assert((version >= 4.0),
369              "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
370              "Changes introduced in SimGrid 3.13:\n"
371              "  - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
372              "  - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
373              "  - DOCTYPE now point to the rignt URL.\n"
374              "  - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
375              "\n\n"
376              "Use simgrid_update_xml to update your file automatically. "
377              "This program is installed automatically with SimGrid, or "
378              "available in the tools/ directory of the source archive.",
379              surf_parsed_filename.c_str(), version);
380   if (version < 4.1) {
381     XBT_INFO("You're using a v%.1f XML file (%s) while the current standard is v4.1 "
382              "That's fine, the new version is backward compatible. \n\n"
383              "Use simgrid_update_xml to update your file automatically to get rid of this warning. "
384              "This program is installed automatically with SimGrid, or "
385              "available in the tools/ directory of the source archive.",
386              version, surf_parsed_filename.c_str());
387   }
388   xbt_assert(version <= 4.1,
389              "******* FILE %s COMES FROM THE FUTURE (v:%.1f) *********\n "
390              "The most recent formalism that this version of SimGrid understands is v4.1.\n"
391              "Please update your code, or use another, more adapted, file.",
392              surf_parsed_filename.c_str(), version);
393 }
394 void ETag_surfxml_platform(){
395   simgrid::s4u::on_platform_created();
396 }
397
398 void STag_surfxml_host(){
399   ZONE_TAG = 0;
400   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
401 }
402
403 void STag_surfxml_prop()
404 {
405   if (ZONE_TAG) { // We need to retrieve the most recently opened zone
406     XBT_DEBUG("Set zone property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
407     simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(A_surfxml_zone_id);
408
409     netzone->set_property(std::string(A_surfxml_prop_id), A_surfxml_prop_value);
410   } else {
411     if (not current_property_set)
412       current_property_set = new std::unordered_map<std::string, std::string>; // Maybe, it should raise an error
413     current_property_set->insert({A_surfxml_prop_id, A_surfxml_prop_value});
414     XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
415               current_property_set);
416   }
417 }
418
419 void ETag_surfxml_host()    {
420   simgrid::kernel::routing::HostCreationArgs host;
421
422   host.properties = current_property_set;
423   current_property_set = nullptr;
424
425   host.id = A_surfxml_host_id;
426
427   host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
428
429   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
430   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
431
432   host.speed_trace = nullptr;
433   if (A_surfxml_host_availability___file[0] != '\0') {
434     XBT_WARN("The availability_file attribute in <host> is now deprecated. Please, use 'speed_file' instead.");
435     host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_availability___file);
436   }
437   if (A_surfxml_host_speed___file[0] != '\0')
438     host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_speed___file);
439   host.state_trace = A_surfxml_host_state___file[0]
440                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_host_state___file)
441                          : nullptr;
442   host.pstate      = surf_parse_get_int(A_surfxml_host_pstate);
443   host.coord       = A_surfxml_host_coordinates;
444
445   sg_platf_new_host(&host);
446 }
447
448 void STag_surfxml_host___link(){
449   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
450   simgrid::kernel::routing::HostLinkCreationArgs host_link;
451
452   host_link.id        = A_surfxml_host___link_id;
453   host_link.link_up   = A_surfxml_host___link_up;
454   host_link.link_down = A_surfxml_host___link_down;
455   sg_platf_new_hostlink(&host_link);
456 }
457
458 void STag_surfxml_router(){
459   sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
460 }
461
462 void ETag_surfxml_cluster(){
463   simgrid::kernel::routing::ClusterCreationArgs cluster;
464   cluster.properties   = current_property_set;
465   current_property_set = nullptr;
466
467   cluster.id          = A_surfxml_cluster_id;
468   cluster.prefix      = A_surfxml_cluster_prefix;
469   cluster.suffix      = A_surfxml_cluster_suffix;
470   cluster.radicals    = explodesRadical(A_surfxml_cluster_radical);
471   cluster.speeds      = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
472   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
473   cluster.bw          = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
474   cluster.lat         = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
475   if(strcmp(A_surfxml_cluster_bb___bw,""))
476     cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
477   if(strcmp(A_surfxml_cluster_bb___lat,""))
478     cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
479   if(strcmp(A_surfxml_cluster_limiter___link,""))
480     cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
481   if(strcmp(A_surfxml_cluster_loopback___bw,""))
482     cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
483   if(strcmp(A_surfxml_cluster_loopback___lat,""))
484     cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
485
486   switch(AX_surfxml_cluster_topology){
487   case A_surfxml_cluster_topology_FLAT:
488     cluster.topology = simgrid::kernel::routing::ClusterTopology::FLAT;
489     break;
490   case A_surfxml_cluster_topology_TORUS:
491     cluster.topology = simgrid::kernel::routing::ClusterTopology::TORUS;
492     break;
493   case A_surfxml_cluster_topology_FAT___TREE:
494     cluster.topology = simgrid::kernel::routing::ClusterTopology::FAT_TREE;
495     break;
496   case A_surfxml_cluster_topology_DRAGONFLY:
497     cluster.topology = simgrid::kernel::routing::ClusterTopology::DRAGONFLY;
498     break;
499   default:
500     surf_parse_error(std::string("Invalid cluster topology for cluster ") + cluster.id);
501   }
502   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
503   cluster.router_id = A_surfxml_cluster_router___id;
504
505   switch (AX_surfxml_cluster_sharing___policy) {
506   case A_surfxml_cluster_sharing___policy_SHARED:
507     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
508     break;
509   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
510     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
511     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
512     break;
513   case A_surfxml_cluster_sharing___policy_SPLITDUPLEX:
514     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
515     break;
516   case A_surfxml_cluster_sharing___policy_FATPIPE:
517     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
518     break;
519   default:
520     surf_parse_error(std::string("Invalid cluster sharing policy for cluster ") + cluster.id);
521   }
522   switch (AX_surfxml_cluster_bb___sharing___policy) {
523   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
524     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
525     break;
526   case A_surfxml_cluster_bb___sharing___policy_SHARED:
527     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
528     break;
529   default:
530     surf_parse_error(std::string("Invalid bb sharing policy in cluster ") + cluster.id);
531   }
532
533   sg_platf_new_cluster(&cluster);
534 }
535
536 void STag_surfxml_cluster(){
537   ZONE_TAG = 0;
538   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
539 }
540
541 void STag_surfxml_cabinet(){
542   simgrid::kernel::routing::CabinetCreationArgs cabinet;
543   cabinet.id      = A_surfxml_cabinet_id;
544   cabinet.prefix  = A_surfxml_cabinet_prefix;
545   cabinet.suffix  = A_surfxml_cabinet_suffix;
546   cabinet.speed    = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id.c_str());
547   cabinet.bw       = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id.c_str());
548   cabinet.lat      = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id.c_str());
549   cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
550
551   sg_platf_new_cabinet(&cabinet);
552 }
553
554 void STag_surfxml_peer(){
555   simgrid::kernel::routing::PeerCreationArgs peer;
556
557   peer.id          = std::string(A_surfxml_peer_id);
558   peer.speed       = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id.c_str());
559   peer.bw_in       = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id.c_str());
560   peer.bw_out      = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id.c_str());
561   peer.coord       = A_surfxml_peer_coordinates;
562   peer.speed_trace = nullptr;
563   if (A_surfxml_peer_availability___file[0] != '\0') {
564     XBT_WARN("The availability_file attribute in <peer> is now deprecated. Please, use 'speed_file' instead.");
565     peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_availability___file);
566   }
567   if (A_surfxml_peer_speed___file[0] != '\0')
568     peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_speed___file);
569   peer.state_trace = A_surfxml_peer_state___file[0]
570                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_state___file)
571                          : nullptr;
572
573   if (A_surfxml_peer_lat[0] != '\0')
574     XBT_WARN("The latency attribute in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
575              A_surfxml_peer_lat);
576
577   sg_platf_new_peer(&peer);
578 }
579
580 void STag_surfxml_link(){
581   ZONE_TAG = 0;
582   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
583 }
584
585 void ETag_surfxml_link(){
586   simgrid::kernel::routing::LinkCreationArgs link;
587
588   link.properties          = current_property_set;
589   current_property_set     = nullptr;
590
591   link.id                  = std::string(A_surfxml_link_id);
592   link.bandwidth           = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
593   link.bandwidth_trace     = A_surfxml_link_bandwidth___file[0]
594                              ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_bandwidth___file)
595                              : nullptr;
596   link.latency             = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
597   link.latency_trace       = A_surfxml_link_latency___file[0]
598                            ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_latency___file)
599                            : nullptr;
600   link.state_trace = A_surfxml_link_state___file[0]
601                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_state___file)
602                          : nullptr;
603
604   switch (A_surfxml_link_sharing___policy) {
605   case A_surfxml_link_sharing___policy_SHARED:
606     link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
607     break;
608   case A_surfxml_link_sharing___policy_FATPIPE:
609     link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
610     break;
611   case A_surfxml_link_sharing___policy_FULLDUPLEX:
612     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
613     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
614     break;
615   case A_surfxml_link_sharing___policy_SPLITDUPLEX:
616     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
617     break;
618   default:
619     surf_parse_error(std::string("Invalid sharing policy in link ") + link.id);
620   }
621
622   sg_platf_new_link(&link);
623 }
624
625 void STag_surfxml_link___ctn()
626 {
627   simgrid::kernel::resource::LinkImpl* link = nullptr;
628   switch (A_surfxml_link___ctn_direction) {
629   case AU_surfxml_link___ctn_direction:
630   case A_surfxml_link___ctn_direction_NONE:
631     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id))->get_impl();
632     break;
633   case A_surfxml_link___ctn_direction_UP:
634     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_UP")->get_impl();
635     break;
636   case A_surfxml_link___ctn_direction_DOWN:
637     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_DOWN")->get_impl();
638     break;
639   default:
640     surf_parse_error(std::string("Invalid direction for link ") + A_surfxml_link___ctn_id);
641   }
642
643   const char* dirname = "";
644   switch (A_surfxml_link___ctn_direction) {
645     case A_surfxml_link___ctn_direction_UP:
646       dirname = " (upward)";
647       break;
648     case A_surfxml_link___ctn_direction_DOWN:
649       dirname = " (downward)";
650       break;
651     default:
652       dirname = "";
653   }
654   surf_parse_assert(link != nullptr, std::string("No such link: '") + A_surfxml_link___ctn_id + "'" + dirname);
655   parsed_link_list.push_back(link);
656 }
657
658 void ETag_surfxml_backbone(){
659   simgrid::kernel::routing::LinkCreationArgs link;
660
661   link.properties = nullptr;
662   link.id = std::string(A_surfxml_backbone_id);
663   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
664   link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
665   link.policy     = simgrid::s4u::Link::SharingPolicy::SHARED;
666
667   sg_platf_new_link(&link);
668   routing_cluster_add_backbone(simgrid::s4u::Link::by_name(std::string(A_surfxml_backbone_id))->get_impl());
669 }
670
671 void STag_surfxml_route(){
672   surf_parse_assert_netpoint(A_surfxml_route_src, "Route src='", "' does name a node.");
673   surf_parse_assert_netpoint(A_surfxml_route_dst, "Route dst='", "' does name a node.");
674 }
675
676 void STag_surfxml_ASroute(){
677   surf_parse_assert_netpoint(A_surfxml_ASroute_src, "ASroute src='", "' does name a node.");
678   surf_parse_assert_netpoint(A_surfxml_ASroute_dst, "ASroute dst='", "' does name a node.");
679
680   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___src, "ASroute gw_src='", "' does name a node.");
681   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___dst, "ASroute gw_dst='", "' does name a node.");
682 }
683 void STag_surfxml_zoneRoute(){
684   surf_parse_assert_netpoint(A_surfxml_zoneRoute_src, "zoneRoute src='", "' does name a node.");
685   surf_parse_assert_netpoint(A_surfxml_zoneRoute_dst, "zoneRoute dst='", "' does name a node.");
686   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___src, "zoneRoute gw_src='", "' does name a node.");
687   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___dst, "zoneRoute gw_dst='", "' does name a node.");
688 }
689
690 void STag_surfxml_bypassRoute(){
691   surf_parse_assert_netpoint(A_surfxml_bypassRoute_src, "bypassRoute src='", "' does name a node.");
692   surf_parse_assert_netpoint(A_surfxml_bypassRoute_dst, "bypassRoute dst='", "' does name a node.");
693 }
694
695 void STag_surfxml_bypassASroute(){
696   surf_parse_assert_netpoint(A_surfxml_bypassASroute_src, "bypassASroute src='", "' does name a node.");
697   surf_parse_assert_netpoint(A_surfxml_bypassASroute_dst, "bypassASroute dst='", "' does name a node.");
698   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___src, "bypassASroute gw_src='", "' does name a node.");
699   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___dst, "bypassASroute gw_dst='", "' does name a node.");
700 }
701 void STag_surfxml_bypassZoneRoute(){
702   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_src, "bypassZoneRoute src='", "' does name a node.");
703   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_dst, "bypassZoneRoute dst='", "' does name a node.");
704   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___src, "bypassZoneRoute gw_src='", "' does name a node.");
705   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___dst, "bypassZoneRoute gw_dst='", "' does name a node.");
706 }
707
708 void ETag_surfxml_route(){
709   simgrid::kernel::routing::RouteCreationArgs route;
710
711   route.src         = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
712   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
713   route.gw_src    = nullptr;
714   route.gw_dst    = nullptr;
715   route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
716
717   route.link_list.swap(parsed_link_list);
718
719   sg_platf_new_route(&route);
720 }
721
722 void ETag_surfxml_ASroute()
723 {
724   AX_surfxml_zoneRoute_src = AX_surfxml_ASroute_src;
725   AX_surfxml_zoneRoute_dst = AX_surfxml_ASroute_dst;
726   AX_surfxml_zoneRoute_gw___src = AX_surfxml_ASroute_gw___src;
727   AX_surfxml_zoneRoute_gw___dst = AX_surfxml_ASroute_gw___dst;
728   AX_surfxml_zoneRoute_symmetrical = (AT_surfxml_zoneRoute_symmetrical)AX_surfxml_ASroute_symmetrical;
729   ETag_surfxml_zoneRoute();
730 }
731 void ETag_surfxml_zoneRoute()
732 {
733   simgrid::kernel::routing::RouteCreationArgs ASroute;
734
735   ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src); // tested to not be nullptr in start tag
736   ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst); // tested to not be nullptr in start tag
737
738   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src); // tested to not be nullptr in start tag
739   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst); // tested to not be nullptr in start tag
740
741   ASroute.link_list.swap(parsed_link_list);
742
743   switch (A_surfxml_zoneRoute_symmetrical) {
744   case AU_surfxml_zoneRoute_symmetrical:
745   case A_surfxml_zoneRoute_symmetrical_YES:
746     ASroute.symmetrical = true;
747     break;
748   case A_surfxml_zoneRoute_symmetrical_NO:
749     ASroute.symmetrical = false;
750     break;
751   default:
752     THROW_IMPOSSIBLE;
753   }
754
755   sg_platf_new_route(&ASroute);
756 }
757
758 void ETag_surfxml_bypassRoute(){
759   simgrid::kernel::routing::RouteCreationArgs route;
760
761   route.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
762   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
763   route.gw_src = nullptr;
764   route.gw_dst = nullptr;
765   route.symmetrical = false;
766
767   route.link_list.swap(parsed_link_list);
768
769   sg_platf_new_bypassRoute(&route);
770 }
771
772 void ETag_surfxml_bypassASroute()
773 {
774   AX_surfxml_bypassZoneRoute_src = AX_surfxml_bypassASroute_src;
775   AX_surfxml_bypassZoneRoute_dst = AX_surfxml_bypassASroute_dst;
776   AX_surfxml_bypassZoneRoute_gw___src = AX_surfxml_bypassASroute_gw___src;
777   AX_surfxml_bypassZoneRoute_gw___dst = AX_surfxml_bypassASroute_gw___dst;
778   ETag_surfxml_bypassZoneRoute();
779 }
780 void ETag_surfxml_bypassZoneRoute()
781 {
782   simgrid::kernel::routing::RouteCreationArgs ASroute;
783
784   ASroute.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src);
785   ASroute.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst);
786   ASroute.link_list.swap(parsed_link_list);
787
788   ASroute.symmetrical = false;
789
790   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src);
791   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst);
792
793   sg_platf_new_bypassRoute(&ASroute);
794 }
795
796 void ETag_surfxml_trace(){
797   simgrid::kernel::routing::ProfileCreationArgs trace;
798
799   trace.id = A_surfxml_trace_id;
800   trace.file = A_surfxml_trace_file;
801   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
802   trace.pc_data = surfxml_pcdata;
803
804   sg_platf_new_trace(&trace);
805 }
806
807 void STag_surfxml_trace___connect()
808 {
809   simgrid::kernel::routing::TraceConnectCreationArgs trace_connect;
810
811   trace_connect.element = A_surfxml_trace___connect_element;
812   trace_connect.trace = A_surfxml_trace___connect_trace;
813
814   switch (A_surfxml_trace___connect_kind) {
815   case AU_surfxml_trace___connect_kind:
816   case A_surfxml_trace___connect_kind_SPEED:
817     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::SPEED;
818     break;
819   case A_surfxml_trace___connect_kind_BANDWIDTH:
820     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::BANDWIDTH;
821     break;
822   case A_surfxml_trace___connect_kind_HOST___AVAIL:
823     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL;
824     break;
825   case A_surfxml_trace___connect_kind_LATENCY:
826     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LATENCY;
827     break;
828   case A_surfxml_trace___connect_kind_LINK___AVAIL:
829     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL;
830     break;
831   default:
832     surf_parse_error("Invalid trace kind");
833   }
834   sg_platf_trace_connect(&trace_connect);
835 }
836
837 void STag_surfxml_AS()
838 {
839   AX_surfxml_zone_id = AX_surfxml_AS_id;
840   AX_surfxml_zone_routing = (AT_surfxml_zone_routing)AX_surfxml_AS_routing;
841   STag_surfxml_zone();
842 }
843
844 void ETag_surfxml_AS()
845 {
846   ETag_surfxml_zone();
847 }
848
849 void STag_surfxml_zone()
850 {
851   ZONE_TAG                 = 1;
852   simgrid::kernel::routing::ZoneCreationArgs zone;
853   zone.id      = A_surfxml_zone_id;
854   zone.routing = static_cast<int>(A_surfxml_zone_routing);
855
856   sg_platf_new_Zone_begin(&zone);
857 }
858
859 void ETag_surfxml_zone()
860 {
861   sg_platf_new_Zone_seal();
862 }
863
864 void STag_surfxml_config()
865 {
866   ZONE_TAG = 0;
867   xbt_assert(current_property_set == nullptr,
868              "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
869   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
870   if (_sg_cfg_init_status == 2) {
871     surf_parse_error("All <config> tags must be given before any platform elements (such as <zone>, <host>, <cluster>, "
872                      "<link>, etc).");
873   }
874 }
875
876 void ETag_surfxml_config()
877 {
878   // Sort config elements before applying.
879   // That's a little waste of time, but not doing so would break the tests
880   std::vector<std::string> keys;
881   for (auto const& kv : *current_property_set) {
882     keys.push_back(kv.first);
883   }
884   std::sort(keys.begin(), keys.end());
885   for (std::string key : keys) {
886     if (simgrid::config::is_default(key.c_str())) {
887       std::string cfg = key + ":" + current_property_set->at(key);
888       simgrid::config::set_parse(std::move(cfg));
889     } else
890       XBT_INFO("The custom configuration '%s' is already defined by user!", key.c_str());
891   }
892   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
893
894   delete current_property_set;
895   current_property_set = nullptr;
896 }
897
898 static std::vector<std::string> arguments;
899
900 void STag_surfxml_process()
901 {
902   AX_surfxml_actor_function = AX_surfxml_process_function;
903   STag_surfxml_actor();
904 }
905
906 void STag_surfxml_actor()
907 {
908   ZONE_TAG  = 0;
909   arguments.assign(1, A_surfxml_actor_function);
910   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
911 }
912
913 void ETag_surfxml_process()
914 {
915   AX_surfxml_actor_host = AX_surfxml_process_host;
916   AX_surfxml_actor_function = AX_surfxml_process_function;
917   AX_surfxml_actor_start___time = AX_surfxml_process_start___time;
918   AX_surfxml_actor_kill___time = AX_surfxml_process_kill___time;
919   AX_surfxml_actor_on___failure = (AT_surfxml_actor_on___failure)AX_surfxml_process_on___failure;
920   ETag_surfxml_actor();
921 }
922
923 void ETag_surfxml_actor()
924 {
925   simgrid::kernel::routing::ActorCreationArgs actor;
926
927   actor.properties     = current_property_set;
928   current_property_set = nullptr;
929
930   actor.args.swap(arguments);
931   actor.host       = A_surfxml_actor_host;
932   actor.function   = A_surfxml_actor_function;
933   actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time);
934   actor.kill_time  = surf_parse_get_double(A_surfxml_actor_kill___time);
935
936   switch (A_surfxml_actor_on___failure) {
937   case AU_surfxml_actor_on___failure:
938   case A_surfxml_actor_on___failure_DIE:
939     actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
940     break;
941   case A_surfxml_actor_on___failure_RESTART:
942     actor.on_failure = simgrid::kernel::routing::ActorOnFailure::RESTART;
943     break;
944   default:
945     surf_parse_error("Invalid on failure behavior");
946   }
947
948   sg_platf_new_actor(&actor);
949 }
950
951 void STag_surfxml_argument(){
952   arguments.push_back(A_surfxml_argument_value);
953 }
954
955 void STag_surfxml_model___prop(){
956   if (not current_model_property_set)
957     current_model_property_set = new std::unordered_map<std::string, std::string>();
958
959   current_model_property_set->insert({A_surfxml_model___prop_id, A_surfxml_model___prop_value});
960 }
961
962 void ETag_surfxml_prop(){/* Nothing to do */}
963 void STag_surfxml_random(){/* Nothing to do */}
964 void ETag_surfxml_random(){/* Nothing to do */}
965 void ETag_surfxml_trace___connect(){/* Nothing to do */}
966 void STag_surfxml_trace()
967 { /* Nothing to do */
968 }
969 void ETag_surfxml_router(){/*Nothing to do*/}
970 void ETag_surfxml_host___link(){/* Nothing to do */}
971 void ETag_surfxml_cabinet(){/* Nothing to do */}
972 void ETag_surfxml_peer(){/* Nothing to do */}
973 void STag_surfxml_backbone(){/* Nothing to do */}
974 void ETag_surfxml_link___ctn(){/* Nothing to do */}
975 void ETag_surfxml_argument(){/* Nothing to do */}
976 void ETag_surfxml_model___prop(){/* Nothing to do */}
977
978 /* Open and Close parse file */
979 YY_BUFFER_STATE surf_input_buffer;
980
981 void surf_parse_open(const std::string& file)
982 {
983   surf_parsed_filename = file;
984   std::string dir      = simgrid::xbt::Path(file).get_dir_name();
985   surf_path.push_back(dir);
986
987   surf_file_to_parse = surf_fopen(file, "r");
988   if (surf_file_to_parse == nullptr) {
989     std::string cwd = simgrid::xbt::Path().get_name();
990     xbt_die("Unable to open '%s' from '%s'\n", file.c_str(), cwd.c_str());
991   }
992   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
993   surf_parse__switch_to_buffer(surf_input_buffer);
994   surf_parse_lineno = 1;
995 }
996
997 void surf_parse_close()
998 {
999   surf_path.pop_back(); // remove the dirname of the opened file, that was added in surf_parse_open()
1000
1001   if (surf_file_to_parse) {
1002     surf_parse__delete_buffer(surf_input_buffer);
1003     fclose(surf_file_to_parse);
1004     surf_file_to_parse = nullptr; //Must be reset for Bypass
1005   }
1006 }
1007
1008 /* Call the lexer to parse the currently opened file */
1009 int surf_parse()
1010 {
1011   return surf_parse_lex();
1012 }