Logo AND Algorithmique Numérique Distribuée

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