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([]() {
33 surf_network_model->addTraces();
41 /************************************************************************/
42 /* New model based on optimizations discussed during Pedro Velho's thesis*/
43 /************************************************************************/
44 /* @techreport{VELHO:2011:HAL-00646896:1, */
45 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
46 /* title = {{Flow-level network models: have we reached the limits?}}, */
47 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
48 /* type = {Rapport de recherche}, */
49 /* institution = {INRIA}, */
50 /* number = {RR-7821}, */
53 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
55 void surf_network_model_init_LegrandVelho(void)
57 if (surf_network_model)
60 surf_network_model = new simgrid::surf::NetworkCm02Model();
61 net_define_callbacks();
62 simgrid::surf::Model *model = surf_network_model;
63 xbt_dynar_push(all_existing_models, &model);
65 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",
67 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
69 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 20537);
72 /***************************************************************************/
73 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
74 /***************************************************************************/
75 /* @TechReport{ rr-lip2002-40, */
76 /* author = {Henri Casanova and Loris Marchal}, */
77 /* institution = {LIP}, */
78 /* title = {A Network Model for Simulation of Grid Application}, */
79 /* number = {2002-40}, */
83 void surf_network_model_init_CM02(void)
86 if (surf_network_model)
89 surf_network_model = new simgrid::surf::NetworkCm02Model();
90 xbt_dynar_push(all_existing_models, &surf_network_model);
91 net_define_callbacks();
93 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 1.0);
94 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 1.0);
95 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 0.0);
98 /***************************************************************************/
99 /* The models from Steven H. Low */
100 /***************************************************************************/
101 /* @article{Low03, */
102 /* author={Steven H. Low}, */
103 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
105 /* journal={{IEEE/ACM} Transactions on Networking}, */
106 /* volume={11}, number={4}, */
108 void surf_network_model_init_Reno(void)
110 if (surf_network_model)
113 surf_network_model = new simgrid::surf::NetworkCm02Model();
114 xbt_dynar_push(all_existing_models, &surf_network_model);
115 net_define_callbacks();
116 lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
117 surf_network_model->f_networkSolve = lagrange_solve;
119 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
120 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
121 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
125 void surf_network_model_init_Reno2(void)
127 if (surf_network_model)
130 surf_network_model = new simgrid::surf::NetworkCm02Model();
131 xbt_dynar_push(all_existing_models, &surf_network_model);
132 net_define_callbacks();
133 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp, func_reno2_fpi);
134 surf_network_model->f_networkSolve = lagrange_solve;
136 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
137 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
138 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
141 void surf_network_model_init_Vegas(void)
143 if (surf_network_model)
146 surf_network_model = new simgrid::surf::NetworkCm02Model();
147 xbt_dynar_push(all_existing_models, &surf_network_model);
148 net_define_callbacks();
149 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, func_vegas_fpi);
150 surf_network_model->f_networkSolve = lagrange_solve;
152 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
153 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor", 0.92);
154 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
160 NetworkCm02Model::NetworkCm02Model()
163 char *optim = xbt_cfg_get_string(_sg_cfg_set, "network/optim");
165 xbt_cfg_get_boolean(_sg_cfg_set, "network/maxmin_selective_update");
167 if (!strcmp(optim, "Full")) {
168 p_updateMechanism = UM_FULL;
169 m_selectiveUpdate = select;
170 } else if (!strcmp(optim, "Lazy")) {
171 p_updateMechanism = UM_LAZY;
172 m_selectiveUpdate = 1;
173 xbt_assert((select == 1)
175 (xbt_cfg_is_default_value
176 (_sg_cfg_set, "network/maxmin_selective_update")),
177 "Disabling selective update while using the lazy update mechanism is dumb!");
179 xbt_die("Unsupported optimization (%s) for this model", optim);
183 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
185 routing_model_create(createLink("__loopback__",
186 498000000, NULL, 0.000015, NULL,
187 1 /*SURF_RESOURCE_ON*/, NULL,
188 SURF_LINK_FATPIPE, NULL));
190 if (p_updateMechanism == UM_LAZY) {
191 p_actionHeap = xbt_heap_new(8, NULL);
192 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
193 p_modifiedSet = new ActionLmmList();
194 p_maxminSystem->keep_track = p_modifiedSet;
198 Link* NetworkCm02Model::createLink(const char *name,
200 tmgr_trace_t bw_trace,
202 tmgr_trace_t lat_trace,
204 tmgr_trace_t state_trace,
205 e_surf_link_sharing_policy_t policy,
206 xbt_dict_t properties)
208 xbt_assert(NULL == Link::byName(name),
209 "Link '%s' declared several times in the platform",
212 Link* link = new NetworkCm02Link(this, name, properties, p_maxminSystem, sg_bandwidth_factor * bw_initial, history,
213 initiallyOn, state_trace, bw_initial, bw_trace, lat_initial, lat_trace, policy);
214 Link::onCreation(link);
218 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
220 NetworkCm02Action *action;
221 while ((xbt_heap_size(p_actionHeap) > 0)
222 && (double_equals(xbt_heap_maxkey(p_actionHeap), now, sg_surf_precision))) {
223 action = static_cast<NetworkCm02Action*> (xbt_heap_pop(p_actionHeap));
224 XBT_DEBUG("Something happened to action %p", action);
225 if (TRACE_is_enabled()) {
226 int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
228 for (i = 0; i < n; i++){
229 lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
230 action->getVariable(),
232 NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
233 TRACE_surf_link_set_utilization(link->getName(),
234 action->getCategory(),
235 (lmm_variable_getvalue(action->getVariable())*
236 lmm_get_cnst_weight_from_var(p_maxminSystem,
237 action->getVariable(),
239 action->getLastUpdate(),
240 now - action->getLastUpdate());
244 // if I am wearing a latency hat
245 if (action->getHat() == LATENCY) {
246 XBT_DEBUG("Latency paid for action %p. Activating", action);
247 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), action->m_weight);
248 action->heapRemove(p_actionHeap);
249 action->refreshLastUpdate();
251 // if I am wearing a max_duration or normal hat
252 } else if (action->getHat() == MAX_DURATION ||
253 action->getHat() == NORMAL) {
254 // no need to communicate anymore
255 // assume that flows that reached max_duration have remaining of 0
256 XBT_DEBUG("Action %p finished", action);
257 action->setRemains(0);
259 action->setState(SURF_ACTION_DONE);
260 action->heapRemove(p_actionHeap);
269 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
271 NetworkCm02Action *action;
272 ActionList *running_actions = getRunningActionSet();
274 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
275 ; it != itend ; it=itNext) {
278 action = static_cast<NetworkCm02Action*> (&*it);
279 XBT_DEBUG("Something happened to action %p", action);
280 double deltap = delta;
281 if (action->m_latency > 0) {
282 if (action->m_latency > deltap) {
283 double_update(&(action->m_latency), deltap, sg_surf_precision);
286 double_update(&(deltap), action->m_latency, sg_surf_precision);
287 action->m_latency = 0.0;
289 if (action->m_latency == 0.0 && !(action->isSuspended()))
290 lmm_update_variable_weight(p_maxminSystem, action->getVariable(),
293 if (TRACE_is_enabled()) {
294 int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
296 for (i = 0; i < n; i++){
297 lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
298 action->getVariable(),
300 NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
301 TRACE_surf_link_set_utilization(link->getName(),
302 action->getCategory(),
303 (lmm_variable_getvalue(action->getVariable())*
304 lmm_get_cnst_weight_from_var(p_maxminSystem,
305 action->getVariable(),
307 action->getLastUpdate(),
308 now - action->getLastUpdate());
311 if (!lmm_get_number_of_cnst_from_var
312 (p_maxminSystem, action->getVariable())) {
313 /* There is actually no link used, hence an infinite bandwidth.
314 * This happens often when using models like vivaldi.
315 * In such case, just make sure that the action completes immediately.
317 action->updateRemains(action->getRemains());
319 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
321 if (action->getMaxDuration() != NO_MAX_DURATION)
322 action->updateMaxDuration(delta);
324 if ((action->getRemains() <= 0) &&
325 (lmm_get_variable_weight(action->getVariable()) > 0)) {
327 action->setState(SURF_ACTION_DONE);
329 } else if (((action->getMaxDuration() != NO_MAX_DURATION)
330 && (action->getMaxDuration() <= 0))) {
332 action->setState(SURF_ACTION_DONE);
339 Action *NetworkCm02Model::communicate(NetCard *src, NetCard *dst,
340 double size, double rate)
344 NetworkCm02Link *link;
346 NetworkCm02Action *action = NULL;
347 double bandwidth_bound;
348 double latency = 0.0;
349 xbt_dynar_t back_route = NULL;
350 int constraints_per_variable = 0;
352 xbt_dynar_t route = xbt_dynar_new(sizeof(NetCard*), NULL);
354 XBT_IN("(%s,%s,%g,%g)", src->getName(), dst->getName(), size, rate);
356 routing_platf->getRouteAndLatency(src, dst, &route, &latency);
357 xbt_assert(!xbt_dynar_is_empty(route) || latency,
358 "You're trying to send data from %s to %s but there is no connecting path between these two hosts.",
359 src->getName(), dst->getName());
361 xbt_dynar_foreach(route, i, _link) {
362 link = static_cast<NetworkCm02Link*>(_link);
368 if (sg_network_crosstraffic == 1) {
369 routing_platf->getRouteAndLatency(dst, src, &back_route, NULL);
370 xbt_dynar_foreach(back_route, i, _link) {
371 link = static_cast<NetworkCm02Link*>(_link);
379 action = new NetworkCm02Action(this, size, failed);
381 #ifdef HAVE_LATENCY_BOUND_TRACKING
382 action->m_latencyLimited = 0;
384 action->m_weight = action->m_latency = latency;
386 action->m_rate = rate;
387 if (p_updateMechanism == UM_LAZY) {
388 action->m_indexHeap = -1;
389 action->m_lastUpdate = surf_get_clock();
392 bandwidth_bound = -1.0;
393 if (sg_weight_S_parameter > 0) {
394 xbt_dynar_foreach(route, i, _link) {
395 link = static_cast<NetworkCm02Link*>(_link);
396 action->m_weight += sg_weight_S_parameter / link->getBandwidth();
399 xbt_dynar_foreach(route, i, _link) {
400 link = static_cast<NetworkCm02Link*>(_link);
401 double bb = bandwidthFactor(size) * link->getBandwidth();
403 (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
406 action->m_latCurrent = action->m_latency;
407 action->m_latency *= latencyFactor(size);
408 action->m_rate = bandwidthConstraint(action->m_rate, bandwidth_bound, size);
410 xbt_assert(!xbt_dynar_is_empty(route),
411 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
413 link = *static_cast<NetworkCm02Link **>(xbt_dynar_get_ptr(route, 0));
414 gapAppend(size, link, action);
415 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
416 action, src->getName(), dst->getName(), action->m_senderGap,
420 constraints_per_variable = xbt_dynar_length(route);
421 if (back_route != NULL)
422 constraints_per_variable += xbt_dynar_length(back_route);
424 if (action->m_latency > 0) {
425 action->p_variable = lmm_variable_new(p_maxminSystem, action, 0.0, -1.0,
426 constraints_per_variable);
427 if (p_updateMechanism == UM_LAZY) {
428 // add to the heap the event when the latency is payed
429 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
430 action->m_latency + action->m_lastUpdate);
431 action->heapInsert(p_actionHeap, action->m_latency + action->m_lastUpdate, xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
434 action->p_variable = lmm_variable_new(p_maxminSystem, action, 1.0, -1.0, constraints_per_variable);
436 if (action->m_rate < 0) {
437 lmm_update_variable_bound(p_maxminSystem, action->getVariable(), (action->m_latCurrent > 0) ? sg_tcp_gamma / (2.0 * action->m_latCurrent) : -1.0);
439 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);
442 xbt_dynar_foreach(route, i, _link) {
443 link = static_cast<NetworkCm02Link*>(_link);
444 lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), 1.0);
447 if (sg_network_crosstraffic == 1) {
448 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
449 xbt_dynar_foreach(back_route, i, _link) {
450 link = static_cast<NetworkCm02Link*>(_link);
451 lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), .05);
455 xbt_dynar_free(&route);
458 networkCommunicateCallbacks(action, src, dst, size, rate);
462 void NetworkCm02Model::addTraces(){
463 xbt_dict_cursor_t cursor = NULL;
464 char *trace_name, *elm;
466 static int called = 0;
471 /* connect all traces relative to network */
472 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
473 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
474 NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
476 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
479 "Cannot connect trace %s to link %s: trace undefined",
482 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
485 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
486 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
487 NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
489 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
492 "Cannot connect trace %s to link %s: trace undefined",
495 link->p_speed.event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
498 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
499 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
500 NetworkCm02Link *link = static_cast<NetworkCm02Link*>(Link::byName(elm));;
502 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
505 "Cannot connect trace %s to link %s: trace undefined",
508 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
515 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
517 double constraint_value,
518 tmgr_history_t history,
520 tmgr_trace_t state_trace,
522 tmgr_trace_t metric_trace,
524 tmgr_trace_t lat_trace,
525 e_surf_link_sharing_policy_t policy)
526 : Link(model, name, props, lmm_constraint_new(system, this, constraint_value), history, state_trace)
534 p_speed.peak = metric_peak;
536 p_speed.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, this);
538 p_speed.event = NULL;
540 m_latCurrent = lat_initial;
542 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
544 if (policy == SURF_LINK_FATPIPE)
545 lmm_constraint_shared(getConstraint());
550 void NetworkCm02Link::updateState(tmgr_trace_event_t event_type,
551 double value, double date)
553 /* printf("[" "%g" "] Asking to update network card \"%s\" with value " */
554 /* "%g" " for event %p\n", surf_get_clock(), nw_link->name, */
555 /* value, event_type); */
557 if (event_type == p_speed.event) {
558 updateBandwidth(value, date);
559 if (tmgr_trace_event_free(event_type))
560 p_speed.event = NULL;
561 } else if (event_type == p_latEvent) {
562 updateLatency(value, date);
563 if (tmgr_trace_event_free(event_type))
565 } else if (event_type == p_stateEvent) {
569 lmm_constraint_t cnst = getConstraint();
570 lmm_variable_t var = NULL;
571 lmm_element_t elem = NULL;
574 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
575 Action *action = static_cast<Action*>( lmm_variable_id(var) );
577 if (action->getState() == SURF_ACTION_RUNNING ||
578 action->getState() == SURF_ACTION_READY) {
579 action->setFinishTime(date);
580 action->setState(SURF_ACTION_FAILED);
584 if (tmgr_trace_event_free(event_type))
587 XBT_CRITICAL("Unknown event ! \n");
592 ("There were a resource state event, need to update actions related to the constraint (%p)",
597 void NetworkCm02Link::updateBandwidth(double value, double date){
598 double delta = sg_weight_S_parameter / value - sg_weight_S_parameter /
599 (p_speed.peak * p_speed.scale);
600 lmm_variable_t var = NULL;
601 lmm_element_t elem = NULL;
602 lmm_element_t nextelem = NULL;
605 NetworkCm02Action *action = NULL;
607 p_speed.peak = value;
608 lmm_update_constraint_bound(getModel()->getMaxminSystem(),
610 sg_bandwidth_factor *
611 (p_speed.peak * p_speed.scale));
612 TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_speed.peak * p_speed.scale);
613 if (sg_weight_S_parameter > 0) {
614 while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
615 action = (NetworkCm02Action*) lmm_variable_id(var);
616 action->m_weight += delta;
617 if (!action->isSuspended())
618 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
623 void NetworkCm02Link::updateLatency(double value, double date){
624 double delta = value - m_latCurrent;
625 lmm_variable_t var = NULL;
626 lmm_element_t elem = NULL;
627 lmm_element_t nextelem = NULL;
629 NetworkCm02Action *action = NULL;
631 m_latCurrent = value;
632 while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
633 action = (NetworkCm02Action*) lmm_variable_id(var);
634 action->m_latCurrent += delta;
635 action->m_weight += delta;
636 if (action->m_rate < 0)
637 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->m_latCurrent));
639 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
640 std::min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)));
642 if (action->m_rate < sg_tcp_gamma / (2.0 * action->m_latCurrent)) {
643 XBT_INFO("Flow is limited BYBANDWIDTH");
645 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
646 action->m_latCurrent);
649 if (!action->isSuspended())
650 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
657 void NetworkCm02Action::updateRemainingLazy(double now)
661 if (m_suspended != 0)
664 delta = now - m_lastUpdate;
667 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
668 double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
670 XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
673 if (m_maxDuration != NO_MAX_DURATION)
674 double_update(&m_maxDuration, delta, sg_surf_precision);
676 if (m_remains <= 0 &&
677 (lmm_get_variable_weight(getVariable()) > 0)) {
679 setState(SURF_ACTION_DONE);
681 heapRemove(getModel()->getActionHeap());
682 } else if (((m_maxDuration != NO_MAX_DURATION)
683 && (m_maxDuration <= 0))) {
685 setState(SURF_ACTION_DONE);
686 heapRemove(getModel()->getActionHeap());
690 m_lastValue = lmm_variable_getvalue(getVariable());