Logo AND Algorithmique Numérique Distribuée

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