Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start renaming tmgr trace into profile
[simgrid.git] / src / surf / network_cm02.cpp
1 /* Copyright (c) 2013-2019. 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 <algorithm>
7 #include <numeric>
8
9 #include "network_cm02.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/sg_config.hpp"
12 #include "src/surf/surf_interface.hpp"
13 #include "surf/surf.hpp"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
16
17 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
18 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
19 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
20
21 /************************************************************************/
22 /* New model based on optimizations discussed during Pedro Velho's thesis*/
23 /************************************************************************/
24 /* @techreport{VELHO:2011:HAL-00646896:1, */
25 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
26 /*      title = {{Flow-level network models: have we reached the limits?}}, */
27 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
28 /*      type = {Rapport de recherche}, */
29 /*      institution = {INRIA}, */
30 /*      number = {RR-7821}, */
31 /*      year = {2011}, */
32 /*      month = Nov, */
33 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
34 /*  } */
35 void surf_network_model_init_LegrandVelho()
36 {
37   xbt_assert(surf_network_model == nullptr, "Cannot set the network model twice");
38
39   surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
40
41   simgrid::config::set_default<double>("network/latency-factor", 13.01);
42   simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
43   simgrid::config::set_default<double>("network/weight-S", 20537);
44 }
45
46 /***************************************************************************/
47 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
48 /***************************************************************************/
49 /* @TechReport{      rr-lip2002-40, */
50 /*   author        = {Henri Casanova and Loris Marchal}, */
51 /*   institution   = {LIP}, */
52 /*   title         = {A Network Model for Simulation of Grid Application}, */
53 /*   number        = {2002-40}, */
54 /*   month         = {oct}, */
55 /*   year          = {2002} */
56 /* } */
57 void surf_network_model_init_CM02()
58 {
59   xbt_assert(surf_network_model == nullptr, "Cannot set the network model twice");
60
61   simgrid::config::set_default<double>("network/latency-factor", 1.0);
62   simgrid::config::set_default<double>("network/bandwidth-factor", 1.0);
63   simgrid::config::set_default<double>("network/weight-S", 0.0);
64
65   surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
66 }
67
68 /***************************************************************************/
69 /* The models from Steven H. Low                                           */
70 /***************************************************************************/
71 /* @article{Low03,                                                         */
72 /*   author={Steven H. Low},                                               */
73 /*   title={A Duality Model of {TCP} and Queue Management Algorithms},     */
74 /*   year={2003},                                                          */
75 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
76 /*    volume={11}, number={4},                                             */
77 /*  }                                                                      */
78 void surf_network_model_init_Reno()
79 {
80   xbt_assert(surf_network_model == nullptr, "Cannot set the network model twice");
81
82   namespace lmm = simgrid::kernel::lmm;
83   lmm::Lagrange::set_default_protocol_function(lmm::func_reno_f, lmm::func_reno_fp, lmm::func_reno_fpi);
84
85   simgrid::config::set_default<double>("network/latency-factor", 13.01);
86   simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
87   simgrid::config::set_default<double>("network/weight-S", 20537);
88
89   surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
90 }
91
92
93 void surf_network_model_init_Reno2()
94 {
95   xbt_assert(surf_network_model == nullptr, "Cannot set the network model twice");
96
97   namespace lmm = simgrid::kernel::lmm;
98   lmm::Lagrange::set_default_protocol_function(lmm::func_reno2_f, lmm::func_reno2_fp, lmm::func_reno2_fpi);
99
100   simgrid::config::set_default<double>("network/latency-factor", 13.01);
101   simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
102   simgrid::config::set_default<double>("network/weight-S", 20537);
103
104   surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
105 }
106
107 void surf_network_model_init_Vegas()
108 {
109   xbt_assert(surf_network_model == nullptr, "Cannot set the network model twice");
110
111   namespace lmm = simgrid::kernel::lmm;
112   lmm::Lagrange::set_default_protocol_function(lmm::func_vegas_f, lmm::func_vegas_fp, lmm::func_vegas_fpi);
113
114   simgrid::config::set_default<double>("network/latency-factor", 13.01);
115   simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
116   simgrid::config::set_default<double>("network/weight-S", 20537);
117
118   surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
119 }
120
121 namespace simgrid {
122 namespace kernel {
123 namespace resource {
124
125 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool))
126     : NetworkModel(simgrid::config::get_value<std::string>("network/optim") == "Full" ? Model::UpdateAlgo::FULL
127                                                                                       : Model::UpdateAlgo::LAZY)
128 {
129   all_existing_models.push_back(this);
130
131   std::string optim = simgrid::config::get_value<std::string>("network/optim");
132   bool select       = simgrid::config::get_value<bool>("network/maxmin-selective-update");
133
134   if (optim == "Lazy") {
135     xbt_assert(select || simgrid::config::is_default("network/maxmin-selective-update"),
136                "You cannot disable network selective update when using the lazy update mechanism");
137     select = true;
138   }
139
140   set_maxmin_system(make_new_lmm_system(select));
141   loopback_ = NetworkCm02Model::create_link("__loopback__", 498000000, 0.000015, s4u::Link::SharingPolicy::FATPIPE);
142 }
143
144 LinkImpl* NetworkCm02Model::create_link(const std::string& name, double bandwidth, double latency,
145                                         s4u::Link::SharingPolicy policy)
146 {
147   return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
148 }
149
150 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
151 {
152   while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
153
154     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(get_action_heap().pop());
155     XBT_DEBUG("Something happened to action %p", action);
156
157     // if I am wearing a latency hat
158     if (action->get_type() == ActionHeap::Type::latency) {
159       XBT_DEBUG("Latency paid for action %p. Activating", action);
160       get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
161       get_action_heap().remove(action);
162       action->set_last_update();
163
164       // if I am wearing a max_duration or normal hat
165     } else if (action->get_type() == ActionHeap::Type::max_duration || action->get_type() == ActionHeap::Type::normal) {
166       // no need to communicate anymore
167       // assume that flows that reached max_duration have remaining of 0
168       XBT_DEBUG("Action %p finished", action);
169       action->finish(Action::State::FINISHED);
170       get_action_heap().remove(action);
171     }
172   }
173 }
174
175 void NetworkCm02Model::update_actions_state_full(double now, double delta)
176 {
177   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
178     NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
179     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
180     XBT_DEBUG("Something happened to action %p", &action);
181     double deltap = delta;
182     if (action.latency_ > 0) {
183       if (action.latency_ > deltap) {
184         double_update(&action.latency_, deltap, sg_surf_precision);
185         deltap = 0.0;
186       } else {
187         double_update(&deltap, action.latency_, sg_surf_precision);
188         action.latency_ = 0.0;
189       }
190       if (action.latency_ <= 0.0 && not action.is_suspended())
191         get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
192     }
193
194     if (not action.get_variable()->get_number_of_constraint()) {
195       /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
196        * vivaldi. In such case, just make sure that the action completes immediately.
197        */
198       action.update_remains(action.get_remains());
199     }
200     action.update_remains(action.get_variable()->get_value() * delta);
201
202     if (action.get_max_duration() > NO_MAX_DURATION)
203       action.update_max_duration(delta);
204
205     if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
206         ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
207       action.finish(Action::State::FINISHED);
208     }
209   }
210 }
211
212 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
213 {
214   double latency = 0.0;
215   std::vector<LinkImpl*> back_route;
216   std::vector<LinkImpl*> route;
217
218   XBT_IN("(%s,%s,%g,%g)", src->get_cname(), dst->get_cname(), size, rate);
219
220   src->route_to(dst, route, &latency);
221   xbt_assert(not route.empty() || latency > 0,
222              "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
223              src->get_cname(), dst->get_cname());
224
225   bool failed = std::any_of(route.begin(), route.end(), [](LinkImpl* link) { return link->is_off(); });
226
227   if (cfg_crosstraffic) {
228     dst->route_to(src, back_route, nullptr);
229     if (not failed)
230       failed = std::any_of(back_route.begin(), back_route.end(), [](LinkImpl* const& link) { return link->is_off(); });
231   }
232
233   NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
234   action->weight_ = latency;
235   action->latency_ = latency;
236   action->rate_ = rate;
237   if (get_update_algorithm() == Model::UpdateAlgo::LAZY) {
238     action->set_last_update();
239   }
240
241   if (sg_weight_S_parameter > 0) {
242     action->weight_ =
243         std::accumulate(route.begin(), route.end(), action->weight_, [](double total, LinkImpl* const& link) {
244           return total + sg_weight_S_parameter / link->get_bandwidth();
245         });
246   }
247
248   double bandwidth_bound = route.empty() ? -1.0 : get_bandwidth_factor(size) * route.front()->get_bandwidth();
249
250   for (auto const& link : route)
251     bandwidth_bound = std::min(bandwidth_bound, get_bandwidth_factor(size) * link->get_bandwidth());
252
253   action->lat_current_ = action->latency_;
254   action->latency_ *= get_latency_factor(size);
255   action->rate_ = get_bandwidth_constraint(action->rate_, bandwidth_bound, size);
256
257   size_t constraints_per_variable = route.size();
258   constraints_per_variable += back_route.size();
259
260   if (action->latency_ > 0) {
261     action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
262     if (get_update_algorithm() == Model::UpdateAlgo::LAZY) {
263       // add to the heap the event when the latency is payed
264       double date = action->latency_ + action->get_last_update();
265
266       ActionHeap::Type type = route.empty() ? ActionHeap::Type::normal : ActionHeap::Type::latency;
267
268       XBT_DEBUG("Added action (%p) one latency event at date %f", action, date);
269       get_action_heap().insert(action, date, type);
270     }
271   } else
272     action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
273
274   if (action->rate_ < 0) {
275     get_maxmin_system()->update_variable_bound(
276         action->get_variable(), (action->lat_current_ > 0) ? cfg_tcp_gamma / (2.0 * action->lat_current_) : -1.0);
277   } else {
278     get_maxmin_system()->update_variable_bound(
279         action->get_variable(), (action->lat_current_ > 0)
280                                     ? std::min(action->rate_, cfg_tcp_gamma / (2.0 * action->lat_current_))
281                                     : action->rate_);
282   }
283
284   for (auto const& link : route)
285     get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), 1.0);
286
287   if (cfg_crosstraffic) {
288     XBT_DEBUG("Crosstraffic active: adding backward flow using 5%% of the available bandwidth");
289     for (auto const& link : back_route)
290       get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), .05);
291
292     // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
293     // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
294     // action->getVariable()->set_concurrency_share(2)
295   }
296   XBT_OUT();
297
298   simgrid::s4u::Link::on_communicate(action, src, dst);
299   return action;
300 }
301
302 /************
303  * Resource *
304  ************/
305 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
306                                  s4u::Link::SharingPolicy policy, kernel::lmm::System* system)
307     : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
308 {
309   bandwidth_.scale = 1.0;
310   bandwidth_.peak  = bandwidth;
311
312   latency_.scale = 1.0;
313   latency_.peak  = latency;
314
315   if (policy == s4u::Link::SharingPolicy::FATPIPE)
316     get_constraint()->unshare();
317
318   simgrid::s4u::Link::on_creation(this->piface_);
319 }
320
321 void NetworkCm02Link::apply_event(kernel::profile::Event* triggered, double value)
322 {
323   /* Find out which of my iterators was triggered, and react accordingly */
324   if (triggered == bandwidth_.event) {
325     set_bandwidth(value);
326     tmgr_trace_event_unref(&bandwidth_.event);
327
328   } else if (triggered == latency_.event) {
329     set_latency(value);
330     tmgr_trace_event_unref(&latency_.event);
331
332   } else if (triggered == state_event_) {
333     if (value > 0)
334       turn_on();
335     else {
336       kernel::lmm::Variable* var = nullptr;
337       const kernel::lmm::Element* elem = nullptr;
338       double now               = surf_get_clock();
339
340       turn_off();
341       while ((var = get_constraint()->get_variable(&elem))) {
342         Action* action = static_cast<Action*>(var->get_id());
343
344         if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
345           action->set_finish_time(now);
346           action->set_state(Action::State::FAILED);
347         }
348       }
349     }
350     tmgr_trace_event_unref(&state_event_);
351   } else {
352     xbt_die("Unknown event!\n");
353   }
354
355   XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
356             get_constraint());
357 }
358
359 void NetworkCm02Link::set_bandwidth(double value)
360 {
361   bandwidth_.peak = value;
362
363   get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
364                                                             sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
365
366   LinkImpl::on_bandwidth_change();
367
368   if (sg_weight_S_parameter > 0) {
369     double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
370
371     kernel::lmm::Variable* var;
372     const kernel::lmm::Element* elem     = nullptr;
373     const kernel::lmm::Element* nextelem = nullptr;
374     int numelem                  = 0;
375     while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
376       NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
377       action->weight_ += delta;
378       if (not action->is_suspended())
379         get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
380     }
381   }
382 }
383
384 void NetworkCm02Link::set_latency(double value)
385 {
386   double delta                 = value - latency_.peak;
387   kernel::lmm::Variable* var   = nullptr;
388   const kernel::lmm::Element* elem     = nullptr;
389   const kernel::lmm::Element* nextelem = nullptr;
390   int numelem                  = 0;
391
392   latency_.peak = value;
393
394   while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
395     NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
396     action->lat_current_ += delta;
397     action->weight_ += delta;
398     if (action->rate_ < 0)
399       get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
400                                                                                           (2.0 * action->lat_current_));
401     else {
402       get_model()->get_maxmin_system()->update_variable_bound(
403           action->get_variable(), std::min(action->rate_, NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
404
405       if (action->rate_ < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
406         XBT_INFO("Flow is limited BYBANDWIDTH");
407       } else {
408         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
409       }
410     }
411     if (not action->is_suspended())
412       get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
413   }
414 }
415
416 /**********
417  * Action *
418  **********/
419
420 void NetworkCm02Action::update_remains_lazy(double now)
421 {
422   if (suspended_ != Action::SuspendStates::not_suspended)
423     return;
424
425   double delta        = now - get_last_update();
426   double max_duration = get_max_duration();
427
428   if (get_remains_no_update() > 0) {
429     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
430               get_last_update());
431     update_remains(get_last_value() * delta);
432
433     XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
434   }
435
436   if (max_duration > NO_MAX_DURATION) {
437     double_update(&max_duration, delta, sg_surf_precision);
438     set_max_duration(max_duration);
439   }
440
441   if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
442       ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
443     finish(Action::State::FINISHED);
444     get_model()->get_action_heap().remove(this);
445   }
446
447   set_last_update();
448   set_last_value(get_variable()->get_value());
449 }
450
451 }
452 }
453 } // namespace simgrid