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. */
8 #include "network_interface.hpp"
9 #include "simgrid/sg_config.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");
20 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
22 LinkImpl* LinkImpl::byName(std::string name)
24 auto link = links->find(name);
25 return link == links->end() ? nullptr : link->second;
27 /** @brief Returns the amount of links in the platform */
28 int LinkImpl::linksCount()
32 void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
34 for (auto const& kv : *links) {
35 linkList->push_back(&kv.second->piface_);
39 /** @brief Returns a list of all existing links */
40 LinkImpl** LinkImpl::linksList()
42 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
44 for (auto const& kv : *links) {
50 /** @brief destructor of the static data */
51 void LinkImpl::linksExit()
53 for (auto const& kv : *links)
54 (kv.second)->destroy();
64 simgrid::surf::NetworkModel *surf_network_model = nullptr;
69 /** @brief Command-line option 'network/TCP-gamma' -- see \ref options_model_network_gamma */
70 simgrid::config::Flag<double> NetworkModel::cfg_tcp_gamma(
71 {"network/TCP-gamma", "network/TCP_gamma"},
72 "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
73 "Use the last given value, which is the max window size)",
76 /** @brief Command-line option 'network/crosstraffic' -- see \ref options_model_network_crosstraffic */
77 simgrid::config::Flag<bool> NetworkModel::cfg_crosstraffic(
78 "network/crosstraffic",
79 "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
81 NetworkModel::~NetworkModel() = default;
83 double NetworkModel::latencyFactor(double /*size*/) {
84 return sg_latency_factor;
87 double NetworkModel::bandwidthFactor(double /*size*/) {
88 return sg_bandwidth_factor;
91 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
95 double NetworkModel::next_occuring_event_full(double now)
97 double minRes = Model::next_occuring_event_full(now);
99 for (kernel::resource::Action const& action : *get_running_action_set()) {
100 const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
101 if (net_action.latency_ > 0)
102 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
105 XBT_DEBUG("Min of share resources %f", minRes);
114 LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, kernel::lmm::Constraint* constraint)
115 : Resource(model, name, constraint), piface_(this)
118 if (name != "__loopback__")
119 xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
122 bandwidth_.scale = 1;
124 links->insert({name, this});
125 XBT_DEBUG("Create link '%s'", name.c_str());
128 /** @brief use destroy() instead of this destructor */
129 LinkImpl::~LinkImpl()
131 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
133 /** @brief Fire the required callbacks and destroy the object
135 * Don't delete directly a Link, call l->destroy() instead.
137 void LinkImpl::destroy()
139 if (not currentlyDestroying_) {
140 currentlyDestroying_ = true;
141 s4u::Link::onDestruction(this->piface_);
146 bool LinkImpl::is_used()
148 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
151 double LinkImpl::latency()
153 return latency_.peak * latency_.scale;
156 double LinkImpl::bandwidth()
158 return bandwidth_.peak * bandwidth_.scale;
161 int LinkImpl::sharingPolicy()
163 return get_constraint()->get_sharing_policy();
166 void LinkImpl::turn_on()
170 s4u::Link::onStateChange(this->piface_);
173 void LinkImpl::turn_off()
176 Resource::turn_off();
177 s4u::Link::onStateChange(this->piface_);
180 void LinkImpl::setStateTrace(tmgr_trace_t trace)
182 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", get_cname());
183 stateEvent_ = future_evt_set->add_trace(trace, this);
185 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
187 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", get_cname());
188 bandwidth_.event = future_evt_set->add_trace(trace, this);
190 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
192 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", get_cname());
193 latency_.event = future_evt_set->add_trace(trace, this);
201 void NetworkAction::set_state(Action::State state)
203 Action::set_state(state);
204 s4u::Link::onCommunicationStateChange(this);
207 /** @brief returns a list of all Links that this action is using */
208 std::list<LinkImpl*> NetworkAction::links()
210 std::list<LinkImpl*> retlist;
211 int llen = get_variable()->get_number_of_constraint();
213 for (int i = 0; i < llen; i++) {
214 /* Beware of composite actions: ptasks put links and cpus together */
215 // extra pb: we cannot dynamic_cast from void*...
216 kernel::resource::Resource* resource =
217 static_cast<kernel::resource::Resource*>(get_variable()->get_constraint(i)->get_id());
218 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
220 retlist.push_back(link);
228 #endif /* NETWORK_INTERFACE_CPP_ */