Logo AND Algorithmique Numérique Distribuée

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