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. */
6 #include "network_cm02.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
10 #include "src/surf/surf_interface.hpp"
11 #include "surf/surf.hpp"
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
15 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
16 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
17 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
19 /************************************************************************/
20 /* New model based on optimizations discussed during Pedro Velho's thesis*/
21 /************************************************************************/
22 /* @techreport{VELHO:2011:HAL-00646896:1, */
23 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
24 /* title = {{Flow-level network models: have we reached the limits?}}, */
25 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
26 /* type = {Rapport de recherche}, */
27 /* institution = {INRIA}, */
28 /* number = {RR-7821}, */
31 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
33 void surf_network_model_init_LegrandVelho()
35 if (surf_network_model)
38 surf_network_model = new simgrid::surf::NetworkCm02Model();
39 all_existing_models->push_back(surf_network_model);
41 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
42 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
43 xbt_cfg_setdefault_double("network/weight-S", 20537);
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}, */
57 void surf_network_model_init_CM02()
60 if (surf_network_model)
63 xbt_cfg_setdefault_double("network/latency-factor", 1.0);
64 xbt_cfg_setdefault_double("network/bandwidth-factor", 1.0);
65 xbt_cfg_setdefault_double("network/weight-S", 0.0);
67 surf_network_model = new simgrid::surf::NetworkCm02Model();
68 all_existing_models->push_back(surf_network_model);
71 /***************************************************************************/
72 /* The models from Steven H. Low */
73 /***************************************************************************/
75 /* author={Steven H. Low}, */
76 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
78 /* journal={{IEEE/ACM} Transactions on Networking}, */
79 /* volume={11}, number={4}, */
81 void surf_network_model_init_Reno()
83 if (surf_network_model)
86 using namespace simgrid::kernel;
87 lmm::Lagrange::set_default_protocol_function(lmm::func_reno_f, lmm::func_reno_fp, lmm::func_reno_fpi);
89 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
90 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
91 xbt_cfg_setdefault_double("network/weight-S", 20537);
93 surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
94 all_existing_models->push_back(surf_network_model);
98 void surf_network_model_init_Reno2()
100 if (surf_network_model)
103 using namespace simgrid::kernel;
104 lmm::Lagrange::set_default_protocol_function(lmm::func_reno2_f, lmm::func_reno2_fp, lmm::func_reno2_fpi);
106 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
107 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
108 xbt_cfg_setdefault_double("network/weight-S", 20537);
110 surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
111 all_existing_models->push_back(surf_network_model);
114 void surf_network_model_init_Vegas()
116 if (surf_network_model)
119 using namespace simgrid::kernel;
120 lmm::Lagrange::set_default_protocol_function(lmm::func_vegas_f, lmm::func_vegas_fp, lmm::func_vegas_fpi);
122 xbt_cfg_setdefault_double("network/latency-factor", 13.01);
123 xbt_cfg_setdefault_double("network/bandwidth-factor", 0.97);
124 xbt_cfg_setdefault_double("network/weight-S", 20537);
126 surf_network_model = new simgrid::surf::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
127 all_existing_models->push_back(surf_network_model);
133 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool))
134 : NetworkModel(xbt_cfg_get_string("network/optim") == "Full" ? kernel::resource::Model::UpdateAlgo::Full
135 : kernel::resource::Model::UpdateAlgo::Lazy)
137 std::string optim = xbt_cfg_get_string("network/optim");
138 bool select = xbt_cfg_get_boolean("network/maxmin-selective-update");
140 if (optim == "Lazy") {
141 xbt_assert(select || xbt_cfg_is_default_value("network/maxmin-selective-update"),
142 "You cannot disable network selective update when using the lazy update mechanism");
146 set_maxmin_system(make_new_lmm_system(select));
147 loopback_ = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, SURF_LINK_FATPIPE);
150 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
151 e_surf_link_sharing_policy_t policy)
153 return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
156 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
158 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
160 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(get_action_heap().pop());
161 XBT_DEBUG("Something happened to action %p", action);
162 if (TRACE_is_enabled()) {
163 int n = action->get_variable()->get_number_of_constraint();
165 for (int i = 0; i < n; i++){
166 kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
167 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
168 double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
169 TRACE_surf_link_set_utilization(link->get_cname(), action->get_category(), value, action->get_last_update(),
170 now - action->get_last_update());
174 // if I am wearing a latency hat
175 if (action->get_type() == kernel::resource::ActionHeap::Type::latency) {
176 XBT_DEBUG("Latency paid for action %p. Activating", action);
177 get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
178 get_action_heap().remove(action);
179 action->set_last_update();
181 // if I am wearing a max_duration or normal hat
182 } else if (action->get_type() == kernel::resource::ActionHeap::Type::max_duration ||
183 action->get_type() == kernel::resource::ActionHeap::Type::normal) {
184 // no need to communicate anymore
185 // assume that flows that reached max_duration have remaining of 0
186 XBT_DEBUG("Action %p finished", action);
187 action->finish(kernel::resource::Action::State::done);
188 get_action_heap().remove(action);
193 void NetworkCm02Model::update_actions_state_full(double now, double delta)
195 for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
196 NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
197 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
198 XBT_DEBUG("Something happened to action %p", &action);
199 double deltap = delta;
200 if (action.latency_ > 0) {
201 if (action.latency_ > deltap) {
202 double_update(&action.latency_, deltap, sg_surf_precision);
205 double_update(&deltap, action.latency_, sg_surf_precision);
206 action.latency_ = 0.0;
208 if (action.latency_ <= 0.0 && not action.is_suspended())
209 get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
211 if (TRACE_is_enabled()) {
212 int n = action.get_variable()->get_number_of_constraint();
213 for (int i = 0; i < n; i++) {
214 kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
215 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
216 TRACE_surf_link_set_utilization(
217 link->get_cname(), action.get_category(),
218 (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
219 action.get_last_update(), now - action.get_last_update());
222 if (not action.get_variable()->get_number_of_constraint()) {
223 /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
224 * vivaldi. In such case, just make sure that the action completes immediately.
226 action.update_remains(action.get_remains());
228 action.update_remains(action.get_variable()->get_value() * delta);
230 if (action.get_max_duration() > NO_MAX_DURATION)
231 action.update_max_duration(delta);
233 if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
234 ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
235 action.finish(kernel::resource::Action::State::done);
240 kernel::resource::Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
243 double latency = 0.0;
244 std::vector<LinkImpl*> back_route;
245 std::vector<LinkImpl*> route;
247 XBT_IN("(%s,%s,%g,%g)", src->get_cname(), dst->get_cname(), size, rate);
249 src->routeTo(dst, route, &latency);
250 xbt_assert(not route.empty() || latency,
251 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
252 src->get_cname(), dst->get_cname());
254 for (auto const& link : route)
258 if (cfg_crosstraffic == 1) {
259 dst->routeTo(src, back_route, nullptr);
260 for (auto const& link : back_route)
265 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
266 action->weight_ = latency;
267 action->latency_ = latency;
268 action->rate_ = rate;
269 if (get_update_algorithm() == kernel::resource::Model::UpdateAlgo::Lazy) {
270 action->set_last_update();
273 double bandwidth_bound = -1.0;
274 if (sg_weight_S_parameter > 0)
275 for (auto const& link : route)
276 action->weight_ += sg_weight_S_parameter / link->bandwidth();
278 for (auto const& link : route) {
279 double bb = bandwidthFactor(size) * link->bandwidth();
280 bandwidth_bound = (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
283 action->lat_current_ = action->latency_;
284 action->latency_ *= latencyFactor(size);
285 action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
287 int constraints_per_variable = route.size();
288 constraints_per_variable += back_route.size();
290 if (action->latency_ > 0) {
291 action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
292 if (get_update_algorithm() == kernel::resource::Model::UpdateAlgo::Lazy) {
293 // add to the heap the event when the latency is payed
294 double date = action->latency_ + action->get_last_update();
295 kernel::resource::ActionHeap::Type type;
297 type = kernel::resource::ActionHeap::Type::normal;
299 type = kernel::resource::ActionHeap::Type::latency;
301 XBT_DEBUG("Added action (%p) one latency event at date %f", action, date);
302 get_action_heap().insert(action, date, type);
305 action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
307 if (action->rate_ < 0) {
308 get_maxmin_system()->update_variable_bound(
309 action->get_variable(), (action->lat_current_ > 0) ? cfg_tcp_gamma / (2.0 * action->lat_current_) : -1.0);
311 get_maxmin_system()->update_variable_bound(
312 action->get_variable(), (action->lat_current_ > 0)
313 ? std::min(action->rate_, cfg_tcp_gamma / (2.0 * action->lat_current_))
317 for (auto const& link : route)
318 get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), 1.0);
320 if (not back_route.empty()) { // cfg_crosstraffic was activated
321 XBT_DEBUG("Crosstraffic active adding backward flow using 5%%");
322 for (auto const& link : back_route)
323 get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), .05);
325 // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
326 // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
327 // action->getVariable()->set_concurrency_share(2)
331 simgrid::s4u::Link::onCommunicate(action, src, dst);
338 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
339 e_surf_link_sharing_policy_t policy, kernel::lmm::System* system)
340 : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
342 bandwidth_.scale = 1.0;
343 bandwidth_.peak = bandwidth;
345 latency_.scale = 1.0;
346 latency_.peak = latency;
348 if (policy == SURF_LINK_FATPIPE)
349 get_constraint()->unshare();
351 simgrid::s4u::Link::onCreation(this->piface_);
354 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
356 /* Find out which of my iterators was triggered, and react accordingly */
357 if (triggered == bandwidth_.event) {
359 tmgr_trace_event_unref(&bandwidth_.event);
361 } else if (triggered == latency_.event) {
363 tmgr_trace_event_unref(&latency_.event);
365 } else if (triggered == stateEvent_) {
369 kernel::lmm::Variable* var = nullptr;
370 const kernel::lmm::Element* elem = nullptr;
371 double now = surf_get_clock();
374 while ((var = get_constraint()->get_variable(&elem))) {
375 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
377 if (action->get_state() == kernel::resource::Action::State::running ||
378 action->get_state() == kernel::resource::Action::State::ready) {
379 action->set_finish_time(now);
380 action->set_state(kernel::resource::Action::State::failed);
384 tmgr_trace_event_unref(&stateEvent_);
386 xbt_die("Unknown event!\n");
389 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
393 void NetworkCm02Link::setBandwidth(double value)
395 bandwidth_.peak = value;
397 get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
398 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
399 TRACE_surf_link_set_bandwidth(surf_get_clock(), get_cname(),
400 sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
402 if (sg_weight_S_parameter > 0) {
403 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
405 kernel::lmm::Variable* var;
406 const kernel::lmm::Element* elem = nullptr;
407 const kernel::lmm::Element* nextelem = nullptr;
409 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
410 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
411 action->weight_ += delta;
412 if (not action->is_suspended())
413 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
418 void NetworkCm02Link::setLatency(double value)
420 double delta = value - latency_.peak;
421 kernel::lmm::Variable* var = nullptr;
422 const kernel::lmm::Element* elem = nullptr;
423 const kernel::lmm::Element* nextelem = nullptr;
426 latency_.peak = value;
428 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
429 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
430 action->lat_current_ += delta;
431 action->weight_ += delta;
432 if (action->rate_ < 0)
433 get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
434 (2.0 * action->lat_current_));
436 get_model()->get_maxmin_system()->update_variable_bound(
437 action->get_variable(), std::min(action->rate_, NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
439 if (action->rate_ < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
440 XBT_INFO("Flow is limited BYBANDWIDTH");
442 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
445 if (not action->is_suspended())
446 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
454 void NetworkCm02Action::update_remains_lazy(double now)
456 if (suspended_ != Action::SuspendStates::not_suspended)
459 double delta = now - get_last_update();
460 double max_duration = get_max_duration();
462 if (get_remains_no_update() > 0) {
463 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
465 update_remains(get_last_value() * delta);
467 XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
470 if (max_duration > NO_MAX_DURATION) {
471 double_update(&max_duration, delta, sg_surf_precision);
472 set_max_duration(max_duration);
475 if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
476 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
477 finish(Action::State::done);
478 get_model()->get_action_heap().remove(this);
482 set_last_value(get_variable()->get_value());