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. */
9 #include "network_cm02.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "simgrid/sg_config.hpp"
12 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
13 #include "src/surf/surf_interface.hpp"
14 #include "surf/surf.hpp"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
18 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
19 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
20 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
22 /************************************************************************/
23 /* New model based on optimizations discussed during Pedro Velho's thesis*/
24 /************************************************************************/
25 /* @techreport{VELHO:2011:HAL-00646896:1, */
26 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
27 /* title = {{Flow-level network models: have we reached the limits?}}, */
28 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
29 /* type = {Rapport de recherche}, */
30 /* institution = {INRIA}, */
31 /* number = {RR-7821}, */
34 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
36 void surf_network_model_init_LegrandVelho()
38 if (surf_network_model)
41 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
42 all_existing_models->push_back(surf_network_model);
44 simgrid::config::set_default<double>("network/latency-factor", 13.01);
45 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
46 simgrid::config::set_default<double>("network/weight-S", 20537);
49 /***************************************************************************/
50 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
51 /***************************************************************************/
52 /* @TechReport{ rr-lip2002-40, */
53 /* author = {Henri Casanova and Loris Marchal}, */
54 /* institution = {LIP}, */
55 /* title = {A Network Model for Simulation of Grid Application}, */
56 /* number = {2002-40}, */
60 void surf_network_model_init_CM02()
63 if (surf_network_model)
66 simgrid::config::set_default<double>("network/latency-factor", 1.0);
67 simgrid::config::set_default<double>("network/bandwidth-factor", 1.0);
68 simgrid::config::set_default<double>("network/weight-S", 0.0);
70 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model();
71 all_existing_models->push_back(surf_network_model);
74 /***************************************************************************/
75 /* The models from Steven H. Low */
76 /***************************************************************************/
78 /* author={Steven H. Low}, */
79 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
81 /* journal={{IEEE/ACM} Transactions on Networking}, */
82 /* volume={11}, number={4}, */
84 void surf_network_model_init_Reno()
86 if (surf_network_model)
89 using namespace simgrid::kernel;
90 lmm::Lagrange::set_default_protocol_function(lmm::func_reno_f, lmm::func_reno_fp, lmm::func_reno_fpi);
92 simgrid::config::set_default<double>("network/latency-factor", 13.01);
93 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
94 simgrid::config::set_default<double>("network/weight-S", 20537);
96 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
97 all_existing_models->push_back(surf_network_model);
101 void surf_network_model_init_Reno2()
103 if (surf_network_model)
106 using namespace simgrid::kernel;
107 lmm::Lagrange::set_default_protocol_function(lmm::func_reno2_f, lmm::func_reno2_fp, lmm::func_reno2_fpi);
109 simgrid::config::set_default<double>("network/latency-factor", 13.01);
110 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
111 simgrid::config::set_default<double>("network/weight-S", 20537);
113 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
114 all_existing_models->push_back(surf_network_model);
117 void surf_network_model_init_Vegas()
119 if (surf_network_model)
122 using namespace simgrid::kernel;
123 lmm::Lagrange::set_default_protocol_function(lmm::func_vegas_f, lmm::func_vegas_fp, lmm::func_vegas_fpi);
125 simgrid::config::set_default<double>("network/latency-factor", 13.01);
126 simgrid::config::set_default<double>("network/bandwidth-factor", 0.97);
127 simgrid::config::set_default<double>("network/weight-S", 20537);
129 surf_network_model = new simgrid::kernel::resource::NetworkCm02Model(&simgrid::kernel::lmm::make_new_lagrange_system);
130 all_existing_models->push_back(surf_network_model);
137 NetworkCm02Model::NetworkCm02Model(kernel::lmm::System* (*make_new_lmm_system)(bool))
138 : NetworkModel(simgrid::config::get_value<std::string>("network/optim") == "Full" ? Model::UpdateAlgo::Full
139 : Model::UpdateAlgo::Lazy)
141 std::string optim = simgrid::config::get_value<std::string>("network/optim");
142 bool select = simgrid::config::get_value<bool>("network/maxmin-selective-update");
144 if (optim == "Lazy") {
145 xbt_assert(select || simgrid::config::is_default("network/maxmin-selective-update"),
146 "You cannot disable network selective update when using the lazy update mechanism");
150 set_maxmin_system(make_new_lmm_system(select));
151 loopback_ = NetworkCm02Model::createLink("__loopback__", 498000000, 0.000015, s4u::Link::SharingPolicy::FATPIPE);
154 LinkImpl* NetworkCm02Model::createLink(const std::string& name, double bandwidth, double latency,
155 s4u::Link::SharingPolicy policy)
157 return new NetworkCm02Link(this, name, bandwidth, latency, policy, get_maxmin_system());
160 void NetworkCm02Model::update_actions_state_lazy(double now, double /*delta*/)
162 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
164 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(get_action_heap().pop());
165 XBT_DEBUG("Something happened to action %p", action);
166 if (TRACE_is_enabled()) {
167 int n = action->get_variable()->get_number_of_constraint();
169 for (int i = 0; i < n; i++){
170 kernel::lmm::Constraint* constraint = action->get_variable()->get_constraint(i);
171 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
172 double value = action->get_variable()->get_value() * action->get_variable()->get_constraint_weight(i);
173 TRACE_surf_link_set_utilization(link->get_cname(), action->get_category(), value, action->get_last_update(),
174 now - action->get_last_update());
178 // if I am wearing a latency hat
179 if (action->get_type() == ActionHeap::Type::latency) {
180 XBT_DEBUG("Latency paid for action %p. Activating", action);
181 get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
182 get_action_heap().remove(action);
183 action->set_last_update();
185 // if I am wearing a max_duration or normal hat
186 } else if (action->get_type() == ActionHeap::Type::max_duration || action->get_type() == ActionHeap::Type::normal) {
187 // no need to communicate anymore
188 // assume that flows that reached max_duration have remaining of 0
189 XBT_DEBUG("Action %p finished", action);
190 action->finish(Action::State::done);
191 get_action_heap().remove(action);
196 void NetworkCm02Model::update_actions_state_full(double now, double delta)
198 for (auto it = std::begin(*get_running_action_set()); it != std::end(*get_running_action_set());) {
199 NetworkCm02Action& action = static_cast<NetworkCm02Action&>(*it);
200 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
201 XBT_DEBUG("Something happened to action %p", &action);
202 double deltap = delta;
203 if (action.latency_ > 0) {
204 if (action.latency_ > deltap) {
205 double_update(&action.latency_, deltap, sg_surf_precision);
208 double_update(&deltap, action.latency_, sg_surf_precision);
209 action.latency_ = 0.0;
211 if (action.latency_ <= 0.0 && not action.is_suspended())
212 get_maxmin_system()->update_variable_weight(action.get_variable(), action.weight_);
214 if (TRACE_is_enabled()) {
215 int n = action.get_variable()->get_number_of_constraint();
216 for (int i = 0; i < n; i++) {
217 kernel::lmm::Constraint* constraint = action.get_variable()->get_constraint(i);
218 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(constraint->get_id());
219 TRACE_surf_link_set_utilization(
220 link->get_cname(), action.get_category(),
221 (action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i)),
222 action.get_last_update(), now - action.get_last_update());
225 if (not action.get_variable()->get_number_of_constraint()) {
226 /* There is actually no link used, hence an infinite bandwidth. This happens often when using models like
227 * vivaldi. In such case, just make sure that the action completes immediately.
229 action.update_remains(action.get_remains());
231 action.update_remains(action.get_variable()->get_value() * delta);
233 if (action.get_max_duration() > NO_MAX_DURATION)
234 action.update_max_duration(delta);
236 if (((action.get_remains() <= 0) && (action.get_variable()->get_weight() > 0)) ||
237 ((action.get_max_duration() > NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
238 action.finish(Action::State::done);
243 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 bool 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);
261 failed = std::any_of(back_route.begin(), back_route.end(), [](LinkImpl* const& link) { return link->is_off(); });
264 NetworkCm02Action *action = new NetworkCm02Action(this, size, failed);
265 action->weight_ = latency;
266 action->latency_ = latency;
267 action->rate_ = rate;
268 if (get_update_algorithm() == Model::UpdateAlgo::Lazy) {
269 action->set_last_update();
272 if (sg_weight_S_parameter > 0) {
274 std::accumulate(route.begin(), route.end(), action->weight_, [](double total, LinkImpl* const& link) {
275 return total + sg_weight_S_parameter / link->bandwidth();
279 double bandwidth_bound = route.empty() ? -1.0 : bandwidthFactor(size) * route.front()->bandwidth();
281 for (auto const& link : route)
282 bandwidth_bound = std::min(bandwidth_bound, bandwidthFactor(size) * link->bandwidth());
284 action->lat_current_ = action->latency_;
285 action->latency_ *= latencyFactor(size);
286 action->rate_ = bandwidthConstraint(action->rate_, bandwidth_bound, size);
288 int constraints_per_variable = route.size();
289 constraints_per_variable += back_route.size();
291 if (action->latency_ > 0) {
292 action->set_variable(get_maxmin_system()->variable_new(action, 0.0, -1.0, constraints_per_variable));
293 if (get_update_algorithm() == Model::UpdateAlgo::Lazy) {
294 // add to the heap the event when the latency is payed
295 double date = action->latency_ + action->get_last_update();
297 ActionHeap::Type type = route.empty() ? ActionHeap::Type::normal : ActionHeap::Type::latency;
299 XBT_DEBUG("Added action (%p) one latency event at date %f", action, date);
300 get_action_heap().insert(action, date, type);
303 action->set_variable(get_maxmin_system()->variable_new(action, 1.0, -1.0, constraints_per_variable));
305 if (action->rate_ < 0) {
306 get_maxmin_system()->update_variable_bound(
307 action->get_variable(), (action->lat_current_ > 0) ? cfg_tcp_gamma / (2.0 * action->lat_current_) : -1.0);
309 get_maxmin_system()->update_variable_bound(
310 action->get_variable(), (action->lat_current_ > 0)
311 ? std::min(action->rate_, cfg_tcp_gamma / (2.0 * action->lat_current_))
315 for (auto const& link : route)
316 get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), 1.0);
318 if (cfg_crosstraffic) {
319 XBT_DEBUG("Crosstraffic active: adding backward flow using 5%% of the available bandwidth");
320 for (auto const& link : back_route)
321 get_maxmin_system()->expand(link->get_constraint(), action->get_variable(), .05);
323 // Change concurrency_share here, if you want that cross-traffic is included in the SURF concurrency
324 // (You would also have to change simgrid::kernel::lmm::Element::get_concurrency())
325 // action->getVariable()->set_concurrency_share(2)
329 simgrid::s4u::Link::onCommunicate(action, src, dst);
336 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model* model, const std::string& name, double bandwidth, double latency,
337 s4u::Link::SharingPolicy policy, kernel::lmm::System* system)
338 : LinkImpl(model, name, system->constraint_new(this, sg_bandwidth_factor * bandwidth))
340 bandwidth_.scale = 1.0;
341 bandwidth_.peak = bandwidth;
343 latency_.scale = 1.0;
344 latency_.peak = latency;
346 if (policy == s4u::Link::SharingPolicy::FATPIPE)
347 get_constraint()->unshare();
349 simgrid::s4u::Link::onCreation(this->piface_);
352 void NetworkCm02Link::apply_event(tmgr_trace_event_t triggered, double value)
354 /* Find out which of my iterators was triggered, and react accordingly */
355 if (triggered == bandwidth_.event) {
357 tmgr_trace_event_unref(&bandwidth_.event);
359 } else if (triggered == latency_.event) {
361 tmgr_trace_event_unref(&latency_.event);
363 } else if (triggered == stateEvent_) {
367 kernel::lmm::Variable* var = nullptr;
368 const kernel::lmm::Element* elem = nullptr;
369 double now = surf_get_clock();
372 while ((var = get_constraint()->get_variable(&elem))) {
373 Action* action = static_cast<Action*>(var->get_id());
375 if (action->get_state() == Action::State::running || action->get_state() == Action::State::ready) {
376 action->set_finish_time(now);
377 action->set_state(Action::State::failed);
381 tmgr_trace_event_unref(&stateEvent_);
383 xbt_die("Unknown event!\n");
386 XBT_DEBUG("There was a resource state event, need to update actions related to the constraint (%p)",
390 void NetworkCm02Link::setBandwidth(double value)
392 bandwidth_.peak = value;
394 get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
395 sg_bandwidth_factor * (bandwidth_.peak * bandwidth_.scale));
397 LinkImpl::on_bandwidth_change();
399 if (sg_weight_S_parameter > 0) {
400 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter / (bandwidth_.peak * bandwidth_.scale);
402 kernel::lmm::Variable* var;
403 const kernel::lmm::Element* elem = nullptr;
404 const kernel::lmm::Element* nextelem = nullptr;
406 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
407 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
408 action->weight_ += delta;
409 if (not action->is_suspended())
410 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
415 void NetworkCm02Link::setLatency(double value)
417 double delta = value - latency_.peak;
418 kernel::lmm::Variable* var = nullptr;
419 const kernel::lmm::Element* elem = nullptr;
420 const kernel::lmm::Element* nextelem = nullptr;
423 latency_.peak = value;
425 while ((var = get_constraint()->get_variable_safe(&elem, &nextelem, &numelem))) {
426 NetworkCm02Action* action = static_cast<NetworkCm02Action*>(var->get_id());
427 action->lat_current_ += delta;
428 action->weight_ += delta;
429 if (action->rate_ < 0)
430 get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(), NetworkModel::cfg_tcp_gamma /
431 (2.0 * action->lat_current_));
433 get_model()->get_maxmin_system()->update_variable_bound(
434 action->get_variable(), std::min(action->rate_, NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)));
436 if (action->rate_ < NetworkModel::cfg_tcp_gamma / (2.0 * action->lat_current_)) {
437 XBT_INFO("Flow is limited BYBANDWIDTH");
439 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f", action->lat_current_);
442 if (not action->is_suspended())
443 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), action->weight_);
451 void NetworkCm02Action::update_remains_lazy(double now)
453 if (suspended_ != Action::SuspendStates::not_suspended)
456 double delta = now - get_last_update();
457 double max_duration = get_max_duration();
459 if (get_remains_no_update() > 0) {
460 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
462 update_remains(get_last_value() * delta);
464 XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
467 if (max_duration > NO_MAX_DURATION) {
468 double_update(&max_duration, delta, sg_surf_precision);
469 set_max_duration(max_duration);
472 if ((get_remains_no_update() <= 0 && (get_variable()->get_weight() > 0)) ||
473 ((max_duration > NO_MAX_DURATION) && (max_duration <= 0))) {
474 finish(Action::State::done);
475 get_model()->get_action_heap().remove(this);
479 set_last_value(get_variable()->get_value());
484 } // namespace simgrid