1 /* Copyright (c) 2013-2017. 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 "maxmin_private.hpp"
9 #include "network_cm02.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/sg_config.h"
12 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
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 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
92 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
93 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
94 xbt_cfg_setdefault_double("network/weight-S", 20537);
96 surf_network_model = new simgrid::surf::NetworkCm02Model(&lagrange_solve);
97 all_existing_models->push_back(surf_network_model);
101 void surf_network_model_init_Reno2()
103 if (surf_network_model)
106 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
108 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
109 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
110 xbt_cfg_setdefault_double("network/weight-S", 20537);
112 surf_network_model = new simgrid::surf::NetworkCm02Model(&lagrange_solve);
113 all_existing_models->push_back(surf_network_model);
116 void surf_network_model_init_Vegas()
118 if (surf_network_model)
121 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
123 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
124 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
125 xbt_cfg_setdefault_double("network/weight-S", 20537);
127 surf_network_model = new simgrid::surf::NetworkCm02Model(&lagrange_solve);
128 all_existing_models->push_back(surf_network_model);
134 NetworkCm02Model::NetworkCm02Model()
137 std::string optim = xbt_cfg_get_string("network/optim");
138 bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
140 if (optim == "Full") {
141 setUpdateMechanism(UM_FULL);
142 selectiveUpdate_ = select;
143 } else if (optim == "Lazy") {
144 setUpdateMechanism(UM_LAZY);
145 selectiveUpdate_ = true;
146 xbt_assert(select || (xbt_cfg_is_default_value("network/maxmin-selective-update")),
147 "You cannot disable selective update when using the lazy update mechanism");
149 xbt_die("Unsupported optimization (%s) for this model. Accepted: Full, Lazy.", optim.c_str());
152 maxminSystem_ = lmm_system_new(selectiveUpdate_);
153 loopback_ = createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
155 if (getUpdateMechanism() == UM_LAZY) {
156 modifiedSet_ = new ActionLmmList();
157 maxminSystem_->keep_track = modifiedSet_;
160 NetworkCm02Model::NetworkCm02Model(void (*specificSolveFun)(lmm_system_t self))
163 maxminSystem_->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, maxminSystem_);
172 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
174 while ((xbt_heap_size(getActionHeap()) > 0) &&
175 (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
177 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(xbt_heap_pop(getActionHeap()));
178 XBT_DEBUG("Something happened to action %p", action);
179 if (TRACE_is_enabled()) {
180 int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
182 for (int i = 0; i < n; i++){
183 lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
184 NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
185 double value = lmm_variable_getvalue(action->getVariable())*
186 lmm_get_cnst_weight_from_var(maxminSystem_, action->getVariable(), i);
187 TRACE_surf_link_set_utilization(link->getCname(), action->getCategory(), value, action->getLastUpdate(),
188 now - action->getLastUpdate());
192 // if I am wearing a latency hat
193 if (action->getHat() == LATENCY) {
194 XBT_DEBUG("Latency paid for action %p. Activating", action);
195 lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
196 action->heapRemove(getActionHeap());
197 action->refreshLastUpdate();
199 // if I am wearing a max_duration or normal hat
200 } else if (action->getHat() == MAX_DURATION || action->getHat() == NORMAL) {
201 // no need to communicate anymore
202 // assume that flows that reached max_duration have remaining of 0
203 XBT_DEBUG("Action %p finished", action);
204 action->setRemains(0);
205 action->finish(Action::State::done);
206 action->heapRemove(getActionHeap());
212 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
214 ActionList *running_actions = getRunningActionSet();
215 ActionList::iterator it(running_actions->begin());
216 ActionList::iterator itend(running_actions->end());
217 while (it != itend) {
218 NetworkCm02Action *action = static_cast<NetworkCm02Action*> (&*it);
220 XBT_DEBUG("Something happened to action %p", action);
221 double deltap = delta;
222 if (action->latency_ > 0) {
223 if (action->latency_ > deltap) {
224 double_update(&(action->latency_), deltap, sg_surf_precision);
227 double_update(&(deltap), action->latency_, sg_surf_precision);
228 action->latency_ = 0.0;
230 if (action->latency_ <= 0.0 && not action->isSuspended())
231 lmm_update_variable_weight(maxminSystem_, action->getVariable(), action->weight_);
233 if (TRACE_is_enabled()) {
234 int n = lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable());
235 for (int i = 0; i < n; i++){
236 lmm_constraint_t constraint = lmm_get_cnst_from_var(maxminSystem_, action->getVariable(), i);
238 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
239 TRACE_surf_link_set_utilization(link->getCname(), action->getCategory(),
240 (lmm_variable_getvalue(action->getVariable()) *
241 lmm_get_cnst_weight_from_var(maxminSystem_, action->getVariable(), i)),
242 action->getLastUpdate(), now - action->getLastUpdate());
245 if (not lmm_get_number_of_cnst_from_var(maxminSystem_, action->getVariable())) {
246 /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
247 * vivaldi. In such case, just make sure that the action completes immediately.
249 action->updateRemains(action->getRemains());
251 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
253 if (action->getMaxDuration() > NO_MAX_DURATION)
254 action->updateMaxDuration(delta);
256 if (((action->getRemains() <= 0) && (lmm_get_variable_weight(action->getVariable()) > 0)) ||
257 ((action->getMaxDuration() > NO_MAX_DURATION) && (action->getMaxDuration() <= 0))) {
258 action->finish(Action::State::done);
263 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
266 double latency = 0.0;
267 std::vector<LinkImpl*> back_route;
268 std::vector<LinkImpl*> route;
270 XBT_IN("(%s,%s,%g,%g)", src->getCname(), dst->getCname(), size, rate);
272 src->routeTo(dst, route, &latency);
273 xbt_assert(not route.empty() || latency,
274 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
275 src->getCname(), dst->getCname());
277 for (auto const& link : route)
281 if (sg_network_crosstraffic == 1) {
282 dst->routeTo(src, back_route, nullptr);
283 for (auto const& link : back_route)
288 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
289 action->weight_ = latency;
290 action->latency_ = latency;
291 action->rate_ = rate;
292 if (getUpdateMechanism() == UM_LAZY) {
293 action->updateIndexHeap(-1);
294 action->refreshLastUpdate();
297 double bandwidth_bound = -1.0;
298 if (sg_weight_S_parameter > 0)
299 for (auto const& link : route)
300 action->weight_ += sg_weight_S_parameter / link->bandwidth();
302 for (auto const& link : route) {
303 double bb = bandwidthFactor(size) * link->bandwidth();
304 bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
307 action->latCurrent_ = action->latency_;
308 action->latency_ *= latencyFactor(size);
309 action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
311 int constraints_per_variable = route.size();
312 constraints_per_variable += back_route.size();
314 if (action->latency_ > 0) {
315 action->setVariable(lmm_variable_new(maxminSystem_, action, 0.0, -1.0, constraints_per_variable));
316 if (getUpdateMechanism() == UM_LAZY) {
317 // add to the heap the event when the latency is payed
318 XBT_DEBUG("Added action (%p) one latency event at date %f", action, action->latency_ + action->getLastUpdate());
319 action->heapInsert(getActionHeap(), action->latency_ + action->getLastUpdate(), route.empty() ? NORMAL : LATENCY);
322 action->setVariable(lmm_variable_new(maxminSystem_, action, 1.0, -1.0, constraints_per_variable));
324 if (action->rate_ < 0) {
325 lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? sg_tcp_gamma / (2.0 * action->latCurrent_) : -1.0);
327 lmm_update_variable_bound(maxminSystem_, action->getVariable(), (action->latCurrent_ > 0) ? std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)) : action->rate_);
330 for (auto const& link : route)
331 lmm_expand(maxminSystem_, link->constraint(), action->getVariable(), 1.0);
333 if (not back_route.empty()) { // sg_network_crosstraffic was activated
334 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
335 for (auto const& link : back_route)
336 lmm_expand(maxminSystem_, 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 lmm_element_concurrency())
340 //lmm_variable_concurrency_share_set(action->getVariable(),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, lmm_system_t system)
353 : LinkImpl(model, name, lmm_constraint_new(system, 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 lmm_constraint_shared(constraint());
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 lmm_variable_t var = nullptr;
383 lmm_element_t elem = nullptr;
384 double now = surf_get_clock();
387 while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
388 Action *action = static_cast<Action*>( lmm_variable_id(var) );
390 if (action->getState() == Action::State::running ||
391 action->getState() == Action::State::ready) {
392 action->setFinishTime(now);
393 action->setState(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)
408 bandwidth_.peak = value;
410 lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(),
411 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
412 TRACE_surf_link_set_bandwidth(surf_get_clock(), getCname(), sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
414 if (sg_weight_S_parameter > 0) {
415 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
418 lmm_element_t elem = nullptr;
419 lmm_element_t nextelem = nullptr;
421 while ((var = lmm_get_var_from_cnst_safe(model()->getMaxminSystem(), constraint(), &elem, &nextelem, &numelem))) {
422 NetworkCm02Action *action = static_cast<NetworkCm02Action*>(lmm_variable_id(var));
423 action->weight_ += delta;
424 if (not action->isSuspended())
425 lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), action->weight_);
430 void NetworkCm02Link::setLatency(double value)
432 double delta = value - latency_.peak;
433 lmm_variable_t var = nullptr;
434 lmm_element_t elem = nullptr;
435 lmm_element_t nextelem = nullptr;
438 latency_.peak = value;
440 while ((var = lmm_get_var_from_cnst_safe(model()->getMaxminSystem(), constraint(), &elem, &nextelem, &numelem))) {
441 NetworkCm02Action *action = static_cast<NetworkCm02Action*>(lmm_variable_id(var));
442 action->latCurrent_ += delta;
443 action->weight_ += delta;
444 if (action->rate_ < 0)
445 lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(),
446 sg_tcp_gamma / (2.0 * action->latCurrent_));
448 lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(),
449 std::min(action->rate_, sg_tcp_gamma / (2.0 * action->latCurrent_)));
451 if (action->rate_ < sg_tcp_gamma / (2.0 * action->latCurrent_)) {
452 XBT_INFO("Flow is limited BYBANDWIDTH");
454 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->latCurrent_);
457 if (not action->isSuspended())
458 lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), action->weight_);
466 void NetworkCm02Action::updateRemainingLazy(double now)
471 double delta = now - getLastUpdate();
472 double max_duration = getMaxDuration();
474 if (getRemainsNoUpdate() > 0) {
475 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(), getLastUpdate());
476 updateRemains(getLastValue() * delta);
478 XBT_DEBUG("Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
481 if (max_duration > NO_MAX_DURATION) {
482 double_update(&max_duration, delta, sg_surf_precision);
483 setMaxDuration(max_duration);
486 if ((getRemainsNoUpdate() <= 0 && (lmm_get_variable_weight(getVariable()) > 0)) ||
487 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
488 finish(Action::State::done);
489 heapRemove(getModel()->getActionHeap());
493 setLastValue(lmm_variable_getvalue(getVariable()));