1 /* Copyright (c) 2013-2018. 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/sg_config.hpp"
8 #include "src/surf/surf_interface.hpp"
10 #ifndef NETWORK_INTERFACE_CPP_
11 #define NETWORK_INTERFACE_CPP_
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
19 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
21 LinkImpl* LinkImpl::byName(std::string name)
23 auto link = links->find(name);
24 return link == links->end() ? nullptr : link->second;
26 /** @brief Returns the amount of links in the platform */
27 int LinkImpl::linksCount()
31 void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
33 for (auto const& kv : *links) {
34 linkList->push_back(&kv.second->piface_);
38 /** @brief Returns a list of all existing links */
39 LinkImpl** LinkImpl::linksList()
41 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
43 for (auto const& kv : *links) {
49 /** @brief destructor of the static data */
50 void LinkImpl::linksExit()
52 for (auto const& kv : *links)
53 (kv.second)->destroy();
63 simgrid::surf::NetworkModel *surf_network_model = nullptr;
68 /** @brief Command-line option 'network/TCP-gamma' -- see \ref options_model_network_gamma */
69 simgrid::config::Flag<double> NetworkModel::cfg_tcp_gamma(
70 {"network/TCP-gamma", "network/TCP_gamma"},
71 "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
72 "Use the last given value, which is the max window size)",
75 /** @brief Command-line option 'network/crosstraffic' -- see \ref options_model_network_crosstraffic */
76 simgrid::config::Flag<bool> NetworkModel::cfg_crosstraffic(
77 "network/crosstraffic",
78 "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
80 NetworkModel::~NetworkModel() = default;
82 double NetworkModel::latencyFactor(double /*size*/) {
83 return sg_latency_factor;
86 double NetworkModel::bandwidthFactor(double /*size*/) {
87 return sg_bandwidth_factor;
90 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
94 double NetworkModel::next_occuring_event_full(double now)
96 double minRes = Model::next_occuring_event_full(now);
98 for (kernel::resource::Action const& action : *get_running_action_set()) {
99 const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
100 if (net_action.latency_ > 0)
101 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
104 XBT_DEBUG("Min of share resources %f", minRes);
113 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, kernel::lmm::Constraint* constraint)
114 : Resource(model, name, constraint), piface_(this)
117 if (name != "__loopback__")
118 xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
121 bandwidth_.scale = 1;
123 links->insert({name, this});
124 XBT_DEBUG("Create link '%s'", name.c_str());
127 /** @brief use destroy() instead of this destructor */
128 LinkImpl::~LinkImpl()
130 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
132 /** @brief Fire the required callbacks and destroy the object
134 * Don't delete directly a Link, call l->destroy() instead.
136 void LinkImpl::destroy()
138 if (not currentlyDestroying_) {
139 currentlyDestroying_ = true;
140 s4u::Link::onDestruction(this->piface_);
145 bool LinkImpl::is_used()
147 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
150 double LinkImpl::latency()
152 return latency_.peak * latency_.scale;
155 double LinkImpl::bandwidth()
157 return bandwidth_.peak * bandwidth_.scale;
160 int LinkImpl::sharingPolicy()
162 return get_constraint()->get_sharing_policy();
165 void LinkImpl::turn_on()
169 s4u::Link::onStateChange(this->piface_);
172 void LinkImpl::turn_off()
175 Resource::turn_off();
176 s4u::Link::onStateChange(this->piface_);
179 void LinkImpl::setStateTrace(tmgr_trace_t trace)
181 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", get_cname());
182 stateEvent_ = future_evt_set->add_trace(trace, this);
184 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
186 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", get_cname());
187 bandwidth_.event = future_evt_set->add_trace(trace, this);
189 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
191 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", get_cname());
192 latency_.event = future_evt_set->add_trace(trace, this);
200 void NetworkAction::set_state(Action::State state)
202 Action::set_state(state);
203 s4u::Link::onCommunicationStateChange(this);
206 /** @brief returns a list of all Links that this action is using */
207 std::list<LinkImpl*> NetworkAction::links()
209 std::list<LinkImpl*> retlist;
210 int llen = get_variable()->get_number_of_constraint();
212 for (int i = 0; i < llen; i++) {
213 /* Beware of composite actions: ptasks put links and cpus together */
214 // extra pb: we cannot dynamic_cast from void*...
215 kernel::resource::Resource* resource =
216 static_cast<kernel::resource::Resource*>(get_variable()->get_constraint(i)->get_id());
217 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
219 retlist.push_back(link);
227 #endif /* NETWORK_INTERFACE_CPP_ */