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::kernel::resource::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::kernel::resource::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::kernel::resource::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::kernel::resource::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::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
127 all_existing_models->push_back(surf_network_model);
134 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool))
135 : NetworkModel(simgrid::config::get_value<std::string>("network/optim") == "Full" ? Model::UpdateAlgo::Full
136 : Model::UpdateAlgo::Lazy)
138 std::string optim = simgrid::config::get_value<std::string>("network/optim");
139 bool select = simgrid::config::get_value<bool>("network/maxmin-selective-update");
141 if (optim == "Lazy") {
142 xbt_assert(select || xbt_cfg_is_default_value("network/maxmin-selective-update"),
143 "You cannot disable network selective update when using the lazy update mechanism");
147 set_maxmin_system(make_new_lmm_system(select));
148 loopback_ = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, s4u::Link::SharingPolicy::FATPIPE);
151 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
152 s4u::Link::SharingPolicy policy)
154 return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
157 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
159 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
161 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(get_action_heap().pop());
162 XBT_DEBUG("Something happened to action %p", action);
163 if (TRACE_is_enabled()) {
164 int n = action->get_variable()->get_number_of_constraint();
166 for (int i = 0; i < n; i++){
167 kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
168 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
169 double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
170 TRACE_surf_link_set_utilization(link->get_cname(), action->get_category(), value, action->get_last_update(),
171 now - action->get_last_update());
175 // if I am wearing a latency hat
176 if (action->get_type() == ActionHeap::Type::latency) {
177 XBT_DEBUG("Latency paid for action %p. Activating", action);
178 get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
179 get_action_heap().remove(action);
180 action->set_last_update();
182 // if I am wearing a max_duration or normal hat
183 } else if (action->get_type() == ActionHeap::Type::max_duration || action->get_type() == 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(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(Action::State::done);
240 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() == 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() == 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 ActionHeap::Type type;
297 type = ActionHeap::Type::normal;
299 type = 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 s4u::Link::SharingPolicy 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 == s4u::Link::SharingPolicy::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 Action* action = static_cast<Action*>(var->get_id());
377 if (action->get_state() == Action::State::running || action->get_state() == Action::State::ready) {
378 action->set_finish_time(now);
379 action->set_state(Action::State::failed);
383 tmgr_trace_event_unref(&stateEvent_);
385 xbt_die("Unknown event!\n");
388 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
392 void NetworkCm02Link::setBandwidth(double value)
394 bandwidth_.peak = value;
396 get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
397 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
398 TRACE_surf_link_set_bandwidth(surf_get_clock(), get_cname(),
399 sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
401 if (sg_weight_S_parameter > 0) {
402 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
404 kernel::lmm::Variable* var;
405 const kernel::lmm::Element* elem = nullptr;
406 const kernel::lmm::Element* nextelem = nullptr;
408 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
409 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
410 action->weight_ += delta;
411 if (not action->is_suspended())
412 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
417 void NetworkCm02Link::setLatency(double value)
419 double delta = value - latency_.peak;
420 kernel::lmm::Variable* var = nullptr;
421 const kernel::lmm::Element* elem = nullptr;
422 const kernel::lmm::Element* nextelem = nullptr;
425 latency_.peak = value;
427 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
428 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
429 action->lat_current_ += delta;
430 action->weight_ += delta;
431 if (action->rate_ < 0)
432 get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
433 (2.0 * action->lat_current_));
435 get_model()->get_maxmin_system()->update_variable_bound(
436 action->get_variable(), std::min(action->rate_, NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
438 if (action->rate_ < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
439 XBT_INFO("Flow is limited BYBANDWIDTH");
441 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
444 if (not action->is_suspended())
445 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
453 void NetworkCm02Action::update_remains_lazy(double now)
455 if (suspended_ != Action::SuspendStates::not_suspended)
458 double delta = now - get_last_update();
459 double max_duration = get_max_duration();
461 if (get_remains_no_update() > 0) {
462 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
464 update_remains(get_last_value() * delta);
466 XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
469 if (max_duration > NO_MAX_DURATION) {
470 double_update(&max_duration, delta, sg_surf_precision);
471 set_max_duration(max_duration);
474 if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
475 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
476 finish(Action::State::done);
477 get_model()->get_action_heap().remove(this);
481 set_last_value(get_variable()->get_value());
486 } // namespace simgrid