Logo AND Algorithmique Numérique Distribuée

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