Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill dead code in the C interface to surf
[simgrid.git] / src / surf / network_cm02.cpp
1 /* Copyright (c) 2013-2015. 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 <algorithm>
8
9 #include "network_cm02.hpp"
10 #include "maxmin_private.hpp"
11 #include "simgrid/sg_config.h"
12 #include "src/surf/platform.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
15
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 */
20
21 double sg_tcp_gamma = 0.0;
22 int sg_network_crosstraffic = 0;
23
24 /*************
25  * CallBacks *
26  *************/
27
28 void net_define_callbacks(void)
29 {
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();
34   });
35 }
36
37 /*********
38  * Model *
39  *********/
40
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}, */
51 /*      year = {2011}, */
52 /*      month = Nov, */
53 /*      pdf = {http://hal.inria.fr/hal-00646896/PDF/rr-validity.pdf}, */
54 /*  } */
55 void surf_network_model_init_LegrandVelho(void)
56 {
57   if (surf_network_model)
58     return;
59
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);
64
65   xbt_cfg_setdefault_double(_sg_cfg_set, "network/latency_factor",
66                             13.01);
67   xbt_cfg_setdefault_double(_sg_cfg_set, "network/bandwidth_factor",
68                             0.97);
69   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 20537);
70 }
71
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}, */
80 /*   month         = {oct}, */
81 /*   year          = {2002} */
82 /* } */
83 void surf_network_model_init_CM02(void)
84 {
85
86   if (surf_network_model)
87     return;
88
89   surf_network_model = new simgrid::surf::NetworkCm02Model();
90   xbt_dynar_push(all_existing_models, &surf_network_model);
91   net_define_callbacks();
92
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);
96 }
97
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},     */
104 /*   year={2003},                                                          */
105 /*   journal={{IEEE/ACM} Transactions on Networking},                      */
106 /*    volume={11}, number={4},                                             */
107 /*  }                                                                      */
108 void surf_network_model_init_Reno(void)
109 {
110   if (surf_network_model)
111     return;
112
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;
118
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);
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 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;
135
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);
139 }
140
141 void surf_network_model_init_Vegas(void)
142 {
143   if (surf_network_model)
144     return;
145
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;
151
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);
155 }
156
157 namespace simgrid {
158 namespace surf {
159
160 NetworkCm02Model::NetworkCm02Model()
161         :NetworkModel()
162 {
163   char *optim = xbt_cfg_get_string(_sg_cfg_set, "network/optim");
164   int select =
165       xbt_cfg_get_boolean(_sg_cfg_set, "network/maxmin_selective_update");
166
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)
174                ||
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!");
178   } else {
179     xbt_die("Unsupported optimization (%s) for this model", optim);
180   }
181
182   if (!p_maxminSystem)
183         p_maxminSystem = lmm_system_new(m_selectiveUpdate);
184
185   routing_model_create(createLink("__loopback__",
186                                       498000000, NULL, 0.000015, NULL,
187                                       1 /*SURF_RESOURCE_ON*/, NULL,
188                                       SURF_LINK_FATPIPE, NULL));
189
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;
195   }
196 }
197
198 Link* NetworkCm02Model::createLink(const char *name,
199                                  double bw_initial,
200                                  tmgr_trace_t bw_trace,
201                                  double lat_initial,
202                                  tmgr_trace_t lat_trace,
203                                  int initiallyOn,
204                                  tmgr_trace_t state_trace,
205                                  e_surf_link_sharing_policy_t policy,
206                                  xbt_dict_t properties)
207 {
208   xbt_assert(NULL == Link::byName(name),
209              "Link '%s' declared several times in the platform",
210              name);
211
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);
215   return link;
216 }
217
218 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
219 {
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());
227       int i;
228       for (i = 0; i < n; i++){
229         lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
230                                                             action->getVariable(),
231                                                             i);
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(),
238                                                 i)),
239                                         action->getLastUpdate(),
240                                         now - action->getLastUpdate());
241       }
242     }
243
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();
250
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);
258       action->finish();
259       action->setState(SURF_ACTION_DONE);
260       action->heapRemove(p_actionHeap);
261
262       action->gapRemove();
263     }
264   }
265   return;
266 }
267
268
269 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
270 {
271   NetworkCm02Action *action;
272   ActionList *running_actions = getRunningActionSet();
273
274   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
275      ; it != itend ; it=itNext) {
276         ++itNext;
277
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);
284           deltap = 0.0;
285         } else {
286           double_update(&(deltap), action->m_latency, sg_surf_precision);
287           action->m_latency = 0.0;
288         }
289         if (action->m_latency == 0.0 && !(action->isSuspended()))
290           lmm_update_variable_weight(p_maxminSystem, action->getVariable(),
291               action->m_weight);
292       }
293       if (TRACE_is_enabled()) {
294         int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
295         int i;
296         for (i = 0; i < n; i++){
297           lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
298                                                             action->getVariable(),
299                                                             i);
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(),
306                                                 i)),
307                                         action->getLastUpdate(),
308                                         now - action->getLastUpdate());
309         }
310       }
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.
316          */
317         action->updateRemains(action->getRemains());
318       }
319     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
320                   
321     if (action->getMaxDuration() != NO_MAX_DURATION)
322       action->updateMaxDuration(delta);
323       
324     if ((action->getRemains() <= 0) &&
325         (lmm_get_variable_weight(action->getVariable()) > 0)) {
326       action->finish();
327       action->setState(SURF_ACTION_DONE);
328       action->gapRemove();
329     } else if (((action->getMaxDuration() != NO_MAX_DURATION)
330         && (action->getMaxDuration() <= 0))) {
331       action->finish();
332       action->setState(SURF_ACTION_DONE);
333       action->gapRemove();
334     }
335   }
336   return;
337 }
338
339 Action *NetworkCm02Model::communicate(NetCard *src, NetCard *dst,
340                                                 double size, double rate)
341 {
342   unsigned int i;
343   void *_link;
344   NetworkCm02Link *link;
345   int failed = 0;
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;
351
352   xbt_dynar_t route = xbt_dynar_new(sizeof(NetCard*), NULL);
353
354   XBT_IN("(%s,%s,%g,%g)", src->getName(), dst->getName(), size, rate);
355
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());
360
361   xbt_dynar_foreach(route, i, _link) {
362         link = static_cast<NetworkCm02Link*>(_link);
363     if (link->isOff()) {
364       failed = 1;
365       break;
366     }
367   }
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);
372       if (link->isOff()) {
373         failed = 1;
374         break;
375       }
376     }
377   }
378
379   action = new NetworkCm02Action(this, size, failed);
380
381 #ifdef HAVE_LATENCY_BOUND_TRACKING
382   action->m_latencyLimited = 0;
383 #endif
384   action->m_weight = action->m_latency = latency;
385
386   action->m_rate = rate;
387   if (p_updateMechanism == UM_LAZY) {
388     action->m_indexHeap = -1;
389     action->m_lastUpdate = surf_get_clock();
390   }
391
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();
397     }
398   }
399   xbt_dynar_foreach(route, i, _link) {
400         link = static_cast<NetworkCm02Link*>(_link);
401     double bb = bandwidthFactor(size) * link->getBandwidth();
402     bandwidth_bound =
403         (bandwidth_bound < 0.0) ? bb : std::min(bandwidth_bound, bb);
404   }
405
406   action->m_latCurrent = action->m_latency;
407   action->m_latency *= latencyFactor(size);
408   action->m_rate = bandwidthConstraint(action->m_rate, bandwidth_bound, size);
409   if (m_haveGap) {
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)!!!");
412
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,
417               action->m_latency);
418   }
419
420   constraints_per_variable = xbt_dynar_length(route);
421   if (back_route != NULL)
422     constraints_per_variable += xbt_dynar_length(back_route);
423
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);
432     }
433   } else
434     action->p_variable = lmm_variable_new(p_maxminSystem, action, 1.0, -1.0, constraints_per_variable);
435
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);
438   } else {
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);
440   }
441
442   xbt_dynar_foreach(route, i, _link) {
443         link = static_cast<NetworkCm02Link*>(_link);
444     lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), 1.0);
445   }
446
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);
452     }
453   }
454
455   xbt_dynar_free(&route);
456   XBT_OUT();
457
458   networkCommunicateCallbacks(action, src, dst, size, rate);
459   return action;
460 }
461
462 void NetworkCm02Model::addTraces(){
463   xbt_dict_cursor_t cursor = NULL;
464   char *trace_name, *elm;
465
466   static int called = 0;
467   if (called)
468     return;
469   called = 1;
470
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) );
475
476     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
477                trace_name, elm);
478     xbt_assert(trace,
479                "Cannot connect trace %s to link %s: trace undefined",
480                trace_name, elm);
481
482     link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
483   }
484
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) );
488
489     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
490                trace_name, elm);
491     xbt_assert(trace,
492                "Cannot connect trace %s to link %s: trace undefined",
493                trace_name, elm);
494
495     link->p_speed.event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
496   }
497
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));;
501
502     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
503                trace_name, elm);
504     xbt_assert(trace,
505                "Cannot connect trace %s to link %s: trace undefined",
506                trace_name, elm);
507
508     link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
509   }
510 }
511
512 /************
513  * Resource *
514  ************/
515 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
516                                    lmm_system_t system,
517                                    double constraint_value,
518                                    tmgr_fes_t history,
519                                    int initiallyOn,
520                                    tmgr_trace_t state_trace,
521                                    double metric_peak,
522                                    tmgr_trace_t metric_trace,
523                                    double lat_initial,
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)
527 {
528   if (initiallyOn)
529     turnOn();
530   else
531     turnOff();
532
533   p_speed.scale = 1.0;
534   p_speed.peak = metric_peak;
535   if (metric_trace)
536     p_speed.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, this);
537   else
538     p_speed.event = NULL;
539
540   m_latCurrent = lat_initial;
541   if (lat_trace)
542         p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
543
544   if (policy == SURF_LINK_FATPIPE)
545         lmm_constraint_shared(getConstraint());
546 }
547
548
549
550 void NetworkCm02Link::updateState(tmgr_trace_iterator_t triggered,
551                                       double value, double date)
552 {
553
554   /* Find out which of my iterators was triggered, and react accordingly */
555   if (triggered == p_speed.event) {
556     updateBandwidth(value, date);
557     if (tmgr_trace_event_free(triggered))
558       p_speed.event = NULL;
559   } else if (triggered == p_latEvent) {
560     updateLatency(value, date);
561     if (tmgr_trace_event_free(triggered))
562       p_latEvent = NULL;
563   } else if (triggered == p_stateEvent) {
564     if (value > 0)
565       turnOn();
566     else {
567       lmm_constraint_t cnst = getConstraint();
568       lmm_variable_t var = NULL;
569       lmm_element_t elem = NULL;
570
571       turnOff();
572       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
573         Action *action = static_cast<Action*>( lmm_variable_id(var) );
574
575         if (action->getState() == SURF_ACTION_RUNNING ||
576             action->getState() == SURF_ACTION_READY) {
577           action->setFinishTime(date);
578           action->setState(SURF_ACTION_FAILED);
579         }
580       }
581     }
582     if (tmgr_trace_event_free(triggered))
583       p_stateEvent = NULL;
584   } else {
585     XBT_CRITICAL("Unknown event ! \n");
586     xbt_abort();
587   }
588
589   XBT_DEBUG
590       ("There were a resource state event, need to update actions related to the constraint (%p)",
591        getConstraint());
592   return;
593 }
594
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;
601   int numelem = 0;
602
603   NetworkCm02Action *action = NULL;
604
605   p_speed.peak = value;
606   lmm_update_constraint_bound(getModel()->getMaxminSystem(),
607                               getConstraint(),
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);
617     }
618   }
619 }
620
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;
626   int numelem = 0;
627   NetworkCm02Action *action = NULL;
628
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));
636     else {
637       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
638                                 std::min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)));
639
640       if (action->m_rate < sg_tcp_gamma / (2.0 * action->m_latCurrent)) {
641         XBT_INFO("Flow is limited BYBANDWIDTH");
642       } else {
643         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
644                  action->m_latCurrent);
645       }
646     }
647     if (!action->isSuspended())
648       lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
649   }
650 }
651
652 /**********
653  * Action *
654  **********/
655 void NetworkCm02Action::updateRemainingLazy(double now)
656 {
657   double delta = 0.0;
658
659   if (m_suspended != 0)
660     return;
661
662   delta = now - m_lastUpdate;
663
664   if (m_remains > 0) {
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);
667
668     XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
669   }
670
671   if (m_maxDuration != NO_MAX_DURATION)
672     double_update(&m_maxDuration, delta, sg_surf_precision);
673
674   if (m_remains <= 0 &&
675       (lmm_get_variable_weight(getVariable()) > 0)) {
676     finish();
677     setState(SURF_ACTION_DONE);
678
679     heapRemove(getModel()->getActionHeap());
680   } else if (((m_maxDuration != NO_MAX_DURATION)
681       && (m_maxDuration <= 0))) {
682     finish();
683     setState(SURF_ACTION_DONE);
684     heapRemove(getModel()->getActionHeap());
685   }
686
687   m_lastUpdate = now;
688   m_lastValue = lmm_variable_getvalue(getVariable());
689 }
690
691 }
692 }