Logo AND Algorithmique Numérique Distribuée

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