Logo AND Algorithmique Numérique Distribuée

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