Logo AND Algorithmique Numérique Distribuée

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