Logo AND Algorithmique Numérique Distribuée

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