1 #include "network_cm02.hpp"
2 #include "maxmin_private.hpp"
3 #include "simgrid/sg_config.h"
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
7 double sg_sender_gap = 0.0;
8 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
9 double sg_bandwidth_factor = 1.0; /* default value; can be set by model or from command line */
10 double sg_weight_S_parameter = 0.0; /* default value; can be set by model or from command line */
12 double sg_tcp_gamma = 0.0;
13 int sg_network_crosstraffic = 0;
19 static void net_parse_link_init(sg_platf_link_cbarg_t link){
20 if (link->policy == SURF_LINK_FULLDUPLEX) {
22 link_id = bprintf("%s_UP", link->id);
23 surf_network_model->createResource(link_id,
25 link->bandwidth_trace,
29 link->state_trace, link->policy, link->properties);
31 link_id = bprintf("%s_DOWN", link->id);
32 surf_network_model->createResource(link_id,
34 link->bandwidth_trace,
38 link->state_trace, link->policy, link->properties);
41 surf_network_model->createResource(link->id,
43 link->bandwidth_trace,
47 link->state_trace, link->policy, link->properties);
51 static void net_add_traces(void){
52 xbt_dict_cursor_t cursor = NULL;
53 char *trace_name, *elm;
55 static int called = 0;
60 /* connect all traces relative to network */
61 xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
62 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
63 NetworkCm02LinkPtr link = static_cast<NetworkCm02LinkPtr>(
64 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
66 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
69 "Cannot connect trace %s to link %s: trace undefined",
72 link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
75 xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
76 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
77 NetworkCm02LinkPtr link = static_cast<NetworkCm02LinkPtr>(
78 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
80 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
83 "Cannot connect trace %s to link %s: trace undefined",
86 link->p_power.event = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
89 xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
90 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
91 NetworkCm02LinkPtr link = static_cast<NetworkCm02LinkPtr>(
92 xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
94 xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
97 "Cannot connect trace %s to link %s: trace undefined",
100 link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
104 void net_define_callbacks(void)
106 /* Figuring out the network links */
107 sg_platf_link_add_cb(net_parse_link_init);
108 sg_platf_postparse_add_cb(net_add_traces);
115 /************************************************************************/
116 /* New model based on optimizations discussed during Pedro Velho's thesis*/
117 /************************************************************************/
118 /* @techreport{VELHO:2011:HAL-00646896:1, */
119 /* url = {http://hal.inria.fr/hal-00646896/en/}, */
120 /* title = {{Flow-level network models: have we reached the limits?}}, */
121 /* author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
122 /* type = {Rapport de recherche}, */
123 /* institution = {INRIA}, */
124 /* number = {RR-7821}, */
127 /* pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
129 void surf_network_model_init_LegrandVelho(void)
131 if (surf_network_model)
134 surf_network_model = new NetworkCm02Model();
135 net_define_callbacks();
136 ModelPtr model = static_cast<ModelPtr>(surf_network_model);
137 xbt_dynar_push(model_list, &model);
139 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",
141 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
143 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 20537);
146 /***************************************************************************/
147 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
148 /***************************************************************************/
149 /* @TechReport{ rr-lip2002-40, */
150 /* author = {Henri Casanova and Loris Marchal}, */
151 /* institution = {LIP}, */
152 /* title = {A Network Model for Simulation of Grid Application}, */
153 /* number = {2002-40}, */
157 void surf_network_model_init_CM02(void)
160 if (surf_network_model)
163 surf_network_model = new NetworkCm02Model();
164 net_define_callbacks();
165 ModelPtr model = static_cast<ModelPtr>(surf_network_model);
166 xbt_dynar_push(model_list, &model);
168 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 1.0);
169 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
171 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 0.0);
174 /***************************************************************************/
175 /* The models from Steven H. Low */
176 /***************************************************************************/
177 /* @article{Low03, */
178 /* author={Steven H. Low}, */
179 /* title={A Duality Model of {TCP} and Queue Management Algorithms}, */
181 /* journal={{IEEE/ACM} Transactions on Networking}, */
182 /* volume={11}, number={4}, */
184 void surf_network_model_init_Reno(void)
186 if (surf_network_model)
189 surf_network_model = new NetworkCm02Model();
190 net_define_callbacks();
191 ModelPtr model = static_cast<ModelPtr>(surf_network_model);
192 xbt_dynar_push(model_list, &model);
193 lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
195 surf_network_model->f_networkSolve = lagrange_solve;
197 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
198 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
200 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
204 void surf_network_model_init_Reno2(void)
206 if (surf_network_model)
209 surf_network_model = new NetworkCm02Model();
210 net_define_callbacks();
211 ModelPtr model = static_cast<ModelPtr>(surf_network_model);
212 xbt_dynar_push(model_list, &model);
213 lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
215 surf_network_model->f_networkSolve = lagrange_solve;
217 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
218 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
220 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S_parameter",
224 void surf_network_model_init_Vegas(void)
226 if (surf_network_model)
229 surf_network_model = new NetworkCm02Model();
230 net_define_callbacks();
231 ModelPtr model = static_cast<ModelPtr>(surf_network_model);
232 xbt_dynar_push(model_list, &model);
233 lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
235 surf_network_model->f_networkSolve = lagrange_solve;
237 xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
238 xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
240 xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
243 void NetworkCm02Model::initialize()
245 char *optim = xbt_cfg_get_string(_sg_cfg_set, "network/optim");
247 xbt_cfg_get_boolean(_sg_cfg_set, "network/maxmin_selective_update");
249 if (!strcmp(optim, "Full")) {
250 p_updateMechanism = UM_FULL;
251 m_selectiveUpdate = select;
252 } else if (!strcmp(optim, "Lazy")) {
253 p_updateMechanism = UM_LAZY;
254 m_selectiveUpdate = 1;
255 xbt_assert((select == 1)
257 (xbt_cfg_is_default_value
258 (_sg_cfg_set, "network/maxmin_selective_update")),
259 "Disabling selective update while using the lazy update mechanism is dumb!");
261 xbt_die("Unsupported optimization (%s) for this model", optim);
265 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
267 const char* lb_name = "__loopback__";
268 routing_model_create(static_cast<ResourcePtr>(createResource(lb_name,
269 498000000, NULL, 0.000015, NULL,
270 SURF_RESOURCE_ON, NULL,
271 SURF_LINK_FATPIPE, NULL)));
273 if (p_updateMechanism == UM_LAZY) {
274 p_actionHeap = xbt_heap_new(8, NULL);
275 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
276 p_modifiedSet = new ActionLmmList();
277 p_maxminSystem->keep_track = p_modifiedSet;
283 NetworkLinkPtr NetworkCm02Model::createResource(const char *name,
285 tmgr_trace_t bw_trace,
287 tmgr_trace_t lat_trace,
288 e_surf_resource_state_t state_initial,
289 tmgr_trace_t state_trace,
290 e_surf_link_sharing_policy_t policy,
291 xbt_dict_t properties)
293 xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
294 "Link '%s' declared several times in the platform file.",
297 NetworkCm02LinkPtr nw_link =
298 new NetworkCm02Link(this, name, properties, p_maxminSystem, sg_bandwidth_factor * bw_initial, history,
299 state_initial, state_trace, bw_initial, bw_trace, lat_initial, lat_trace, policy);
302 xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, static_cast<ResourcePtr>(nw_link));
303 XBT_DEBUG("Create link '%s'",name);
308 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
310 NetworkCm02ActionPtr action;
311 while ((xbt_heap_size(p_actionHeap) > 0)
312 && (double_equals(xbt_heap_maxkey(p_actionHeap), now))) {
313 action = (NetworkCm02ActionPtr) xbt_heap_pop(p_actionHeap);
314 XBT_DEBUG("Something happened to action %p", action);
316 if (TRACE_is_enabled()) {
317 int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
319 for (i = 0; i < n; i++){
320 lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
321 action->getVariable(),
323 NetworkCm02LinkPtr link = static_cast<NetworkCm02LinkPtr>(lmm_constraint_id(constraint));
324 TRACE_surf_link_set_utilization(link->getName(),
325 action->getCategory(),
326 (lmm_variable_getvalue(action->getVariable())*
327 lmm_get_cnst_weight_from_var(p_maxminSystem,
328 action->getVariable(),
330 action->getLastUpdate(),
331 now - action->getLastUpdate());
336 // if I am wearing a latency hat
337 if (action->getHat() == LATENCY) {
338 XBT_DEBUG("Latency paid for action %p. Activating", action);
339 lmm_update_variable_weight(p_maxminSystem, action->getVariable(), action->m_weight);
340 action->heapRemove(p_actionHeap);
341 action->refreshLastUpdate();
343 // if I am wearing a max_duration or normal hat
344 } else if (action->getHat() == MAX_DURATION ||
345 action->getHat() == NORMAL) {
346 // no need to communicate anymore
347 // assume that flows that reached max_duration have remaining of 0
348 XBT_DEBUG("Action %p finished", action);
349 action->setRemains(0);
351 action->setState(SURF_ACTION_DONE);
352 action->heapRemove(p_actionHeap);
360 ActionPtr NetworkCm02Model::communicate(RoutingEdgePtr src, RoutingEdgePtr dst,
361 double size, double rate)
365 NetworkCm02LinkPtr link;
367 NetworkCm02ActionPtr action = NULL;
368 double bandwidth_bound;
369 double latency = 0.0;
370 xbt_dynar_t back_route = NULL;
371 int constraints_per_variable = 0;
373 xbt_dynar_t route = xbt_dynar_new(sizeof(RoutingEdgePtr), NULL);
375 XBT_IN("(%s,%s,%g,%g)", src->p_name, dst->p_name, size, rate);
377 routing_platf->getRouteAndLatency(src, dst, &route, &latency);
378 xbt_assert(!xbt_dynar_is_empty(route) || latency,
379 "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
380 src->p_name, dst->p_name);
382 xbt_dynar_foreach(route, i, _link) {
383 link = static_cast<NetworkCm02LinkPtr>(_link);
384 if (link->getState() == SURF_RESOURCE_OFF) {
389 if (sg_network_crosstraffic == 1) {
390 routing_platf->getRouteAndLatency(dst, src, &back_route, NULL);
391 xbt_dynar_foreach(back_route, i, _link) {
392 link = static_cast<NetworkCm02LinkPtr>(_link);
393 if (link->getState() == SURF_RESOURCE_OFF) {
400 action = new NetworkCm02Action(this, size, failed);
402 #ifdef HAVE_LATENCY_BOUND_TRACKING
403 action->m_latencyLimited = 0;
405 action->m_weight = action->m_latency = latency;
407 action->m_rate = rate;
408 if (p_updateMechanism == UM_LAZY) {
409 action->m_indexHeap = -1;
410 action->m_lastUpdate = surf_get_clock();
413 bandwidth_bound = -1.0;
414 if (sg_weight_S_parameter > 0) {
415 xbt_dynar_foreach(route, i, _link) {
416 link = static_cast<NetworkCm02LinkPtr>(_link);
417 action->m_weight += sg_weight_S_parameter / link->getBandwidth();
420 xbt_dynar_foreach(route, i, _link) {
421 link = static_cast<NetworkCm02LinkPtr>(_link);
422 double bb = bandwidthFactor(size) * link->getBandwidth(); //(link->p_power.peak * link->p_power.scale);
424 (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
427 action->m_latCurrent = action->m_latency;
428 action->m_latency *= latencyFactor(size);
429 action->m_rate = bandwidthConstraint(action->m_rate, bandwidth_bound, size);
431 xbt_assert(!xbt_dynar_is_empty(route),
432 "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
434 link = *static_cast<NetworkCm02LinkPtr *>(xbt_dynar_get_ptr(route, 0));
435 gapAppend(size, link, action);
436 XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
437 action, src->p_name, dst->p_name, action->m_senderGap,
441 constraints_per_variable = xbt_dynar_length(route);
442 if (back_route != NULL)
443 constraints_per_variable += xbt_dynar_length(back_route);
445 if (action->m_latency > 0) {
446 action->p_variable = lmm_variable_new(p_maxminSystem, static_cast<ActionPtr>(action), 0.0, -1.0,
447 constraints_per_variable);
448 if (p_updateMechanism == UM_LAZY) {
449 // add to the heap the event when the latency is payed
450 XBT_DEBUG("Added action (%p) one latency event at date %f", action,
451 action->m_latency + action->m_lastUpdate);
452 action->heapInsert(p_actionHeap, action->m_latency + action->m_lastUpdate, xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
455 action->p_variable = lmm_variable_new(p_maxminSystem, static_cast<ActionPtr>(action), 1.0, -1.0, constraints_per_variable);
457 if (action->m_rate < 0) {
458 lmm_update_variable_bound(p_maxminSystem, action->getVariable(), (action->m_latCurrent > 0) ? sg_tcp_gamma / (2.0 * action->m_latCurrent) : -1.0);
460 lmm_update_variable_bound(p_maxminSystem, action->getVariable(), (action->m_latCurrent > 0) ? min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)) : action->m_rate);
463 xbt_dynar_foreach(route, i, _link) {
464 link = static_cast<NetworkCm02LinkPtr>(_link);
465 lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), 1.0);
468 if (sg_network_crosstraffic == 1) {
469 XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
470 xbt_dynar_foreach(back_route, i, _link) {
471 link = static_cast<NetworkCm02LinkPtr>(_link);
472 lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), .05);
476 xbt_dynar_free(&route);
487 NetworkCm02Link::NetworkCm02Link(NetworkCm02ModelPtr model, const char *name, xbt_dict_t props,
489 double constraint_value,
490 tmgr_history_t history,
491 e_surf_resource_state_t state_init,
492 tmgr_trace_t state_trace,
494 tmgr_trace_t metric_trace,
496 tmgr_trace_t lat_trace,
497 e_surf_link_sharing_policy_t policy)
498 : NetworkLink(model, name, props, lmm_constraint_new(system, this, constraint_value), history, state_trace)
500 m_stateCurrent = state_init;
503 p_power.peak = metric_peak;
505 p_power.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, static_cast<ResourcePtr>(this));
507 p_power.event = NULL;
509 m_latCurrent = lat_initial;
511 p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, static_cast<ResourcePtr>(this));
513 if (policy == SURF_LINK_FATPIPE)
514 lmm_constraint_shared(getConstraint());
519 void NetworkCm02Link::updateState(tmgr_trace_event_t event_type,
520 double value, double date)
522 /* printf("[" "%g" "] Asking to update network card \"%s\" with value " */
523 /* "%g" " for event %p\n", surf_get_clock(), nw_link->name, */
524 /* value, event_type); */
526 if (event_type == p_power.event) {
528 sg_weight_S_parameter / value - sg_weight_S_parameter /
529 (p_power.peak * p_power.scale);
530 lmm_variable_t var = NULL;
531 lmm_element_t elem = NULL;
532 NetworkCm02ActionPtr action = NULL;
534 p_power.peak = value;
535 lmm_update_constraint_bound(getModel()->getMaxminSystem(),
537 sg_bandwidth_factor *
538 (p_power.peak * p_power.scale));
540 TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_power.peak * p_power.scale);
542 if (sg_weight_S_parameter > 0) {
543 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
544 action = (NetworkCm02ActionPtr) lmm_variable_id(var);
545 action->m_weight += delta;
546 if (!action->isSuspended())
547 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
550 if (tmgr_trace_event_free(event_type))
551 p_power.event = NULL;
552 } else if (event_type == p_latEvent) {
553 double delta = value - m_latCurrent;
554 lmm_variable_t var = NULL;
555 lmm_element_t elem = NULL;
556 NetworkCm02ActionPtr action = NULL;
558 m_latCurrent = value;
559 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
560 action = (NetworkCm02ActionPtr) lmm_variable_id(var);
561 action->m_latCurrent += delta;
562 action->m_weight += delta;
563 if (action->m_rate < 0)
564 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->m_latCurrent));
566 lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
567 min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)));
569 if (action->m_rate < sg_tcp_gamma / (2.0 * action->m_latCurrent)) {
570 XBT_INFO("Flow is limited BYBANDWIDTH");
572 XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
573 action->m_latCurrent);
576 if (!action->isSuspended())
577 lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
580 if (tmgr_trace_event_free(event_type))
582 } else if (event_type == p_stateEvent) {
584 m_stateCurrent = SURF_RESOURCE_ON;
586 lmm_constraint_t cnst = getConstraint();
587 lmm_variable_t var = NULL;
588 lmm_element_t elem = NULL;
590 m_stateCurrent = SURF_RESOURCE_OFF;
591 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
592 ActionPtr action = (ActionPtr) lmm_variable_id(var);
594 if (action->getState() == SURF_ACTION_RUNNING ||
595 action->getState() == SURF_ACTION_READY) {
596 action->setFinishTime(date);
597 action->setState(SURF_ACTION_FAILED);
601 if (tmgr_trace_event_free(event_type))
604 XBT_CRITICAL("Unknown event ! \n");
609 ("There were a resource state event, need to update actions related to the constraint (%p)",
617 void NetworkCm02Action::updateRemainingLazy(double now)
621 if (m_suspended != 0)
624 delta = now - m_lastUpdate;
627 XBT_DEBUG("Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
628 double_update(&(m_remains), m_lastValue * delta);
630 XBT_DEBUG("Updating action(%p): remains is now %lf", this, m_remains);
633 if (m_maxDuration != NO_MAX_DURATION)
634 double_update(&m_maxDuration, delta);
636 if (m_remains <= 0 &&
637 (lmm_get_variable_weight(getVariable()) > 0)) {
639 setState(SURF_ACTION_DONE);
641 heapRemove(getModel()->getActionHeap());
642 } else if (((m_maxDuration != NO_MAX_DURATION)
643 && (m_maxDuration <= 0))) {
645 setState(SURF_ACTION_DONE);
646 heapRemove(getModel()->getActionHeap());
650 m_lastValue = lmm_variable_getvalue(getVariable());
652 void NetworkCm02Action::recycle()