1 /* Copyright (c) 2013-2021. 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 "network_interface.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/resource/profile/Profile.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include "surf/surf.hpp"
15 #ifndef NETWORK_INTERFACE_CPP_
16 #define NETWORK_INTERFACE_CPP_
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_network, ker_resource, "Network resources, that fuel communications");
28 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
29 config::Flag<double> NetworkModel::cfg_tcp_gamma(
31 "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
32 "Use the last given value, which is the max window size)",
35 /** @brief Command-line option 'network/crosstraffic' -- see @ref options_model_network_crosstraffic */
36 config::Flag<bool> NetworkModel::cfg_crosstraffic(
37 "network/crosstraffic",
38 "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
40 NetworkModel::~NetworkModel() = default;
42 double NetworkModel::next_occurring_event_full(double now)
44 double minRes = Model::next_occurring_event_full(now);
46 for (Action const& action : *get_started_action_set()) {
47 const auto& net_action = static_cast<const NetworkAction&>(action);
48 if (net_action.latency_ > 0)
49 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
52 XBT_DEBUG("Min of share resources %f", minRes);
61 LinkImpl::LinkImpl(const std::string& name) : Resource_T(name), piface_(this)
63 if (name != "__loopback__")
64 xbt_assert(not s4u::Link::by_name_or_null(name), "Link '%s' declared several times in the platform.", name.c_str());
66 s4u::Engine::get_instance()->link_register(name, &piface_);
67 XBT_DEBUG("Create link '%s'", name.c_str());
70 /** @brief Fire the required callbacks and destroy the object
72 * Don't delete directly a Link, call l->destroy() instead.
74 void LinkImpl::destroy()
76 s4u::Link::on_destruction(this->piface_);
80 bool LinkImpl::is_used() const
82 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
85 LinkImpl* LinkImpl::set_sharing_policy(s4u::Link::SharingPolicy policy)
87 lmm::Constraint::SharingPolicy ct_policy = lmm::Constraint::SharingPolicy::SHARED;
88 if (policy == s4u::Link::SharingPolicy::FATPIPE)
89 ct_policy = lmm::Constraint::SharingPolicy::FATPIPE;
90 get_constraint()->set_sharing_policy(ct_policy);
91 sharing_policy_ = policy;
94 s4u::Link::SharingPolicy LinkImpl::get_sharing_policy() const
96 return sharing_policy_;
99 void LinkImpl::latency_check(double latency) const
101 static double last_warned_latency = sg_surf_precision;
102 if (latency != 0.0 && latency < last_warned_latency) {
103 XBT_WARN("Latency for link %s is smaller than surf/precision (%g < %g)."
104 " For more accuracy, consider setting \"--cfg=surf/precision:%g\".",
105 get_cname(), latency, sg_surf_precision, latency);
106 last_warned_latency = latency;
110 void LinkImpl::turn_on()
114 s4u::Link::on_state_change(piface_);
118 void LinkImpl::turn_off()
121 Resource::turn_off();
122 s4u::Link::on_state_change(piface_);
124 const kernel::lmm::Element* elem = nullptr;
125 double now = surf_get_clock();
126 while (const auto* var = get_constraint()->get_variable(&elem)) {
127 Action* action = var->get_id();
128 if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
129 action->set_finish_time(now);
130 action->set_state(Action::State::FAILED);
136 void LinkImpl::seal()
141 xbt_assert(this->get_model(), "Cannot seal Link(%s) without setting the Network model first", this->get_cname());
143 s4u::Link::on_creation(piface_);
146 void LinkImpl::on_bandwidth_change() const
148 s4u::Link::on_bandwidth_change(piface_);
151 LinkImpl* LinkImpl::set_bandwidth_profile(profile::Profile* profile)
154 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth profile to Link %s", get_cname());
155 bandwidth_.event = profile->schedule(&profile::future_evt_set, this);
160 LinkImpl* LinkImpl::set_latency_profile(profile::Profile* profile)
163 xbt_assert(latency_.event == nullptr, "Cannot set a second latency profile to Link %s", get_cname());
164 latency_.event = profile->schedule(&profile::future_evt_set, this);
169 void LinkImpl::set_concurrency_limit(int limit) const
172 get_constraint()->reset_concurrency_maximum();
174 get_constraint()->set_concurrency_limit(limit);
181 void NetworkAction::set_state(Action::State state)
183 Action::State previous = get_state();
184 if (previous != state) { // Trigger only if the state changed
185 Action::set_state(state);
186 s4u::Link::on_communication_state_change(*this, previous);
190 /** @brief returns a list of all Links that this action is using */
191 std::list<LinkImpl*> NetworkAction::get_links() const
193 std::list<LinkImpl*> retlist;
194 int llen = get_variable()->get_number_of_constraint();
196 for (int i = 0; i < llen; i++) {
197 /* Beware of composite actions: ptasks put links and cpus together */
198 if (auto* link = dynamic_cast<LinkImpl*>(get_variable()->get_constraint(i)->get_id()))
199 retlist.push_back(link);
205 static void add_latency(const std::vector<LinkImpl*>& links, double* latency)
208 *latency = std::accumulate(begin(links), end(links), *latency,
209 [](double lat, const auto* link) { return lat + link->get_latency(); });
212 void add_link_latency(std::vector<LinkImpl*>& result, LinkImpl* link, double* latency)
214 result.push_back(link);
216 *latency += link->get_latency();
219 void add_link_latency(std::vector<LinkImpl*>& result, const std::vector<LinkImpl*>& links, double* latency)
221 result.insert(result.end(), begin(links), end(links));
222 add_latency(links, latency);
225 void insert_link_latency(std::vector<LinkImpl*>& result, const std::vector<LinkImpl*>& links, double* latency)
227 result.insert(result.begin(), rbegin(links), rend(links));
228 add_latency(links, latency);
231 } // namespace resource
232 } // namespace kernel
233 } // namespace simgrid
235 #endif /* NETWORK_INTERFACE_CPP_ */