Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Declare method const.
[simgrid.git] / src / surf / network_interface.cpp
1 /* Copyright (c) 2013-2021. 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/s4u/Engine.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/resource/profile/Profile.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include "surf/surf.hpp"
12
13 #include <numeric>
14
15 #ifndef NETWORK_INTERFACE_CPP_
16 #define NETWORK_INTERFACE_CPP_
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_network, ker_resource, "Network resources, that fuel communications");
19
20 /*********
21  * Model *
22  *********/
23
24 namespace simgrid {
25 namespace kernel {
26 namespace resource {
27
28 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
29 config::Flag<double> NetworkModel::cfg_tcp_gamma(
30     "network/TCP-gamma",
31     "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
32     "Use the last given value, which is the max window size)",
33     4194304.0);
34
35 /** @brief Command-line option 'network/crosstraffic' -- see @ref options_model_network_crosstraffic */
36 config::Flag<bool> NetworkModel::cfg_crosstraffic(
37     "network/crosstraffic",
38     "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
39
40 NetworkModel::~NetworkModel() = default;
41
42 double NetworkModel::next_occurring_event_full(double now)
43 {
44   double minRes = Model::next_occurring_event_full(now);
45
46   for (Action const& action : *get_started_action_set()) {
47     const auto& net_action = static_cast<const NetworkAction&>(action);
48     if (net_action.latency_ > 0)
49       minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
50   }
51
52   XBT_DEBUG("Min of share resources %f", minRes);
53
54   return minRes;
55 }
56
57 /************
58  * Resource *
59  ************/
60
61 LinkImpl::LinkImpl(const std::string& name) : Resource_T(name), piface_(this)
62 {
63   if (name != "__loopback__")
64     xbt_assert(not s4u::Link::by_name_or_null(name), "Link '%s' declared several times in the platform.", name.c_str());
65
66   s4u::Engine::get_instance()->link_register(name, &piface_);
67   XBT_DEBUG("Create link '%s'", name.c_str());
68 }
69
70 /** @brief Fire the required callbacks and destroy the object
71  *
72  * Don't delete directly a Link, call l->destroy() instead.
73  */
74 void LinkImpl::destroy()
75 {
76   s4u::Link::on_destruction(this->piface_);
77   delete this;
78 }
79
80 bool LinkImpl::is_used() const
81 {
82   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
83 }
84
85 LinkImpl* LinkImpl::set_sharing_policy(s4u::Link::SharingPolicy policy)
86 {
87   get_constraint()->set_sharing_policy(policy);
88   return this;
89 }
90 s4u::Link::SharingPolicy LinkImpl::get_sharing_policy() const
91 {
92   return get_constraint()->get_sharing_policy();
93 }
94
95 void LinkImpl::latency_check(double latency) const
96 {
97   static double last_warned_latency = sg_surf_precision;
98   if (latency != 0.0 && latency < last_warned_latency) {
99     XBT_WARN("Latency for link %s is smaller than surf/precision (%g < %g)."
100         " For more accuracy, consider setting \"--cfg=surf/precision:%g\".",
101         get_cname(), latency, sg_surf_precision, latency);
102     last_warned_latency = latency;
103   }
104 }
105
106 void LinkImpl::turn_on()
107 {
108   if (not is_on()) {
109     Resource::turn_on();
110     s4u::Link::on_state_change(piface_);
111   }
112 }
113
114 void LinkImpl::turn_off()
115 {
116   if (is_on()) {
117     Resource::turn_off();
118     s4u::Link::on_state_change(piface_);
119
120     const kernel::lmm::Element* elem = nullptr;
121     double now                       = surf_get_clock();
122     while (const auto* var = get_constraint()->get_variable(&elem)) {
123       Action* action = var->get_id();
124       if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
125         action->set_finish_time(now);
126         action->set_state(Action::State::FAILED);
127       }
128     }
129   }
130 }
131
132 void LinkImpl::seal()
133 {
134   if (is_sealed())
135     return;
136
137   xbt_assert(this->get_model(), "Cannot seal Link(%s) without setting the Network model first", this->get_cname());
138   Resource::seal();
139   s4u::Link::on_creation(piface_);
140 }
141
142 void LinkImpl::on_bandwidth_change() const
143 {
144   s4u::Link::on_bandwidth_change(piface_);
145 }
146
147 LinkImpl* LinkImpl::set_bandwidth_profile(profile::Profile* profile)
148 {
149   if (profile) {
150     xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth profile to Link %s", get_cname());
151     bandwidth_.event = profile->schedule(&profile::future_evt_set, this);
152   }
153   return this;
154 }
155
156 LinkImpl* LinkImpl::set_latency_profile(profile::Profile* profile)
157 {
158   if (profile) {
159     xbt_assert(latency_.event == nullptr, "Cannot set a second latency profile to Link %s", get_cname());
160     latency_.event = profile->schedule(&profile::future_evt_set, this);
161   }
162   return this;
163 }
164
165 void LinkImpl::set_concurrency_limit(int limit) const
166 {
167   if (limit != -1) {
168     get_constraint()->reset_concurrency_maximum();
169   }
170   get_constraint()->set_concurrency_limit(limit);
171 }
172
173 /**********
174  * Action *
175  **********/
176
177 void NetworkAction::set_state(Action::State state)
178 {
179   Action::State previous = get_state();
180   if (previous != state) { // Trigger only if the state changed
181     Action::set_state(state);
182     s4u::Link::on_communication_state_change(*this, previous);
183   }
184 }
185
186 /** @brief returns a list of all Links that this action is using */
187 std::list<LinkImpl*> NetworkAction::get_links() const
188 {
189   std::list<LinkImpl*> retlist;
190   int llen = get_variable()->get_number_of_constraint();
191
192   for (int i = 0; i < llen; i++) {
193     /* Beware of composite actions: ptasks put links and cpus together */
194     if (auto* link = dynamic_cast<LinkImpl*>(get_variable()->get_constraint(i)->get_id()))
195       retlist.push_back(link);
196   }
197
198   return retlist;
199 }
200
201 static void add_latency(const std::vector<LinkImpl*>& links, double* latency)
202 {
203   if (latency)
204     *latency = std::accumulate(begin(links), end(links), *latency,
205                                [](double lat, const auto* link) { return lat + link->get_latency(); });
206 }
207
208 void add_link_latency(std::vector<LinkImpl*>& result, LinkImpl* link, double* latency)
209 {
210   result.push_back(link);
211   if (latency)
212     *latency += link->get_latency();
213 }
214
215 void add_link_latency(std::vector<LinkImpl*>& result, const std::vector<LinkImpl*>& links, double* latency)
216 {
217   result.insert(result.end(), begin(links), end(links));
218   add_latency(links, latency);
219 }
220
221 void insert_link_latency(std::vector<LinkImpl*>& result, const std::vector<LinkImpl*>& links, double* latency)
222 {
223   result.insert(result.begin(), rbegin(links), rend(links));
224   add_latency(links, latency);
225 }
226
227 } // namespace resource
228 } // namespace kernel
229 } // namespace simgrid
230
231 #endif /* NETWORK_INTERFACE_CPP_ */