1 /* Copyright (c) 2006-2022. 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/s4u/Engine.hpp>
8 #include "src/kernel/resource/CpuImpl.hpp"
9 #include "src/kernel/resource/LinkImpl.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include "src/surf/xml/platf.hpp"
12 #include "src/surf/xml/platf_private.hpp"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
18 /* Trace related stuff */
19 XBT_PRIVATE std::unordered_map<std::string, simgrid::kernel::profile::Profile*> traces_set_list;
20 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_avail;
21 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_host_speed;
22 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_avail;
23 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_bw;
24 XBT_PRIVATE std::unordered_map<std::string, std::string> trace_connect_list_link_lat;
26 void sg_platf_trace_connect(simgrid::kernel::routing::TraceConnectCreationArgs* trace_connect)
28 surf_parse_assert(traces_set_list.find(trace_connect->trace) != traces_set_list.end(),
29 std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
32 switch (trace_connect->kind) {
33 case simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL:
34 trace_connect_list_host_avail.insert({trace_connect->trace, trace_connect->element});
36 case simgrid::kernel::routing::TraceConnectKind::SPEED:
37 trace_connect_list_host_speed.insert({trace_connect->trace, trace_connect->element});
39 case simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL:
40 trace_connect_list_link_avail.insert({trace_connect->trace, trace_connect->element});
42 case simgrid::kernel::routing::TraceConnectKind::BANDWIDTH:
43 trace_connect_list_link_bw.insert({trace_connect->trace, trace_connect->element});
45 case simgrid::kernel::routing::TraceConnectKind::LATENCY:
46 trace_connect_list_link_lat.insert({trace_connect->trace, trace_connect->element});
49 surf_parse_error(std::string("Cannot connect trace ") + trace_connect->trace + " to " + trace_connect->element +
50 ": unknown kind of trace");
54 /* This function acts as a main in the parsing area. */
55 void parse_platform_file(const std::string& file)
59 /* init the flex parser */
60 surf_parse_open(file);
62 /* Do the actual parsing */
65 /* Get the Engine singleton once and for all*/
66 const auto engine = simgrid::s4u::Engine::get_instance();
68 /* connect all profiles relative to hosts */
69 for (auto const& elm : trace_connect_list_host_avail) {
70 surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(),
71 std::string("<trace_connect kind=\"HOST_AVAIL\">: Trace ") + elm.first + " undefined.");
72 auto profile = traces_set_list.at(elm.first);
74 auto host = engine->host_by_name_or_null(elm.second);
75 surf_parse_assert(host, std::string("<trace_connect kind=\"HOST_AVAIL\">: Host ") + elm.second + " undefined.");
76 host->set_state_profile(profile);
79 for (auto const& elm : trace_connect_list_host_speed) {
80 surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(),
81 std::string("<trace_connect kind=\"SPEED\">: Trace ") + elm.first + " undefined.");
82 auto profile = traces_set_list.at(elm.first);
84 auto host = engine->host_by_name_or_null(elm.second);
85 surf_parse_assert(host, std::string("<trace_connect kind=\"SPEED\">: Host ") + elm.second + " undefined.");
86 host->set_speed_profile(profile);
89 for (auto const& elm : trace_connect_list_link_avail) {
90 surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(),
91 std::string("<trace_connect kind=\"LINK_AVAIL\">: Trace ") + elm.first + " undefined.");
92 auto profile = traces_set_list.at(elm.first);
94 auto link = engine->link_by_name_or_null(elm.second);
95 surf_parse_assert(link, std::string("<trace_connect kind=\"LINK_AVAIL\">: Link ") + elm.second + " undefined.");
96 link->set_state_profile(profile);
99 for (auto const& elm : trace_connect_list_link_bw) {
100 surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(),
101 std::string("<trace_connect kind=\"BANDWIDTH\">: Trace ") + elm.first + " undefined.");
102 auto profile = traces_set_list.at(elm.first);
104 auto link = engine->link_by_name_or_null(elm.second);
105 surf_parse_assert(link, std::string("<trace_connect kind=\"BANDWIDTH\">: Link ") + elm.second + " undefined.");
106 link->set_bandwidth_profile(profile);
109 for (auto const& elm : trace_connect_list_link_lat) {
110 surf_parse_assert(traces_set_list.find(elm.first) != traces_set_list.end(),
111 std::string("<trace_connect kind=\"LATENCY\">: Trace ") + elm.first + " undefined.");
112 auto profile = traces_set_list.at(elm.first);
114 auto link = engine->link_by_name_or_null(elm.second);
115 surf_parse_assert(link, std::string("<trace_connect kind=\"LATENCY\">: Link ") + elm.second + " undefined.");
116 link->set_latency_profile(profile);