1 /* Copyright (c) 2013-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 "simgrid/sg_config.hpp"
9 #include "src/kernel/resource/NetworkModel.hpp"
10 #include "src/kernel/resource/profile/Profile.hpp"
11 #include "src/smpi/include/smpi_utils.hpp"
12 #include "src/surf/surf_interface.hpp"
16 #ifndef NETWORK_INTERFACE_CPP_
17 #define NETWORK_INTERFACE_CPP_
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_network, ker_resource, "Network resources, that fuel communications");
25 namespace simgrid::kernel::resource {
26 static smpi::utils::FactorSet cfg_latency_factor("network/latency-factor");
27 static smpi::utils::FactorSet cfg_bandwidth_factor("network/bandwidth-factor");
29 config::Flag<std::string> cfg_latency_factor_str(
30 "network/latency-factor", {{"smpi/lat-factor"}},
31 "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0");
32 static config::Flag<std::string> cfg_bandwidth_factor_str(
33 "network/bandwidth-factor", {{"smpi/bw-factor"}},
34 "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
36 double NetworkModel::get_latency_factor(double size)
38 if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
39 cfg_latency_factor.parse(cfg_latency_factor_str.get());
41 return cfg_latency_factor(size);
43 double NetworkModel::get_bandwidth_factor(double size)
45 if (not cfg_bandwidth_factor.is_initialized())
46 cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
48 return cfg_bandwidth_factor(size);
51 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
52 config::Flag<double> NetworkModel::cfg_tcp_gamma(
54 "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
55 "Use the last given value, which is the max window size)",
58 /** @brief Command-line option 'network/crosstraffic' -- see @ref options_model_network_crosstraffic */
59 config::Flag<bool> NetworkModel::cfg_crosstraffic(
60 "network/crosstraffic",
61 "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
63 config::Flag<double> NetworkModel::cfg_weight_S_parameter(
65 "Correction factor to apply to the weight of competing streams (default value set by network model)", 0.0);
67 NetworkModel::~NetworkModel() = default;
69 double NetworkModel::next_occurring_event_full(double now)
71 double minRes = Model::next_occurring_event_full(now);
73 for (Action const& action : *get_started_action_set()) {
74 const auto& net_action = static_cast<const NetworkAction&>(action);
75 if (net_action.latency_ > 0)
76 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
79 XBT_DEBUG("Min of share resources %f", minRes);
88 void NetworkAction::set_state(Action::State state)
90 Action::State previous = get_state();
91 if (previous != state) { // Trigger only if the state changed
92 Action::set_state(state);
93 s4u::Link::on_communication_state_change(*this, previous);
97 /** @brief returns a list of all Links that this action is using */
98 std::list<StandardLinkImpl*> NetworkAction::get_links() const
100 std::list<StandardLinkImpl*> retlist;
101 int llen = get_variable()->get_number_of_constraint();
103 for (int i = 0; i < llen; i++) {
104 /* Beware of composite actions: ptasks put links and cpus together */
105 if (auto* link = dynamic_cast<StandardLinkImpl*>(get_variable()->get_constraint(i)->get_id()))
106 retlist.push_back(link);
112 static void add_latency(const std::vector<StandardLinkImpl*>& links, double* latency)
115 *latency = std::accumulate(begin(links), end(links), *latency,
116 [](double lat, const auto* link) { return lat + link->get_latency(); });
119 void add_link_latency(std::vector<StandardLinkImpl*>& result, StandardLinkImpl* link, double* latency)
121 result.push_back(link);
123 *latency += link->get_latency();
126 void add_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
129 result.insert(result.end(), begin(links), end(links));
130 add_latency(links, latency);
133 void insert_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
136 result.insert(result.begin(), rbegin(links), rend(links));
137 add_latency(links, latency);
140 } // namespace simgrid::kernel::resource
142 #endif /* NETWORK_INTERFACE_CPP_ */