1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved. */
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. */
8 #include "network_cm02.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "simgrid/sg_config.h"
11 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
12 #include "src/kernel/lmm/maxmin.hpp"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
16 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
17 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
18 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
20 double sg_tcp_gamma = 0.0;
21 int sg_network_crosstraffic = 0;
23 /************************************************************************/
24 /* New model based on optimizations discussed during Pedro Velho's thesis*/
25 /************************************************************************/
26 /* @techreport{VELHO:2011:HAL-00646896:1, */
27 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
28 /* title = {{Flow-level network models: have we reached the limits?}}, */
29 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
30 /* type = {Rapport de recherche}, */
31 /* institution = {INRIA}, */
32 /* number = {RR-7821}, */
35 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
37 void surf_network_model_init_LegrandVelho()
39 if (surf_network_model)
42 surf_network_model = new simgrid::surf::NetworkCm02Model();
43 all_existing_models->push_back(surf_network_model);
45 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
46 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
47 xbt_cfg_setdefault_double("network/weight-S", 20537);
50 /***************************************************************************/
51 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
52 /***************************************************************************/
53 /* @TechReport{ rr-lip2002-40, */
54 /* author = {Henri Casanova and Loris Marchal}, */
55 /* institution = {LIP}, */
56 /* title = {A Network Model for Simulation of Grid Application}, */
57 /* number = {2002-40}, */
61 void surf_network_model_init_CM02()
64 if (surf_network_model)
67 xbt_cfg_setdefault_double("network/latency-factor", 1.0);
68 xbt_cfg_setdefault_double("network/bandwidth-factor", 1.0);
69 xbt_cfg_setdefault_double("network/weight-S", 0.0);
71 surf_network_model = new simgrid::surf::NetworkCm02Model();
72 all_existing_models->push_back(surf_network_model);
75 /***************************************************************************/
76 /* The models from Steven H. Low */
77 /***************************************************************************/
79 /* author={Steven H. Low}, */
80 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
82 /* journal={{IEEE/ACM} Transactions on Networking}, */
83 /* volume={11}, number={4}, */
85 void surf_network_model_init_Reno()
87 if (surf_network_model)
90 set_default_protocol_function(simgrid::kernel::lmm::func_reno_f, simgrid::kernel::lmm::func_reno_fp,
91 simgrid::kernel::lmm::func_reno_fpi);
93 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
94 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
95 xbt_cfg_setdefault_double("network/weight-S", 20537);
97 surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
98 all_existing_models->push_back(surf_network_model);
102 void surf_network_model_init_Reno2()
104 if (surf_network_model)
107 set_default_protocol_function(simgrid::kernel::lmm::func_reno2_f, simgrid::kernel::lmm::func_reno2_fp,
108 simgrid::kernel::lmm::func_reno2_fpi);
110 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
111 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
112 xbt_cfg_setdefault_double("network/weight-S", 20537);
114 surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
115 all_existing_models->push_back(surf_network_model);
118 void surf_network_model_init_Vegas()
120 if (surf_network_model)
123 set_default_protocol_function(simgrid::kernel::lmm::func_vegas_f, simgrid::kernel::lmm::func_vegas_fp,
124 simgrid::kernel::lmm::func_vegas_fpi);
126 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
127 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
128 xbt_cfg_setdefault_double("network/weight-S", 20537);
130 surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
131 all_existing_models->push_back(surf_network_model);
137 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool)) : NetworkModel()
139 std::string optim = xbt_cfg_get_string("network/optim");
140 bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
142 if (optim == "Full") {
143 setUpdateMechanism(kernel::resource::Model::UpdateAlgo::Full);
144 } else if (optim == "Lazy") {
145 xbt_assert(select || xbt_cfg_is_default_value("network/maxmin-selective-update"),
146 "You cannot disable network selective update when using the lazy update mechanism");
148 setUpdateMechanism(kernel::resource::Model::UpdateAlgo::Lazy);
150 xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim.c_str());
153 set_maxmin_system(make_new_lmm_system(select));
154 loopback_ = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
156 if (getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy)
157 get_maxmin_system()->modified_set_ = new kernel::resource::Action::ModifiedSet();
160 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
161 e_surf_link_sharing_policy_t policy)
163 return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
166 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
168 while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
170 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(actionHeapPop());
171 XBT_DEBUG("Something happened to action %p", action);
172 if (TRACE_is_enabled()) {
173 int n = action->get_variable()->get_number_of_constraint();
175 for (int i = 0; i < n; i++){
176 kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
177 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
178 double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
179 TRACE_surf_link_set_utilization(link->getCname(), action->get_category(), value, action->get_last_update(),
180 now - action->get_last_update());
184 // if I am wearing a latency hat
185 if (action->get_type() == kernel::resource::Action::Type::LATENCY) {
186 XBT_DEBUG("Latency paid for action %p. Activating", action);
187 get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
188 action->heapRemove();
189 action->set_last_update();
191 // if I am wearing a max_duration or normal hat
192 } else if (action->get_type() == kernel::resource::Action::Type::MAX_DURATION ||
193 action->get_type() == kernel::resource::Action::Type::NORMAL) {
194 // no need to communicate anymore
195 // assume that flows that reached max_duration have remaining of 0
196 XBT_DEBUG("Action %p finished", action);
197 action->finish(kernel::resource::Action::State::done);
198 action->heapRemove();
203 void NetworkCm02Model::update_actions_state_full(double now, double delta)
205 for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
206 NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
207 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
208 XBT_DEBUG("Something happened to action %p", &action);
209 double deltap = delta;
210 if (action.latency_ > 0) {
211 if (action.latency_ > deltap) {
212 double_update(&action.latency_, deltap, sg_surf_precision);
215 double_update(&deltap, action.latency_, sg_surf_precision);
216 action.latency_ = 0.0;
218 if (action.latency_ <= 0.0 && not action.is_suspended())
219 get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
221 if (TRACE_is_enabled()) {
222 int n = action.get_variable()->get_number_of_constraint();
223 for (int i = 0; i < n; i++) {
224 kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
225 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
226 TRACE_surf_link_set_utilization(
227 link->getCname(), action.get_category(),
228 (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
229 action.get_last_update(), now - action.get_last_update());
232 if (not action.get_variable()->get_number_of_constraint()) {
233 /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
234 * vivaldi. In such case, just make sure that the action completes immediately.
236 action.update_remains(action.get_remains());
238 action.update_remains(action.get_variable()->get_value() * delta);
240 if (action.get_max_duration() > NO_MAX_DURATION)
241 action.update_max_duration(delta);
243 if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
244 ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
245 action.finish(kernel::resource::Action::State::done);
250 kernel::resource::Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
253 double latency = 0.0;
254 std::vector<LinkImpl*> back_route;
255 std::vector<LinkImpl*> route;
257 XBT_IN("(%s,%s,%g,%g)", src->getCname(), dst->getCname(), size, rate);
259 src->routeTo(dst, route, &latency);
260 xbt_assert(not route.empty() || latency,
261 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
262 src->getCname(), dst->getCname());
264 for (auto const& link : route)
268 if (sg_network_crosstraffic == 1) {
269 dst->routeTo(src, back_route, nullptr);
270 for (auto const& link : back_route)
275 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
276 action->weight_ = latency;
277 action->latency_ = latency;
278 action->rate_ = rate;
279 if (getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
280 action->set_last_update();
283 double bandwidth_bound = -1.0;
284 if (sg_weight_S_parameter > 0)
285 for (auto const& link : route)
286 action->weight_ += sg_weight_S_parameter / link->bandwidth();
288 for (auto const& link : route) {
289 double bb = bandwidthFactor(size) * link->bandwidth();
290 bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
293 action->latCurrent_ = action->latency_;
294 action->latency_ *= latencyFactor(size);
295 action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
297 int constraints_per_variable = route.size();
298 constraints_per_variable += back_route.size();
300 if (action->latency_ > 0) {
301 action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
302 if (getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
303 // add to the heap the event when the latency is payed
304 XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->get_last_update());
305 action->heapInsert(action->latency_ + action->get_last_update(), route.empty()
306 ? kernel::resource::Action::Type::NORMAL
307 : kernel::resource::Action::Type::LATENCY);
310 action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
312 if (action->rate_ < 0) {
313 get_maxmin_system()->update_variable_bound(
314 action->get_variable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
316 get_maxmin_system()->update_variable_bound(action->get_variable(),
317 (action->latCurrent_ > 0)
318 ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_))
322 for (auto const& link : route)
323 get_maxmin_system()->expand(link->constraint(), action->get_variable(), 1.0);
325 if (not back_route.empty()) { // sg_network_crosstraffic was activated
326 XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
327 for (auto const& link : back_route)
328 get_maxmin_system()->expand(link->constraint(), action->get_variable(), .05);
330 // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
331 // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
332 // action->getVariable()->set_concurrency_share(2)
336 simgrid::s4u::Link::onCommunicate(action, src, dst);
343 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
344 e_surf_link_sharing_policy_t policy, kernel::lmm::System* system)
345 : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
347 bandwidth_.scale = 1.0;
348 bandwidth_.peak = bandwidth;
350 latency_.scale = 1.0;
351 latency_.peak = latency;
353 if (policy == SURF_LINK_FATPIPE)
354 constraint()->unshare();
356 simgrid::s4u::Link::onCreation(this->piface_);
359 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
361 /* Find out which of my iterators was triggered, and react accordingly */
362 if (triggered == bandwidth_.event) {
364 tmgr_trace_event_unref(&bandwidth_.event);
366 } else if (triggered == latency_.event) {
368 tmgr_trace_event_unref(&latency_.event);
370 } else if (triggered == stateEvent_) {
374 kernel::lmm::Variable* var = nullptr;
375 const_lmm_element_t elem = nullptr;
376 double now = surf_get_clock();
379 while ((var = constraint()->get_variable(&elem))) {
380 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
382 if (action->get_state() == kernel::resource::Action::State::running ||
383 action->get_state() == kernel::resource::Action::State::ready) {
384 action->set_finish_time(now);
385 action->set_state(kernel::resource::Action::State::failed);
389 tmgr_trace_event_unref(&stateEvent_);
391 xbt_die("Unknown event!\n");
394 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)", constraint());
397 void NetworkCm02Link::setBandwidth(double value)
399 bandwidth_.peak = value;
401 model()->get_maxmin_system()->update_constraint_bound(constraint(),
402 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
403 TRACE_surf_link_set_bandwidth(surf_get_clock(), getCname(), sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
405 if (sg_weight_S_parameter > 0) {
406 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
408 kernel::lmm::Variable* var;
409 const_lmm_element_t elem = nullptr;
410 const_lmm_element_t nextelem = nullptr;
412 while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
413 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
414 action->weight_ += delta;
415 if (not action->is_suspended())
416 model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
421 void NetworkCm02Link::setLatency(double value)
423 double delta = value - latency_.peak;
424 kernel::lmm::Variable* var = nullptr;
425 const_lmm_element_t elem = nullptr;
426 const_lmm_element_t nextelem = nullptr;
429 latency_.peak = value;
431 while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
432 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
433 action->latCurrent_ += delta;
434 action->weight_ += delta;
435 if (action->rate_ < 0)
436 model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
437 sg_tcp_gamma / (2.0 * action->latCurrent_));
439 model()->get_maxmin_system()->update_variable_bound(
440 action->get_variable(), std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
442 if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
443 XBT_INFO("Flow is limited BYBANDWIDTH");
445 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
448 if (not action->is_suspended())
449 model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
457 void NetworkCm02Action::update_remains_lazy(double now)
459 if (suspended_ != Action::SuspendStates::not_suspended)
462 double delta = now - get_last_update();
463 double max_duration = get_max_duration();
465 if (get_remains_no_update() > 0) {
466 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
468 update_remains(get_last_value() * delta);
470 XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
473 if (max_duration > NO_MAX_DURATION) {
474 double_update(&max_duration, delta, sg_surf_precision);
475 set_max_duration(max_duration);
478 if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
479 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
480 finish(Action::State::done);
485 set_last_value(get_variable()->get_value());