Logo AND Algorithmique Numérique Distribuée

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