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/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
9 #include "src/surf/surf_interface.hpp"
10 #include "surf/surf.hpp"
12 #ifndef NETWORK_INTERFACE_CPP_
13 #define NETWORK_INTERFACE_CPP_
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
22 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
24 LinkImpl* LinkImpl::byName(std::string name)
26 auto link = links->find(name);
27 return link == links->end() ? nullptr : link->second;
29 /** @brief Returns the amount of links in the platform */
30 int LinkImpl::linksCount()
34 void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
36 for (auto const& kv : *links) {
37 linkList->push_back(&kv.second->piface_);
41 /** @brief Returns a list of all existing links */
42 LinkImpl** LinkImpl::linksList()
44 LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
46 for (auto const& kv : *links) {
52 /** @brief destructor of the static data */
53 void LinkImpl::linksExit()
55 for (auto const& kv : *links)
56 (kv.second)->destroy();
61 } // namespace simgrid
67 simgrid::kernel::resource::NetworkModel* surf_network_model = nullptr;
73 /** @brief Command-line option 'network/TCP-gamma' -- see \ref options_model_network_gamma */
74 simgrid::config::Flag<double> NetworkModel::cfg_tcp_gamma(
75 "network/TCP-gamma", {"network/TCP_gamma"},
76 "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
77 "Use the last given value, which is the max window size)",
80 /** @brief Command-line option 'network/crosstraffic' -- see \ref options_model_network_crosstraffic */
81 simgrid::config::Flag<bool> NetworkModel::cfg_crosstraffic(
82 "network/crosstraffic",
83 "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
85 NetworkModel::~NetworkModel() = default;
87 double NetworkModel::latencyFactor(double /*size*/)
89 return sg_latency_factor;
92 double NetworkModel::bandwidthFactor(double /*size*/)
94 return sg_bandwidth_factor;
97 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/)
102 double NetworkModel::next_occuring_event_full(double now)
104 double minRes = Model::next_occuring_event_full(now);
106 for (Action const& action : *get_running_action_set()) {
107 const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
108 if (net_action.latency_ > 0)
109 minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
112 XBT_DEBUG("Min of share resources %f", minRes);
121 LinkImpl::LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint)
122 : Resource(model, name, constraint), piface_(this)
125 if (name != "__loopback__")
126 xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
129 bandwidth_.scale = 1;
131 links->insert({name, this});
132 XBT_DEBUG("Create link '%s'", name.c_str());
135 /** @brief use destroy() instead of this destructor */
136 LinkImpl::~LinkImpl()
138 xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
140 /** @brief Fire the required callbacks and destroy the object
142 * Don't delete directly a Link, call l->destroy() instead.
144 void LinkImpl::destroy()
146 if (not currentlyDestroying_) {
147 currentlyDestroying_ = true;
148 s4u::Link::onDestruction(this->piface_);
153 bool LinkImpl::is_used()
155 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
158 double LinkImpl::latency()
160 return latency_.peak * latency_.scale;
163 double LinkImpl::bandwidth()
165 return bandwidth_.peak * bandwidth_.scale;
168 s4u::Link::SharingPolicy LinkImpl::sharingPolicy()
170 return get_constraint()->get_sharing_policy();
173 void LinkImpl::turn_on()
177 s4u::Link::onStateChange(this->piface_);
180 void LinkImpl::turn_off()
183 Resource::turn_off();
184 s4u::Link::onStateChange(this->piface_);
188 void LinkImpl::on_bandwidth_change()
190 if (TRACE_categorized() || TRACE_uncategorized() || TRACE_platform())
191 instr::Container::by_name(get_cname())
192 ->get_variable("bandwidth")
193 ->set_event(surf_get_clock(), sg_bandwidth_factor * bandwidth_.scale * bandwidth_.peak);
194 s4u::Link::on_bandwidth_change(this->piface_);
197 void LinkImpl::setStateTrace(tmgr_trace_t trace)
199 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", get_cname());
200 stateEvent_ = future_evt_set->add_trace(trace, this);
202 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
204 xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", get_cname());
205 bandwidth_.event = future_evt_set->add_trace(trace, this);
207 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
209 xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", get_cname());
210 latency_.event = future_evt_set->add_trace(trace, this);
217 void NetworkAction::set_state(Action::State state)
219 Action::set_state(state);
220 s4u::Link::onCommunicationStateChange(this);
223 /** @brief returns a list of all Links that this action is using */
224 std::list<LinkImpl*> NetworkAction::links()
226 std::list<LinkImpl*> retlist;
227 int llen = get_variable()->get_number_of_constraint();
229 for (int i = 0; i < llen; i++) {
230 /* Beware of composite actions: ptasks put links and cpus together */
231 // extra pb: we cannot dynamic_cast from void*...
232 Resource* resource = static_cast<Resource*>(get_variable()->get_constraint(i)->get_id());
233 LinkImpl* link = dynamic_cast<LinkImpl*>(resource);
235 retlist.push_back(link);
241 } // namespace kernel
244 #endif /* NETWORK_INTERFACE_CPP_ */