Logo AND Algorithmique Numérique Distribuée

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