Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4537df7b6727ce33ab80f599bc0d407d6cbedfc3
[simgrid.git] / src / surf / surf_interface.cpp
1 /* Copyright (c) 2004-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 "surf_private.h"
8 #include "surf_interface.hpp"
9 #include "network_interface.hpp"
10 #include "cpu_interface.hpp"
11 #include "src/surf/HostImpl.hpp"
12 #include "src/simix/smx_host_private.h"
13 #include "surf_routing.hpp"
14 #include "simgrid/sg_config.h"
15 #include "mc/mc.h"
16 #include "virtual_machine.hpp"
17 #include "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
18
19
20 XBT_LOG_NEW_CATEGORY(surf, "All SURF categories");
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_kernel, surf,
22                                 "Logging specific to SURF (kernel)");
23
24 /*********
25  * Utils *
26  *********/
27
28 /* model_list_invoke contains only surf_host and surf_vm.
29  * The callback functions of cpu_model and network_model will be called from
30  * those of these host models. */
31 xbt_dynar_t all_existing_models = NULL; /* to destroy models correctly */
32 xbt_dynar_t model_list_invoke = NULL;  /* to invoke callbacks */
33
34 simgrid::trace_mgr::future_evt_set *future_evt_set = nullptr;
35 xbt_dynar_t surf_path = NULL;
36 xbt_dynar_t host_that_restart = xbt_dynar_new(sizeof(char*), NULL);
37 xbt_dict_t watched_hosts_lib;
38
39 namespace simgrid {
40 namespace surf {
41
42 simgrid::xbt::signal<void(void)> surfExitCallbacks;
43
44 }
45 }
46
47 #include <simgrid/plugins/energy.h> // FIXME: this plugin should not be linked to the core
48
49 s_surf_model_description_t surf_plugin_description[] = {
50     {"Energy", "Cpu energy consumption.", sg_energy_plugin_init},
51      {NULL, NULL,  NULL}      /* this array must be NULL terminated */
52 };
53
54 /* Don't forget to update the option description in smx_config when you change
55    this */
56 s_surf_model_description_t surf_network_model_description[] = {
57   {"LV08",
58    "Realistic network analytic model (slow-start modeled by multiplying latency by 10.4, bandwidth by .92; bottleneck sharing uses a payload of S=8775 for evaluating RTT). ",
59    surf_network_model_init_LegrandVelho},
60   {"Constant",
61    "Simplistic network model where all communication take a constant time (one second). This model provides the lowest realism, but is (marginally) faster.",
62    surf_network_model_init_Constant},
63   {"SMPI",
64    "Realistic network model specifically tailored for HPC settings (accurate modeling of slow start with correction factors on three intervals: < 1KiB, < 64 KiB, >= 64 KiB)",
65    surf_network_model_init_SMPI},
66   {"IB",
67    "Realistic network model specifically tailored for HPC settings, with Infiniband contention model",
68    surf_network_model_init_IB},
69   {"CM02",
70    "Legacy network analytic model (Very similar to LV08, but without corrective factors. The timings of small messages are thus poorly modeled).",
71    surf_network_model_init_CM02},
72 #ifdef HAVE_NS3
73   {"NS3",
74    "Network pseudo-model using the NS3 tcp model instead of an analytic model",
75   surf_network_model_init_NS3},
76 #endif
77   {"Reno",
78    "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
79    surf_network_model_init_Reno},
80   {"Reno2",
81    "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
82    surf_network_model_init_Reno2},
83   {"Vegas",
84    "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
85    surf_network_model_init_Vegas},
86   {NULL, NULL, NULL}      /* this array must be NULL terminated */
87 };
88
89 s_surf_model_description_t surf_cpu_model_description[] = {
90   {"Cas01",
91    "Simplistic CPU model (time=size/power).",
92    surf_cpu_model_init_Cas01},
93   {NULL, NULL,  NULL}      /* this array must be NULL terminated */
94 };
95
96 s_surf_model_description_t surf_host_model_description[] = {
97   {"default",
98    "Default host model. Currently, CPU:Cas01 and network:LV08 (with cross traffic enabled)",
99    surf_host_model_init_current_default},
100   {"compound",
101    "Host model that is automatically chosen if you change the network and CPU models",
102    surf_host_model_init_compound},
103   {"ptask_L07", "Host model somehow similar to Cas01+CM02 but allowing parallel tasks",
104    surf_host_model_init_ptask_L07},
105   {NULL, NULL, NULL}      /* this array must be NULL terminated */
106 };
107
108 s_surf_model_description_t surf_vm_model_description[] = {
109   {"default",
110    "Default vm model.",
111    surf_vm_model_init_HL13},
112   {NULL, NULL, NULL}      /* this array must be NULL terminated */
113 };
114
115 s_surf_model_description_t surf_optimization_mode_description[] = {
116   {"Lazy",
117    "Lazy action management (partial invalidation in lmm + heap in action remaining).",
118    NULL},
119   {"TI",
120    "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU model for now).",
121     NULL},
122   {"Full",
123    "Full update of remaining and variables. Slow but may be useful when debugging.",
124    NULL},
125   {NULL, NULL, NULL}      /* this array must be NULL terminated */
126 };
127
128 s_surf_model_description_t surf_storage_model_description[] = {
129   {"default",
130    "Simplistic storage model.",
131    surf_storage_model_init_default},
132   {NULL, NULL,  NULL}      /* this array must be NULL terminated */
133 };
134
135 #ifdef HAVE_THREAD_CONTEXTS
136 static xbt_parmap_t surf_parmap = NULL; /* parallel map on models */
137 #endif
138
139 double NOW = 0;
140
141 double surf_get_clock(void)
142 {
143   return NOW;
144 }
145
146 #ifdef _XBT_WIN32
147 # define FILE_DELIM "\\"
148 #else
149 # define FILE_DELIM "/"         /* FIXME: move to better location */
150 #endif
151
152 FILE *surf_fopen(const char *name, const char *mode)
153 {
154   unsigned int cpt;
155   char *path_elm = NULL;
156   char *buff;
157   FILE *file = NULL;
158
159   xbt_assert(name);
160
161   if (__surf_is_absolute_file_path(name))       /* don't mess with absolute file names */
162     return fopen(name, mode);
163
164   /* search relative files in the path */
165   xbt_dynar_foreach(surf_path, cpt, path_elm) {
166     buff = bprintf("%s" FILE_DELIM "%s", path_elm, name);
167     file = fopen(buff, mode);
168     free(buff);
169
170     if (file)
171       return file;
172   }
173   return NULL;
174 }
175
176 #ifdef _XBT_WIN32
177 #include <windows.h>
178 #define MAX_DRIVE 26
179 static const char *disk_drives_letter_table[MAX_DRIVE] = {
180   "A:\\","B:\\","C:\\","D:\\","E:\\","F:\\","G:\\","H:\\","I:\\","J:\\","K:\\","L:\\","M:\\",
181   "N:\\","O:\\","P:\\","Q:\\","R:\\","S:\\","T:\\","U:\\","V:\\","W:\\","X:\\","Y:\\","Z:\\"
182 };
183 #endif
184
185 /*
186  * Returns the initial path. On Windows the initial path is
187  * the current directory for the current process in the other
188  * case the function returns "./" that represents the current
189  * directory on Unix/Linux platforms.
190  */
191
192 const char *__surf_get_initial_path(void)
193 {
194
195 #ifdef _XBT_WIN32
196   unsigned i;
197   char current_directory[MAX_PATH + 1] = { 0 };
198   unsigned int len = GetCurrentDirectory(MAX_PATH + 1, current_directory);
199   char root[4] = { 0 };
200
201   if (!len)
202     return NULL;
203
204   strncpy(root, current_directory, 3);
205
206   for (i = 0; i < MAX_DRIVE; i++) {
207     if (toupper(root[0]) == disk_drives_letter_table[i][0])
208       return disk_drives_letter_table[i];
209   }
210
211   return NULL;
212 #else
213   return "./";
214 #endif
215 }
216
217 /* The __surf_is_absolute_file_path() returns 1 if
218  * file_path is a absolute file path, in the other
219  * case the function returns 0.
220  */
221 int __surf_is_absolute_file_path(const char *file_path)
222 {
223 #ifdef _XBT_WIN32
224   WIN32_FIND_DATA wfd = { 0 };
225   HANDLE hFile = FindFirstFile(file_path, &wfd);
226
227   if (INVALID_HANDLE_VALUE == hFile)
228     return 0;
229
230   FindClose(hFile);
231   return 1;
232 #else
233   return (file_path[0] == '/');
234 #endif
235 }
236
237 /** Displays the long description of all registered models, and quit */
238 void model_help(const char *category, s_surf_model_description_t * table)
239 {
240   int i;
241   printf("Long description of the %s models accepted by this simulator:\n",
242          category);
243   for (i = 0; table[i].name; i++)
244     printf("  %s: %s\n", table[i].name, table[i].description);
245 }
246
247 int find_model_description(s_surf_model_description_t * table,
248                            const char *name)
249 {
250   int i;
251   char *name_list = NULL;
252
253   for (i = 0; table[i].name; i++)
254     if (!strcmp(name, table[i].name)) {
255       return i;
256     }
257   if (!table[0].name)
258     xbt_die("No model is valid! This is a bug.");
259   name_list = xbt_strdup(table[0].name);
260   for (i = 1; table[i].name; i++) {
261     name_list = (char *) xbt_realloc(name_list, strlen(name_list) + strlen(table[i].name) + 3);
262     strcat(name_list, ", ");
263     strcat(name_list, table[i].name);
264   }
265   xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list);
266   return -1;
267 }
268
269 static XBT_INLINE void routing_asr_prop_free(void *p)
270 {
271   //xbt_dict_t elm = (xbt_dict_t) p;
272   //xbt_dict_free(&elm); FIXME: leaking in some case? That's a sometimes double-free with AsCluster::~AsCluster
273 }
274
275 static XBT_INLINE void surf_storage_free(void *r)
276 {
277   delete static_cast<simgrid::surf::Storage*>(r);
278 }
279
280 void sg_version_check(int lib_version_major,int lib_version_minor,int lib_version_patch) {
281     if ((lib_version_major != SIMGRID_VERSION_MAJOR) || (lib_version_minor != SIMGRID_VERSION_MINOR)) {
282       fprintf(stderr,
283           "FATAL ERROR: Your program was compiled with SimGrid version %d.%d.%d, "
284           "and then linked against SimGrid %d.%d.%d. Please fix this.\n",
285               SIMGRID_VERSION_MAJOR,SIMGRID_VERSION_MINOR,SIMGRID_VERSION_PATCH,
286         lib_version_major,lib_version_minor,lib_version_patch);
287       abort();
288     }
289     if (lib_version_patch != SIMGRID_VERSION_PATCH) {
290         fprintf(stderr,
291             "Warning: Your program was compiled with SimGrid version %d.%d.%d, "
292             "and then linked against SimGrid %d.%d.%d. Proceeding anyway.\n",
293                 SIMGRID_VERSION_MAJOR,SIMGRID_VERSION_MINOR,SIMGRID_VERSION_PATCH,
294           lib_version_major,lib_version_minor,lib_version_patch);
295     }
296 }
297
298 void sg_version(int *ver_major,int *ver_minor,int *ver_patch) {
299   *ver_major = SIMGRID_VERSION_MAJOR;
300   *ver_minor = SIMGRID_VERSION_MINOR;
301   *ver_patch = SIMGRID_VERSION_PATCH;
302 }
303
304 void surf_init(int *argc, char **argv)
305 {
306   XBT_DEBUG("Create all Libs");
307   host_list = xbt_dict_new_homogeneous([](void*p) {
308     simgrid::s4u::Host* host = static_cast<simgrid::s4u::Host*>(p);
309     simgrid::s4u::Host::onDestruction(*host);
310     delete host;
311   });
312   as_router_lib = xbt_lib_new();
313   storage_lib = xbt_lib_new();
314   storage_type_lib = xbt_lib_new();
315   file_lib = xbt_lib_new();
316   watched_hosts_lib = xbt_dict_new_homogeneous(NULL);
317
318   sg_host_init();
319
320   XBT_DEBUG("Add routing levels");
321   ROUTING_PROP_ASR_LEVEL = xbt_lib_add_level(as_router_lib,routing_asr_prop_free);
322
323   XBT_DEBUG("Add SURF levels");
324   simgrid::surf::HostImpl::classInit();
325   SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,surf_storage_free);
326
327   xbt_init(argc, argv);
328   if (!all_existing_models)
329     all_existing_models = xbt_dynar_new(sizeof(simgrid::surf::Model*), NULL);
330   if (!model_list_invoke)
331     model_list_invoke = xbt_dynar_new(sizeof(simgrid::surf::Model*), NULL);
332   if (!future_evt_set)
333     future_evt_set = new simgrid::trace_mgr::future_evt_set();
334
335   TRACE_add_start_function(TRACE_surf_alloc);
336   TRACE_add_end_function(TRACE_surf_release);
337
338   sg_config_init(argc, argv);
339
340   if (MC_is_active())
341     MC_memory_init();
342 }
343
344 void surf_exit(void)
345 {
346   unsigned int iter;
347   simgrid::surf::Model *model = NULL;
348
349   TRACE_end();                  /* Just in case it was not called by the upper
350                                  * layer (or there is no upper layer) */
351
352   sg_config_finalize();
353
354   xbt_dynar_free(&host_that_restart);
355   xbt_dynar_free(&surf_path);
356
357   xbt_dict_free(&host_list);
358   xbt_lib_free(&as_router_lib);
359   xbt_lib_free(&storage_lib);
360   sg_link_exit();
361   xbt_lib_free(&storage_type_lib);
362   xbt_lib_free(&file_lib);
363   xbt_dict_free(&watched_hosts_lib);
364
365   xbt_dynar_foreach(all_existing_models, iter, model)
366     delete model;
367   xbt_dynar_free(&all_existing_models);
368   xbt_dynar_free(&model_list_invoke);
369   routing_exit();
370
371   simgrid::surf::surfExitCallbacks();
372
373   if (future_evt_set) {
374     delete future_evt_set;
375     future_evt_set = nullptr;
376   }
377
378 #ifdef HAVE_THREAD_CONTEXTS
379   xbt_parmap_destroy(surf_parmap);
380 #endif
381
382   tmgr_finalize();
383   sg_platf_exit();
384
385   NOW = 0;                      /* Just in case the user plans to restart the simulation afterward */
386 }
387
388 /*********
389  * Model *
390  *********/
391
392 namespace simgrid {
393 namespace surf {
394
395 Model::Model()
396   : p_maxminSystem(NULL)
397 {
398   p_readyActionSet = new ActionList();
399   p_runningActionSet = new ActionList();
400   p_failedActionSet = new ActionList();
401   p_doneActionSet = new ActionList();
402
403   p_modifiedSet = NULL;
404   p_actionHeap = NULL;
405   p_updateMechanism = UM_UNDEFINED;
406   m_selectiveUpdate = 0;
407 }
408
409 Model::~Model(){
410   delete p_readyActionSet;
411   delete p_runningActionSet;
412   delete p_failedActionSet;
413   delete p_doneActionSet;
414 }
415
416 double Model::next_occuring_event(double now)
417 {
418   //FIXME: set the good function once and for all
419   if (p_updateMechanism == UM_LAZY)
420     return next_occuring_event_lazy(now);
421   else if (p_updateMechanism == UM_FULL)
422     return next_occuring_event_full(now);
423   else
424     xbt_die("Invalid cpu update mechanism!");
425 }
426
427 double Model::next_occuring_event_lazy(double now)
428 {
429   Action *action = NULL;
430   double min = -1;
431   double share;
432
433   XBT_DEBUG
434       ("Before share resources, the size of modified actions set is %zd",
435        p_modifiedSet->size());
436
437   lmm_solve(p_maxminSystem);
438
439   XBT_DEBUG
440       ("After share resources, The size of modified actions set is %zd",
441        p_modifiedSet->size());
442
443   while(!p_modifiedSet->empty()) {
444     action = &(p_modifiedSet->front());
445     p_modifiedSet->pop_front();
446     int max_dur_flag = 0;
447
448     if (action->getStateSet() != p_runningActionSet)
449       continue;
450
451     /* bogus priority, skip it */
452     if (action->getPriority() <= 0 || action->getHat()==LATENCY)
453       continue;
454
455     action->updateRemainingLazy(now);
456
457     min = -1;
458     share = lmm_variable_getvalue(action->getVariable());
459
460     if (share > 0) {
461       double time_to_completion;
462       if (action->getRemains() > 0) {
463         time_to_completion = action->getRemainsNoUpdate() / share;
464       } else {
465         time_to_completion = 0.0;
466       }
467       min = now + time_to_completion; // when the task will complete if nothing changes
468     }
469
470     if ((action->getMaxDuration() != NO_MAX_DURATION)
471         && (min == -1
472             || action->getStartTime() +
473             action->getMaxDuration() < min)) {
474       min = action->getStartTime() +
475           action->getMaxDuration();  // when the task will complete anyway because of the deadline if any
476       max_dur_flag = 1;
477     }
478
479
480     XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->getVariable()->id_int);
481
482     XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
483         action->getStartTime(), min, share,
484         action->getMaxDuration());
485
486     if (min != -1) {
487       action->heapUpdate(p_actionHeap, min, max_dur_flag ? MAX_DURATION : NORMAL);
488       XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min,
489                 now);
490     } else DIE_IMPOSSIBLE;
491   }
492
493   //hereafter must have already the min value for this resource model
494   if (xbt_heap_size(p_actionHeap) > 0)
495     min = xbt_heap_maxkey(p_actionHeap) - now;
496   else
497     min = -1;
498
499   XBT_DEBUG("The minimum with the HEAP %f", min);
500
501   return min;
502 }
503
504 double Model::next_occuring_event_full(double /*now*/) {
505   THROW_UNIMPLEMENTED;
506 }
507
508 double Model::shareResourcesMaxMin(ActionList *running_actions,
509                           lmm_system_t sys,
510                           void (*solve) (lmm_system_t))
511 {
512   Action *action = NULL;
513   double min = -1;
514   double value = -1;
515
516   solve(sys);
517
518   ActionList::iterator it(running_actions->begin()), itend(running_actions->end());
519   for(; it != itend ; ++it) {
520     action = &*it;
521     value = lmm_variable_getvalue(action->getVariable());
522     if ((value > 0) || (action->getMaxDuration() >= 0))
523       break;
524   }
525
526   if (!action)
527     return -1.0;
528
529   if (value > 0) {
530     if (action->getRemains() > 0)
531       min = action->getRemainsNoUpdate() / value;
532     else
533       min = 0.0;
534     if ((action->getMaxDuration() >= 0) && (action->getMaxDuration() < min))
535       min = action->getMaxDuration();
536   } else
537     min = action->getMaxDuration();
538
539
540   for (++it; it != itend; ++it) {
541   action = &*it;
542     value = lmm_variable_getvalue(action->getVariable());
543     if (value > 0) {
544       if (action->getRemains() > 0)
545         value = action->getRemainsNoUpdate() / value;
546       else
547         value = 0.0;
548       if (value < min) {
549         min = value;
550         XBT_DEBUG("Updating min (value) with %p: %f", action, min);
551       }
552     }
553     if ((action->getMaxDuration() >= 0) && (action->getMaxDuration() < min)) {
554       min = action->getMaxDuration();
555       XBT_DEBUG("Updating min (duration) with %p: %f", action, min);
556     }
557   }
558   XBT_DEBUG("min value : %f", min);
559
560   return min;
561 }
562
563 void Model::updateActionsState(double now, double delta)
564 {
565   if (p_updateMechanism == UM_FULL)
566   updateActionsStateFull(now, delta);
567   else if (p_updateMechanism == UM_LAZY)
568   updateActionsStateLazy(now, delta);
569   else
570   xbt_die("Invalid cpu update mechanism!");
571 }
572
573 void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
574 {
575  THROW_UNIMPLEMENTED;
576 }
577
578 void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
579 {
580   THROW_UNIMPLEMENTED;
581 }
582
583 }
584 }
585
586 /************
587  * Resource *
588  ************/
589
590 namespace simgrid {
591 namespace surf {
592
593 Resource::Resource(Model *model, const char *name)
594   : Resource(model, name, 1/*ON*/)
595 {}
596
597 Resource::Resource(Model *model, const char *name, lmm_constraint_t constraint)
598   : Resource(model, name, constraint, 1/*ON*/)
599 {}
600   
601 Resource::Resource(Model *model, const char *name, lmm_constraint_t constraint, int initiallyOn)
602   : p_name(xbt_strdup(name))
603   , p_model(model)
604   , m_isOn(initiallyOn)
605   , p_constraint(constraint)
606 {}
607
608 Resource::Resource(Model *model, const char *name, int initiallyOn)
609   : p_name(xbt_strdup(name))
610   , p_model(model)
611   , m_isOn(initiallyOn)
612 {}
613
614
615 Resource::~Resource() {
616   xbt_free((void*)p_name);
617 }
618
619 bool Resource::isOn() {
620   return m_isOn;
621 }
622 bool Resource::isOff() {
623   return ! m_isOn;
624 }
625
626 void Resource::turnOn()
627 {
628   if (!m_isOn) {
629     m_isOn = true;
630   }
631 }
632
633 void Resource::turnOff()
634 {
635   if (m_isOn) {
636     m_isOn = false;
637   }
638 }
639
640 Model *Resource::getModel() {
641   return p_model;
642 }
643
644 const char *Resource::getName() {
645   return p_name;
646 }
647
648 lmm_constraint_t Resource::getConstraint() {
649   return p_constraint;
650 }
651
652 }
653 }
654
655 /**********
656  * Action *
657  **********/
658
659 const char *surf_action_state_names[6] = {
660   "SURF_ACTION_READY",
661   "SURF_ACTION_RUNNING",
662   "SURF_ACTION_FAILED",
663   "SURF_ACTION_DONE",
664   "SURF_ACTION_TO_FREE",
665   "SURF_ACTION_NOT_IN_THE_SYSTEM"
666 };
667
668 /* added to manage the communication action's heap */
669 void surf_action_lmm_update_index_heap(void *action, int i) {
670   static_cast<simgrid::surf::Action*>(action)->updateIndexHeap(i);
671 }
672
673 namespace simgrid {
674 namespace surf {
675
676 void Action::initialize(simgrid::surf::Model *model, double cost, bool failed,
677                         lmm_variable_t var)
678 {
679   m_remains = cost;
680   m_start = surf_get_clock();
681   m_cost = cost;
682   p_model = model;
683   p_variable = var;
684   if (failed)
685     p_stateSet = getModel()->getFailedActionSet();
686   else
687     p_stateSet = getModel()->getRunningActionSet();
688
689   p_stateSet->push_back(*this);
690 }
691
692 Action::Action(simgrid::surf::Model *model, double cost, bool failed)
693 {
694   initialize(model, cost, failed);
695 }
696
697 Action::Action(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
698 {
699   initialize(model, cost, failed, var);
700 }
701
702 Action::~Action() {
703   xbt_free(p_category);
704 }
705
706 void Action::finish() {
707     m_finish = surf_get_clock();
708 }
709
710 e_surf_action_state_t Action::getState()
711 {
712   if (p_stateSet ==  getModel()->getReadyActionSet())
713     return SURF_ACTION_READY;
714   if (p_stateSet ==  getModel()->getRunningActionSet())
715     return SURF_ACTION_RUNNING;
716   if (p_stateSet ==  getModel()->getFailedActionSet())
717     return SURF_ACTION_FAILED;
718   if (p_stateSet ==  getModel()->getDoneActionSet())
719     return SURF_ACTION_DONE;
720   return SURF_ACTION_NOT_IN_THE_SYSTEM;
721 }
722
723 void Action::setState(e_surf_action_state_t state)
724 {
725   //surf_action_state_t action_state = &(action->model_type->states);
726   XBT_IN("(%p,%s)", this, surf_action_state_names[state]);
727   p_stateSet->erase(p_stateSet->iterator_to(*this));
728   if (state == SURF_ACTION_READY)
729     p_stateSet = getModel()->getReadyActionSet();
730   else if (state == SURF_ACTION_RUNNING)
731     p_stateSet = getModel()->getRunningActionSet();
732   else if (state == SURF_ACTION_FAILED)
733     p_stateSet = getModel()->getFailedActionSet();
734   else if (state == SURF_ACTION_DONE)
735     p_stateSet = getModel()->getDoneActionSet();
736   else
737     p_stateSet = NULL;
738
739   if (p_stateSet)
740     p_stateSet->push_back(*this);
741   XBT_OUT();
742 }
743
744 double Action::getBound()
745 {
746   return (p_variable) ? lmm_variable_getbound(p_variable) : 0;
747 }
748
749 void Action::setBound(double bound)
750 {
751   XBT_IN("(%p,%g)", this, bound);
752   if (p_variable)
753     lmm_update_variable_bound(getModel()->getMaxminSystem(), p_variable, bound);
754
755   if (getModel()->getUpdateMechanism() == UM_LAZY && getLastUpdate()!=surf_get_clock())
756     heapRemove(getModel()->getActionHeap());
757   XBT_OUT();
758 }
759
760 double Action::getStartTime()
761 {
762   return m_start;
763 }
764
765 double Action::getFinishTime()
766 {
767   /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
768   return m_remains == 0 ? m_finish : -1;
769 }
770
771 void Action::setData(void* data)
772 {
773   p_data = data;
774 }
775
776 void Action::setCategory(const char *category)
777 {
778   XBT_IN("(%p,%s)", this, category);
779   p_category = xbt_strdup(category);
780   XBT_OUT();
781 }
782
783 void Action::ref(){
784   m_refcount++;
785 }
786
787 void Action::setMaxDuration(double duration)
788 {
789   XBT_IN("(%p,%g)", this, duration);
790   m_maxDuration = duration;
791   if (getModel()->getUpdateMechanism() == UM_LAZY)      // remove action from the heap
792     heapRemove(getModel()->getActionHeap());
793   XBT_OUT();
794 }
795
796 void Action::gapRemove() {}
797
798 void Action::setPriority(double priority)
799 {
800   XBT_IN("(%p,%g)", this, priority);
801   m_priority = priority;
802   lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), priority);
803
804   if (getModel()->getUpdateMechanism() == UM_LAZY)
805     heapRemove(getModel()->getActionHeap());
806   XBT_OUT();
807 }
808
809 void Action::cancel(){
810   setState(SURF_ACTION_FAILED);
811   if (getModel()->getUpdateMechanism() == UM_LAZY) {
812     if (action_lmm_hook.is_linked())
813       getModel()->getModifiedSet()->erase(getModel()->getModifiedSet()->iterator_to(*this));
814     heapRemove(getModel()->getActionHeap());
815   }
816 }
817
818 int Action::unref(){
819   m_refcount--;
820   if (!m_refcount) {
821     if (action_hook.is_linked())
822       p_stateSet->erase(p_stateSet->iterator_to(*this));
823     if (getVariable())
824       lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
825     if (getModel()->getUpdateMechanism() == UM_LAZY) {
826       /* remove from heap */
827       heapRemove(getModel()->getActionHeap());
828       if (action_lmm_hook.is_linked())
829         getModel()->getModifiedSet()->erase(getModel()->getModifiedSet()->iterator_to(*this));
830     }
831     delete this;
832     return 1;
833   }
834   return 0;
835 }
836
837 void Action::suspend()
838 {
839   XBT_IN("(%p)", this);
840   if (m_suspended != 2) {
841     lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 0.0);
842     m_suspended = 1;
843     if (getModel()->getUpdateMechanism() == UM_LAZY)
844       heapRemove(getModel()->getActionHeap());
845   }
846   XBT_OUT();
847 }
848
849 void Action::resume()
850 {
851   XBT_IN("(%p)", this);
852   if (m_suspended != 2) {
853     lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), m_priority);
854     m_suspended = 0;
855     if (getModel()->getUpdateMechanism() == UM_LAZY)
856       heapRemove(getModel()->getActionHeap());
857   }
858   XBT_OUT();
859 }
860
861 bool Action::isSuspended()
862 {
863   return m_suspended == 1;
864 }
865 /* insert action on heap using a given key and a hat (heap_action_type)
866  * a hat can be of three types for communications:
867  *
868  * NORMAL = this is a normal heap entry stating the date to finish transmitting
869  * LATENCY = this is a heap entry to warn us when the latency is payed
870  * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
871  */
872 void Action::heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat)
873 {
874   m_hat = hat;
875   xbt_heap_push(heap, this, key);
876 }
877
878 void Action::heapRemove(xbt_heap_t heap)
879 {
880   m_hat = NOTSET;
881   if (m_indexHeap >= 0) {
882     xbt_heap_remove(heap, m_indexHeap);
883   }
884 }
885
886 void Action::heapUpdate(xbt_heap_t heap, double key, enum heap_action_type hat)
887 {
888   m_hat = hat;
889   if (m_indexHeap >= 0) {
890     xbt_heap_update(heap, m_indexHeap, key);
891   }else{
892     xbt_heap_push(heap, this, key);
893   }
894 }
895
896 void Action::updateIndexHeap(int i) {
897   m_indexHeap = i;
898 }
899
900 double Action::getRemains()
901 {
902   XBT_IN("(%p)", this);
903   /* update remains before return it */
904   if (getModel()->getUpdateMechanism() == UM_LAZY)      /* update remains before return it */
905     updateRemainingLazy(surf_get_clock());
906   XBT_OUT();
907   return m_remains;
908 }
909
910 double Action::getRemainsNoUpdate()
911 {
912   return m_remains;
913 }
914
915 //FIXME split code in the right places
916 void Action::updateRemainingLazy(double now)
917 {
918   double delta = 0.0;
919
920   if(getModel() == surf_network_model)
921   {
922     if (m_suspended != 0)
923       return;
924   }
925   else
926   {
927     xbt_assert(p_stateSet == getModel()->getRunningActionSet(),
928         "You're updating an action that is not running.");
929
930       /* bogus priority, skip it */
931     xbt_assert(m_priority > 0,
932         "You're updating an action that seems suspended.");
933   }
934
935   delta = now - m_lastUpdate;
936
937   if (m_remains > 0) {
938     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
939     double_update(&m_remains, m_lastValue * delta, sg_surf_precision*sg_maxmin_precision);
940
941     if (getModel() == surf_cpu_model_pm && TRACE_is_enabled()) {
942       simgrid::surf::Resource *cpu = static_cast<simgrid::surf::Resource*>(
943         lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
944       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
945     }
946     XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
947   }
948
949   if(getModel() == surf_network_model)
950   {
951     if (m_maxDuration != NO_MAX_DURATION)
952       double_update(&m_maxDuration, delta, sg_surf_precision);
953
954     //FIXME: duplicated code
955     if ((m_remains <= 0) &&
956         (lmm_get_variable_weight(getVariable()) > 0)) {
957       finish();
958       setState(SURF_ACTION_DONE);
959       heapRemove(getModel()->getActionHeap());
960     } else if (((m_maxDuration != NO_MAX_DURATION)
961         && (m_maxDuration <= 0))) {
962       finish();
963       setState(SURF_ACTION_DONE);
964       heapRemove(getModel()->getActionHeap());
965     }
966   }
967
968   m_lastUpdate = now;
969   m_lastValue = lmm_variable_getvalue(getVariable());
970 }
971
972 }
973 }