Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make HostL07 behave more like the regular Host
[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 "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(all_existing_models, &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(all_existing_models, &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(all_existing_models, &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(all_existing_models, &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(all_existing_models, &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   Link* link = 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   surf_callback_emit(networkLinkCreatedCallbacks, link);
221   return link;
222 }
223
224 void NetworkCm02Model::updateActionsStateLazy(double now, double /*delta*/)
225 {
226   NetworkCm02Action *action;
227   while ((xbt_heap_size(p_actionHeap) > 0)
228          && (double_equals(xbt_heap_maxkey(p_actionHeap), now, sg_surf_precision))) {
229     action = static_cast<NetworkCm02Action*> (xbt_heap_pop(p_actionHeap));
230     XBT_DEBUG("Something happened to action %p", action);
231     if (TRACE_is_enabled()) {
232       int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
233       int i;
234       for (i = 0; i < n; i++){
235         lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
236                                                             action->getVariable(),
237                                                             i);
238         NetworkCm02Link *link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
239         TRACE_surf_link_set_utilization(link->getName(),
240                                         action->getCategory(),
241                                         (lmm_variable_getvalue(action->getVariable())*
242                                             lmm_get_cnst_weight_from_var(p_maxminSystem,
243                                                 action->getVariable(),
244                                                 i)),
245                                         action->getLastUpdate(),
246                                         now - action->getLastUpdate());
247       }
248     }
249
250     // if I am wearing a latency hat
251     if (action->getHat() == LATENCY) {
252       XBT_DEBUG("Latency paid for action %p. Activating", action);
253       lmm_update_variable_weight(p_maxminSystem, action->getVariable(), action->m_weight);
254       action->heapRemove(p_actionHeap);
255       action->refreshLastUpdate();
256
257         // if I am wearing a max_duration or normal hat
258     } else if (action->getHat() == MAX_DURATION ||
259         action->getHat() == NORMAL) {
260         // no need to communicate anymore
261         // assume that flows that reached max_duration have remaining of 0
262       XBT_DEBUG("Action %p finished", action);
263       action->setRemains(0);
264       action->finish();
265       action->setState(SURF_ACTION_DONE);
266       action->heapRemove(p_actionHeap);
267
268       action->gapRemove();
269     }
270   }
271   return;
272 }
273
274
275 void NetworkCm02Model::updateActionsStateFull(double now, double delta)
276 {
277   NetworkCm02Action *action;
278   ActionList *running_actions = getRunningActionSet();
279
280   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
281      ; it != itend ; it=itNext) {
282         ++itNext;
283
284     action = static_cast<NetworkCm02Action*> (&*it);
285     XBT_DEBUG("Something happened to action %p", action);
286       double deltap = delta;
287       if (action->m_latency > 0) {
288         if (action->m_latency > deltap) {
289           double_update(&(action->m_latency), deltap, sg_surf_precision);
290           deltap = 0.0;
291         } else {
292           double_update(&(deltap), action->m_latency, sg_surf_precision);
293           action->m_latency = 0.0;
294         }
295         if (action->m_latency == 0.0 && !(action->isSuspended()))
296           lmm_update_variable_weight(p_maxminSystem, action->getVariable(),
297               action->m_weight);
298       }
299       if (TRACE_is_enabled()) {
300         int n = lmm_get_number_of_cnst_from_var(p_maxminSystem, action->getVariable());
301         int i;
302         for (i = 0; i < n; i++){
303           lmm_constraint_t constraint = lmm_get_cnst_from_var(p_maxminSystem,
304                                                             action->getVariable(),
305                                                             i);
306           NetworkCm02Link* link = static_cast<NetworkCm02Link*>(lmm_constraint_id(constraint));
307           TRACE_surf_link_set_utilization(link->getName(),
308                                         action->getCategory(),
309                                         (lmm_variable_getvalue(action->getVariable())*
310                                             lmm_get_cnst_weight_from_var(p_maxminSystem,
311                                                 action->getVariable(),
312                                                 i)),
313                                         action->getLastUpdate(),
314                                         now - action->getLastUpdate());
315         }
316       }
317       if (!lmm_get_number_of_cnst_from_var
318           (p_maxminSystem, action->getVariable())) {
319         /* There is actually no link used, hence an infinite bandwidth.
320          * This happens often when using models like vivaldi.
321          * In such case, just make sure that the action completes immediately.
322          */
323         action->updateRemains(action->getRemains());
324       }
325     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
326                   
327     if (action->getMaxDuration() != NO_MAX_DURATION)
328       action->updateMaxDuration(delta);
329       
330     if ((action->getRemains() <= 0) &&
331         (lmm_get_variable_weight(action->getVariable()) > 0)) {
332       action->finish();
333       action->setState(SURF_ACTION_DONE);
334       action->gapRemove();
335     } else if (((action->getMaxDuration() != NO_MAX_DURATION)
336         && (action->getMaxDuration() <= 0))) {
337       action->finish();
338       action->setState(SURF_ACTION_DONE);
339       action->gapRemove();
340     }
341   }
342   return;
343 }
344
345 Action *NetworkCm02Model::communicate(RoutingEdge *src, RoutingEdge *dst,
346                                                 double size, double rate)
347 {
348   unsigned int i;
349   void *_link;
350   NetworkCm02Link *link;
351   int failed = 0;
352   NetworkCm02Action *action = NULL;
353   double bandwidth_bound;
354   double latency = 0.0;
355   xbt_dynar_t back_route = NULL;
356   int constraints_per_variable = 0;
357
358   xbt_dynar_t route = xbt_dynar_new(sizeof(RoutingEdge*), NULL);
359
360   XBT_IN("(%s,%s,%g,%g)", src->getName(), dst->getName(), size, rate);
361
362   routing_platf->getRouteAndLatency(src, dst, &route, &latency);
363   xbt_assert(!xbt_dynar_is_empty(route) || latency,
364              "You're trying to send data from %s to %s but there is no connection at all between these two hosts.",
365              src->getName(), dst->getName());
366
367   xbt_dynar_foreach(route, i, _link) {
368         link = static_cast<NetworkCm02Link*>(_link);
369     if (link->getState() == SURF_RESOURCE_OFF) {
370       failed = 1;
371       break;
372     }
373   }
374   if (sg_network_crosstraffic == 1) {
375           routing_platf->getRouteAndLatency(dst, src, &back_route, NULL);
376     xbt_dynar_foreach(back_route, i, _link) {
377       link = static_cast<NetworkCm02Link*>(_link);
378       if (link->getState() == SURF_RESOURCE_OFF) {
379         failed = 1;
380         break;
381       }
382     }
383   }
384
385   action = new NetworkCm02Action(this, size, failed);
386
387 #ifdef HAVE_LATENCY_BOUND_TRACKING
388   action->m_latencyLimited = 0;
389 #endif
390   action->m_weight = action->m_latency = latency;
391
392   action->m_rate = rate;
393   if (p_updateMechanism == UM_LAZY) {
394     action->m_indexHeap = -1;
395     action->m_lastUpdate = surf_get_clock();
396   }
397
398   bandwidth_bound = -1.0;
399   if (sg_weight_S_parameter > 0) {
400     xbt_dynar_foreach(route, i, _link) {
401       link = static_cast<NetworkCm02Link*>(_link);
402       action->m_weight += sg_weight_S_parameter / link->getBandwidth();
403     }
404   }
405   xbt_dynar_foreach(route, i, _link) {
406         link = static_cast<NetworkCm02Link*>(_link);
407     double bb = bandwidthFactor(size) * link->getBandwidth(); //(link->p_power.peak * link->p_power.scale);
408     bandwidth_bound =
409         (bandwidth_bound < 0.0) ? bb : min(bandwidth_bound, bb);
410   }
411
412   action->m_latCurrent = action->m_latency;
413   action->m_latency *= latencyFactor(size);
414   action->m_rate = bandwidthConstraint(action->m_rate, bandwidth_bound, size);
415   if (m_haveGap) {
416     xbt_assert(!xbt_dynar_is_empty(route),
417                "Using a model with a gap (e.g., SMPI) with a platform without links (e.g. vivaldi)!!!");
418
419     link = *static_cast<NetworkCm02Link **>(xbt_dynar_get_ptr(route, 0));
420     gapAppend(size, link, action);
421     XBT_DEBUG("Comm %p: %s -> %s gap=%f (lat=%f)",
422               action, src->getName(), dst->getName(), action->m_senderGap,
423               action->m_latency);
424   }
425
426   constraints_per_variable = xbt_dynar_length(route);
427   if (back_route != NULL)
428     constraints_per_variable += xbt_dynar_length(back_route);
429
430   if (action->m_latency > 0) {
431     action->p_variable = lmm_variable_new(p_maxminSystem, action, 0.0, -1.0,
432                          constraints_per_variable);
433     if (p_updateMechanism == UM_LAZY) {
434       // add to the heap the event when the latency is payed
435       XBT_DEBUG("Added action (%p) one latency event at date %f", action,
436                 action->m_latency + action->m_lastUpdate);
437       action->heapInsert(p_actionHeap, action->m_latency + action->m_lastUpdate, xbt_dynar_is_empty(route) ? NORMAL : LATENCY);
438     }
439   } else
440     action->p_variable = lmm_variable_new(p_maxminSystem, action, 1.0, -1.0, constraints_per_variable);
441
442   if (action->m_rate < 0) {
443     lmm_update_variable_bound(p_maxminSystem, action->getVariable(), (action->m_latCurrent > 0) ? sg_tcp_gamma / (2.0 * action->m_latCurrent) : -1.0);
444   } else {
445     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);
446   }
447
448   xbt_dynar_foreach(route, i, _link) {
449         link = static_cast<NetworkCm02Link*>(_link);
450     lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), 1.0);
451   }
452
453   if (sg_network_crosstraffic == 1) {
454     XBT_DEBUG("Fullduplex active adding backward flow using 5%%");
455     xbt_dynar_foreach(back_route, i, _link) {
456       link = static_cast<NetworkCm02Link*>(_link);
457       lmm_expand(p_maxminSystem, link->getConstraint(), action->getVariable(), .05);
458     }
459   }
460
461   xbt_dynar_free(&route);
462   XBT_OUT();
463
464   surf_callback_emit(networkCommunicateCallbacks, action, src, dst, size, rate);
465   return action;
466 }
467
468 void NetworkCm02Model::addTraces(){
469   xbt_dict_cursor_t cursor = NULL;
470   char *trace_name, *elm;
471
472   static int called = 0;
473   if (called)
474     return;
475   called = 1;
476
477   /* connect all traces relative to network */
478   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
479     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
480     NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
481
482     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
483                trace_name, elm);
484     xbt_assert(trace,
485                "Cannot connect trace %s to link %s: trace undefined",
486                trace_name, elm);
487
488     link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
489   }
490
491   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
492     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
493     NetworkCm02Link *link = static_cast<NetworkCm02Link*>( Link::byName(elm) );
494
495     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
496                trace_name, elm);
497     xbt_assert(trace,
498                "Cannot connect trace %s to link %s: trace undefined",
499                trace_name, elm);
500
501     link->p_power.event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
502   }
503
504   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
505     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
506     NetworkCm02Link *link = static_cast<NetworkCm02Link*>(Link::byName(elm));;
507
508     xbt_assert(link, "Cannot connect trace %s to link %s: link undefined",
509                trace_name, elm);
510     xbt_assert(trace,
511                "Cannot connect trace %s to link %s: trace undefined",
512                trace_name, elm);
513
514     link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, link);
515   }
516 }
517
518 /************
519  * Resource *
520  ************/
521 NetworkCm02Link::NetworkCm02Link(NetworkCm02Model *model, const char *name, xbt_dict_t props,
522                                    lmm_system_t system,
523                                    double constraint_value,
524                                    tmgr_history_t history,
525                                    e_surf_resource_state_t state_init,
526                                    tmgr_trace_t state_trace,
527                                    double metric_peak,
528                                    tmgr_trace_t metric_trace,
529                                    double lat_initial,
530                                    tmgr_trace_t lat_trace,
531                                    e_surf_link_sharing_policy_t policy)
532 : Link(model, name, props, lmm_constraint_new(system, this, constraint_value), history, state_trace)
533 {
534   setState(state_init);
535
536   p_power.scale = 1.0;
537   p_power.peak = metric_peak;
538   if (metric_trace)
539     p_power.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, this);
540   else
541     p_power.event = NULL;
542
543   m_latCurrent = lat_initial;
544   if (lat_trace)
545         p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, this);
546
547   if (policy == SURF_LINK_FATPIPE)
548         lmm_constraint_shared(getConstraint());
549 }
550
551
552
553 void NetworkCm02Link::updateState(tmgr_trace_event_t event_type,
554                                       double value, double date)
555 {
556   /*   printf("[" "%g" "] Asking to update network card \"%s\" with value " */
557   /*     "%g" " for event %p\n", surf_get_clock(), nw_link->name, */
558   /*     value, event_type); */
559
560   if (event_type == p_power.event) {
561     updateBandwidth(value, date);
562     if (tmgr_trace_event_free(event_type))
563       p_power.event = NULL;
564   } else if (event_type == p_latEvent) {
565     updateLatency(value, date);
566     if (tmgr_trace_event_free(event_type))
567       p_latEvent = NULL;
568   } else if (event_type == p_stateEvent) {
569     if (value > 0)
570       setState(SURF_RESOURCE_ON);
571     else {
572       lmm_constraint_t cnst = getConstraint();
573       lmm_variable_t var = NULL;
574       lmm_element_t elem = NULL;
575
576       setState(SURF_RESOURCE_OFF);
577       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
578         Action *action = static_cast<Action*>( lmm_variable_id(var) );
579
580         if (action->getState() == SURF_ACTION_RUNNING ||
581             action->getState() == SURF_ACTION_READY) {
582           action->setFinishTime(date);
583           action->setState(SURF_ACTION_FAILED);
584         }
585       }
586     }
587     if (tmgr_trace_event_free(event_type))
588       p_stateEvent = NULL;
589   } else {
590     XBT_CRITICAL("Unknown event ! \n");
591     xbt_abort();
592   }
593
594   XBT_DEBUG
595       ("There were a resource state event, need to update actions related to the constraint (%p)",
596        getConstraint());
597   return;
598 }
599
600 void NetworkCm02Link::updateBandwidth(double value, double date){
601   double delta = sg_weight_S_parameter / value - sg_weight_S_parameter /
602                  (p_power.peak * p_power.scale);
603   lmm_variable_t var = NULL;
604   lmm_element_t elem = NULL;
605   lmm_element_t nextelem = NULL;
606   int numelem = 0;
607
608   NetworkCm02Action *action = NULL;
609
610   p_power.peak = value;
611   lmm_update_constraint_bound(getModel()->getMaxminSystem(),
612                               getConstraint(),
613                               sg_bandwidth_factor *
614                               (p_power.peak * p_power.scale));
615   TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_power.peak * p_power.scale);
616   if (sg_weight_S_parameter > 0) {
617     while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
618       action = (NetworkCm02Action*) lmm_variable_id(var);
619       action->m_weight += delta;
620       if (!action->isSuspended())
621         lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
622     }
623   }
624 }
625
626 void NetworkCm02Link::updateLatency(double value, double date){
627   double delta = value - m_latCurrent;
628   lmm_variable_t var = NULL;
629   lmm_element_t elem = NULL;
630   lmm_element_t nextelem = NULL;
631   int numelem = 0;
632   NetworkCm02Action *action = NULL;
633
634   m_latCurrent = value;
635   while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
636     action = (NetworkCm02Action*) lmm_variable_id(var);
637     action->m_latCurrent += delta;
638     action->m_weight += delta;
639     if (action->m_rate < 0)
640       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), sg_tcp_gamma / (2.0 * action->m_latCurrent));
641     else {
642       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(),
643                                 min(action->m_rate, sg_tcp_gamma / (2.0 * action->m_latCurrent)));
644
645       if (action->m_rate < sg_tcp_gamma / (2.0 * action->m_latCurrent)) {
646         XBT_INFO("Flow is limited BYBANDWIDTH");
647       } else {
648         XBT_INFO("Flow is limited BYLATENCY, latency of flow is %f",
649                  action->m_latCurrent);
650       }
651     }
652     if (!action->isSuspended())
653       lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), action->m_weight);
654   }
655 }
656
657 /**********
658  * Action *
659  **********/
660 void NetworkCm02Action::updateRemainingLazy(double now)
661 {
662   double delta = 0.0;
663
664   if (m_suspended != 0)
665     return;
666
667   delta = now - m_lastUpdate;
668
669   if (m_remains > 0) {
670     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
671     double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
672
673     XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
674   }
675
676   if (m_maxDuration != NO_MAX_DURATION)
677     double_update(&m_maxDuration, delta, sg_surf_precision);
678
679   if (m_remains <= 0 &&
680       (lmm_get_variable_weight(getVariable()) > 0)) {
681     finish();
682     setState(SURF_ACTION_DONE);
683
684     heapRemove(getModel()->getActionHeap());
685   } else if (((m_maxDuration != NO_MAX_DURATION)
686       && (m_maxDuration <= 0))) {
687     finish();
688     setState(SURF_ACTION_DONE);
689     heapRemove(getModel()->getActionHeap());
690   }
691
692   m_lastUpdate = now;
693   m_lastValue = lmm_variable_getvalue(getVariable());
694 }
695