1 /* Copyright (c) 2013-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "network_cm02.hpp"
10 #include "maxmin_private.hpp"
11 #include "simgrid/sg_config.h"
12 #include "src/surf/platform.hpp"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
16 double sg_sender_gap = 0.0;
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 double sg_tcp_gamma = 0.0;
22 int sg_network_crosstraffic = 0;
28 void net_define_callbacks(void)
30 /* Figuring out the network links */
31 simgrid::surf::on_link.connect(netlink_parse_init);
32 simgrid::surf::on_postparse.connect(net_add_traces);
39 /************************************************************************/
40 /* New model based on optimizations discussed during Pedro Velho's thesis*/
41 /************************************************************************/
42 /* @techreport{VELHO:2011:HAL-00646896:1, */
43 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
44 /* title = {{Flow-level network models: have we reached the limits?}}, */
45 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
46 /* type = {Rapport de recherche}, */
47 /* institution = {INRIA}, */
48 /* number = {RR-7821}, */
51 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
53 void surf_network_model_init_LegrandVelho(void)
55 if (surf_network_model)
58 surf_network_model = new simgrid::surf::NetworkCm02Model();
59 net_define_callbacks();
60 simgrid::surf::Model *model = surf_network_model;
61 xbt_dynar_push(all_existing_models, &model);
63 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",
65 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
67 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 20537);
70 /***************************************************************************/
71 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
72 /***************************************************************************/
73 /* @TechReport{ rr-lip2002-40, */
74 /* author = {Henri Casanova and Loris Marchal}, */
75 /* institution = {LIP}, */
76 /* title = {A Network Model for Simulation of Grid Application}, */
77 /* number = {2002-40}, */
81 void surf_network_model_init_CM02(void)
84 if (surf_network_model)
87 surf_network_model = new simgrid::surf::NetworkCm02Model();
88 xbt_dynar_push(all_existing_models, &surf_network_model);
89 net_define_callbacks();
91 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 1.0);
92 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 1.0);
93 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 0.0);
96 /***************************************************************************/
97 /* The models from Steven H. Low */
98 /***************************************************************************/
100 /* author={Steven H. Low}, */
101 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
103 /* journal={{IEEE/ACM} Transactions on Networking}, */
104 /* volume={11}, number={4}, */
106 void surf_network_model_init_Reno(void)
108 if (surf_network_model)
111 surf_network_model = new simgrid::surf::NetworkCm02Model();
112 xbt_dynar_push(all_existing_models, &surf_network_model);
113 net_define_callbacks();
114 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
115 surf_network_model->f_networkSolve = lagrange_solve;
117 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
118 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
119 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
123 void surf_network_model_init_Reno2(void)
125 if (surf_network_model)
128 surf_network_model = new simgrid::surf::NetworkCm02Model();
129 xbt_dynar_push(all_existing_models, &surf_network_model);
130 net_define_callbacks();
131 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
132 surf_network_model->f_networkSolve = lagrange_solve;
134 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
135 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
136 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
139 void surf_network_model_init_Vegas(void)
141 if (surf_network_model)
144 surf_network_model = new simgrid::surf::NetworkCm02Model();
145 xbt_dynar_push(all_existing_models, &surf_network_model);
146 net_define_callbacks();
147 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
148 surf_network_model->f_networkSolve = lagrange_solve;
150 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
151 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
152 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
158 NetworkCm02Model::NetworkCm02Model()
161 char *optim = xbt_cfg_get_string(_sg_cfg_set, "network/optim");
163 xbt_cfg_get_boolean(_sg_cfg_set, "network/maxmin_selective_update");
165 if (!strcmp(optim, "Full")) {
166 p_updateMechanism = UM_FULL;
167 m_selectiveUpdate = select;
168 } else if (!strcmp(optim, "Lazy")) {
169 p_updateMechanism = UM_LAZY;
170 m_selectiveUpdate = 1;
171 xbt_assert((select == 1)
173 (xbt_cfg_is_default_value
174 (_sg_cfg_set, "network/maxmin_selective_update")),
175 "Disabling selective update while using the lazy update mechanism is dumb!");
177 xbt_die("Unsupported optimization (%s) for this model", optim);
181 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
183 routing_model_create(createLink("__loopback__",
184 498000000, NULL, 0.000015, NULL,
185 1 /*SURF_RESOURCE_ON*/, NULL,
186 SURF_LINK_FATPIPE, NULL));
188 if (p_updateMechanism == UM_LAZY) {
189 p_actionHeap = xbt_heap_new(8, NULL);
190 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
191 p_modifiedSet = new ActionLmmList();
192 p_maxminSystem->keep_track = p_modifiedSet;
196 Link* NetworkCm02Model::createLink(const char *name,
198 tmgr_trace_t bw_trace,
200 tmgr_trace_t lat_trace,
202 tmgr_trace_t state_trace,
203 e_surf_link_sharing_policy_t policy,
204 xbt_dict_t properties)
206 xbt_assert(NULL == Link::byName(name),
207 "Link '%s' declared several times in the platform",
210 Link* link = new NetworkCm02Link(this, name, properties, p_maxminSystem, sg_bandwidth_factor * bw_initial, history,
211 initiallyOn, state_trace, bw_initial, bw_trace, lat_initial, lat_trace, policy);
212 Link::onCreation(link);
216 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
218 NetworkCm02Action *action;
219 while ((xbt_heap_size(p_actionHeap) > 0)
220 && (double_equals(xbt_heap_maxkey(p_actionHeap), now, sg_surf_precision))) {
221 action = static_cast<NetworkCm02Action*> (xbt_heap_pop(p_actionHeap));
222 XBT_DEBUG("Something happened to action %p", action);
223 if (TRACE_is_enabled()) {
224 int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
226 for (i = 0; i < n; i++){
227 lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
228 action->getVariable(),
230 NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
231 TRACE_surf_link_set_utilization(link->getName(),
232 action->getCategory(),
233 (lmm_variable_getvalue(action->getVariable())*
234 lmm_get_cnst_weight_from_var(p_maxminSystem,
235 action->getVariable(),
237 action->getLastUpdate(),
238 now - action->getLastUpdate());
242 // if I am wearing a latency hat
243 if (action->getHat() == LATENCY) {
244 XBT_DEBUG("Latency paid for action %p. Activating", action);
245 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), action->m_weight);
246 action->heapRemove(p_actionHeap);
247 action->refreshLastUpdate();
249 // if I am wearing a max_duration or normal hat
250 } else if (action->getHat() == MAX_DURATION ||
251 action->getHat() == NORMAL) {
252 // no need to communicate anymore
253 // assume that flows that reached max_duration have remaining of 0
254 XBT_DEBUG("Action %p finished", action);
255 action->setRemains(0);
257 action->setState(SURF_ACTION_DONE);
258 action->heapRemove(p_actionHeap);
267 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
269 NetworkCm02Action *action;
270 ActionList *running_actions = getRunningActionSet();
272 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
273 ; it != itend ; it=itNext) {
276 action = static_cast<NetworkCm02Action*> (&*it);
277 XBT_DEBUG("Something happened to action %p", action);
278 double deltap = delta;
279 if (action->m_latency > 0) {
280 if (action->m_latency > deltap) {
281 double_update(&(action->m_latency), deltap, sg_surf_precision);
284 double_update(&(deltap), action->m_latency, sg_surf_precision);
285 action->m_latency = 0.0;
287 if (action->m_latency == 0.0 && !(action->isSuspended()))
288 lmm_update_variable_weight(p_maxminSystem, action->getVariable(),
291 if (TRACE_is_enabled()) {
292 int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
294 for (i = 0; i < n; i++){
295 lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
296 action->getVariable(),
298 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
299 TRACE_surf_link_set_utilization(link->getName(),
300 action->getCategory(),
301 (lmm_variable_getvalue(action->getVariable())*
302 lmm_get_cnst_weight_from_var(p_maxminSystem,
303 action->getVariable(),
305 action->getLastUpdate(),
306 now - action->getLastUpdate());
309 if (!lmm_get_number_of_cnst_from_var
310 (p_maxminSystem, action->getVariable())) {
311 /* There is actually no link used, hence an infinite bandwidth.
312 * This happens often when using models like vivaldi.
313 * In such case, just make sure that the action completes immediately.
315 action->updateRemains(action->getRemains());
317 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
319 if (action->getMaxDuration() != NO_MAX_DURATION)
320 action->updateMaxDuration(delta);
322 if ((action->getRemains() <= 0) &&
323 (lmm_get_variable_weight(action->getVariable()) > 0)) {
325 action->setState(SURF_ACTION_DONE);
327 } else if (((action->getMaxDuration() != NO_MAX_DURATION)
328 && (action->getMaxDuration() <= 0))) {
330 action->setState(SURF_ACTION_DONE);
337 Action *NetworkCm02Model::communicate(NetCard *src, NetCard *dst,
338 double size, double rate)
342 NetworkCm02Link *link;
344 NetworkCm02Action *action = NULL;
345 double bandwidth_bound;
346 double latency = 0.0;
347 xbt_dynar_t back_route = NULL;
348 int constraints_per_variable = 0;
350 xbt_dynar_t route = xbt_dynar_new(sizeof(NetCard*), NULL);
352 XBT_IN("(%s,%s,%g,%g)", src->getName(), dst->getName(), size, rate);
354 routing_platf->getRouteAndLatency(src, dst, &route, &latency);
355 xbt_assert(!xbt_dynar_is_empty(route) || latency,
356 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
357 src->getName(), dst->getName());
359 xbt_dynar_foreach(route, i, _link) {
360 link = static_cast<NetworkCm02Link*>(_link);
366 if (sg_network_crosstraffic == 1) {
367 routing_platf->getRouteAndLatency(dst, src, &back_route, NULL);
368 xbt_dynar_foreach(back_route, i, _link) {
369 link = static_cast<NetworkCm02Link*>(_link);
377 action = new NetworkCm02Action(this, size, failed);
379 #ifdef HAVE_LATENCY_BOUND_TRACKING
380 action->m_latencyLimited = 0;
382 action->m_weight = action->m_latency = latency;
384 action->m_rate = rate;
385 if (p_updateMechanism == UM_LAZY) {
386 action->m_indexHeap = -1;
387 action->m_lastUpdate = surf_get_clock();
390 bandwidth_bound = -1.0;
391 if (sg_weight_S_parameter > 0) {
392 xbt_dynar_foreach(route, i, _link) {
393 link = static_cast<NetworkCm02Link*>(_link);
394 action->m_weight += sg_weight_S_parameter / link->getBandwidth();
397 xbt_dynar_foreach(route, i, _link) {
398 link = static_cast<NetworkCm02Link*>(_link);
399 double bb = bandwidthFactor(size) * link->getBandwidth();
401 (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
404 action->m_latCurrent = action->m_latency;
405 action->m_latency *= latencyFactor(size);
406 action->m_rate = bandwidthConstraint(action->m_rate, bandwidth_bound, size);
408 xbt_assert(!xbt_dynar_is_empty(route),
409 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
411 link = *static_cast<NetworkCm02Link **>(xbt_dynar_get_ptr(route, 0));
412 gapAppend(size, link, action);
413 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
414 action, src->getName(), dst->getName(), action->m_senderGap,
418 constraints_per_variable = xbt_dynar_length(route);
419 if (back_route != NULL)
420 constraints_per_variable += xbt_dynar_length(back_route);
422 if (action->m_latency > 0) {
423 action->p_variable = lmm_variable_new(p_maxminSystem, action, 0.0, -1.0,
424 constraints_per_variable);
425 if (p_updateMechanism == UM_LAZY) {
426 // add to the heap the event when the latency is payed
427 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
428 action->m_latency + action->m_lastUpdate);
429 action->heapInsert(p_actionHeap, action->m_latency + action->m_lastUpdate, xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
432 action->p_variable = lmm_variable_new(p_maxminSystem, action, 1.0, -1.0, constraints_per_variable);
434 if (action->m_rate < 0) {
435 lmm_update_variable_bound(p_maxminSystem, action->getVariable(), (action->m_latCurrent > 0) ? sg_tcp_gamma / (2.0 * action->m_latCurrent) : -1.0);
437 lmm_update_variable_bound(p_maxminSystem, action->getVariable(), (action->m_latCurrent > 0) ? std::min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)) : action->m_rate);
440 xbt_dynar_foreach(route, i, _link) {
441 link = static_cast<NetworkCm02Link*>(_link);
442 lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), 1.0);
445 if (sg_network_crosstraffic == 1) {
446 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
447 xbt_dynar_foreach(back_route, i, _link) {
448 link = static_cast<NetworkCm02Link*>(_link);
449 lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), .05);
453 xbt_dynar_free(&route);
456 networkCommunicateCallbacks(action, src, dst, size, rate);
460 void NetworkCm02Model::addTraces(){
461 xbt_dict_cursor_t cursor = NULL;
462 char *trace_name, *elm;
464 static int called = 0;
469 /* connect all traces relative to network */
470 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
471 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
472 NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
474 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
477 "Cannot connect trace %s to link %s: trace undefined",
480 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
483 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
484 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
485 NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
487 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
490 "Cannot connect trace %s to link %s: trace undefined",
493 link->p_speed.event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
496 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
497 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
498 NetworkCm02Link *link = static_cast<NetworkCm02Link*>(Link::byName(elm));;
500 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
503 "Cannot connect trace %s to link %s: trace undefined",
506 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
513 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
515 double constraint_value,
516 tmgr_history_t history,
518 tmgr_trace_t state_trace,
520 tmgr_trace_t metric_trace,
522 tmgr_trace_t lat_trace,
523 e_surf_link_sharing_policy_t policy)
524 : Link(model, name, props, lmm_constraint_new(system, this, constraint_value), history, state_trace)
532 p_speed.peak = metric_peak;
534 p_speed.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, this);
536 p_speed.event = NULL;
538 m_latCurrent = lat_initial;
540 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
542 if (policy == SURF_LINK_FATPIPE)
543 lmm_constraint_shared(getConstraint());
548 void NetworkCm02Link::updateState(tmgr_trace_event_t event_type,
549 double value, double date)
551 /* printf("[" "%g" "] Asking to update network card \"%s\" with value " */
552 /* "%g" " for event %p\n", surf_get_clock(), nw_link->name, */
553 /* value, event_type); */
555 if (event_type == p_speed.event) {
556 updateBandwidth(value, date);
557 if (tmgr_trace_event_free(event_type))
558 p_speed.event = NULL;
559 } else if (event_type == p_latEvent) {
560 updateLatency(value, date);
561 if (tmgr_trace_event_free(event_type))
563 } else if (event_type == p_stateEvent) {
567 lmm_constraint_t cnst = getConstraint();
568 lmm_variable_t var = NULL;
569 lmm_element_t elem = NULL;
572 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
573 Action *action = static_cast<Action*>( lmm_variable_id(var) );
575 if (action->getState() == SURF_ACTION_RUNNING ||
576 action->getState() == SURF_ACTION_READY) {
577 action->setFinishTime(date);
578 action->setState(SURF_ACTION_FAILED);
582 if (tmgr_trace_event_free(event_type))
585 XBT_CRITICAL("Unknown event ! \n");
590 ("There were a resource state event, need to update actions related to the constraint (%p)",
595 void NetworkCm02Link::updateBandwidth(double value, double date){
596 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter /
597 (p_speed.peak * p_speed.scale);
598 lmm_variable_t var = NULL;
599 lmm_element_t elem = NULL;
600 lmm_element_t nextelem = NULL;
603 NetworkCm02Action *action = NULL;
605 p_speed.peak = value;
606 lmm_update_constraint_bound(getModel()->getMaxminSystem(),
608 sg_bandwidth_factor *
609 (p_speed.peak * p_speed.scale));
610 TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_speed.peak * p_speed.scale);
611 if (sg_weight_S_parameter > 0) {
612 while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
613 action = (NetworkCm02Action*) lmm_variable_id(var);
614 action->m_weight += delta;
615 if (!action->isSuspended())
616 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
621 void NetworkCm02Link::updateLatency(double value, double date){
622 double delta = value - m_latCurrent;
623 lmm_variable_t var = NULL;
624 lmm_element_t elem = NULL;
625 lmm_element_t nextelem = NULL;
627 NetworkCm02Action *action = NULL;
629 m_latCurrent = value;
630 while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
631 action = (NetworkCm02Action*) lmm_variable_id(var);
632 action->m_latCurrent += delta;
633 action->m_weight += delta;
634 if (action->m_rate < 0)
635 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->m_latCurrent));
637 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
638 std::min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)));
640 if (action->m_rate < sg_tcp_gamma / (2.0 * action->m_latCurrent)) {
641 XBT_INFO("Flow is limited BYBANDWIDTH");
643 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
644 action->m_latCurrent);
647 if (!action->isSuspended())
648 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
655 void NetworkCm02Action::updateRemainingLazy(double now)
659 if (m_suspended != 0)
662 delta = now - m_lastUpdate;
665 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
666 double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
668 XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
671 if (m_maxDuration != NO_MAX_DURATION)
672 double_update(&m_maxDuration, delta, sg_surf_precision);
674 if (m_remains <= 0 &&
675 (lmm_get_variable_weight(getVariable()) > 0)) {
677 setState(SURF_ACTION_DONE);
679 heapRemove(getModel()->getActionHeap());
680 } else if (((m_maxDuration != NO_MAX_DURATION)
681 && (m_maxDuration <= 0))) {
683 setState(SURF_ACTION_DONE);
684 heapRemove(getModel()->getActionHeap());
688 m_lastValue = lmm_variable_getvalue(getVariable());