Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / surf / network_interface.cpp
1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "network_interface.hpp"
7 #include "simgrid/sg_config.hpp"
8 #include "src/surf/surf_interface.hpp"
9 #include "surf/surf.hpp"
10
11 #ifndef NETWORK_INTERFACE_CPP_
12 #define NETWORK_INTERFACE_CPP_
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
15
16 namespace simgrid {
17 namespace kernel {
18 namespace resource {
19
20 /* List of links */
21 std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
22
23 LinkImpl* LinkImpl::byName(std::string name)
24 {
25   auto link = links->find(name);
26   return link == links->end() ? nullptr : link->second;
27 }
28 /** @brief Returns the amount of links in the platform */
29 int LinkImpl::linksCount()
30 {
31   return links->size();
32 }
33 void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
34 {
35   for (auto const& kv : *links) {
36     linkList->push_back(&kv.second->piface_);
37   }
38 }
39
40 /** @brief Returns a list of all existing links */
41 LinkImpl** LinkImpl::linksList()
42 {
43   LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
44   int i          = 0;
45   for (auto const& kv : *links) {
46     res[i] = kv.second;
47     i++;
48   }
49   return res;
50 }
51 /** @brief destructor of the static data */
52 void LinkImpl::linksExit()
53 {
54   for (auto const& kv : *links)
55     (kv.second)->destroy();
56   delete links;
57 }
58 }
59 }
60 } // namespace simgrid
61
62 /*********
63  * Model *
64  *********/
65
66 simgrid::kernel::resource::NetworkModel* surf_network_model = nullptr;
67
68 namespace simgrid {
69 namespace kernel {
70 namespace resource {
71
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)",
77     4194304.0);
78
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");
83
84 NetworkModel::~NetworkModel() = default;
85
86 double NetworkModel::latencyFactor(double /*size*/)
87 {
88   return sg_latency_factor;
89 }
90
91 double NetworkModel::bandwidthFactor(double /*size*/)
92 {
93   return sg_bandwidth_factor;
94 }
95
96 double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/)
97 {
98   return rate;
99 }
100
101 double NetworkModel::next_occuring_event_full(double now)
102 {
103   double minRes = Model::next_occuring_event_full(now);
104
105   for (Action const& action : *get_running_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_);
109   }
110
111   XBT_DEBUG("Min of share resources %f", minRes);
112
113   return minRes;
114 }
115
116 /************
117  * Resource *
118  ************/
119
120 LinkImpl::LinkImpl(NetworkModel* model, const std::string& name, lmm::Constraint* constraint)
121     : Resource(model, name, constraint), piface_(this)
122 {
123
124   if (name != "__loopback__")
125     xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
126
127   latency_.scale   = 1;
128   bandwidth_.scale = 1;
129
130   links->insert({name, this});
131   XBT_DEBUG("Create link '%s'", name.c_str());
132 }
133
134 /** @brief use destroy() instead of this destructor */
135 LinkImpl::~LinkImpl()
136 {
137   xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
138 }
139 /** @brief Fire the required callbacks and destroy the object
140  *
141  * Don't delete directly a Link, call l->destroy() instead.
142  */
143 void LinkImpl::destroy()
144 {
145   if (not currentlyDestroying_) {
146     currentlyDestroying_ = true;
147     s4u::Link::on_destruction(this->piface_);
148     delete this;
149   }
150 }
151
152 bool LinkImpl::is_used()
153 {
154   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
155 }
156
157 double LinkImpl::latency()
158 {
159   return latency_.peak * latency_.scale;
160 }
161
162 double LinkImpl::bandwidth()
163 {
164   return bandwidth_.peak * bandwidth_.scale;
165 }
166
167 s4u::Link::SharingPolicy LinkImpl::sharingPolicy()
168 {
169   return get_constraint()->get_sharing_policy();
170 }
171
172 void LinkImpl::turn_on()
173 {
174   if (is_off()) {
175     Resource::turn_on();
176     s4u::Link::on_state_change(this->piface_);
177   }
178 }
179
180 void LinkImpl::turn_off()
181 {
182   if (is_on()) {
183     Resource::turn_off();
184     s4u::Link::on_state_change(this->piface_);
185   }
186 }
187
188 void LinkImpl::on_bandwidth_change()
189 {
190   s4u::Link::on_bandwidth_change(this->piface_);
191 }
192
193 void LinkImpl::setStateTrace(tmgr_trace_t trace)
194 {
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);
197 }
198 void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
199 {
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);
202 }
203 void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
204 {
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);
207 }
208
209 /**********
210  * Action *
211  **********/
212
213 void NetworkAction::set_state(Action::State state)
214 {
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);
219 }
220
221 /** @brief returns a list of all Links that this action is using */
222 std::list<LinkImpl*> NetworkAction::links()
223 {
224   std::list<LinkImpl*> retlist;
225   int llen = get_variable()->get_number_of_constraint();
226
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);
232     if (link != nullptr)
233       retlist.push_back(link);
234   }
235
236   return retlist;
237 }
238 }
239 } // namespace kernel
240 }
241
242 #endif /* NETWORK_INTERFACE_CPP_ */