Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
casting an int to void* is not ok
[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, future_evt_set,
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     lmm_variable_concurrency_share_set(action->getVariable(),2);
454   }
455
456   xbt_dynar_free(&route);
457   XBT_OUT();
458
459   networkCommunicateCallbacks(action, src, dst, size, rate);
460   return action;
461 }
462
463 void NetworkCm02Model::addTraces(){
464   xbt_dict_cursor_t cursor = NULL;
465   char *trace_name, *elm;
466
467   static int called = 0;
468   if (called)
469     return;
470   called = 1;
471
472   /* connect all traces relative to network */
473   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
474     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
475     NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
476
477     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
478                trace_name, elm);
479     xbt_assert(trace,
480                "Cannot connect trace %s to link %s: trace undefined",
481                trace_name, elm);
482
483     link->p_stateEvent = future_evt_set->add_trace(trace, 0.0, link);
484   }
485
486   xbt_dict_foreach(trace_connect_list_link_bw, cursor, trace_name, elm) {
487     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
488     NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
489
490     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
491                trace_name, elm);
492     xbt_assert(trace,
493                "Cannot connect trace %s to link %s: trace undefined",
494                trace_name, elm);
495
496     link->p_speed.event = future_evt_set->add_trace(trace, 0.0, link);
497   }
498
499   xbt_dict_foreach(trace_connect_list_link_lat, cursor, trace_name, elm) {
500     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
501     NetworkCm02Link *link = static_cast<NetworkCm02Link*>(Link::byName(elm));;
502
503     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
504                trace_name, elm);
505     xbt_assert(trace,
506                "Cannot connect trace %s to link %s: trace undefined",
507                trace_name, elm);
508
509     link->p_latEvent = future_evt_set->add_trace(trace, 0.0, link);
510   }
511 }
512
513 /************
514  * Resource *
515  ************/
516 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
517                              lmm_system_t system,
518                              double constraint_value,
519                              sg_future_evt_set_t fes,
520                              int initiallyOn,
521                              tmgr_trace_t state_trace,
522                              double metric_peak,
523                              tmgr_trace_t metric_trace,
524                              double lat_initial,
525                              tmgr_trace_t lat_trace,
526                              e_surf_link_sharing_policy_t policy)
527 : Link(model, name, props, lmm_constraint_new(system, this, constraint_value), fes, state_trace)
528 {
529   if (initiallyOn)
530     turnOn();
531   else
532     turnOff();
533
534   p_speed.scale = 1.0;
535   p_speed.peak = metric_peak;
536   if (metric_trace)
537     p_speed.event = fes->add_trace(metric_trace, 0.0, this);
538   else
539     p_speed.event = NULL;
540
541   m_latCurrent = lat_initial;
542   if (lat_trace)
543   p_latEvent = fes->add_trace(lat_trace, 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_iterator_t triggered,
552                                       double value, double date)
553 {
554
555   /* Find out which of my iterators was triggered, and react accordingly */
556   if (triggered == p_speed.event) {
557     updateBandwidth(value, date);
558     tmgr_trace_event_unref(&p_speed.event);
559   } else if (triggered == p_latEvent) {
560     updateLatency(value, date);
561     tmgr_trace_event_unref(&p_latEvent);
562   } else if (triggered == p_stateEvent) {
563     if (value > 0)
564       turnOn();
565     else {
566       lmm_variable_t var = NULL;
567       lmm_element_t elem = NULL;
568
569       turnOff();
570       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
571         Action *action = static_cast<Action*>( lmm_variable_id(var) );
572
573         if (action->getState() == SURF_ACTION_RUNNING ||
574             action->getState() == SURF_ACTION_READY) {
575           action->setFinishTime(date);
576           action->setState(SURF_ACTION_FAILED);
577         }
578       }
579     }
580     tmgr_trace_event_unref(&p_stateEvent);
581   } else {
582     xbt_die("Unknown event!\n");
583   }
584
585   XBT_DEBUG
586       ("There was a resource state event, need to update actions related to the constraint (%p)",
587        getConstraint());
588 }
589
590 void NetworkCm02Link::updateBandwidth(double value, double date){
591   double delta = sg_weight_S_parameter / value - sg_weight_S_parameter /
592                  (p_speed.peak * p_speed.scale);
593   lmm_variable_t var = NULL;
594   lmm_element_t elem = NULL;
595   lmm_element_t nextelem = NULL;
596   int numelem = 0;
597
598   NetworkCm02Action *action = NULL;
599
600   p_speed.peak = value;
601   lmm_update_constraint_bound(getModel()->getMaxminSystem(),
602                               getConstraint(),
603                               sg_bandwidth_factor *
604                               (p_speed.peak * p_speed.scale));
605   TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_speed.peak * p_speed.scale);
606   if (sg_weight_S_parameter > 0) {
607     while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
608       action = (NetworkCm02Action*) lmm_variable_id(var);
609       action->m_weight += delta;
610       if (!action->isSuspended())
611         lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
612     }
613   }
614 }
615
616 void NetworkCm02Link::updateLatency(double value, double date){
617   double delta = value - m_latCurrent;
618   lmm_variable_t var = NULL;
619   lmm_element_t elem = NULL;
620   lmm_element_t nextelem = NULL;
621   int numelem = 0;
622   NetworkCm02Action *action = NULL;
623
624   m_latCurrent = value;
625   while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
626     action = (NetworkCm02Action*) lmm_variable_id(var);
627     action->m_latCurrent += delta;
628     action->m_weight += delta;
629     if (action->m_rate < 0)
630       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->m_latCurrent));
631     else {
632       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
633                                 std::min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)));
634
635       if (action->m_rate < sg_tcp_gamma / (2.0 * action->m_latCurrent)) {
636         XBT_INFO("Flow is limited BYBANDWIDTH");
637       } else {
638         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
639                  action->m_latCurrent);
640       }
641     }
642     if (!action->isSuspended())
643       lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
644   }
645 }
646
647 /**********
648  * Action *
649  **********/
650 void NetworkCm02Action::updateRemainingLazy(double now)
651 {
652   double delta = 0.0;
653
654   if (m_suspended != 0)
655     return;
656
657   delta = now - m_lastUpdate;
658
659   if (m_remains > 0) {
660     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
661     double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
662
663     XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
664   }
665
666   if (m_maxDuration != NO_MAX_DURATION)
667     double_update(&m_maxDuration, delta, sg_surf_precision);
668
669   if (m_remains <= 0 &&
670       (lmm_get_variable_weight(getVariable()) > 0)) {
671     finish();
672     setState(SURF_ACTION_DONE);
673
674     heapRemove(getModel()->getActionHeap());
675   } else if (((m_maxDuration != NO_MAX_DURATION)
676       && (m_maxDuration <= 0))) {
677     finish();
678     setState(SURF_ACTION_DONE);
679     heapRemove(getModel()->getActionHeap());
680   }
681
682   m_lastUpdate = now;
683   m_lastValue = lmm_variable_getvalue(getVariable());
684 }
685
686 }
687 }