Logo AND Algorithmique Numérique Distribuée

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