1 /* Copyright (c) 2013-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 "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"
13 #ifndef NETWORK_INTERFACE_CPP_
14 #define NETWORK_INTERFACE_CPP_
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
22 simgrid::kernel::resource::NetworkModel* surf_network_model = nullptr;
28 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
29 simgrid::config::Flag<double> NetworkModel::cfg_tcp_gamma(
30 "network/TCP-gamma", {"network/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 simgrid::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::get_latency_factor(double /*size*/)
44 return sg_latency_factor;
47 double NetworkModel::get_bandwidth_factor(double /*size*/)
49 return sg_bandwidth_factor;
52 double NetworkModel::get_bandwidth_constraint(double rate, double /*bound*/, double /*size*/)
57 double NetworkModel::next_occurring_event_full(double now)
59 double minRes = Model::next_occurring_event_full(now);
61 for (Action const& action : *get_started_action_set()) {
62 const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
63 if (net_action.latency_ > 0)
64 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
67 XBT_DEBUG("Min of share resources %f", minRes);
76 LinkImpl::LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint)
77 : Resource(model, name, constraint), piface_(this)
80 if (name != "__loopback__")
81 xbt_assert(not s4u::Link::by_name_or_null(name), "Link '%s' declared several times in the platform.", name.c_str());
86 s4u::Engine::get_instance()->link_register(name, &piface_);
87 XBT_DEBUG("Create link '%s'", name.c_str());
90 /** @brief use destroy() instead of this destructor */
93 xbt_assert(currently_destroying_, "Don't delete Links directly. Call destroy() instead.");
95 /** @brief Fire the required callbacks and destroy the object
97 * Don't delete directly a Link, call l->destroy() instead.
99 void LinkImpl::destroy()
101 if (not currently_destroying_) {
102 currently_destroying_ = true;
103 s4u::Link::on_destruction(this->piface_);
108 bool LinkImpl::is_used()
110 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
113 double LinkImpl::get_latency()
115 return latency_.peak * latency_.scale;
118 double LinkImpl::get_bandwidth()
120 return bandwidth_.peak * bandwidth_.scale;
123 s4u::Link::SharingPolicy LinkImpl::get_sharing_policy()
125 return get_constraint()->get_sharing_policy();
128 void LinkImpl::turn_on()
132 s4u::Link::on_state_change(this->piface_);
136 void LinkImpl::turn_off()
139 Resource::turn_off();
140 s4u::Link::on_state_change(this->piface_);
142 kernel::lmm::Variable* var = nullptr;
143 const kernel::lmm::Element* elem = nullptr;
144 double now = surf_get_clock();
145 while ((var = get_constraint()->get_variable(&elem))) {
146 Action* action = static_cast<Action*>(var->get_id());
147 if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
148 action->set_finish_time(now);
149 action->set_state(Action::State::FAILED);
155 void LinkImpl::on_bandwidth_change()
157 s4u::Link::on_bandwidth_change(this->piface_);
160 void LinkImpl::set_bandwidth_profile(profile::Profile* profile)
162 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth profile to Link %s", get_cname());
163 bandwidth_.event = profile->schedule(&profile::future_evt_set, this);
166 void LinkImpl::set_latency_profile(profile::Profile* profile)
168 xbt_assert(latency_.event == nullptr, "Cannot set a second latency profile to Link %s", get_cname());
169 latency_.event = profile->schedule(&profile::future_evt_set, this);
176 void NetworkAction::set_state(Action::State state)
178 Action::State previous = get_state();
179 Action::set_state(state);
180 if (previous != state) // Trigger only if the state changed
181 s4u::Link::on_communication_state_change(*this, previous);
184 /** @brief returns a list of all Links that this action is using */
185 std::list<LinkImpl*> NetworkAction::links() const
187 std::list<LinkImpl*> retlist;
188 int llen = get_variable()->get_number_of_constraint();
190 for (int i = 0; i < llen; i++) {
191 /* Beware of composite actions: ptasks put links and cpus together */
192 // extra pb: we cannot dynamic_cast from void*...
193 Resource* resource = get_variable()->get_constraint(i)->get_id();
194 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
196 retlist.push_back(link);
202 } // namespace kernel
205 #endif /* NETWORK_INTERFACE_CPP_ */