Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
delete Timer when shutting down EngineImpl
[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 fuel 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 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 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::next_occurring_event_full(double now)
41 {
42   double minRes = Model::next_occurring_event_full(now);
43
44   for (Action const& action : *get_started_action_set()) {
45     const auto& net_action = static_cast<const NetworkAction&>(action);
46     if (net_action.latency_ > 0)
47       minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
48   }
49
50   XBT_DEBUG("Min of share resources %f", minRes);
51
52   return minRes;
53 }
54
55 double NetworkModel::get_bandwidth_constraint(double rate, double bound, double /* size */) const
56 {
57   return rate < 0 ? bound : std::min(bound, rate);
58 }
59
60 /************
61  * Resource *
62  ************/
63
64 LinkImpl::LinkImpl(const std::string& name) : Resource_T(name), piface_(this)
65 {
66   if (name != "__loopback__")
67     xbt_assert(not s4u::Link::by_name_or_null(name), "Link '%s' declared several times in the platform.", name.c_str());
68
69   s4u::Engine::get_instance()->link_register(name, &piface_);
70   XBT_DEBUG("Create link '%s'", name.c_str());
71 }
72
73 /** @brief Fire the required callbacks and destroy the object
74  *
75  * Don't delete directly a Link, call l->destroy() instead.
76  */
77 void LinkImpl::destroy()
78 {
79   s4u::Link::on_destruction(this->piface_);
80   delete this;
81 }
82
83 bool LinkImpl::is_used() const
84 {
85   return get_model()->get_maxmin_system()->constraint_used(get_constraint());
86 }
87
88 LinkImpl* LinkImpl::set_sharing_policy(s4u::Link::SharingPolicy policy)
89 {
90   get_constraint()->set_sharing_policy(policy);
91   return this;
92 }
93 s4u::Link::SharingPolicy LinkImpl::get_sharing_policy() const
94 {
95   return get_constraint()->get_sharing_policy();
96 }
97
98 void LinkImpl::latency_check(double latency) const
99 {
100   static double last_warned_latency = sg_surf_precision;
101   if (latency != 0.0 && latency < last_warned_latency) {
102     XBT_WARN("Latency for link %s is smaller than surf/precision (%g < %g)."
103         " For more accuracy, consider setting \"--cfg=surf/precision:%g\".",
104         get_cname(), latency, sg_surf_precision, latency);
105     last_warned_latency = latency;
106   }
107 }
108
109 void LinkImpl::turn_on()
110 {
111   if (not is_on()) {
112     Resource::turn_on();
113     s4u::Link::on_state_change(piface_);
114   }
115 }
116
117 void LinkImpl::turn_off()
118 {
119   if (is_on()) {
120     Resource::turn_off();
121     s4u::Link::on_state_change(piface_);
122
123     const kernel::lmm::Element* elem = nullptr;
124     double now                       = surf_get_clock();
125     while (const auto* var = get_constraint()->get_variable(&elem)) {
126       Action* action = var->get_id();
127       if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
128         action->set_finish_time(now);
129         action->set_state(Action::State::FAILED);
130       }
131     }
132   }
133 }
134
135 void LinkImpl::seal()
136 {
137   if (is_sealed())
138     return;
139
140   xbt_assert(this->get_model(), "Cannot seal Link(%s) without setting the Network model first", this->get_cname());
141   Resource::seal();
142   s4u::Link::on_creation(piface_);
143 }
144
145 void LinkImpl::on_bandwidth_change() const
146 {
147   s4u::Link::on_bandwidth_change(piface_);
148 }
149
150 LinkImpl* LinkImpl::set_bandwidth_profile(profile::Profile* profile)
151 {
152   if (profile) {
153     xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth profile to Link %s", get_cname());
154     bandwidth_.event = profile->schedule(&profile::future_evt_set, this);
155   }
156   return this;
157 }
158
159 LinkImpl* LinkImpl::set_latency_profile(profile::Profile* profile)
160 {
161   if (profile) {
162     xbt_assert(latency_.event == nullptr, "Cannot set a second latency profile to Link %s", get_cname());
163     latency_.event = profile->schedule(&profile::future_evt_set, this);
164   }
165   return this;
166 }
167
168 /**********
169  * Action *
170  **********/
171
172 void NetworkAction::set_state(Action::State state)
173 {
174   Action::State previous = get_state();
175   if (previous != state) { // Trigger only if the state changed
176     Action::set_state(state);
177     s4u::Link::on_communication_state_change(*this, previous);
178   }
179 }
180
181 /** @brief returns a list of all Links that this action is using */
182 std::list<LinkImpl*> NetworkAction::get_links() const
183 {
184   std::list<LinkImpl*> retlist;
185   int llen = get_variable()->get_number_of_constraint();
186
187   for (int i = 0; i < llen; i++) {
188     /* Beware of composite actions: ptasks put links and cpus together */
189     if (auto* link = dynamic_cast<LinkImpl*>(get_variable()->get_constraint(i)->get_id()))
190       retlist.push_back(link);
191   }
192
193   return retlist;
194 }
195 } // namespace resource
196 } // namespace kernel
197 } // namespace simgrid
198
199 #endif /* NETWORK_INTERFACE_CPP_ */