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::lagrange_solve);
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::lagrange_solve);
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::lagrange_solve);
131 all_existing_models->push_back(surf_network_model);
137 NetworkCm02Model::NetworkCm02Model()
140 std::string optim = xbt_cfg_get_string("network/optim");
141 bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
143 if (optim == "Full") {
144 setUpdateMechanism(UM_FULL);
145 } else if (optim == "Lazy") {
147 setUpdateMechanism(UM_LAZY);
148 xbt_assert(select || (xbt_cfg_is_default_value("network/maxmin-selective-update")),
149 "You cannot disable selective update when using the lazy update mechanism");
151 xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim.c_str());
154 maxmin_system_ = new simgrid::kernel::lmm::System(select);
155 loopback_ = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
157 if (getUpdateMechanism() == UM_LAZY)
158 maxmin_system_->modified_set_ = new kernel::resource::Action::ModifiedSet();
161 NetworkCm02Model::NetworkCm02Model(void (*specificSolveFun)(kernel::lmm::System* self)) : NetworkCm02Model()
163 maxmin_system_->solve_fun = specificSolveFun;
166 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
167 e_surf_link_sharing_policy_t policy)
169 return new NetworkCm02Link(this, name, bandwidth, latency, policy, maxmin_system_);
172 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
174 while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
176 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(actionHeapPop());
177 XBT_DEBUG("Something happened to action %p", action);
178 if (TRACE_is_enabled()) {
179 int n = action->getVariable()->get_number_of_constraint();
181 for (int i = 0; i < n; i++){
182 kernel::lmm::Constraint* constraint = action->getVariable()->get_constraint(i);
183 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
184 double value = action->getVariable()->get_value() * action->getVariable()->get_constraint_weight(i);
185 TRACE_surf_link_set_utilization(link->getCname(), action->getCategory(), value, action->getLastUpdate(),
186 now - action->getLastUpdate());
190 // if I am wearing a latency hat
191 if (action->getType() == kernel::resource::Action::Type::LATENCY) {
192 XBT_DEBUG("Latency paid for action %p. Activating", action);
193 maxmin_system_->update_variable_weight(action->getVariable(), action->weight_);
194 action->heapRemove(getActionHeap());
195 action->refreshLastUpdate();
197 // if I am wearing a max_duration or normal hat
198 } else if (action->getType() == kernel::resource::Action::Type::MAX_DURATION ||
199 action->getType() == kernel::resource::Action::Type::NORMAL) {
200 // no need to communicate anymore
201 // assume that flows that reached max_duration have remaining of 0
202 XBT_DEBUG("Action %p finished", action);
203 action->setRemains(0);
204 action->finish(kernel::resource::Action::State::done);
205 action->heapRemove(getActionHeap());
211 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
213 for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
214 NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
215 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
216 XBT_DEBUG("Something happened to action %p", &action);
217 double deltap = delta;
218 if (action.latency_ > 0) {
219 if (action.latency_ > deltap) {
220 double_update(&action.latency_, deltap, sg_surf_precision);
223 double_update(&deltap, action.latency_, sg_surf_precision);
224 action.latency_ = 0.0;
226 if (action.latency_ <= 0.0 && not action.isSuspended())
227 maxmin_system_->update_variable_weight(action.getVariable(), action.weight_);
229 if (TRACE_is_enabled()) {
230 int n = action.getVariable()->get_number_of_constraint();
231 for (int i = 0; i < n; i++) {
232 kernel::lmm::Constraint* constraint = action.getVariable()->get_constraint(i);
233 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
234 TRACE_surf_link_set_utilization(
235 link->getCname(), action.getCategory(),
236 (action.getVariable()->get_value() * action.getVariable()->get_constraint_weight(i)),
237 action.getLastUpdate(), now - action.getLastUpdate());
240 if (not action.getVariable()->get_number_of_constraint()) {
241 /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
242 * vivaldi. In such case, just make sure that the action completes immediately.
244 action.updateRemains(action.getRemains());
246 action.updateRemains(action.getVariable()->get_value() * delta);
248 if (action.getMaxDuration() > NO_MAX_DURATION)
249 action.updateMaxDuration(delta);
251 if (((action.getRemains() <= 0) && (action.getVariable()->get_weight() > 0)) ||
252 ((action.getMaxDuration() > NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
253 action.finish(kernel::resource::Action::State::done);
258 kernel::resource::Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
261 double latency = 0.0;
262 std::vector<LinkImpl*> back_route;
263 std::vector<LinkImpl*> route;
265 XBT_IN("(%s,%s,%g,%g)", src->getCname(), dst->getCname(), size, rate);
267 src->routeTo(dst, route, &latency);
268 xbt_assert(not route.empty() || latency,
269 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
270 src->getCname(), dst->getCname());
272 for (auto const& link : route)
276 if (sg_network_crosstraffic == 1) {
277 dst->routeTo(src, back_route, nullptr);
278 for (auto const& link : back_route)
283 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
284 action->weight_ = latency;
285 action->latency_ = latency;
286 action->rate_ = rate;
287 if (getUpdateMechanism() == UM_LAZY) {
288 action->refreshLastUpdate();
291 double bandwidth_bound = -1.0;
292 if (sg_weight_S_parameter > 0)
293 for (auto const& link : route)
294 action->weight_ += sg_weight_S_parameter / link->bandwidth();
296 for (auto const& link : route) {
297 double bb = bandwidthFactor(size) * link->bandwidth();
298 bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
301 action->latCurrent_ = action->latency_;
302 action->latency_ *= latencyFactor(size);
303 action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
305 int constraints_per_variable = route.size();
306 constraints_per_variable += back_route.size();
308 if (action->latency_ > 0) {
309 action->setVariable(maxmin_system_->variable_new(action, 0.0, -1.0, constraints_per_variable));
310 if (getUpdateMechanism() == UM_LAZY) {
311 // add to the heap the event when the latency is payed
312 XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->getLastUpdate());
313 action->heapInsert(getActionHeap(), action->latency_ + action->getLastUpdate(),
314 route.empty() ? kernel::resource::Action::Type::NORMAL
315 : kernel::resource::Action::Type::LATENCY);
318 action->setVariable(maxmin_system_->variable_new(action, 1.0, -1.0, constraints_per_variable));
320 if (action->rate_ < 0) {
321 maxmin_system_->update_variable_bound(
322 action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
324 maxmin_system_->update_variable_bound(action->getVariable(),
325 (action->latCurrent_ > 0)
326 ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_))
330 for (auto const& link : route)
331 maxmin_system_->expand(link->constraint(), action->getVariable(), 1.0);
333 if (not back_route.empty()) { // sg_network_crosstraffic was activated
334 XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
335 for (auto const& link : back_route)
336 maxmin_system_->expand(link->constraint(), action->getVariable(), .05);
338 // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
339 // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
340 // action->getVariable()->set_concurrency_share(2)
344 simgrid::s4u::Link::onCommunicate(action, src, dst);
351 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
352 e_surf_link_sharing_policy_t policy, kernel::lmm::System* system)
353 : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
355 bandwidth_.scale = 1.0;
356 bandwidth_.peak = bandwidth;
358 latency_.scale = 1.0;
359 latency_.peak = latency;
361 if (policy == SURF_LINK_FATPIPE)
362 constraint()->unshare();
364 simgrid::s4u::Link::onCreation(this->piface_);
367 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
369 /* Find out which of my iterators was triggered, and react accordingly */
370 if (triggered == bandwidth_.event) {
372 tmgr_trace_event_unref(&bandwidth_.event);
374 } else if (triggered == latency_.event) {
376 tmgr_trace_event_unref(&latency_.event);
378 } else if (triggered == stateEvent_) {
382 kernel::lmm::Variable* var = nullptr;
383 const_lmm_element_t elem = nullptr;
384 double now = surf_get_clock();
387 while ((var = constraint()->get_variable(&elem))) {
388 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
390 if (action->get_state() == kernel::resource::Action::State::running ||
391 action->get_state() == kernel::resource::Action::State::ready) {
392 action->setFinishTime(now);
393 action->set_state(kernel::resource::Action::State::failed);
397 tmgr_trace_event_unref(&stateEvent_);
399 xbt_die("Unknown event!\n");
402 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)", constraint());
405 void NetworkCm02Link::setBandwidth(double value)
407 bandwidth_.peak = value;
409 model()->getMaxminSystem()->update_constraint_bound(constraint(),
410 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
411 TRACE_surf_link_set_bandwidth(surf_get_clock(), getCname(), sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
413 if (sg_weight_S_parameter > 0) {
414 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
416 kernel::lmm::Variable* var;
417 const_lmm_element_t elem = nullptr;
418 const_lmm_element_t nextelem = nullptr;
420 while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
421 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
422 action->weight_ += delta;
423 if (not action->isSuspended())
424 model()->getMaxminSystem()->update_variable_weight(action->getVariable(), action->weight_);
429 void NetworkCm02Link::setLatency(double value)
431 double delta = value - latency_.peak;
432 kernel::lmm::Variable* var = nullptr;
433 const_lmm_element_t elem = nullptr;
434 const_lmm_element_t nextelem = nullptr;
437 latency_.peak = value;
439 while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
440 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
441 action->latCurrent_ += delta;
442 action->weight_ += delta;
443 if (action->rate_ < 0)
444 model()->getMaxminSystem()->update_variable_bound(action->getVariable(),
445 sg_tcp_gamma / (2.0 * action->latCurrent_));
447 model()->getMaxminSystem()->update_variable_bound(
448 action->getVariable(), std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
450 if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
451 XBT_INFO("Flow is limited BYBANDWIDTH");
453 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
456 if (not action->isSuspended())
457 model()->getMaxminSystem()->update_variable_weight(action->getVariable(), action->weight_);
465 void NetworkCm02Action::updateRemainingLazy(double now)
467 if (suspended_ != Action::SuspendStates::not_suspended)
470 double delta = now - getLastUpdate();
471 double max_duration = getMaxDuration();
473 if (getRemainsNoUpdate() > 0) {
474 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(), getLastUpdate());
475 updateRemains(getLastValue() * delta);
477 XBT_DEBUG("Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
480 if (max_duration > NO_MAX_DURATION) {
481 double_update(&max_duration, delta, sg_surf_precision);
482 setMaxDuration(max_duration);
485 if ((getRemainsNoUpdate() <= 0 && (getVariable()->get_weight() > 0)) ||
486 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
487 finish(Action::State::done);
488 heapRemove(getModel()->getActionHeap());
492 setLastValue(getVariable()->get_value());