Logo AND Algorithmique Numérique Distribuée

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