Logo AND Algorithmique Numérique Distribuée

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