1 /* Copyright (c) 2006-2019. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/kernel/routing/NetPoint.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
10 #include "src/kernel/resource/profile/Profile.hpp"
11 #include "src/surf/network_interface.hpp"
12 #include "src/surf/surf_interface.hpp"
13 #include "src/surf/xml/platf_private.hpp"
14 #include "surf/surf.hpp"
15 #include "xbt/file.hpp"
17 #include <boost/algorithm/string.hpp>
18 #include <boost/algorithm/string/classification.hpp>
19 #include <boost/algorithm/string/split.hpp>
22 #include <unordered_map>
25 #include "simgrid_dtd.c"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
29 static std::string surf_parsed_filename; // Currently parsed file (for the error messages)
30 std::vector<simgrid::kernel::resource::LinkImpl*>
31 parsed_link_list; /* temporary store of current list link of a route */
36 void surf_parse_assert(bool cond, const std::string& msg)
39 int lineno = surf_parse_lineno;
41 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename.c_str(), lineno, msg.c_str());
43 xbt_die("Exiting now");
47 void surf_parse_error(const std::string& msg)
49 int lineno = surf_parse_lineno;
51 XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename.c_str(), lineno, msg.c_str());
53 xbt_die("Exiting now");
56 void surf_parse_assert_netpoint(const std::string& hostname, const std::string& pre, const std::string& post)
58 if (simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(hostname) != nullptr) // found
61 std::string msg = pre + hostname + post + " Existing netpoints: \n";
63 std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
64 simgrid::s4u::Engine::get_instance()->get_all_netpoints();
65 std::sort(netpoints.begin(), netpoints.end(),
66 [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
67 return a->get_name() < b->get_name();
70 for (auto const& np : netpoints) {
77 msg += "'" + np->get_name() + "'";
78 if (msg.length() > 4096) {
79 msg.pop_back(); // remove trailing quote
80 msg += "...(list truncated)......";
84 surf_parse_error(msg);
87 double surf_parse_get_double(const std::string& s)
91 } catch (const std::invalid_argument&) {
92 surf_parse_error(s + " is not a double");
96 int surf_parse_get_int(const std::string& s)
100 } catch (const std::invalid_argument&) {
101 surf_parse_error(s + " is not a double");
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(const std::string& radicals)
110 std::vector<int>* exploded = new std::vector<int>();
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());
121 switch (radical_ends.size()) {
126 end = surf_parse_get_int(radical_ends.back());
129 surf_parse_error(std::string("Malformed radical: ") + group);
131 for (int i = start; i <= end; i++)
132 exploded->push_back(i);
138 class unit_scale : public std::unordered_map<std::string, double> {
140 using std::unordered_map<std::string, double>::unordered_map;
141 // tuples are : <unit, value for unit, base (2 or 10), true if abbreviated>
142 explicit unit_scale(std::initializer_list<std::tuple<const std::string, double, int, bool>> generators);
145 unit_scale::unit_scale(std::initializer_list<std::tuple<const std::string, double, int, bool>> generators)
147 for (const auto& gen : generators) {
148 const std::string& unit = std::get<0>(gen);
149 double value = std::get<1>(gen);
150 const int base = std::get<2>(gen);
151 const bool abbrev = std::get<3>(gen);
153 std::vector<std::string> prefixes;
157 prefixes = abbrev ? std::vector<std::string>{"Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
158 : std::vector<std::string>{"kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"};
162 prefixes = abbrev ? std::vector<std::string>{"k", "M", "G", "T", "P", "E", "Z", "Y"}
163 : std::vector<std::string>{"kilo", "mega", "giga", "tera", "peta", "exa", "zeta", "yotta"};
168 emplace(unit, value);
169 for (const auto& prefix : prefixes) {
171 emplace(prefix + unit, value);
176 /* Note: no warning is issued for unit-less values when `name' is empty. */
177 double surf_parse_get_value_with_unit(const char* string, const unit_scale& units, const char* entity_kind,
178 const std::string& name, const char* error_msg, const char* default_unit)
182 double res = strtod(string, &endptr);
183 const char* ptr = endptr; // for const-correctness
185 surf_parse_error(std::string("value out of range: ") + string);
187 surf_parse_error(std::string("cannot parse number:") + string);
188 if (ptr[0] == '\0') {
189 // Ok, 0 can be unit-less
190 if (res != 0 && not name.empty())
191 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s", string, entity_kind, name.c_str(), error_msg);
194 auto u = units.find(ptr);
195 if (u == units.end())
196 surf_parse_error(std::string("unknown unit: ") + ptr);
197 return res * u->second;
201 double surf_parse_get_time(const char* string, const char* entity_kind, const std::string& name)
203 static const unit_scale units{std::make_pair("w", 7 * 24 * 60 * 60),
204 std::make_pair("d", 24 * 60 * 60),
205 std::make_pair("h", 60 * 60),
206 std::make_pair("m", 60),
207 std::make_pair("s", 1.0),
208 std::make_pair("ms", 1e-3),
209 std::make_pair("us", 1e-6),
210 std::make_pair("ns", 1e-9),
211 std::make_pair("ps", 1e-12)};
212 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
213 "Append 's' to your time to get seconds", "s");
216 double surf_parse_get_size(const char* string, const char* entity_kind, const std::string& name)
218 static const unit_scale units{std::make_tuple("b", 0.125, 2, true), std::make_tuple("b", 0.125, 10, true),
219 std::make_tuple("B", 1.0, 2, true), std::make_tuple("B", 1.0, 10, true)};
220 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
221 "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
224 double surf_parse_get_bandwidth(const char* string, const char* entity_kind, const std::string& name)
226 static const unit_scale units{std::make_tuple("bps", 0.125, 2, true), std::make_tuple("bps", 0.125, 10, true),
227 std::make_tuple("Bps", 1.0, 2, true), std::make_tuple("Bps", 1.0, 10, true)};
228 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
229 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
232 std::vector<double> surf_parse_get_bandwidths(const char* string, const char* entity_kind, const std::string& name)
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)};
237 std::vector<double> bandwidths;
238 std::vector<std::string> tokens;
239 boost::split(tokens, string, boost::is_any_of(";"));
240 for (auto token : tokens) {
241 bandwidths.push_back(surf_parse_get_value_with_unit(
242 token.c_str(), units, entity_kind, name,
243 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps"));
249 double surf_parse_get_speed(const char* string, const char* entity_kind, const std::string& name)
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");
256 static std::vector<double> surf_parse_get_all_speeds(char* speeds, const char* entity_kind, const std::string& id)
259 std::vector<double> speed_per_pstate;
261 if (strchr(speeds, ',') == nullptr){
262 double speed = surf_parse_get_speed(speeds, entity_kind, id);
263 speed_per_pstate.push_back(speed);
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);
274 return speed_per_pstate;
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)
282 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
284 /* The default current property receiver. Setup in the corresponding opening callbacks. */
285 std::unordered_map<std::string, std::string>* current_property_set = nullptr;
286 std::unordered_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)
289 FILE *surf_file_to_parse = nullptr;
291 /* Stuff relative to storage */
292 void STag_surfxml_storage()
295 XBT_DEBUG("STag_surfxml_storage");
296 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
299 void ETag_surfxml_storage()
301 simgrid::kernel::routing::StorageCreationArgs storage;
303 storage.properties = current_property_set;
304 current_property_set = nullptr;
306 storage.id = A_surfxml_storage_id;
307 storage.type_id = A_surfxml_storage_typeId;
308 storage.content = A_surfxml_storage_content;
309 storage.attach = A_surfxml_storage_attach;
311 sg_platf_new_storage(&storage);
313 void STag_surfxml_storage___type()
316 XBT_DEBUG("STag_surfxml_storage___type");
317 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
318 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 void ETag_surfxml_storage___type()
322 simgrid::kernel::routing::StorageTypeCreationArgs storage_type;
324 storage_type.properties = current_property_set;
325 current_property_set = nullptr;
327 storage_type.model_properties = current_model_property_set;
328 current_model_property_set = nullptr;
330 storage_type.content = A_surfxml_storage___type_content;
331 storage_type.id = A_surfxml_storage___type_id;
332 storage_type.model = A_surfxml_storage___type_model;
334 surf_parse_get_size(A_surfxml_storage___type_size, "size of storage type", storage_type.id.c_str());
335 sg_platf_new_storage_type(&storage_type);
338 void STag_surfxml_mount()
340 XBT_DEBUG("STag_surfxml_mount");
343 void ETag_surfxml_mount()
345 simgrid::kernel::routing::MountCreationArgs mount;
347 mount.name = A_surfxml_mount_name;
348 mount.storageId = A_surfxml_mount_storageId;
349 sg_platf_new_mount(&mount);
352 void STag_surfxml_include()
354 xbt_die("<include> tag was removed in SimGrid v3.18. Please stop using it now.");
357 void ETag_surfxml_include()
359 /* Won't happen since <include> is now removed since v3.18. */
362 /* Stag and Etag parse functions */
363 void STag_surfxml_platform() {
364 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
366 xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
367 "You're using an ancient XML file.\n"
368 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
369 "instead of MBytes, MFlops and seconds.\n"
371 "Use simgrid_update_xml to update your file automatically. "
372 "This program is installed automatically with SimGrid, or "
373 "available in the tools/ directory of the source archive.\n"
375 "Please check also out the SURF section of the ChangeLog for "
376 "the 3.1 version for more information. \n"
378 "Last, do not forget to also update your values for "
379 "the calls to MSG_task_create (if any).");
380 xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
381 "You're using an old XML file.\n"
382 "Use simgrid_update_xml to update your file automatically. "
383 "This program is installed automatically with SimGrid, or "
384 "available in the tools/ directory of the source archive.");
385 xbt_assert((version >= 4.0),
386 "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
387 "Changes introduced in SimGrid 3.13:\n"
388 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
389 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
390 " - DOCTYPE now point to the rignt URL.\n"
391 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
393 "Use simgrid_update_xml to update your file automatically. "
394 "This program is installed automatically with SimGrid, or "
395 "available in the tools/ directory of the source archive.",
396 surf_parsed_filename.c_str(), version);
398 XBT_INFO("You're using a v%.1f XML file (%s) while the current standard is v4.1 "
399 "That's fine, the new version is backward compatible. \n\n"
400 "Use simgrid_update_xml to update your file automatically to get rid of this warning. "
401 "This program is installed automatically with SimGrid, or "
402 "available in the tools/ directory of the source archive.",
403 version, surf_parsed_filename.c_str());
405 xbt_assert(version <= 4.1,
406 "******* FILE %s COMES FROM THE FUTURE (v:%.1f) *********\n "
407 "The most recent formalism that this version of SimGrid understands is v4.1.\n"
408 "Please update your code, or use another, more adapted, file.",
409 surf_parsed_filename.c_str(), version);
411 void ETag_surfxml_platform(){
412 simgrid::s4u::on_platform_created();
415 void STag_surfxml_host(){
417 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
420 void STag_surfxml_prop()
422 if (ZONE_TAG) { // We need to retrieve the most recently opened zone
423 XBT_DEBUG("Set zone property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
424 simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(A_surfxml_zone_id);
426 netzone->set_property(std::string(A_surfxml_prop_id), A_surfxml_prop_value);
428 if (not current_property_set)
429 current_property_set = new std::unordered_map<std::string, std::string>; // Maybe, it should raise an error
430 current_property_set->insert({A_surfxml_prop_id, A_surfxml_prop_value});
431 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
432 current_property_set);
436 void ETag_surfxml_host() {
437 simgrid::kernel::routing::HostCreationArgs host;
439 host.properties = current_property_set;
440 current_property_set = nullptr;
442 host.id = A_surfxml_host_id;
444 host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
446 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
447 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
449 host.speed_trace = nullptr;
450 if (A_surfxml_host_availability___file[0] != '\0') {
451 XBT_WARN("The availability_file attribute in <host> is now deprecated. Please, use 'speed_file' instead.");
452 host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_availability___file);
454 if (A_surfxml_host_speed___file[0] != '\0')
455 host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_speed___file);
456 host.state_trace = A_surfxml_host_state___file[0]
457 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_host_state___file)
459 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
460 host.coord = A_surfxml_host_coordinates;
462 sg_platf_new_host(&host);
465 void STag_surfxml_host___link(){
466 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
467 simgrid::kernel::routing::HostLinkCreationArgs host_link;
469 host_link.id = A_surfxml_host___link_id;
470 host_link.link_up = A_surfxml_host___link_up;
471 host_link.link_down = A_surfxml_host___link_down;
472 sg_platf_new_hostlink(&host_link);
475 void STag_surfxml_router(){
476 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
479 void ETag_surfxml_cluster(){
480 simgrid::kernel::routing::ClusterCreationArgs cluster;
481 cluster.properties = current_property_set;
482 current_property_set = nullptr;
484 cluster.id = A_surfxml_cluster_id;
485 cluster.prefix = A_surfxml_cluster_prefix;
486 cluster.suffix = A_surfxml_cluster_suffix;
487 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
488 cluster.speeds = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
489 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
490 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
491 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
492 if(strcmp(A_surfxml_cluster_bb___bw,""))
493 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
494 if(strcmp(A_surfxml_cluster_bb___lat,""))
495 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
496 if(strcmp(A_surfxml_cluster_limiter___link,""))
497 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
498 if(strcmp(A_surfxml_cluster_loopback___bw,""))
499 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
500 if(strcmp(A_surfxml_cluster_loopback___lat,""))
501 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
503 switch(AX_surfxml_cluster_topology){
504 case A_surfxml_cluster_topology_FLAT:
505 cluster.topology = simgrid::kernel::routing::ClusterTopology::FLAT;
507 case A_surfxml_cluster_topology_TORUS:
508 cluster.topology = simgrid::kernel::routing::ClusterTopology::TORUS;
510 case A_surfxml_cluster_topology_FAT___TREE:
511 cluster.topology = simgrid::kernel::routing::ClusterTopology::FAT_TREE;
513 case A_surfxml_cluster_topology_DRAGONFLY:
514 cluster.topology = simgrid::kernel::routing::ClusterTopology::DRAGONFLY;
517 surf_parse_error(std::string("Invalid cluster topology for cluster ") + cluster.id);
519 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
520 cluster.router_id = A_surfxml_cluster_router___id;
522 switch (AX_surfxml_cluster_sharing___policy) {
523 case A_surfxml_cluster_sharing___policy_SHARED:
524 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
526 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
527 XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
528 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
530 case A_surfxml_cluster_sharing___policy_SPLITDUPLEX:
531 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
533 case A_surfxml_cluster_sharing___policy_FATPIPE:
534 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
537 surf_parse_error(std::string("Invalid cluster sharing policy for cluster ") + cluster.id);
539 switch (AX_surfxml_cluster_bb___sharing___policy) {
540 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
541 cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
543 case A_surfxml_cluster_bb___sharing___policy_SHARED:
544 cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
547 surf_parse_error(std::string("Invalid bb sharing policy in cluster ") + cluster.id);
550 sg_platf_new_cluster(&cluster);
553 void STag_surfxml_cluster(){
555 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
558 void STag_surfxml_cabinet(){
559 simgrid::kernel::routing::CabinetCreationArgs cabinet;
560 cabinet.id = A_surfxml_cabinet_id;
561 cabinet.prefix = A_surfxml_cabinet_prefix;
562 cabinet.suffix = A_surfxml_cabinet_suffix;
563 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id.c_str());
564 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id.c_str());
565 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id.c_str());
566 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
568 sg_platf_new_cabinet(&cabinet);
571 void STag_surfxml_peer(){
572 simgrid::kernel::routing::PeerCreationArgs peer;
574 peer.id = std::string(A_surfxml_peer_id);
575 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id.c_str());
576 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id.c_str());
577 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id.c_str());
578 peer.coord = A_surfxml_peer_coordinates;
579 peer.speed_trace = nullptr;
580 if (A_surfxml_peer_availability___file[0] != '\0') {
581 XBT_WARN("The availability_file attribute in <peer> is now deprecated. Please, use 'speed_file' instead.");
582 peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_availability___file);
584 if (A_surfxml_peer_speed___file[0] != '\0')
585 peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_speed___file);
586 peer.state_trace = A_surfxml_peer_state___file[0]
587 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_state___file)
590 if (A_surfxml_peer_lat[0] != '\0')
591 XBT_WARN("The latency attribute in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
594 sg_platf_new_peer(&peer);
597 void STag_surfxml_link(){
599 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
602 void ETag_surfxml_link(){
603 simgrid::kernel::routing::LinkCreationArgs link;
605 link.properties = current_property_set;
606 current_property_set = nullptr;
608 link.id = std::string(A_surfxml_link_id);
609 link.bandwidths = surf_parse_get_bandwidths(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
610 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0]
611 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_bandwidth___file)
613 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
614 link.latency_trace = A_surfxml_link_latency___file[0]
615 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_latency___file)
617 link.state_trace = A_surfxml_link_state___file[0]
618 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_state___file)
621 switch (A_surfxml_link_sharing___policy) {
622 case A_surfxml_link_sharing___policy_SHARED:
623 link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
625 case A_surfxml_link_sharing___policy_FATPIPE:
626 link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
628 case A_surfxml_link_sharing___policy_FULLDUPLEX:
629 XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
630 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
632 case A_surfxml_link_sharing___policy_SPLITDUPLEX:
633 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
635 case A_surfxml_link_sharing___policy_WIFI:
636 link.policy = simgrid::s4u::Link::SharingPolicy::WIFI;
639 surf_parse_error(std::string("Invalid sharing policy in link ") + link.id);
642 sg_platf_new_link(&link);
645 void STag_surfxml_link___ctn()
647 simgrid::kernel::resource::LinkImpl* link = nullptr;
648 switch (A_surfxml_link___ctn_direction) {
649 case AU_surfxml_link___ctn_direction:
650 case A_surfxml_link___ctn_direction_NONE:
651 link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id))->get_impl();
653 case A_surfxml_link___ctn_direction_UP:
654 link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_UP")->get_impl();
656 case A_surfxml_link___ctn_direction_DOWN:
657 link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_DOWN")->get_impl();
660 surf_parse_error(std::string("Invalid direction for link ") + A_surfxml_link___ctn_id);
663 const char* dirname = "";
664 switch (A_surfxml_link___ctn_direction) {
665 case A_surfxml_link___ctn_direction_UP:
666 dirname = " (upward)";
668 case A_surfxml_link___ctn_direction_DOWN:
669 dirname = " (downward)";
674 surf_parse_assert(link != nullptr, std::string("No such link: '") + A_surfxml_link___ctn_id + "'" + dirname);
675 parsed_link_list.push_back(link);
678 void ETag_surfxml_backbone(){
679 simgrid::kernel::routing::LinkCreationArgs link;
681 link.properties = nullptr;
682 link.id = std::string(A_surfxml_backbone_id);
683 link.bandwidths.push_back(
684 surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str()));
685 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
686 link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
688 sg_platf_new_link(&link);
689 routing_cluster_add_backbone(simgrid::s4u::Link::by_name(std::string(A_surfxml_backbone_id))->get_impl());
692 void STag_surfxml_route(){
693 surf_parse_assert_netpoint(A_surfxml_route_src, "Route src='", "' does name a node.");
694 surf_parse_assert_netpoint(A_surfxml_route_dst, "Route dst='", "' does name a node.");
697 void STag_surfxml_ASroute(){
698 surf_parse_assert_netpoint(A_surfxml_ASroute_src, "ASroute src='", "' does name a node.");
699 surf_parse_assert_netpoint(A_surfxml_ASroute_dst, "ASroute dst='", "' does name a node.");
701 surf_parse_assert_netpoint(A_surfxml_ASroute_gw___src, "ASroute gw_src='", "' does name a node.");
702 surf_parse_assert_netpoint(A_surfxml_ASroute_gw___dst, "ASroute gw_dst='", "' does name a node.");
704 void STag_surfxml_zoneRoute(){
705 surf_parse_assert_netpoint(A_surfxml_zoneRoute_src, "zoneRoute src='", "' does name a node.");
706 surf_parse_assert_netpoint(A_surfxml_zoneRoute_dst, "zoneRoute dst='", "' does name a node.");
707 surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___src, "zoneRoute gw_src='", "' does name a node.");
708 surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___dst, "zoneRoute gw_dst='", "' does name a node.");
711 void STag_surfxml_bypassRoute(){
712 surf_parse_assert_netpoint(A_surfxml_bypassRoute_src, "bypassRoute src='", "' does name a node.");
713 surf_parse_assert_netpoint(A_surfxml_bypassRoute_dst, "bypassRoute dst='", "' does name a node.");
716 void STag_surfxml_bypassASroute(){
717 surf_parse_assert_netpoint(A_surfxml_bypassASroute_src, "bypassASroute src='", "' does name a node.");
718 surf_parse_assert_netpoint(A_surfxml_bypassASroute_dst, "bypassASroute dst='", "' does name a node.");
719 surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___src, "bypassASroute gw_src='", "' does name a node.");
720 surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___dst, "bypassASroute gw_dst='", "' does name a node.");
722 void STag_surfxml_bypassZoneRoute(){
723 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_src, "bypassZoneRoute src='", "' does name a node.");
724 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_dst, "bypassZoneRoute dst='", "' does name a node.");
725 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___src, "bypassZoneRoute gw_src='", "' does name a node.");
726 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___dst, "bypassZoneRoute gw_dst='", "' does name a node.");
729 void ETag_surfxml_route(){
730 simgrid::kernel::routing::RouteCreationArgs route;
732 route.src = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
733 route.dst = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
734 route.gw_src = nullptr;
735 route.gw_dst = nullptr;
736 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
738 route.link_list.swap(parsed_link_list);
740 sg_platf_new_route(&route);
743 void ETag_surfxml_ASroute()
745 AX_surfxml_zoneRoute_src = AX_surfxml_ASroute_src;
746 AX_surfxml_zoneRoute_dst = AX_surfxml_ASroute_dst;
747 AX_surfxml_zoneRoute_gw___src = AX_surfxml_ASroute_gw___src;
748 AX_surfxml_zoneRoute_gw___dst = AX_surfxml_ASroute_gw___dst;
749 AX_surfxml_zoneRoute_symmetrical = (AT_surfxml_zoneRoute_symmetrical)AX_surfxml_ASroute_symmetrical;
750 ETag_surfxml_zoneRoute();
752 void ETag_surfxml_zoneRoute()
754 simgrid::kernel::routing::RouteCreationArgs ASroute;
756 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src); // tested to not be nullptr in start tag
757 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst); // tested to not be nullptr in start tag
759 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src); // tested to not be nullptr in start tag
760 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst); // tested to not be nullptr in start tag
762 ASroute.link_list.swap(parsed_link_list);
764 switch (A_surfxml_zoneRoute_symmetrical) {
765 case AU_surfxml_zoneRoute_symmetrical:
766 case A_surfxml_zoneRoute_symmetrical_YES:
767 ASroute.symmetrical = true;
769 case A_surfxml_zoneRoute_symmetrical_NO:
770 ASroute.symmetrical = false;
776 sg_platf_new_route(&ASroute);
779 void ETag_surfxml_bypassRoute(){
780 simgrid::kernel::routing::RouteCreationArgs route;
782 route.src = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
783 route.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
784 route.gw_src = nullptr;
785 route.gw_dst = nullptr;
786 route.symmetrical = false;
788 route.link_list.swap(parsed_link_list);
790 sg_platf_new_bypassRoute(&route);
793 void ETag_surfxml_bypassASroute()
795 AX_surfxml_bypassZoneRoute_src = AX_surfxml_bypassASroute_src;
796 AX_surfxml_bypassZoneRoute_dst = AX_surfxml_bypassASroute_dst;
797 AX_surfxml_bypassZoneRoute_gw___src = AX_surfxml_bypassASroute_gw___src;
798 AX_surfxml_bypassZoneRoute_gw___dst = AX_surfxml_bypassASroute_gw___dst;
799 ETag_surfxml_bypassZoneRoute();
801 void ETag_surfxml_bypassZoneRoute()
803 simgrid::kernel::routing::RouteCreationArgs ASroute;
805 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src);
806 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst);
807 ASroute.link_list.swap(parsed_link_list);
809 ASroute.symmetrical = false;
811 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src);
812 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst);
814 sg_platf_new_bypassRoute(&ASroute);
817 void ETag_surfxml_trace(){
818 simgrid::kernel::routing::ProfileCreationArgs trace;
820 trace.id = A_surfxml_trace_id;
821 trace.file = A_surfxml_trace_file;
822 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
823 trace.pc_data = surfxml_pcdata;
825 sg_platf_new_trace(&trace);
828 void STag_surfxml_trace___connect()
830 simgrid::kernel::routing::TraceConnectCreationArgs trace_connect;
832 trace_connect.element = A_surfxml_trace___connect_element;
833 trace_connect.trace = A_surfxml_trace___connect_trace;
835 switch (A_surfxml_trace___connect_kind) {
836 case AU_surfxml_trace___connect_kind:
837 case A_surfxml_trace___connect_kind_SPEED:
838 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::SPEED;
840 case A_surfxml_trace___connect_kind_BANDWIDTH:
841 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::BANDWIDTH;
843 case A_surfxml_trace___connect_kind_HOST___AVAIL:
844 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL;
846 case A_surfxml_trace___connect_kind_LATENCY:
847 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LATENCY;
849 case A_surfxml_trace___connect_kind_LINK___AVAIL:
850 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL;
853 surf_parse_error("Invalid trace kind");
855 sg_platf_trace_connect(&trace_connect);
858 void STag_surfxml_AS()
860 AX_surfxml_zone_id = AX_surfxml_AS_id;
861 AX_surfxml_zone_routing = (AT_surfxml_zone_routing)AX_surfxml_AS_routing;
865 void ETag_surfxml_AS()
870 void STag_surfxml_zone()
873 simgrid::kernel::routing::ZoneCreationArgs zone;
874 zone.id = A_surfxml_zone_id;
875 zone.routing = static_cast<int>(A_surfxml_zone_routing);
877 sg_platf_new_Zone_begin(&zone);
880 void ETag_surfxml_zone()
882 sg_platf_new_Zone_seal();
885 void STag_surfxml_config()
888 xbt_assert(current_property_set == nullptr,
889 "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
890 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
891 if (_sg_cfg_init_status == 2) {
892 surf_parse_error("All <config> tags must be given before any platform elements (such as <zone>, <host>, <cluster>, "
897 void ETag_surfxml_config()
899 // Sort config elements before applying.
900 // That's a little waste of time, but not doing so would break the tests
901 std::vector<std::string> keys;
902 for (auto const& kv : *current_property_set) {
903 keys.push_back(kv.first);
905 std::sort(keys.begin(), keys.end());
906 for (std::string key : keys) {
907 if (simgrid::config::is_default(key.c_str())) {
908 std::string cfg = key + ":" + current_property_set->at(key);
909 simgrid::config::set_parse(std::move(cfg));
911 XBT_INFO("The custom configuration '%s' is already defined by user!", key.c_str());
913 XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
915 delete current_property_set;
916 current_property_set = nullptr;
919 static std::vector<std::string> arguments;
921 void STag_surfxml_process()
923 AX_surfxml_actor_function = AX_surfxml_process_function;
924 STag_surfxml_actor();
927 void STag_surfxml_actor()
930 arguments.assign(1, A_surfxml_actor_function);
931 xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
934 void ETag_surfxml_process()
936 AX_surfxml_actor_host = AX_surfxml_process_host;
937 AX_surfxml_actor_function = AX_surfxml_process_function;
938 AX_surfxml_actor_start___time = AX_surfxml_process_start___time;
939 AX_surfxml_actor_kill___time = AX_surfxml_process_kill___time;
940 AX_surfxml_actor_on___failure = (AT_surfxml_actor_on___failure)AX_surfxml_process_on___failure;
941 ETag_surfxml_actor();
944 void ETag_surfxml_actor()
946 simgrid::kernel::routing::ActorCreationArgs actor;
948 actor.properties = current_property_set;
949 current_property_set = nullptr;
951 actor.args.swap(arguments);
952 actor.host = A_surfxml_actor_host;
953 actor.function = A_surfxml_actor_function;
954 actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time);
955 actor.kill_time = surf_parse_get_double(A_surfxml_actor_kill___time);
957 switch (A_surfxml_actor_on___failure) {
958 case AU_surfxml_actor_on___failure:
959 case A_surfxml_actor_on___failure_DIE:
960 actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
962 case A_surfxml_actor_on___failure_RESTART:
963 actor.on_failure = simgrid::kernel::routing::ActorOnFailure::RESTART;
966 surf_parse_error("Invalid on failure behavior");
969 sg_platf_new_actor(&actor);
972 void STag_surfxml_argument(){
973 arguments.push_back(A_surfxml_argument_value);
976 void STag_surfxml_model___prop(){
977 if (not current_model_property_set)
978 current_model_property_set = new std::unordered_map<std::string, std::string>();
980 current_model_property_set->insert({A_surfxml_model___prop_id, A_surfxml_model___prop_value});
983 void ETag_surfxml_prop(){/* Nothing to do */}
984 void STag_surfxml_random(){/* Nothing to do */}
985 void ETag_surfxml_random(){/* Nothing to do */}
986 void ETag_surfxml_trace___connect(){/* Nothing to do */}
987 void STag_surfxml_trace()
988 { /* Nothing to do */
990 void ETag_surfxml_router(){/*Nothing to do*/}
991 void ETag_surfxml_host___link(){/* Nothing to do */}
992 void ETag_surfxml_cabinet(){/* Nothing to do */}
993 void ETag_surfxml_peer(){/* Nothing to do */}
994 void STag_surfxml_backbone(){/* Nothing to do */}
995 void ETag_surfxml_link___ctn(){/* Nothing to do */}
996 void ETag_surfxml_argument(){/* Nothing to do */}
997 void ETag_surfxml_model___prop(){/* Nothing to do */}
999 /* Open and Close parse file */
1000 YY_BUFFER_STATE surf_input_buffer;
1002 void surf_parse_open(const std::string& file)
1004 surf_parsed_filename = file;
1005 std::string dir = simgrid::xbt::Path(file).get_dir_name();
1006 surf_path.push_back(dir);
1008 surf_file_to_parse = surf_fopen(file, "r");
1009 if (surf_file_to_parse == nullptr) {
1010 std::string cwd = simgrid::xbt::Path().get_name();
1011 xbt_die("Unable to open '%s' from '%s'\n", file.c_str(), cwd.c_str());
1013 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1014 surf_parse__switch_to_buffer(surf_input_buffer);
1015 surf_parse_lineno = 1;
1018 void surf_parse_close()
1020 surf_path.pop_back(); // remove the dirname of the opened file, that was added in surf_parse_open()
1022 if (surf_file_to_parse) {
1023 surf_parse__delete_buffer(surf_input_buffer);
1024 fclose(surf_file_to_parse);
1025 surf_file_to_parse = nullptr; //Must be reset for Bypass
1029 /* Call the lexer to parse the currently opened file */
1032 return surf_parse_lex();