Logo AND Algorithmique Numérique Distribuée

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