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 selectiveUpdate_ = select;
146 } else if (optim == "Lazy") {
147 setUpdateMechanism(UM_LAZY);
148 selectiveUpdate_ = true;
149 xbt_assert(select || (xbt_cfg_is_default_value("network/maxmin-selective-update")),
150 "You cannot disable selective update when using the lazy update mechanism");
152 xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim.c_str());
155 maxminSystem_ = new simgrid::kernel::lmm::System(selectiveUpdate_);
156 loopback_ = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
158 if (getUpdateMechanism() == UM_LAZY) {
159 modifiedSet_ = new kernel::resource::ActionLmmList();
160 maxminSystem_->keep_track = modifiedSet_;
164 NetworkCm02Model::NetworkCm02Model(void (*specificSolveFun)(lmm_system_t self)) : NetworkCm02Model()
166 maxminSystem_->solve_fun = specificSolveFun;
169 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
170 e_surf_link_sharing_policy_t policy)
172 return new NetworkCm02Link(this, name, bandwidth, latency, policy, maxminSystem_);
175 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
177 while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
179 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(actionHeapPop());
180 XBT_DEBUG("Something happened to action %p", action);
181 if (TRACE_is_enabled()) {
182 int n = action->getVariable()->get_number_of_constraint();
184 for (int i = 0; i < n; i++){
185 kernel::lmm::Constraint* constraint = action->getVariable()->get_constraint(i);
186 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
187 double value = action->getVariable()->get_value() * action->getVariable()->get_constraint_weight(i);
188 TRACE_surf_link_set_utilization(link->getCname(), action->getCategory(), value, action->getLastUpdate(),
189 now - action->getLastUpdate());
193 // if I am wearing a latency hat
194 if (action->getType() == kernel::resource::Action::Type::LATENCY) {
195 XBT_DEBUG("Latency paid for action %p. Activating", action);
196 maxminSystem_->update_variable_weight(action->getVariable(), action->weight_);
197 action->heapRemove(getActionHeap());
198 action->refreshLastUpdate();
200 // if I am wearing a max_duration or normal hat
201 } else if (action->getType() == kernel::resource::Action::Type::MAX_DURATION ||
202 action->getType() == kernel::resource::Action::Type::NORMAL) {
203 // no need to communicate anymore
204 // assume that flows that reached max_duration have remaining of 0
205 XBT_DEBUG("Action %p finished", action);
206 action->setRemains(0);
207 action->finish(kernel::resource::Action::State::done);
208 action->heapRemove(getActionHeap());
214 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
216 for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
217 NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
218 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
219 XBT_DEBUG("Something happened to action %p", &action);
220 double deltap = delta;
221 if (action.latency_ > 0) {
222 if (action.latency_ > deltap) {
223 double_update(&action.latency_, deltap, sg_surf_precision);
226 double_update(&deltap, action.latency_, sg_surf_precision);
227 action.latency_ = 0.0;
229 if (action.latency_ <= 0.0 && not action.isSuspended())
230 maxminSystem_->update_variable_weight(action.getVariable(), action.weight_);
232 if (TRACE_is_enabled()) {
233 int n = action.getVariable()->get_number_of_constraint();
234 for (int i = 0; i < n; i++) {
235 kernel::lmm::Constraint* constraint = action.getVariable()->get_constraint(i);
236 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
237 TRACE_surf_link_set_utilization(
238 link->getCname(), action.getCategory(),
239 (action.getVariable()->get_value() * action.getVariable()->get_constraint_weight(i)),
240 action.getLastUpdate(), now - action.getLastUpdate());
243 if (not action.getVariable()->get_number_of_constraint()) {
244 /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
245 * vivaldi. In such case, just make sure that the action completes immediately.
247 action.updateRemains(action.getRemains());
249 action.updateRemains(action.getVariable()->get_value() * delta);
251 if (action.getMaxDuration() > NO_MAX_DURATION)
252 action.updateMaxDuration(delta);
254 if (((action.getRemains() <= 0) && (action.getVariable()->get_weight() > 0)) ||
255 ((action.getMaxDuration() > NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
256 action.finish(kernel::resource::Action::State::done);
261 kernel::resource::Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
264 double latency = 0.0;
265 std::vector<LinkImpl*> back_route;
266 std::vector<LinkImpl*> route;
268 XBT_IN("(%s,%s,%g,%g)", src->getCname(), dst->getCname(), size, rate);
270 src->routeTo(dst, route, &latency);
271 xbt_assert(not route.empty() || latency,
272 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
273 src->getCname(), dst->getCname());
275 for (auto const& link : route)
279 if (sg_network_crosstraffic == 1) {
280 dst->routeTo(src, back_route, nullptr);
281 for (auto const& link : back_route)
286 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
287 action->weight_ = latency;
288 action->latency_ = latency;
289 action->rate_ = rate;
290 if (getUpdateMechanism() == UM_LAZY) {
291 action->refreshLastUpdate();
294 double bandwidth_bound = -1.0;
295 if (sg_weight_S_parameter > 0)
296 for (auto const& link : route)
297 action->weight_ += sg_weight_S_parameter / link->bandwidth();
299 for (auto const& link : route) {
300 double bb = bandwidthFactor(size) * link->bandwidth();
301 bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
304 action->latCurrent_ = action->latency_;
305 action->latency_ *= latencyFactor(size);
306 action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
308 int constraints_per_variable = route.size();
309 constraints_per_variable += back_route.size();
311 if (action->latency_ > 0) {
312 action->setVariable(maxminSystem_->variable_new(action, 0.0, -1.0, constraints_per_variable));
313 if (getUpdateMechanism() == UM_LAZY) {
314 // add to the heap the event when the latency is payed
315 XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->getLastUpdate());
316 action->heapInsert(getActionHeap(), action->latency_ + action->getLastUpdate(),
317 route.empty() ? kernel::resource::Action::Type::NORMAL
318 : kernel::resource::Action::Type::LATENCY);
321 action->setVariable(maxminSystem_->variable_new(action, 1.0, -1.0, constraints_per_variable));
323 if (action->rate_ < 0) {
324 maxminSystem_->update_variable_bound(action->getVariable(),
325 (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
327 maxminSystem_->update_variable_bound(action->getVariable(),
328 (action->latCurrent_ > 0)
329 ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_))
333 for (auto const& link : route)
334 maxminSystem_->expand(link->constraint(), action->getVariable(), 1.0);
336 if (not back_route.empty()) { // sg_network_crosstraffic was activated
337 XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
338 for (auto const& link : back_route)
339 maxminSystem_->expand(link->constraint(), action->getVariable(), .05);
341 // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
342 // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
343 // action->getVariable()->set_concurrency_share(2)
347 simgrid::s4u::Link::onCommunicate(action, src, dst);
354 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
355 e_surf_link_sharing_policy_t policy, lmm_system_t system)
356 : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
358 bandwidth_.scale = 1.0;
359 bandwidth_.peak = bandwidth;
361 latency_.scale = 1.0;
362 latency_.peak = latency;
364 if (policy == SURF_LINK_FATPIPE)
365 constraint()->unshare();
367 simgrid::s4u::Link::onCreation(this->piface_);
370 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
372 /* Find out which of my iterators was triggered, and react accordingly */
373 if (triggered == bandwidth_.event) {
375 tmgr_trace_event_unref(&bandwidth_.event);
377 } else if (triggered == latency_.event) {
379 tmgr_trace_event_unref(&latency_.event);
381 } else if (triggered == stateEvent_) {
385 kernel::lmm::Variable* var = nullptr;
386 const_lmm_element_t elem = nullptr;
387 double now = surf_get_clock();
390 while ((var = constraint()->get_variable(&elem))) {
391 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
393 if (action->getState() == kernel::resource::Action::State::running ||
394 action->getState() == kernel::resource::Action::State::ready) {
395 action->setFinishTime(now);
396 action->setState(kernel::resource::Action::State::failed);
400 tmgr_trace_event_unref(&stateEvent_);
402 xbt_die("Unknown event!\n");
405 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)", constraint());
408 void NetworkCm02Link::setBandwidth(double value)
410 bandwidth_.peak = value;
412 model()->getMaxminSystem()->update_constraint_bound(constraint(),
413 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
414 TRACE_surf_link_set_bandwidth(surf_get_clock(), getCname(), sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
416 if (sg_weight_S_parameter > 0) {
417 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
419 kernel::lmm::Variable* var;
420 const_lmm_element_t elem = nullptr;
421 const_lmm_element_t nextelem = nullptr;
423 while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
424 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
425 action->weight_ += delta;
426 if (not action->isSuspended())
427 model()->getMaxminSystem()->update_variable_weight(action->getVariable(), action->weight_);
432 void NetworkCm02Link::setLatency(double value)
434 double delta = value - latency_.peak;
435 kernel::lmm::Variable* var = nullptr;
436 const_lmm_element_t elem = nullptr;
437 const_lmm_element_t nextelem = nullptr;
440 latency_.peak = value;
442 while ((var = constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
443 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
444 action->latCurrent_ += delta;
445 action->weight_ += delta;
446 if (action->rate_ < 0)
447 model()->getMaxminSystem()->update_variable_bound(action->getVariable(),
448 sg_tcp_gamma / (2.0 * action->latCurrent_));
450 model()->getMaxminSystem()->update_variable_bound(
451 action->getVariable(), std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
453 if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
454 XBT_INFO("Flow is limited BYBANDWIDTH");
456 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
459 if (not action->isSuspended())
460 model()->getMaxminSystem()->update_variable_weight(action->getVariable(), action->weight_);
468 void NetworkCm02Action::updateRemainingLazy(double now)
470 if (suspended_ != Action::SuspendStates::not_suspended)
473 double delta = now - getLastUpdate();
474 double max_duration = getMaxDuration();
476 if (getRemainsNoUpdate() > 0) {
477 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(), getLastUpdate());
478 updateRemains(getLastValue() * delta);
480 XBT_DEBUG("Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
483 if (max_duration > NO_MAX_DURATION) {
484 double_update(&max_duration, delta, sg_surf_precision);
485 setMaxDuration(max_duration);
488 if ((getRemainsNoUpdate() <= 0 && (getVariable()->get_weight() > 0)) ||
489 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
490 finish(Action::State::done);
491 heapRemove(getModel()->getActionHeap());
495 setLastValue(getVariable()->get_value());