Logo AND Algorithmique Numérique Distribuée

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