Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
actually kill models' names
[simgrid.git] / src / surf / network_cm02.cpp
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "network_cm02.hpp"
8 #include "maxmin_private.hpp"
9 #include "simgrid/sg_config.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
12
13 double sg_sender_gap = 0.0;
14 double sg_latency_factor = 1.0; /* default value; can be set by model or from command line */
15 double sg_bandwidth_factor = 1.0;       /* default value; can be set by model or from command line */
16 double sg_weight_S_parameter = 0.0;     /* default value; can be set by model or from command line */
17
18 double sg_tcp_gamma = 0.0;
19 int sg_network_crosstraffic = 0;
20
21 /*************
22  * CallBacks *
23  *************/
24
25 void net_define_callbacks(void)
26 {
27   /* Figuring out the network links */
28   sg_platf_link_add_cb(netlink_parse_init);
29   sg_platf_postparse_add_cb(net_add_traces);
30 }
31
32 /*********
33  * Model *
34  *********/
35
36 /************************************************************************/
37 /* New model based on optimizations discussed during Pedro Velho's thesis*/
38 /************************************************************************/
39 /* @techreport{VELHO:2011:HAL-00646896:1, */
40 /*      url = {http://hal.inria.fr/hal-00646896/en/}, */
41 /*      title = {{Flow-level network models: have we reached the limits?}}, */
42 /*      author = {Velho, Pedro and Schnorr, Lucas and Casanova, Henri and Legrand, Arnaud}, */
43 /*      type = {Rapport de recherche}, */
44 /*      institution = {INRIA}, */
45 /*      number = {RR-7821}, */
46 /*      year = {2011}, */
47 /*      month = Nov, */
48 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
49 /*  } */
50 void surf_network_model_init_LegrandVelho(void)
51 {
52   if (surf_network_model)
53     return;
54
55   surf_network_model = new NetworkCm02Model();
56   net_define_callbacks();
57   Model *model = surf_network_model;
58   xbt_dynar_push(model_list, &model);
59
60   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",
61                             13.01);
62   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
63                             0.97);
64   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 20537);
65 }
66
67 /***************************************************************************/
68 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
69 /***************************************************************************/
70 /* @TechReport{      rr-lip2002-40, */
71 /*   author        = {Henri Casanova and Loris Marchal}, */
72 /*   institution   = {LIP}, */
73 /*   title         = {A Network Model for Simulation of Grid Application}, */
74 /*   number        = {2002-40}, */
75 /*   month         = {oct}, */
76 /*   year          = {2002} */
77 /* } */
78 void surf_network_model_init_CM02(void)
79 {
80
81   if (surf_network_model)
82     return;
83
84   surf_network_model = new NetworkCm02Model();
85   net_define_callbacks();
86   Model *model = surf_network_model;
87   xbt_dynar_push(model_list, &model);
88
89   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 1.0);
90   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
91                             1.0);
92   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 0.0);
93 }
94
95 /***************************************************************************/
96 /* The models from Steven H. Low                                           */
97 /***************************************************************************/
98 /* @article{Low03,                                                         */
99 /*   author={Steven H. Low},                                               */
100 /*   title={A Duality Model of {TCP} and Queue Management Algorithms},     */
101 /*   year={2003},                                                          */
102 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
103 /*    volume={11}, number={4},                                             */
104 /*  }                                                                      */
105 void surf_network_model_init_Reno(void)
106 {
107   if (surf_network_model)
108     return;
109
110   surf_network_model = new NetworkCm02Model();
111   net_define_callbacks();
112   Model *model = surf_network_model;
113   xbt_dynar_push(model_list, &model);
114   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
115                                     func_reno_fpi);
116   surf_network_model->f_networkSolve = lagrange_solve;
117
118   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
119   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
120                             0.92);
121   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
122 }
123
124
125 void surf_network_model_init_Reno2(void)
126 {
127   if (surf_network_model)
128     return;
129
130   surf_network_model = new NetworkCm02Model();
131   net_define_callbacks();
132   Model *model = surf_network_model;
133   xbt_dynar_push(model_list, &model);
134   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
135                                     func_reno2_fpi);
136   surf_network_model->f_networkSolve = lagrange_solve;
137
138   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
139   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
140                             0.92);
141   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S",
142                             8775);
143 }
144
145 void surf_network_model_init_Vegas(void)
146 {
147   if (surf_network_model)
148     return;
149
150   surf_network_model = new NetworkCm02Model();
151   net_define_callbacks();
152   Model *model = surf_network_model;
153   xbt_dynar_push(model_list, &model);
154   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
155                                     func_vegas_fpi);
156   surf_network_model->f_networkSolve = lagrange_solve;
157
158   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor", 10.4);
159   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
160                             0.92);
161   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
162 }
163
164 NetworkCm02Model::NetworkCm02Model()
165         :NetworkModel()
166 {
167   char *optim = xbt_cfg_get_string(_sg_cfg_set, "network/optim");
168   int select =
169       xbt_cfg_get_boolean(_sg_cfg_set, "network/maxmin_selective_update");
170
171   if (!strcmp(optim, "Full")) {
172     p_updateMechanism = UM_FULL;
173     m_selectiveUpdate = select;
174   } else if (!strcmp(optim, "Lazy")) {
175     p_updateMechanism = UM_LAZY;
176     m_selectiveUpdate = 1;
177     xbt_assert((select == 1)
178                ||
179                (xbt_cfg_is_default_value
180                 (_sg_cfg_set, "network/maxmin_selective_update")),
181                "Disabling selective update while using the lazy update mechanism is dumb!");
182   } else {
183     xbt_die("Unsupported optimization (%s) for this model", optim);
184   }
185
186   if (!p_maxminSystem)
187         p_maxminSystem = lmm_system_new(m_selectiveUpdate);
188
189   routing_model_create(createLink("__loopback__",
190                                       498000000, NULL, 0.000015, NULL,
191                                       SURF_RESOURCE_ON, NULL,
192                                       SURF_LINK_FATPIPE, NULL));
193
194   if (p_updateMechanism == UM_LAZY) {
195         p_actionHeap = xbt_heap_new(8, NULL);
196         xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
197         p_modifiedSet = new ActionLmmList();
198         p_maxminSystem->keep_track = p_modifiedSet;
199   }
200
201   m_haveGap = false;
202 }
203
204 Link* NetworkCm02Model::createLink(const char *name,
205                                  double bw_initial,
206                                  tmgr_trace_t bw_trace,
207                                  double lat_initial,
208                                  tmgr_trace_t lat_trace,
209                                  e_surf_resource_state_t state_initial,
210                                  tmgr_trace_t state_trace,
211                                  e_surf_link_sharing_policy_t policy,
212                                  xbt_dict_t properties)
213 {
214   xbt_assert(NULL == Link::byName(name),
215              "Link '%s' declared several times in the platform",
216              name);
217
218   return new NetworkCm02Link(this, name, properties, p_maxminSystem, sg_bandwidth_factor * bw_initial, history,
219                                              state_initial, state_trace, bw_initial, bw_trace, lat_initial, lat_trace, policy);
220 }
221
222 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
223 {
224   NetworkCm02Action *action;
225   while ((xbt_heap_size(p_actionHeap) > 0)
226          && (double_equals(xbt_heap_maxkey(p_actionHeap), now, sg_surf_precision))) {
227     action = static_cast<NetworkCm02Action*> (xbt_heap_pop(p_actionHeap));
228     XBT_DEBUG("Something happened to action %p", action);
229     if (TRACE_is_enabled()) {
230       int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
231       int i;
232       for (i = 0; i < n; i++){
233         lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
234                                                             action->getVariable(),
235                                                             i);
236         NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
237         TRACE_surf_link_set_utilization(link->getName(),
238                                         action->getCategory(),
239                                         (lmm_variable_getvalue(action->getVariable())*
240                                             lmm_get_cnst_weight_from_var(p_maxminSystem,
241                                                 action->getVariable(),
242                                                 i)),
243                                         action->getLastUpdate(),
244                                         now - action->getLastUpdate());
245       }
246     }
247
248     // if I am wearing a latency hat
249     if (action->getHat() == LATENCY) {
250       XBT_DEBUG("Latency paid for action %p. Activating", action);
251       lmm_update_variable_weight(p_maxminSystem, action->getVariable(), action->m_weight);
252       action->heapRemove(p_actionHeap);
253       action->refreshLastUpdate();
254
255         // if I am wearing a max_duration or normal hat
256     } else if (action->getHat() == MAX_DURATION ||
257         action->getHat() == NORMAL) {
258         // no need to communicate anymore
259         // assume that flows that reached max_duration have remaining of 0
260       XBT_DEBUG("Action %p finished", action);
261       action->setRemains(0);
262       action->finish();
263       action->setState(SURF_ACTION_DONE);
264       action->heapRemove(p_actionHeap);
265
266       action->gapRemove();
267     }
268   }
269   return;
270 }
271
272
273 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
274 {
275   NetworkCm02Action *action;
276   ActionList *running_actions = getRunningActionSet();
277
278   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
279      ; it != itend ; it=itNext) {
280         ++itNext;
281
282     action = static_cast<NetworkCm02Action*> (&*it);
283     XBT_DEBUG("Something happened to action %p", action);
284       double deltap = delta;
285       if (action->m_latency > 0) {
286         if (action->m_latency > deltap) {
287           double_update(&(action->m_latency), deltap, sg_surf_precision);
288           deltap = 0.0;
289         } else {
290           double_update(&(deltap), action->m_latency, sg_surf_precision);
291           action->m_latency = 0.0;
292         }
293         if (action->m_latency == 0.0 && !(action->isSuspended()))
294           lmm_update_variable_weight(p_maxminSystem, action->getVariable(),
295               action->m_weight);
296       }
297       if (TRACE_is_enabled()) {
298         int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
299         int i;
300         for (i = 0; i < n; i++){
301           lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
302                                                             action->getVariable(),
303                                                             i);
304           NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
305           TRACE_surf_link_set_utilization(link->getName(),
306                                         action->getCategory(),
307                                         (lmm_variable_getvalue(action->getVariable())*
308                                             lmm_get_cnst_weight_from_var(p_maxminSystem,
309                                                 action->getVariable(),
310                                                 i)),
311                                         action->getLastUpdate(),
312                                         now - action->getLastUpdate());
313         }
314       }
315       if (!lmm_get_number_of_cnst_from_var
316           (p_maxminSystem, action->getVariable())) {
317         /* There is actually no link used, hence an infinite bandwidth.
318          * This happens often when using models like vivaldi.
319          * In such case, just make sure that the action completes immediately.
320          */
321         action->updateRemains(action->getRemains());
322       }
323     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
324                   
325     if (action->getMaxDuration() != NO_MAX_DURATION)
326       action->updateMaxDuration(delta);
327       
328     if ((action->getRemains() <= 0) &&
329         (lmm_get_variable_weight(action->getVariable()) > 0)) {
330       action->finish();
331       action->setState(SURF_ACTION_DONE);
332       action->gapRemove();
333     } else if (((action->getMaxDuration() != NO_MAX_DURATION)
334         && (action->getMaxDuration() <= 0))) {
335       action->finish();
336       action->setState(SURF_ACTION_DONE);
337       action->gapRemove();
338     }
339   }
340   return;
341 }
342
343 Action *NetworkCm02Model::communicate(RoutingEdge *src, RoutingEdge *dst,
344                                                 double size, double rate)
345 {
346   unsigned int i;
347   void *_link;
348   NetworkCm02Link *link;
349   int failed = 0;
350   NetworkCm02Action *action = NULL;
351   double bandwidth_bound;
352   double latency = 0.0;
353   xbt_dynar_t back_route = NULL;
354   int constraints_per_variable = 0;
355
356   xbt_dynar_t route = xbt_dynar_new(sizeof(RoutingEdge*), NULL);
357
358   XBT_IN("(%s,%s,%g,%g)", src->getName(), dst->getName(), size, rate);
359
360   routing_platf->getRouteAndLatency(src, dst, &route, &latency);
361   xbt_assert(!xbt_dynar_is_empty(route) || latency,
362              "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
363              src->getName(), dst->getName());
364
365   xbt_dynar_foreach(route, i, _link) {
366         link = static_cast<NetworkCm02Link*>(_link);
367     if (link->getState() == SURF_RESOURCE_OFF) {
368       failed = 1;
369       break;
370     }
371   }
372   if (sg_network_crosstraffic == 1) {
373           routing_platf->getRouteAndLatency(dst, src, &back_route, NULL);
374     xbt_dynar_foreach(back_route, i, _link) {
375       link = static_cast<NetworkCm02Link*>(_link);
376       if (link->getState() == SURF_RESOURCE_OFF) {
377         failed = 1;
378         break;
379       }
380     }
381   }
382
383   action = new NetworkCm02Action(this, size, failed);
384
385 #ifdef HAVE_LATENCY_BOUND_TRACKING
386   action->m_latencyLimited = 0;
387 #endif
388   action->m_weight = action->m_latency = latency;
389
390   action->m_rate = rate;
391   if (p_updateMechanism == UM_LAZY) {
392     action->m_indexHeap = -1;
393     action->m_lastUpdate = surf_get_clock();
394   }
395
396   bandwidth_bound = -1.0;
397   if (sg_weight_S_parameter > 0) {
398     xbt_dynar_foreach(route, i, _link) {
399       link = static_cast<NetworkCm02Link*>(_link);
400       action->m_weight += sg_weight_S_parameter / link->getBandwidth();
401     }
402   }
403   xbt_dynar_foreach(route, i, _link) {
404         link = static_cast<NetworkCm02Link*>(_link);
405     double bb = bandwidthFactor(size) * link->getBandwidth(); //(link->p_power.peak * link->p_power.scale);
406     bandwidth_bound =
407         (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
408   }
409
410   action->m_latCurrent = action->m_latency;
411   action->m_latency *= latencyFactor(size);
412   action->m_rate = bandwidthConstraint(action->m_rate, bandwidth_bound, size);
413   if (m_haveGap) {
414     xbt_assert(!xbt_dynar_is_empty(route),
415                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
416
417     link = *static_cast<NetworkCm02Link **>(xbt_dynar_get_ptr(route, 0));
418     gapAppend(size, link, action);
419     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
420               action, src->getName(), dst->getName(), action->m_senderGap,
421               action->m_latency);
422   }
423
424   constraints_per_variable = xbt_dynar_length(route);
425   if (back_route != NULL)
426     constraints_per_variable += xbt_dynar_length(back_route);
427
428   if (action->m_latency > 0) {
429     action->p_variable = lmm_variable_new(p_maxminSystem, action, 0.0, -1.0,
430                          constraints_per_variable);
431     if (p_updateMechanism == UM_LAZY) {
432       // add to the heap the event when the latency is payed
433       XBT_DEBUG("Added action (%p) one latency event at date %f", action,
434                 action->m_latency + action->m_lastUpdate);
435       action->heapInsert(p_actionHeap, action->m_latency + action->m_lastUpdate, xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
436     }
437   } else
438     action->p_variable = lmm_variable_new(p_maxminSystem, action, 1.0, -1.0, constraints_per_variable);
439
440   if (action->m_rate < 0) {
441     lmm_update_variable_bound(p_maxminSystem, action->getVariable(), (action->m_latCurrent > 0) ? sg_tcp_gamma / (2.0 * action->m_latCurrent) : -1.0);
442   } else {
443     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);
444   }
445
446   xbt_dynar_foreach(route, i, _link) {
447         link = static_cast<NetworkCm02Link*>(_link);
448     lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), 1.0);
449   }
450
451   if (sg_network_crosstraffic == 1) {
452     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
453     xbt_dynar_foreach(back_route, i, _link) {
454       link = static_cast<NetworkCm02Link*>(_link);
455       lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), .05);
456     }
457   }
458
459   xbt_dynar_free(&route);
460   XBT_OUT();
461
462   surf_callback_emit(networkCommunicateCallbacks, action, src, dst, size, rate);
463   return action;
464 }
465
466 void NetworkCm02Model::addTraces(){
467   xbt_dict_cursor_t cursor = NULL;
468   char *trace_name, *elm;
469
470   static int called = 0;
471   if (called)
472     return;
473   called = 1;
474
475   /* connect all traces relative to network */
476   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
477     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
478     NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
479
480     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
481                trace_name, elm);
482     xbt_assert(trace,
483                "Cannot connect trace %s to link %s: trace undefined",
484                trace_name, elm);
485
486     link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
487   }
488
489   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
490     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
491     NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
492
493     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
494                trace_name, elm);
495     xbt_assert(trace,
496                "Cannot connect trace %s to link %s: trace undefined",
497                trace_name, elm);
498
499     link->p_power.event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
500   }
501
502   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
503     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
504     NetworkCm02Link *link = static_cast<NetworkCm02Link*>(Link::byName(elm));;
505
506     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
507                trace_name, elm);
508     xbt_assert(trace,
509                "Cannot connect trace %s to link %s: trace undefined",
510                trace_name, elm);
511
512     link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
513   }
514 }
515
516 /************
517  * Resource *
518  ************/
519 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
520                                    lmm_system_t system,
521                                    double constraint_value,
522                                    tmgr_history_t history,
523                                    e_surf_resource_state_t state_init,
524                                    tmgr_trace_t state_trace,
525                                    double metric_peak,
526                                    tmgr_trace_t metric_trace,
527                                    double lat_initial,
528                                    tmgr_trace_t lat_trace,
529                                    e_surf_link_sharing_policy_t policy)
530 : Link(model, name, props, lmm_constraint_new(system, this, constraint_value), history, state_trace)
531 {
532   setState(state_init);
533
534   p_power.scale = 1.0;
535   p_power.peak = metric_peak;
536   if (metric_trace)
537     p_power.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, this);
538   else
539     p_power.event = NULL;
540
541   m_latCurrent = lat_initial;
542   if (lat_trace)
543         p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
544
545   if (policy == SURF_LINK_FATPIPE)
546         lmm_constraint_shared(getConstraint());
547 }
548
549
550
551 void NetworkCm02Link::updateState(tmgr_trace_event_t event_type,
552                                       double value, double date)
553 {
554   /*   printf("[" "%g" "] Asking to update network card \"%s\" with value " */
555   /*     "%g" " for event %p\n", surf_get_clock(), nw_link->name, */
556   /*     value, event_type); */
557
558   if (event_type == p_power.event) {
559     updateBandwidth(value, date);
560     if (tmgr_trace_event_free(event_type))
561       p_power.event = NULL;
562   } else if (event_type == p_latEvent) {
563     updateLatency(value, date);
564     if (tmgr_trace_event_free(event_type))
565       p_latEvent = NULL;
566   } else if (event_type == p_stateEvent) {
567     if (value > 0)
568       setState(SURF_RESOURCE_ON);
569     else {
570       lmm_constraint_t cnst = getConstraint();
571       lmm_variable_t var = NULL;
572       lmm_element_t elem = NULL;
573
574       setState(SURF_RESOURCE_OFF);
575       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
576         Action *action = static_cast<Action*>( lmm_variable_id(var) );
577
578         if (action->getState() == SURF_ACTION_RUNNING ||
579             action->getState() == SURF_ACTION_READY) {
580           action->setFinishTime(date);
581           action->setState(SURF_ACTION_FAILED);
582         }
583       }
584     }
585     if (tmgr_trace_event_free(event_type))
586       p_stateEvent = NULL;
587   } else {
588     XBT_CRITICAL("Unknown event ! \n");
589     xbt_abort();
590   }
591
592   XBT_DEBUG
593       ("There were a resource state event, need to update actions related to the constraint (%p)",
594        getConstraint());
595   return;
596 }
597
598 void NetworkCm02Link::updateBandwidth(double value, double date){
599   double delta = sg_weight_S_parameter / value - sg_weight_S_parameter /
600                  (p_power.peak * p_power.scale);
601   lmm_variable_t var = NULL;
602   lmm_element_t elem = NULL;
603   lmm_element_t nextelem = NULL;
604   int numelem = 0;
605
606   NetworkCm02Action *action = NULL;
607
608   p_power.peak = value;
609   lmm_update_constraint_bound(getModel()->getMaxminSystem(),
610                               getConstraint(),
611                               sg_bandwidth_factor *
612                               (p_power.peak * p_power.scale));
613   TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_power.peak * p_power.scale);
614   if (sg_weight_S_parameter > 0) {
615     while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
616       action = (NetworkCm02Action*) lmm_variable_id(var);
617       action->m_weight += delta;
618       if (!action->isSuspended())
619         lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
620     }
621   }
622 }
623
624 void NetworkCm02Link::updateLatency(double value, double date){
625   double delta = value - m_latCurrent;
626   lmm_variable_t var = NULL;
627   lmm_element_t elem = NULL;
628   lmm_element_t nextelem = NULL;
629   int numelem = 0;
630   NetworkCm02Action *action = NULL;
631
632   m_latCurrent = value;
633   while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
634     action = (NetworkCm02Action*) lmm_variable_id(var);
635     action->m_latCurrent += delta;
636     action->m_weight += delta;
637     if (action->m_rate < 0)
638       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->m_latCurrent));
639     else {
640       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
641                                 min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)));
642
643       if (action->m_rate < sg_tcp_gamma / (2.0 * action->m_latCurrent)) {
644         XBT_INFO("Flow is limited BYBANDWIDTH");
645       } else {
646         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
647                  action->m_latCurrent);
648       }
649     }
650     if (!action->isSuspended())
651       lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
652   }
653 }
654
655 /**********
656  * Action *
657  **********/
658 void NetworkCm02Action::updateRemainingLazy(double now)
659 {
660   double delta = 0.0;
661
662   if (m_suspended != 0)
663     return;
664
665   delta = now - m_lastUpdate;
666
667   if (m_remains > 0) {
668     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
669     double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
670
671     XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
672   }
673
674   if (m_maxDuration != NO_MAX_DURATION)
675     double_update(&m_maxDuration, delta, sg_surf_precision);
676
677   if (m_remains <= 0 &&
678       (lmm_get_variable_weight(getVariable()) > 0)) {
679     finish();
680     setState(SURF_ACTION_DONE);
681
682     heapRemove(getModel()->getActionHeap());
683   } else if (((m_maxDuration != NO_MAX_DURATION)
684       && (m_maxDuration <= 0))) {
685     finish();
686     setState(SURF_ACTION_DONE);
687     heapRemove(getModel()->getActionHeap());
688   }
689
690   m_lastUpdate = now;
691   m_lastValue = lmm_variable_getvalue(getVariable());
692 }
693