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"
9 #include "surf/surf.hpp"
11 #ifndef NETWORK_INTERFACE_CPP_
12 #define NETWORK_INTERFACE_CPP_
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
21 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
23 LinkImpl* LinkImpl::byName(std::string name)
25 auto link = links->find(name);
26 return link == links->end() ? nullptr : link->second;
28 /** @brief Returns the amount of links in the platform */
29 int LinkImpl::linksCount()
33 void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
35 for (auto const& kv : *links) {
36 linkList->push_back(&kv.second->piface_);
40 /** @brief Returns a list of all existing links */
41 LinkImpl** LinkImpl::linksList()
43 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
45 for (auto const& kv : *links) {
51 /** @brief destructor of the static data */
52 void LinkImpl::linksExit()
54 for (auto const& kv : *links)
55 (kv.second)->destroy();
60 } // namespace simgrid
66 simgrid::kernel::resource::NetworkModel* surf_network_model = nullptr;
72 /** @brief Command-line option 'network/TCP-gamma' -- see \ref options_model_network_gamma */
73 simgrid::config::Flag<double> NetworkModel::cfg_tcp_gamma(
74 "network/TCP-gamma", {"network/TCP_gamma"},
75 "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
76 "Use the last given value, which is the max window size)",
79 /** @brief Command-line option 'network/crosstraffic' -- see \ref options_model_network_crosstraffic */
80 simgrid::config::Flag<bool> NetworkModel::cfg_crosstraffic(
81 "network/crosstraffic",
82 "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
84 NetworkModel::~NetworkModel() = default;
86 double NetworkModel::latencyFactor(double /*size*/)
88 return sg_latency_factor;
91 double NetworkModel::bandwidthFactor(double /*size*/)
93 return sg_bandwidth_factor;
96 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/)
101 double NetworkModel::next_occuring_event_full(double now)
103 double minRes = Model::next_occuring_event_full(now);
105 for (Action const& action : *get_started_action_set()) {
106 const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
107 if (net_action.latency_ > 0)
108 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
111 XBT_DEBUG("Min of share resources %f", minRes);
120 LinkImpl::LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint)
121 : Resource(model, name, constraint), piface_(this)
124 if (name != "__loopback__")
125 xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
128 bandwidth_.scale = 1;
130 links->insert({name, this});
131 XBT_DEBUG("Create link '%s'", name.c_str());
134 /** @brief use destroy() instead of this destructor */
135 LinkImpl::~LinkImpl()
137 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
139 /** @brief Fire the required callbacks and destroy the object
141 * Don't delete directly a Link, call l->destroy() instead.
143 void LinkImpl::destroy()
145 if (not currentlyDestroying_) {
146 currentlyDestroying_ = true;
147 s4u::Link::on_destruction(this->piface_);
152 bool LinkImpl::is_used()
154 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
157 double LinkImpl::latency()
159 return latency_.peak * latency_.scale;
162 double LinkImpl::bandwidth()
164 return bandwidth_.peak * bandwidth_.scale;
167 s4u::Link::SharingPolicy LinkImpl::sharingPolicy()
169 return get_constraint()->get_sharing_policy();
172 void LinkImpl::turn_on()
176 s4u::Link::on_state_change(this->piface_);
180 void LinkImpl::turn_off()
183 Resource::turn_off();
184 s4u::Link::on_state_change(this->piface_);
188 void LinkImpl::on_bandwidth_change()
190 s4u::Link::on_bandwidth_change(this->piface_);
193 void LinkImpl::setStateTrace(tmgr_trace_t trace)
195 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", get_cname());
196 stateEvent_ = future_evt_set->add_trace(trace, this);
198 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
200 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", get_cname());
201 bandwidth_.event = future_evt_set->add_trace(trace, this);
203 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
205 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", get_cname());
206 latency_.event = future_evt_set->add_trace(trace, this);
213 void NetworkAction::set_state(Action::State state)
215 Action::State previous = get_state();
216 Action::set_state(state);
217 if (previous != state) // Trigger only if the state changed
218 s4u::Link::on_communication_state_change(this);
221 /** @brief returns a list of all Links that this action is using */
222 std::list<LinkImpl*> NetworkAction::links()
224 std::list<LinkImpl*> retlist;
225 int llen = get_variable()->get_number_of_constraint();
227 for (int i = 0; i < llen; i++) {
228 /* Beware of composite actions: ptasks put links and cpus together */
229 // extra pb: we cannot dynamic_cast from void*...
230 Resource* resource = static_cast<Resource*>(get_variable()->get_constraint(i)->get_id());
231 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
233 retlist.push_back(link);
239 } // namespace kernel
242 #endif /* NETWORK_INTERFACE_CPP_ */