Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix deadlock found by hcasanova in case of self communications
[simgrid.git] / src / surf / sio_S22.cpp
1 /* Copyright (c) 2022. 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 <simgrid/kernel/routing/NetZoneImpl.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <xbt/config.hpp>
9
10 #include "simgrid/config.h"
11 #include "src/kernel/EngineImpl.hpp"
12 #if SIMGRID_HAVE_EIGEN3
13 #include "src/kernel/lmm/bmf.hpp"
14 #endif
15 #include "src/kernel/resource/profile/Event.hpp"
16 #include "src/surf/sio_S22.hpp"
17
18 #include <unordered_set>
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_host);
21 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
22
23 /***********
24  * Options *
25  ***********/
26  static simgrid::config::Flag<std::string> cfg_sio_solver("host/sio_solver",
27                                                            "Set linear equations solver used by sio model",
28                                                            "fairbottleneck",
29                                                            &simgrid::kernel::lmm::System::validate_solver);
30
31 /**************************************/
32 /*** Resource Creation & Destruction **/
33 /**************************************/
34 void surf_host_model_init_sio_S22()
35 {
36   XBT_CINFO(xbt_cfg, "Switching to the S22 model to handle streaming I/Os.");
37   xbt_assert(cfg_sio_solver != "maxmin", "Invalid configuration. Cannot use maxmin solver with streaming I/Os.");
38
39   auto* system    = simgrid::kernel::lmm::System::build(cfg_sio_solver.get(), true /* selective update */);
40   auto host_model = std::make_shared<simgrid::kernel::resource::HostS22Model>("Host_Sio", system);
41   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
42   engine->add_model(host_model);
43   engine->get_netzone_root()->set_host_model(host_model);
44 }
45
46 namespace simgrid::kernel::resource {
47
48 HostS22Model::HostS22Model(const std::string& name, lmm::System* sys) : HostModel(name)
49 {
50   set_maxmin_system(sys);
51
52   auto net_model = std::make_shared<NetworkS22Model>("Network_Sio", this, sys);
53   auto engine    = EngineImpl::get_instance();
54   engine->add_model(net_model);
55   engine->get_netzone_root()->set_network_model(net_model);
56
57   auto disk_model = std::make_shared<DiskS22Model>("Disk_Sio", this, sys);
58   engine->add_model(disk_model);
59   engine->get_netzone_root()->set_disk_model(disk_model);
60
61   surf_cpu_model_init_Cas01();
62 }
63
64 DiskS22Model::DiskS22Model(const std::string& name, HostS22Model* hmodel, lmm::System* sys)
65     : DiskModel(name), hostModel_(hmodel)
66 {
67   set_maxmin_system(sys);
68 }
69
70 DiskS22Model::~DiskS22Model()
71 {
72   set_maxmin_system(nullptr);
73 }
74
75 NetworkS22Model::NetworkS22Model(const std::string& name, HostS22Model* hmodel, lmm::System* sys)
76     : NetworkModel(name), hostModel_(hmodel)
77 {
78   set_maxmin_system(sys);
79   loopback_.reset(create_link("__loopback__", {simgrid::config::get_value<double>("network/loopback-bw")}));
80   loopback_->set_sharing_policy(s4u::Link::SharingPolicy::FATPIPE, {});
81   loopback_->set_latency(simgrid::config::get_value<double>("network/loopback-lat"));
82   loopback_->get_iface()->seal();
83 }
84
85 NetworkS22Model::~NetworkS22Model()
86 {
87   set_maxmin_system(nullptr);
88 }
89
90 double HostS22Model::next_occurring_event(double now)
91 {
92   double min = HostModel::next_occurring_event_full(now);
93   for (Action const& action : *get_started_action_set()) {
94     const auto& net_action = static_cast<const S22Action&>(action);
95     if (net_action.get_latency() > 0 && (min < 0 || net_action.get_latency() < min)) {
96       min = net_action.get_latency();
97       XBT_DEBUG("Updating min with %p (start %f): %f", &net_action, net_action.get_start_time(), min);
98     }
99   }
100   XBT_DEBUG("min value: %f", min);
101
102   return min;
103 }
104
105 void HostS22Model::update_actions_state(double /*now*/, double delta)
106 {
107   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
108     auto& action = static_cast<S22Action&>(*it);
109     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
110     if (action.get_latency() > 0) {
111       if (action.get_latency() > delta) {
112         action.update_latency(delta, sg_surf_precision);
113       } else {
114         action.set_latency(0.0);
115       }
116       if ((action.get_latency() <= 0.0) && (action.is_suspended() == 0)) {
117         action.update_bound();
118         get_maxmin_system()->update_variable_penalty(action.get_variable(), 1.0);
119         action.set_last_update();
120       }
121     }
122     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.", &action, action.get_remains(), action.get_rate() * delta);
123     action.update_remains(action.get_rate() * delta);
124     action.update_max_duration(delta);
125
126     XBT_DEBUG("Action (%p) : remains (%g).", &action, action.get_remains());
127
128     /* In the next if cascade, the action can be finished either because:
129      *  - The amount of remaining work reached 0
130      *  - The max duration was reached
131      * If it's not done, it may have failed.
132      */
133
134     if (((action.get_remains() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
135         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
136       action.finish(Action::State::FINISHED);
137       continue;
138     }
139
140     /* Need to check that none of the model has failed */
141     int i                               = 0;
142     const lmm::Constraint* cnst         = action.get_variable()->get_constraint(i);
143     while (cnst != nullptr) {
144       i++;
145       if (not cnst->get_id()->is_on()) {
146         XBT_DEBUG("Action (%p) Failed!!", &action);
147         action.finish(Action::State::FAILED);
148         break;
149       }
150       cnst = action.get_variable()->get_constraint(i);
151     }
152   }
153 }
154
155 S22Action::S22Action(Model* model, s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk, double size)
156     : DiskAction(model, 1, false)
157     , src_host_(src_host)
158     , src_disk_(src_disk)
159     , dst_host_(dst_host)
160     , dst_disk_(dst_disk)
161     , size_(size)
162 {
163   size_t disk_nb       = 0;
164   size_t link_nb       = 0;
165   double latency       = 0.0;
166   this->set_last_update();
167
168   if (src_disk_ != nullptr)
169     disk_nb++;
170   if (dst_disk_ != nullptr)
171     disk_nb++;
172
173   if (size_ > 0) {
174     std::unordered_set<const char*> affected_links;
175     double lat = 0.0;
176     std::vector<StandardLinkImpl*> route;
177     src_host_->route_to(dst_host_, route, &lat);
178     latency = std::max(latency, lat);
179
180     for (auto const& link : route)
181       affected_links.insert(link->get_cname());
182
183     link_nb = affected_links.size();
184   }
185
186   XBT_DEBUG("Creating a stream io (%p) with %zu disk(s) and %zu unique link(s).", this, disk_nb, link_nb);
187   latency_ = latency;
188
189   set_variable(model->get_maxmin_system()->variable_new(this, 1.0, -1.0, disk_nb + link_nb));
190
191   if (latency_ > 0)
192     model->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
193
194   /* Expand it for the disks even if there is nothing to read/write, to make sure that it gets expended even if there is no
195    * communication either */
196   if (src_disk_ != nullptr)
197     model->get_maxmin_system()->expand(src_disk_->get_constraint(), get_variable(), size, true);
198   if (dst_disk_ != nullptr)
199     model->get_maxmin_system()->expand(dst_disk_->get_constraint(), get_variable(), size, true);
200
201   if (link_nb > 0) {
202     std::vector<StandardLinkImpl*> route;
203     src_host_->route_to(dst_host_, route, nullptr);
204     for (auto const& link : route)
205       model->get_maxmin_system()->expand(link->get_constraint(), this->get_variable(), size_);
206   }
207
208   if (link_nb + disk_nb == 0) {
209     this->set_cost(1.0);
210     this->set_remains(0.0);
211   }
212
213   /* finally calculate the initial bound value */
214   update_bound();
215 }
216
217 DiskAction* HostS22Model::io_stream(s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk,
218                                    double size)
219 {
220   return new S22Action(this, src_host, src_disk, dst_host, dst_disk, size);
221 }
222
223 Action* NetworkS22Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double /*rate*/)
224 {
225   return hostModel_->io_stream(src, nullptr, dst, nullptr, size);
226 }
227
228 DiskImpl* DiskS22Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
229 {
230   return (new DiskS22(name, read_bandwidth, write_bandwidth))->set_model(this);
231 }
232
233 StandardLinkImpl* NetworkS22Model::create_link(const std::string& name, const std::vector<double>& bandwidths)
234 {
235   xbt_assert(bandwidths.size() == 1, "Non WIFI link must have only 1 bandwidth.");
236   auto link = new LinkS22(name, bandwidths[0], get_maxmin_system());
237   link->set_model(this);
238   return link;
239 }
240
241 StandardLinkImpl* NetworkS22Model::create_wifi_link(const std::string& name, const std::vector<double>& bandwidths)
242 {
243   THROW_UNIMPLEMENTED;
244 }
245
246 /************
247  * Resource *
248  ************/
249 DiskAction* DiskS22::io_start(sg_size_t size, s4u::Io::OpType type)
250 {
251   DiskAction* res;
252   switch (type) {
253     case s4u::Io::OpType::READ:
254       res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), this, get_host(), nullptr, size);
255     break;
256     case s4u::Io::OpType::WRITE:
257       res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), nullptr, get_host(), this, size);
258     break;
259     default:
260       THROW_UNIMPLEMENTED;
261   }
262
263    return res;
264 }
265
266 void DiskS22::apply_event(kernel::profile::Event* triggered, double value)
267 {
268   /* Find out which of my iterators was triggered, and react accordingly */
269   if (triggered == get_read_event()) {
270     set_read_bandwidth(value);
271     unref_read_event();
272   } else if (triggered == get_write_event()) {
273     set_write_bandwidth(value);
274     unref_write_event();
275   } else if (triggered == get_state_event()) {
276     if (value > 0)
277       turn_on();
278     else
279       turn_off();
280     unref_state_event();
281   } else {
282     xbt_die("Unknown event!\n");
283   }
284 }
285
286 LinkS22::LinkS22(const std::string& name, double bandwidth, lmm::System* system) : StandardLinkImpl(name)
287 {
288   this->set_constraint(system->constraint_new(this, bandwidth));
289   bandwidth_.peak = bandwidth;
290 }
291
292 void LinkS22::apply_event(profile::Event* triggered, double value)
293 {
294   XBT_DEBUG("Updating link %s (%p) with value=%f", get_cname(), this, value);
295   if (triggered == bandwidth_.event) {
296     set_bandwidth(value);
297     tmgr_trace_event_unref(&bandwidth_.event);
298
299   } else if (triggered == latency_.event) {
300     set_latency(value);
301     tmgr_trace_event_unref(&latency_.event);
302
303   } else if (triggered == get_state_event()) {
304     if (value > 0)
305       turn_on();
306     else
307       turn_off();
308     unref_state_event();
309   } else {
310     xbt_die("Unknown event ! \n");
311   }
312 }
313
314 void LinkS22::set_bandwidth(double value)
315 {
316   bandwidth_.peak = value;
317   StandardLinkImpl::on_bandwidth_change();
318
319   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), bandwidth_.peak * bandwidth_.scale);
320 }
321
322 void LinkS22::set_latency(double value)
323 {
324   latency_check(value);
325   const lmm::Element* elem = nullptr;
326
327   latency_.peak = value;
328   while (const auto* var = get_constraint()->get_variable(&elem)) {
329     const auto* action = static_cast<S22Action*>(var->get_id());
330     action->update_bound();
331   }
332 }
333
334 /**********
335  * Action *
336  **********/
337
338 double S22Action::calculate_io_read_bound() const
339 {
340   double io_read_bound = std::numeric_limits<double>::max();
341
342   if (src_disk_ == nullptr)
343     return io_read_bound;
344
345   if (size_ > 0)
346     io_read_bound = std::min(io_read_bound, src_disk_->get_read_bandwidth() / size_);
347
348   return io_read_bound;
349 }
350
351 double S22Action::calculate_io_write_bound() const
352 {
353   double io_write_bound = std::numeric_limits<double>::max();
354
355   if (dst_disk_ == nullptr)
356     return io_write_bound;
357
358   if (size_ > 0)
359     io_write_bound = std::min(io_write_bound, dst_disk_->get_write_bandwidth() / size_);
360
361   return io_write_bound;
362 }
363
364 double S22Action::calculate_network_bound() const
365 {
366   double lat = 0.0;
367   double lat_bound   = std::numeric_limits<double>::max();
368
369   if (src_host_ == dst_host_)
370     return lat_bound;
371
372   if (size_ <= 0){
373     std::vector<StandardLinkImpl*> route;
374     src_host_->route_to(dst_host_, route, &lat);
375   }
376
377   if (lat > 0)
378     lat_bound = NetworkModel::cfg_tcp_gamma / (2.0 * lat);
379
380   return lat_bound;
381 }
382
383 void S22Action::update_bound() const
384 {
385   double bound = std::min(std::min(calculate_io_read_bound(), calculate_io_write_bound()),
386                           calculate_network_bound());
387
388   XBT_DEBUG("action (%p) : bound = %g", this, bound);
389
390   /* latency has been paid (or no latency), we can set the appropriate bound for network limit */
391   if ((bound < std::numeric_limits<double>::max()) && (latency_ <= 0.0))
392     get_model()->get_maxmin_system()->update_variable_bound(get_variable(), bound);
393  }
394
395 } // namespace simgrid::kernel::resource