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.hpp"
11 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
12 #include "src/surf/surf_interface.hpp"
13 #include "surf/surf.hpp"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
17 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
18 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
19 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
21 /************************************************************************/
22 /* New model based on optimizations discussed during Pedro Velho's thesis*/
23 /************************************************************************/
24 /* @techreport{VELHO:2011:HAL-00646896:1, */
25 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
26 /* title = {{Flow-level network models: have we reached the limits?}}, */
27 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
28 /* type = {Rapport de recherche}, */
29 /* institution = {INRIA}, */
30 /* number = {RR-7821}, */
33 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
35 void surf_network_model_init_LegrandVelho()
37 if (surf_network_model)
40 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
41 all_existing_models->push_back(surf_network_model);
43 simgrid::config::set_default<double>("network/latency-factor", 13.01);
44 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
45 simgrid::config::set_default<double>("network/weight-S", 20537);
48 /***************************************************************************/
49 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
50 /***************************************************************************/
51 /* @TechReport{ rr-lip2002-40, */
52 /* author = {Henri Casanova and Loris Marchal}, */
53 /* institution = {LIP}, */
54 /* title = {A Network Model for Simulation of Grid Application}, */
55 /* number = {2002-40}, */
59 void surf_network_model_init_CM02()
62 if (surf_network_model)
65 simgrid::config::set_default<double>("network/latency-factor", 1.0);
66 simgrid::config::set_default<double>("network/bandwidth-factor", 1.0);
67 simgrid::config::set_default<double>("network/weight-S", 0.0);
69 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
70 all_existing_models->push_back(surf_network_model);
73 /***************************************************************************/
74 /* The models from Steven H. Low */
75 /***************************************************************************/
77 /* author={Steven H. Low}, */
78 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
80 /* journal={{IEEE/ACM} Transactions on Networking}, */
81 /* volume={11}, number={4}, */
83 void surf_network_model_init_Reno()
85 if (surf_network_model)
88 using namespace simgrid::kernel;
89 lmm::Lagrange::set_default_protocol_function(lmm::func_reno_f, lmm::func_reno_fp, lmm::func_reno_fpi);
91 simgrid::config::set_default<double>("network/latency-factor", 13.01);
92 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
93 simgrid::config::set_default<double>("network/weight-S", 20537);
95 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
96 all_existing_models->push_back(surf_network_model);
100 void surf_network_model_init_Reno2()
102 if (surf_network_model)
105 using namespace simgrid::kernel;
106 lmm::Lagrange::set_default_protocol_function(lmm::func_reno2_f, lmm::func_reno2_fp, lmm::func_reno2_fpi);
108 simgrid::config::set_default<double>("network/latency-factor", 13.01);
109 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
110 simgrid::config::set_default<double>("network/weight-S", 20537);
112 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
113 all_existing_models->push_back(surf_network_model);
116 void surf_network_model_init_Vegas()
118 if (surf_network_model)
121 using namespace simgrid::kernel;
122 lmm::Lagrange::set_default_protocol_function(lmm::func_vegas_f, lmm::func_vegas_fp, lmm::func_vegas_fpi);
124 simgrid::config::set_default<double>("network/latency-factor", 13.01);
125 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
126 simgrid::config::set_default<double>("network/weight-S", 20537);
128 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
129 all_existing_models->push_back(surf_network_model);
136 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool))
137 : NetworkModel(simgrid::config::get_value<std::string>("network/optim") == "Full" ? Model::UpdateAlgo::Full
138 : Model::UpdateAlgo::Lazy)
140 std::string optim = simgrid::config::get_value<std::string>("network/optim");
141 bool select = simgrid::config::get_value<bool>("network/maxmin-selective-update");
143 if (optim == "Lazy") {
144 xbt_assert(select || simgrid::config::is_default("network/maxmin-selective-update"),
145 "You cannot disable network selective update when using the lazy update mechanism");
149 set_maxmin_system(make_new_lmm_system(select));
150 loopback_ = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, s4u::Link::SharingPolicy::FATPIPE);
153 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
154 s4u::Link::SharingPolicy policy)
156 return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
159 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
161 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
163 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(get_action_heap().pop());
164 XBT_DEBUG("Something happened to action %p", action);
165 if (TRACE_is_enabled()) {
166 int n = action->get_variable()->get_number_of_constraint();
168 for (int i = 0; i < n; i++){
169 kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
170 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
171 double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
172 TRACE_surf_link_set_utilization(link->get_cname(), action->get_category(), value, action->get_last_update(),
173 now - action->get_last_update());
177 // if I am wearing a latency hat
178 if (action->get_type() == ActionHeap::Type::latency) {
179 XBT_DEBUG("Latency paid for action %p. Activating", action);
180 get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
181 get_action_heap().remove(action);
182 action->set_last_update();
184 // if I am wearing a max_duration or normal hat
185 } else if (action->get_type() == ActionHeap::Type::max_duration || action->get_type() == ActionHeap::Type::normal) {
186 // no need to communicate anymore
187 // assume that flows that reached max_duration have remaining of 0
188 XBT_DEBUG("Action %p finished", action);
189 action->finish(Action::State::done);
190 get_action_heap().remove(action);
195 void NetworkCm02Model::update_actions_state_full(double now, double delta)
197 for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
198 NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
199 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
200 XBT_DEBUG("Something happened to action %p", &action);
201 double deltap = delta;
202 if (action.latency_ > 0) {
203 if (action.latency_ > deltap) {
204 double_update(&action.latency_, deltap, sg_surf_precision);
207 double_update(&deltap, action.latency_, sg_surf_precision);
208 action.latency_ = 0.0;
210 if (action.latency_ <= 0.0 && not action.is_suspended())
211 get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
213 if (TRACE_is_enabled()) {
214 int n = action.get_variable()->get_number_of_constraint();
215 for (int i = 0; i < n; i++) {
216 kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
217 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
218 TRACE_surf_link_set_utilization(
219 link->get_cname(), action.get_category(),
220 (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
221 action.get_last_update(), now - action.get_last_update());
224 if (not action.get_variable()->get_number_of_constraint()) {
225 /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
226 * vivaldi. In such case, just make sure that the action completes immediately.
228 action.update_remains(action.get_remains());
230 action.update_remains(action.get_variable()->get_value() * delta);
232 if (action.get_max_duration() > NO_MAX_DURATION)
233 action.update_max_duration(delta);
235 if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
236 ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
237 action.finish(Action::State::done);
242 Action* NetworkCm02Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double rate)
245 double latency = 0.0;
246 std::vector<LinkImpl*> back_route;
247 std::vector<LinkImpl*> route;
249 XBT_IN("(%s,%s,%g,%g)", src->get_cname(), dst->get_cname(), size, rate);
251 src->routeTo(dst, route, &latency);
252 xbt_assert(not route.empty() || latency,
253 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
254 src->get_cname(), dst->get_cname());
256 failed = std::any_of(route.begin(), route.end(), [](LinkImpl* link) { return link->is_off(); });
258 if (cfg_crosstraffic) {
259 dst->routeTo(src, back_route, nullptr);
260 failed = std::any_of(back_route.begin(), back_route.end(), [](LinkImpl* const& link) { return link->is_off(); });
263 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
264 action->weight_ = latency;
265 action->latency_ = latency;
266 action->rate_ = rate;
267 if (get_update_algorithm() == Model::UpdateAlgo::Lazy) {
268 action->set_last_update();
271 if (sg_weight_S_parameter > 0) {
273 std::accumulate(route.begin(), route.end(), action->weight_, [](double total, LinkImpl* const& link) {
274 return total + sg_weight_S_parameter / link->bandwidth();
278 double bandwidth_bound = route.empty() ? -1.0 : bandwidthFactor(size) * route.front()->bandwidth();
280 for (auto const& link : route)
281 bandwidth_bound = std::min(bandwidth_bound, bandwidthFactor(size) * link->bandwidth());
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();
296 ActionHeap::Type type = route.empty() ? ActionHeap::Type::normal : ActionHeap::Type::latency;
298 XBT_DEBUG("Added action (%p) one latency event at date %f", action, date);
299 get_action_heap().insert(action, date, type);
302 action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
304 if (action->rate_ < 0) {
305 get_maxmin_system()->update_variable_bound(
306 action->get_variable(), (action->lat_current_ > 0) ? cfg_tcp_gamma / (2.0 * action->lat_current_) : -1.0);
308 get_maxmin_system()->update_variable_bound(
309 action->get_variable(), (action->lat_current_ > 0)
310 ? std::min(action->rate_, cfg_tcp_gamma / (2.0 * action->lat_current_))
314 for (auto const& link : route)
315 get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), 1.0);
317 if (cfg_crosstraffic) {
318 XBT_DEBUG("Crosstraffic active: adding backward flow using 5%% of the available bandwidth");
319 for (auto const& link : back_route)
320 get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), .05);
322 // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
323 // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
324 // action->getVariable()->set_concurrency_share(2)
328 simgrid::s4u::Link::onCommunicate(action, src, dst);
335 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
336 s4u::Link::SharingPolicy policy, kernel::lmm::System* system)
337 : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
339 bandwidth_.scale = 1.0;
340 bandwidth_.peak = bandwidth;
342 latency_.scale = 1.0;
343 latency_.peak = latency;
345 if (policy == s4u::Link::SharingPolicy::FATPIPE)
346 get_constraint()->unshare();
348 simgrid::s4u::Link::onCreation(this->piface_);
351 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
353 /* Find out which of my iterators was triggered, and react accordingly */
354 if (triggered == bandwidth_.event) {
356 tmgr_trace_event_unref(&bandwidth_.event);
358 } else if (triggered == latency_.event) {
360 tmgr_trace_event_unref(&latency_.event);
362 } else if (triggered == stateEvent_) {
366 kernel::lmm::Variable* var = nullptr;
367 const kernel::lmm::Element* elem = nullptr;
368 double now = surf_get_clock();
371 while ((var = get_constraint()->get_variable(&elem))) {
372 Action* action = static_cast<Action*>(var->get_id());
374 if (action->get_state() == Action::State::running || action->get_state() == Action::State::ready) {
375 action->set_finish_time(now);
376 action->set_state(Action::State::failed);
380 tmgr_trace_event_unref(&stateEvent_);
382 xbt_die("Unknown event!\n");
385 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
389 void NetworkCm02Link::setBandwidth(double value)
391 bandwidth_.peak = value;
393 get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
394 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
395 TRACE_surf_link_set_bandwidth(surf_get_clock(), get_cname(),
396 sg_bandwidth_factor * bandwidth_.peak * bandwidth_.scale);
398 if (sg_weight_S_parameter > 0) {
399 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
401 kernel::lmm::Variable* var;
402 const kernel::lmm::Element* elem = nullptr;
403 const kernel::lmm::Element* nextelem = nullptr;
405 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
406 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
407 action->weight_ += delta;
408 if (not action->is_suspended())
409 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
414 void NetworkCm02Link::setLatency(double value)
416 double delta = value - latency_.peak;
417 kernel::lmm::Variable* var = nullptr;
418 const kernel::lmm::Element* elem = nullptr;
419 const kernel::lmm::Element* nextelem = nullptr;
422 latency_.peak = value;
424 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
425 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
426 action->lat_current_ += delta;
427 action->weight_ += delta;
428 if (action->rate_ < 0)
429 get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
430 (2.0 * action->lat_current_));
432 get_model()->get_maxmin_system()->update_variable_bound(
433 action->get_variable(), std::min(action->rate_, NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
435 if (action->rate_ < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
436 XBT_INFO("Flow is limited BYBANDWIDTH");
438 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
441 if (not action->is_suspended())
442 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
450 void NetworkCm02Action::update_remains_lazy(double now)
452 if (suspended_ != Action::SuspendStates::not_suspended)
455 double delta = now - get_last_update();
456 double max_duration = get_max_duration();
458 if (get_remains_no_update() > 0) {
459 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
461 update_remains(get_last_value() * delta);
463 XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
466 if (max_duration > NO_MAX_DURATION) {
467 double_update(&max_duration, delta, sg_surf_precision);
468 set_max_duration(max_duration);
471 if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
472 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
473 finish(Action::State::done);
474 get_model()->get_action_heap().remove(this);
478 set_last_value(get_variable()->get_value());
483 } // namespace simgrid