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/Exception.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/sg_config.hpp"
10 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
11 #include "src/kernel/resource/profile/Profile.hpp"
12 #include "src/surf/network_interface.hpp"
13 #include "src/surf/surf_interface.hpp"
14 #include "src/surf/xml/platf_private.hpp"
15 #include "surf/surf.hpp"
16 #include "xbt/file.hpp"
18 #include <boost/algorithm/string.hpp>
19 #include <boost/algorithm/string/classification.hpp>
20 #include <boost/algorithm/string/split.hpp>
23 #include <unordered_map>
26 #include "simgrid_dtd.c"
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
30 static std::string surf_parsed_filename; // Currently parsed file (for the error messages)
31 std::vector<simgrid::kernel::resource::LinkImpl*>
32 parsed_link_list; /* temporary store of current link list of a route */
33 std::vector<simgrid::kernel::resource::DiskImpl*> parsed_disk_list; /* temporary store of current disk list of a host */
37 void surf_parse_assert(bool cond, std::string&& msg)
40 surf_parse_error(std::move(msg));
43 void surf_parse_error(const char* msg) {
44 throw simgrid::ParseError(surf_parse_lineno, surf_parsed_filename, std::string(msg));
47 void surf_parse_error(std::string&& msg)
49 throw simgrid::ParseError(surf_parse_lineno, surf_parsed_filename, std::move(msg));
52 void surf_parse_assert_netpoint(const std::string& hostname, const std::string& pre, const std::string& post)
54 if (simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(hostname) != nullptr) // found
57 std::string msg = pre + hostname + post + " Existing netpoints: \n";
59 std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
60 simgrid::s4u::Engine::get_instance()->get_all_netpoints();
61 std::sort(netpoints.begin(), netpoints.end(),
62 [](simgrid::kernel::routing::NetPoint* a, simgrid::kernel::routing::NetPoint* b) {
63 return a->get_name() < b->get_name();
66 for (auto const& np : netpoints) {
73 msg += "'" + np->get_name() + "'";
74 if (msg.length() > 4096) {
75 msg.pop_back(); // remove trailing quote
76 msg += "...(list truncated)......";
80 surf_parse_error(std::move(msg));
83 double surf_parse_get_double(const std::string& s)
87 } catch (const std::invalid_argument&) {
88 surf_parse_error(s + " is not a double");
92 int surf_parse_get_int(const std::string& s)
96 } catch (const std::invalid_argument&) {
97 surf_parse_error(s + " is not a double");
103 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
104 std::vector<int>* explodesRadical(const std::string& radicals)
106 std::vector<int>* exploded = new std::vector<int>();
109 std::vector<std::string> radical_elements;
110 boost::split(radical_elements, radicals, boost::is_any_of(","));
111 for (auto const& group : radical_elements) {
112 std::vector<std::string> radical_ends;
113 boost::split(radical_ends, group, boost::is_any_of("-"));
114 int start = surf_parse_get_int(radical_ends.front());
117 switch (radical_ends.size()) {
122 end = surf_parse_get_int(radical_ends.back());
125 surf_parse_error(std::string("Malformed radical: ") + group);
127 for (int i = start; i <= end; i++)
128 exploded->push_back(i);
134 class unit_scale : public std::unordered_map<std::string, double> {
136 using std::unordered_map<std::string, double>::unordered_map;
137 // tuples are : <unit, value for unit, base (2 or 10), true if abbreviated>
138 explicit unit_scale(std::initializer_list<std::tuple<const std::string, double, int, bool>> generators);
141 unit_scale::unit_scale(std::initializer_list<std::tuple<const std::string, double, int, bool>> generators)
143 for (const auto& gen : generators) {
144 const std::string& unit = std::get<0>(gen);
145 double value = std::get<1>(gen);
146 const int base = std::get<2>(gen);
147 const bool abbrev = std::get<3>(gen);
149 std::vector<std::string> prefixes;
153 prefixes = abbrev ? std::vector<std::string>{"Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
154 : std::vector<std::string>{"kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"};
158 prefixes = abbrev ? std::vector<std::string>{"k", "M", "G", "T", "P", "E", "Z", "Y"}
159 : std::vector<std::string>{"kilo", "mega", "giga", "tera", "peta", "exa", "zeta", "yotta"};
164 emplace(unit, value);
165 for (const auto& prefix : prefixes) {
167 emplace(prefix + unit, value);
172 /* Note: no warning is issued for unit-less values when `name' is empty. */
173 double surf_parse_get_value_with_unit(const char* string, const unit_scale& units, const char* entity_kind,
174 const std::string& name, const char* error_msg, const char* default_unit)
178 double res = strtod(string, &endptr);
179 const char* ptr = endptr; // for const-correctness
181 surf_parse_error(std::string("value out of range: ") + string);
183 surf_parse_error(std::string("cannot parse number:") + string);
184 if (ptr[0] == '\0') {
185 // Ok, 0 can be unit-less
186 if (res != 0 && not name.empty())
187 XBT_WARN("Deprecated unit-less value '%s' for %s %s. %s", string, entity_kind, name.c_str(), error_msg);
190 auto u = units.find(ptr);
191 if (u == units.end())
192 surf_parse_error(std::string("unknown unit: ") + ptr);
193 return res * u->second;
197 double surf_parse_get_time(const char* string, const char* entity_kind, const std::string& name)
199 static const unit_scale units{std::make_pair("w", 7 * 24 * 60 * 60),
200 std::make_pair("d", 24 * 60 * 60),
201 std::make_pair("h", 60 * 60),
202 std::make_pair("m", 60),
203 std::make_pair("s", 1.0),
204 std::make_pair("ms", 1e-3),
205 std::make_pair("us", 1e-6),
206 std::make_pair("ns", 1e-9),
207 std::make_pair("ps", 1e-12)};
208 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
209 "Append 's' to your time to get seconds", "s");
212 double surf_parse_get_size(const char* string, const char* entity_kind, const std::string& name)
214 static const unit_scale units{std::make_tuple("b", 0.125, 2, true), std::make_tuple("b", 0.125, 10, true),
215 std::make_tuple("B", 1.0, 2, true), std::make_tuple("B", 1.0, 10, true)};
216 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
217 "Append 'B' to get bytes (or 'b' for bits but 1B = 8b).", "B");
220 double surf_parse_get_bandwidth(const char* string, const char* entity_kind, const std::string& name)
222 static const unit_scale units{std::make_tuple("bps", 0.125, 2, true), std::make_tuple("bps", 0.125, 10, true),
223 std::make_tuple("Bps", 1.0, 2, true), std::make_tuple("Bps", 1.0, 10, true)};
224 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
225 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps");
228 std::vector<double> surf_parse_get_bandwidths(const char* string, const char* entity_kind, const std::string& name)
230 static const unit_scale units{std::make_tuple("bps", 0.125, 2, true), std::make_tuple("bps", 0.125, 10, true),
231 std::make_tuple("Bps", 1.0, 2, true), std::make_tuple("Bps", 1.0, 10, true)};
233 std::vector<double> bandwidths;
234 std::vector<std::string> tokens;
235 boost::split(tokens, string, boost::is_any_of(";"));
236 for (auto token : tokens) {
237 bandwidths.push_back(surf_parse_get_value_with_unit(
238 token.c_str(), units, entity_kind, name,
239 "Append 'Bps' to get bytes per second (or 'bps' for bits but 1Bps = 8bps)", "Bps"));
245 double surf_parse_get_speed(const char* string, const char* entity_kind, const std::string& name)
247 static const unit_scale units{std::make_tuple("f", 1.0, 10, true), std::make_tuple("flops", 1.0, 10, false)};
248 return surf_parse_get_value_with_unit(string, units, entity_kind, name,
249 "Append 'f' or 'flops' to your speed to get flop per second", "f");
252 static std::vector<double> surf_parse_get_all_speeds(char* speeds, const char* entity_kind, const std::string& id)
255 std::vector<double> speed_per_pstate;
257 if (strchr(speeds, ',') == nullptr){
258 double speed = surf_parse_get_speed(speeds, entity_kind, id);
259 speed_per_pstate.push_back(speed);
261 std::vector<std::string> pstate_list;
262 boost::split(pstate_list, speeds, boost::is_any_of(","));
263 for (auto speed_str : pstate_list) {
264 boost::trim(speed_str);
265 double speed = surf_parse_get_speed(speed_str.c_str(), entity_kind, id);
266 speed_per_pstate.push_back(speed);
267 XBT_DEBUG("Speed value: %f", speed);
270 return speed_per_pstate;
274 * All the callback lists that can be overridden anywhere.
275 * (this list should probably be reduced to the bare minimum to allow the models to work)
278 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
280 std::vector<std::unordered_map<std::string, std::string>*> property_sets;
282 /* The default current property receiver. Setup in the corresponding opening callbacks. */
283 std::unordered_map<std::string, std::string>* current_model_property_set = nullptr;
285 FILE *surf_file_to_parse = nullptr;
287 /* Stuff relative to storage */
288 void STag_surfxml_storage()
290 XBT_DEBUG("STag_surfxml_storage");
291 property_sets.push_back(new std::unordered_map<std::string, std::string>());
294 void ETag_surfxml_storage()
296 simgrid::kernel::routing::StorageCreationArgs storage;
298 storage.properties = property_sets.back();
299 property_sets.pop_back();
301 storage.id = A_surfxml_storage_id;
302 storage.type_id = A_surfxml_storage_typeId;
303 storage.content = A_surfxml_storage_content;
304 storage.attach = A_surfxml_storage_attach;
306 sg_platf_new_storage(&storage);
308 void STag_surfxml_storage___type()
310 XBT_DEBUG("STag_surfxml_storage___type");
311 property_sets.push_back(new std::unordered_map<std::string, std::string>());
312 xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
314 void ETag_surfxml_storage___type()
316 simgrid::kernel::routing::StorageTypeCreationArgs storage_type;
318 storage_type.properties = property_sets.back();
319 property_sets.pop_back();
321 storage_type.model_properties = current_model_property_set;
322 current_model_property_set = nullptr;
324 storage_type.content = A_surfxml_storage___type_content;
325 storage_type.id = A_surfxml_storage___type_id;
326 storage_type.model = A_surfxml_storage___type_model;
328 surf_parse_get_size(A_surfxml_storage___type_size, "size of storage type", storage_type.id.c_str());
329 sg_platf_new_storage_type(&storage_type);
332 void STag_surfxml_mount()
334 XBT_DEBUG("STag_surfxml_mount");
337 void ETag_surfxml_mount()
339 simgrid::kernel::routing::MountCreationArgs mount;
341 mount.name = A_surfxml_mount_name;
342 mount.storageId = A_surfxml_mount_storageId;
343 sg_platf_new_mount(&mount);
346 void STag_surfxml_include()
348 xbt_die("<include> tag was removed in SimGrid v3.18. Please stop using it now.");
351 void ETag_surfxml_include()
353 /* Won't happen since <include> is now removed since v3.18. */
356 /* Stag and Etag parse functions */
357 void STag_surfxml_platform() {
358 XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
360 surf_parse_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
361 "You're using an ancient XML file.\n"
362 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
363 "instead of MBytes, MFlops and seconds.\n"
365 "Use simgrid_update_xml to update your file automatically. "
366 "This program is installed automatically with SimGrid, or "
367 "available in the tools/ directory of the source archive.\n"
369 "Please check also out the SURF section of the ChangeLog for "
370 "the 3.1 version for more information. \n"
372 "Last, do not forget to also update your values for "
373 "the calls to MSG_task_create (if any).");
374 surf_parse_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
375 "You're using an old XML file.\n"
376 "Use simgrid_update_xml to update your file automatically. "
377 "This program is installed automatically with SimGrid, or "
378 "available in the tools/ directory of the source archive.");
379 surf_parse_assert((version >= 4.0),
380 std::string("******* THIS FILE IS TOO OLD (v:")+std::to_string(version)+") *********\n "
381 "Changes introduced in SimGrid 3.13:\n"
382 " - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
383 " - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
384 " - DOCTYPE now point to the rignt URL.\n"
385 " - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
387 "Use simgrid_update_xml to update your file automatically. "
388 "This program is installed automatically with SimGrid, or "
389 "available in the tools/ directory of the source archive.");
391 XBT_INFO("You're using a v%.1f XML file (%s) while the current standard is v4.1 "
392 "That's fine, the new version is backward compatible. \n\n"
393 "Use simgrid_update_xml to update your file automatically to get rid of this warning. "
394 "This program is installed automatically with SimGrid, or "
395 "available in the tools/ directory of the source archive.",
396 version, surf_parsed_filename.c_str());
398 surf_parse_assert(version <= 4.1,
399 std::string("******* THIS FILE COMES FROM THE FUTURE (v:")+std::to_string(version)+") *********\n "
400 "The most recent formalism that this version of SimGrid understands is v4.1.\n"
401 "Please update your code, or use another, more adapted, file.");
403 void ETag_surfxml_platform(){
404 simgrid::s4u::Engine::on_platform_created();
407 void STag_surfxml_host(){
408 property_sets.push_back(new std::unordered_map<std::string, std::string>());
411 void STag_surfxml_prop()
413 property_sets.back()->insert({A_surfxml_prop_id, A_surfxml_prop_value});
414 XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
415 property_sets.back());
418 void ETag_surfxml_host() {
419 simgrid::kernel::routing::HostCreationArgs host;
421 host.properties = property_sets.back();
422 property_sets.pop_back();
424 host.id = A_surfxml_host_id;
426 host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
428 XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
429 host.core_amount = surf_parse_get_int(A_surfxml_host_core);
431 host.speed_trace = nullptr;
432 if (A_surfxml_host_availability___file[0] != '\0') {
433 XBT_WARN("The availability_file attribute in <host> is now deprecated. Please, use 'speed_file' instead.");
434 host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_availability___file);
436 if (A_surfxml_host_speed___file[0] != '\0')
437 host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_speed___file);
438 host.state_trace = A_surfxml_host_state___file[0]
439 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_host_state___file)
441 host.pstate = surf_parse_get_int(A_surfxml_host_pstate);
442 host.coord = A_surfxml_host_coordinates;
443 host.disks.swap(parsed_disk_list);
445 sg_platf_new_host(&host);
448 void STag_surfxml_disk() {
449 property_sets.push_back(new std::unordered_map<std::string, std::string>());
452 void ETag_surfxml_disk() {
453 simgrid::kernel::routing::DiskCreationArgs disk;
454 disk.properties = property_sets.back();
455 property_sets.pop_back();
457 disk.id = A_surfxml_disk_id;
458 disk.read_bw = surf_parse_get_bandwidth(A_surfxml_disk_read___bw, "read_bw of disk ", disk.id);
459 disk.write_bw = surf_parse_get_bandwidth(A_surfxml_disk_write___bw, "write_bw of disk ", disk.id);
461 parsed_disk_list.push_back(sg_platf_new_disk(&disk));
464 void STag_surfxml_host___link(){
465 XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
466 simgrid::kernel::routing::HostLinkCreationArgs host_link;
468 host_link.id = A_surfxml_host___link_id;
469 host_link.link_up = A_surfxml_host___link_up;
470 host_link.link_down = A_surfxml_host___link_down;
471 sg_platf_new_hostlink(&host_link);
474 void STag_surfxml_router(){
475 sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
478 void ETag_surfxml_cluster(){
479 simgrid::kernel::routing::ClusterCreationArgs cluster;
480 cluster.properties = property_sets.back();
481 property_sets.pop_back();
483 cluster.id = A_surfxml_cluster_id;
484 cluster.prefix = A_surfxml_cluster_prefix;
485 cluster.suffix = A_surfxml_cluster_suffix;
486 cluster.radicals = explodesRadical(A_surfxml_cluster_radical);
487 cluster.speeds = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
488 cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
489 cluster.bw = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
490 cluster.lat = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
491 if(strcmp(A_surfxml_cluster_bb___bw,""))
492 cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
493 if(strcmp(A_surfxml_cluster_bb___lat,""))
494 cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
495 if(strcmp(A_surfxml_cluster_limiter___link,""))
496 cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
497 if(strcmp(A_surfxml_cluster_loopback___bw,""))
498 cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
499 if(strcmp(A_surfxml_cluster_loopback___lat,""))
500 cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
502 switch(AX_surfxml_cluster_topology){
503 case A_surfxml_cluster_topology_FLAT:
504 cluster.topology = simgrid::kernel::routing::ClusterTopology::FLAT;
506 case A_surfxml_cluster_topology_TORUS:
507 cluster.topology = simgrid::kernel::routing::ClusterTopology::TORUS;
509 case A_surfxml_cluster_topology_FAT___TREE:
510 cluster.topology = simgrid::kernel::routing::ClusterTopology::FAT_TREE;
512 case A_surfxml_cluster_topology_DRAGONFLY:
513 cluster.topology = simgrid::kernel::routing::ClusterTopology::DRAGONFLY;
516 surf_parse_error(std::string("Invalid cluster topology for cluster ") + cluster.id);
518 cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
519 cluster.router_id = A_surfxml_cluster_router___id;
521 switch (AX_surfxml_cluster_sharing___policy) {
522 case A_surfxml_cluster_sharing___policy_SHARED:
523 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
525 case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
526 XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
527 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
529 case A_surfxml_cluster_sharing___policy_SPLITDUPLEX:
530 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
532 case A_surfxml_cluster_sharing___policy_FATPIPE:
533 cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
536 surf_parse_error(std::string("Invalid cluster sharing policy for cluster ") + cluster.id);
538 switch (AX_surfxml_cluster_bb___sharing___policy) {
539 case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
540 cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
542 case A_surfxml_cluster_bb___sharing___policy_SHARED:
543 cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
546 surf_parse_error(std::string("Invalid bb sharing policy in cluster ") + cluster.id);
549 sg_platf_new_cluster(&cluster);
552 void STag_surfxml_cluster(){
553 property_sets.push_back(new std::unordered_map<std::string, std::string>());
556 void STag_surfxml_cabinet(){
557 simgrid::kernel::routing::CabinetCreationArgs cabinet;
558 cabinet.id = A_surfxml_cabinet_id;
559 cabinet.prefix = A_surfxml_cabinet_prefix;
560 cabinet.suffix = A_surfxml_cabinet_suffix;
561 cabinet.speed = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id.c_str());
562 cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id.c_str());
563 cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id.c_str());
564 cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
566 sg_platf_new_cabinet(&cabinet);
569 void STag_surfxml_peer(){
570 simgrid::kernel::routing::PeerCreationArgs peer;
572 peer.id = std::string(A_surfxml_peer_id);
573 peer.speed = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id.c_str());
574 peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id.c_str());
575 peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id.c_str());
576 peer.coord = A_surfxml_peer_coordinates;
577 peer.speed_trace = nullptr;
578 if (A_surfxml_peer_availability___file[0] != '\0') {
579 XBT_WARN("The availability_file attribute in <peer> is now deprecated. Please, use 'speed_file' instead.");
580 peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_availability___file);
582 if (A_surfxml_peer_speed___file[0] != '\0')
583 peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_speed___file);
584 peer.state_trace = A_surfxml_peer_state___file[0]
585 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_state___file)
588 if (A_surfxml_peer_lat[0] != '\0')
589 XBT_WARN("The latency attribute in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
592 sg_platf_new_peer(&peer);
595 void STag_surfxml_link(){
596 property_sets.push_back(new std::unordered_map<std::string, std::string>());
599 void ETag_surfxml_link(){
600 simgrid::kernel::routing::LinkCreationArgs link;
602 link.properties = property_sets.back();
603 property_sets.pop_back();
605 link.id = std::string(A_surfxml_link_id);
606 link.bandwidths = surf_parse_get_bandwidths(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
607 link.bandwidth_trace = A_surfxml_link_bandwidth___file[0]
608 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_bandwidth___file)
610 link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
611 link.latency_trace = A_surfxml_link_latency___file[0]
612 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_latency___file)
614 link.state_trace = A_surfxml_link_state___file[0]
615 ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_state___file)
618 switch (A_surfxml_link_sharing___policy) {
619 case A_surfxml_link_sharing___policy_SHARED:
620 link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
622 case A_surfxml_link_sharing___policy_FATPIPE:
623 link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
625 case A_surfxml_link_sharing___policy_FULLDUPLEX:
626 XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
627 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
629 case A_surfxml_link_sharing___policy_SPLITDUPLEX:
630 link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
632 case A_surfxml_link_sharing___policy_WIFI:
633 link.policy = simgrid::s4u::Link::SharingPolicy::WIFI;
636 surf_parse_error(std::string("Invalid sharing policy in link ") + link.id);
639 sg_platf_new_link(&link);
642 void STag_surfxml_link___ctn()
644 simgrid::kernel::resource::LinkImpl* link = nullptr;
645 switch (A_surfxml_link___ctn_direction) {
646 case AU_surfxml_link___ctn_direction:
647 case A_surfxml_link___ctn_direction_NONE:
648 link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id))->get_impl();
650 case A_surfxml_link___ctn_direction_UP:
651 link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_UP")->get_impl();
653 case A_surfxml_link___ctn_direction_DOWN:
654 link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_DOWN")->get_impl();
657 surf_parse_error(std::string("Invalid direction for link ") + A_surfxml_link___ctn_id);
660 const char* dirname = "";
661 switch (A_surfxml_link___ctn_direction) {
662 case A_surfxml_link___ctn_direction_UP:
663 dirname = " (upward)";
665 case A_surfxml_link___ctn_direction_DOWN:
666 dirname = " (downward)";
671 surf_parse_assert(link != nullptr, std::string("No such link: '") + A_surfxml_link___ctn_id + "'" + dirname);
672 parsed_link_list.push_back(link);
675 void ETag_surfxml_backbone(){
676 simgrid::kernel::routing::LinkCreationArgs link;
678 link.properties = nullptr;
679 link.id = std::string(A_surfxml_backbone_id);
680 link.bandwidths.push_back(
681 surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str()));
682 link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
683 link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
685 sg_platf_new_link(&link);
686 routing_cluster_add_backbone(simgrid::s4u::Link::by_name(std::string(A_surfxml_backbone_id))->get_impl());
689 void STag_surfxml_route(){
690 surf_parse_assert_netpoint(A_surfxml_route_src, "Route src='", "' does name a node.");
691 surf_parse_assert_netpoint(A_surfxml_route_dst, "Route dst='", "' does name a node.");
694 void STag_surfxml_ASroute(){
695 surf_parse_assert_netpoint(A_surfxml_ASroute_src, "ASroute src='", "' does name a node.");
696 surf_parse_assert_netpoint(A_surfxml_ASroute_dst, "ASroute dst='", "' does name a node.");
698 surf_parse_assert_netpoint(A_surfxml_ASroute_gw___src, "ASroute gw_src='", "' does name a node.");
699 surf_parse_assert_netpoint(A_surfxml_ASroute_gw___dst, "ASroute gw_dst='", "' does name a node.");
701 void STag_surfxml_zoneRoute(){
702 surf_parse_assert_netpoint(A_surfxml_zoneRoute_src, "zoneRoute src='", "' does name a node.");
703 surf_parse_assert_netpoint(A_surfxml_zoneRoute_dst, "zoneRoute dst='", "' does name a node.");
704 surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___src, "zoneRoute gw_src='", "' does name a node.");
705 surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___dst, "zoneRoute gw_dst='", "' does name a node.");
708 void STag_surfxml_bypassRoute(){
709 surf_parse_assert_netpoint(A_surfxml_bypassRoute_src, "bypassRoute src='", "' does name a node.");
710 surf_parse_assert_netpoint(A_surfxml_bypassRoute_dst, "bypassRoute dst='", "' does name a node.");
713 void STag_surfxml_bypassASroute(){
714 surf_parse_assert_netpoint(A_surfxml_bypassASroute_src, "bypassASroute src='", "' does name a node.");
715 surf_parse_assert_netpoint(A_surfxml_bypassASroute_dst, "bypassASroute dst='", "' does name a node.");
716 surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___src, "bypassASroute gw_src='", "' does name a node.");
717 surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___dst, "bypassASroute gw_dst='", "' does name a node.");
719 void STag_surfxml_bypassZoneRoute(){
720 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_src, "bypassZoneRoute src='", "' does name a node.");
721 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_dst, "bypassZoneRoute dst='", "' does name a node.");
722 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___src, "bypassZoneRoute gw_src='", "' does name a node.");
723 surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___dst, "bypassZoneRoute gw_dst='", "' does name a node.");
726 void ETag_surfxml_route(){
727 simgrid::kernel::routing::RouteCreationArgs route;
729 route.src = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
730 route.dst = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
731 route.gw_src = nullptr;
732 route.gw_dst = nullptr;
733 route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
735 route.link_list.swap(parsed_link_list);
737 sg_platf_new_route(&route);
740 void ETag_surfxml_ASroute()
742 AX_surfxml_zoneRoute_src = AX_surfxml_ASroute_src;
743 AX_surfxml_zoneRoute_dst = AX_surfxml_ASroute_dst;
744 AX_surfxml_zoneRoute_gw___src = AX_surfxml_ASroute_gw___src;
745 AX_surfxml_zoneRoute_gw___dst = AX_surfxml_ASroute_gw___dst;
746 AX_surfxml_zoneRoute_symmetrical = (AT_surfxml_zoneRoute_symmetrical)AX_surfxml_ASroute_symmetrical;
747 ETag_surfxml_zoneRoute();
749 void ETag_surfxml_zoneRoute()
751 simgrid::kernel::routing::RouteCreationArgs ASroute;
753 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src); // tested to not be nullptr in start tag
754 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst); // tested to not be nullptr in start tag
756 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src); // tested to not be nullptr in start tag
757 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst); // tested to not be nullptr in start tag
759 ASroute.link_list.swap(parsed_link_list);
761 switch (A_surfxml_zoneRoute_symmetrical) {
762 case AU_surfxml_zoneRoute_symmetrical:
763 case A_surfxml_zoneRoute_symmetrical_YES:
764 ASroute.symmetrical = true;
766 case A_surfxml_zoneRoute_symmetrical_NO:
767 ASroute.symmetrical = false;
773 sg_platf_new_route(&ASroute);
776 void ETag_surfxml_bypassRoute(){
777 simgrid::kernel::routing::RouteCreationArgs route;
779 route.src = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
780 route.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
781 route.gw_src = nullptr;
782 route.gw_dst = nullptr;
783 route.symmetrical = false;
785 route.link_list.swap(parsed_link_list);
787 sg_platf_new_bypassRoute(&route);
790 void ETag_surfxml_bypassASroute()
792 AX_surfxml_bypassZoneRoute_src = AX_surfxml_bypassASroute_src;
793 AX_surfxml_bypassZoneRoute_dst = AX_surfxml_bypassASroute_dst;
794 AX_surfxml_bypassZoneRoute_gw___src = AX_surfxml_bypassASroute_gw___src;
795 AX_surfxml_bypassZoneRoute_gw___dst = AX_surfxml_bypassASroute_gw___dst;
796 ETag_surfxml_bypassZoneRoute();
798 void ETag_surfxml_bypassZoneRoute()
800 simgrid::kernel::routing::RouteCreationArgs ASroute;
802 ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src);
803 ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst);
804 ASroute.link_list.swap(parsed_link_list);
806 ASroute.symmetrical = false;
808 ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src);
809 ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst);
811 sg_platf_new_bypassRoute(&ASroute);
814 void ETag_surfxml_trace(){
815 simgrid::kernel::routing::ProfileCreationArgs trace;
817 trace.id = A_surfxml_trace_id;
818 trace.file = A_surfxml_trace_file;
819 trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
820 trace.pc_data = surfxml_pcdata;
822 sg_platf_new_trace(&trace);
825 void STag_surfxml_trace___connect()
827 simgrid::kernel::routing::TraceConnectCreationArgs trace_connect;
829 trace_connect.element = A_surfxml_trace___connect_element;
830 trace_connect.trace = A_surfxml_trace___connect_trace;
832 switch (A_surfxml_trace___connect_kind) {
833 case AU_surfxml_trace___connect_kind:
834 case A_surfxml_trace___connect_kind_SPEED:
835 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::SPEED;
837 case A_surfxml_trace___connect_kind_BANDWIDTH:
838 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::BANDWIDTH;
840 case A_surfxml_trace___connect_kind_HOST___AVAIL:
841 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL;
843 case A_surfxml_trace___connect_kind_LATENCY:
844 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LATENCY;
846 case A_surfxml_trace___connect_kind_LINK___AVAIL:
847 trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL;
850 surf_parse_error("Invalid trace kind");
852 sg_platf_trace_connect(&trace_connect);
855 void STag_surfxml_AS()
857 AX_surfxml_zone_id = AX_surfxml_AS_id;
858 AX_surfxml_zone_routing = (AT_surfxml_zone_routing)AX_surfxml_AS_routing;
862 void ETag_surfxml_AS()
867 void STag_surfxml_zone()
869 property_sets.push_back(new std::unordered_map<std::string, std::string>());
870 simgrid::kernel::routing::ZoneCreationArgs zone;
871 zone.id = A_surfxml_zone_id;
872 zone.routing = static_cast<int>(A_surfxml_zone_routing);
873 sg_platf_new_Zone_begin(&zone);
876 void ETag_surfxml_zone()
878 sg_platf_new_Zone_set_properties(property_sets.back());
879 delete property_sets.back();
880 property_sets.pop_back();
882 sg_platf_new_Zone_seal();
885 void STag_surfxml_config()
887 property_sets.push_back(new std::unordered_map<std::string, std::string>());
888 XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
889 if (_sg_cfg_init_status == 2) {
890 surf_parse_error("All <config> tags must be given before any platform elements (such as <zone>, <host>, <cluster>, "
895 void ETag_surfxml_config()
897 // Sort config elements before applying.
898 // That's a little waste of time, but not doing so would break the tests
899 auto current_property_set = property_sets.back();
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 property_sets.pop_back();
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()
929 property_sets.push_back(new std::unordered_map<std::string, std::string>());
930 arguments.assign(1, A_surfxml_actor_function);
933 void ETag_surfxml_process()
935 AX_surfxml_actor_host = AX_surfxml_process_host;
936 AX_surfxml_actor_function = AX_surfxml_process_function;
937 AX_surfxml_actor_start___time = AX_surfxml_process_start___time;
938 AX_surfxml_actor_kill___time = AX_surfxml_process_kill___time;
939 AX_surfxml_actor_on___failure = (AT_surfxml_actor_on___failure)AX_surfxml_process_on___failure;
940 ETag_surfxml_actor();
943 void ETag_surfxml_actor()
945 simgrid::kernel::routing::ActorCreationArgs actor;
947 actor.properties = property_sets.back();
948 property_sets.pop_back();
950 actor.args.swap(arguments);
951 actor.host = A_surfxml_actor_host;
952 actor.function = A_surfxml_actor_function;
953 actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time);
954 actor.kill_time = surf_parse_get_double(A_surfxml_actor_kill___time);
956 switch (A_surfxml_actor_on___failure) {
957 case AU_surfxml_actor_on___failure:
958 case A_surfxml_actor_on___failure_DIE:
959 actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
961 case A_surfxml_actor_on___failure_RESTART:
962 actor.on_failure = simgrid::kernel::routing::ActorOnFailure::RESTART;
965 surf_parse_error("Invalid on failure behavior");
968 sg_platf_new_actor(&actor);
971 void STag_surfxml_argument(){
972 arguments.push_back(A_surfxml_argument_value);
975 void STag_surfxml_model___prop(){
976 if (not current_model_property_set)
977 current_model_property_set = new std::unordered_map<std::string, std::string>();
979 current_model_property_set->insert({A_surfxml_model___prop_id, A_surfxml_model___prop_value});
982 void ETag_surfxml_prop(){/* Nothing to do */}
983 void STag_surfxml_random(){/* Nothing to do */}
984 void ETag_surfxml_random(){/* Nothing to do */}
985 void ETag_surfxml_trace___connect(){/* Nothing to do */}
986 void STag_surfxml_trace()
987 { /* Nothing to do */
989 void ETag_surfxml_router(){/*Nothing to do*/}
990 void ETag_surfxml_host___link(){/* Nothing to do */}
991 void ETag_surfxml_cabinet(){/* Nothing to do */}
992 void ETag_surfxml_peer(){/* Nothing to do */}
993 void STag_surfxml_backbone(){/* Nothing to do */}
994 void ETag_surfxml_link___ctn(){/* Nothing to do */}
995 void ETag_surfxml_argument(){/* Nothing to do */}
996 void ETag_surfxml_model___prop(){/* Nothing to do */}
998 /* Open and Close parse file */
999 YY_BUFFER_STATE surf_input_buffer;
1001 void surf_parse_open(const std::string& file)
1003 surf_parsed_filename = file;
1004 std::string dir = simgrid::xbt::Path(file).get_dir_name();
1005 surf_path.push_back(dir);
1007 surf_file_to_parse = surf_fopen(file, "r");
1008 if (surf_file_to_parse == nullptr)
1009 throw std::invalid_argument(std::string("Unable to open ')") + file + "' from '" + simgrid::xbt::Path().get_name() +
1010 "'. Does this file exist?");
1011 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1012 surf_parse__switch_to_buffer(surf_input_buffer);
1013 surf_parse_lineno = 1;
1016 void surf_parse_close()
1018 surf_path.pop_back(); // remove the dirname of the opened file, that was added in surf_parse_open()
1020 if (surf_file_to_parse) {
1021 surf_parse__delete_buffer(surf_input_buffer);
1022 fclose(surf_file_to_parse);
1023 surf_file_to_parse = nullptr; //Must be reset for Bypass
1027 /* Call the lexer to parse the currently opened file */
1030 bool err = surf_parse_lex();
1031 surf_parse_assert(not err, "Flex returned an error code");