Logo AND Algorithmique Numérique Distribuée

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