Logo AND Algorithmique Numérique Distribuée

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