Logo AND Algorithmique Numérique Distribuée

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