Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clean surf interface
[simgrid.git] / src / surf / surf_interface.cpp
1 #include "surf_private.h"
2 #include "surf_interface.hpp"
3 #include "network_interface.hpp"
4 #include "cpu_interface.hpp"
5 #include "workstation_interface.hpp"
6 #include "vm_workstation_interface.hpp"
7 #include "simix/smx_host_private.h"
8 #include "surf_routing.hpp"
9 #include "simgrid/sg_config.h"
10 #include "mc/mc.h"
11
12 XBT_LOG_NEW_CATEGORY(surf, "All SURF categories");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_kernel, surf,
14                                 "Logging specific to SURF (kernel)");
15
16 /*********
17  * Utils *
18  *********/
19
20 /* This function is a pimple that we ought to fix. But it won't be easy.
21  *
22  * The surf_solve() function does properly return the set of actions that changed.
23  * Instead, each model change a global data, and then the caller of surf_solve must
24  * pick into these sets of action_failed and action_done.
25  *
26  * This was not clean but ok as long as we didn't had to restart the processes when the resource comes back up.
27  * We worked by putting sentinel actions on every resources we are interested in,
28  * so that surf informs us if/when the corresponding resource fails.
29  *
30  * But this does not work to get Simix informed of when a resource comes back up, and this is where this pimple comes.
31  * We have a set of resources that are currently down and for which simix needs to know when it comes back up.
32  * And the current function is called *at every simulation step* to sweep over that set, searching for a resource
33  * that was turned back up in the meanwhile. This is UGLY and slow.
34  *
35  * The proper solution would be to not rely on globals for the action_failed and action_done swags.
36  * They must be passed as parameter by the caller (the handling of these actions in simix may let you
37  * think that these two sets can be merged, but their handling in SimDag induce the contrary unless this
38  * simdag code can check by itself whether the action is done of failed -- seems very doable, but yet more
39  * cleanup to do).
40  *
41  * Once surf_solve() is passed the set of actions that changed, you want to add a new set of resources back up
42  * as parameter to this function. You also want to add a boolean field "restart_watched" to each resource, and
43  * make sure that whenever a resource with this field enabled comes back up, it's added to that set so that Simix
44  * sees it and react accordingly. This would kill that need for surf to call simix.
45  *
46  */
47
48 /*static void remove_watched_host(void *key)
49 {
50   xbt_dict_remove(watched_hosts_lib, *(char**)key);
51 }*/
52
53 /*void surf_watched_hosts(void)
54 {
55   char *key;
56   void *host;
57   xbt_dict_cursor_t cursor;
58   xbt_dynar_t hosts = xbt_dynar_new(sizeof(char*), NULL);
59
60   XBT_DEBUG("Check for host SURF_RESOURCE_ON on watched_hosts_lib");
61   xbt_dict_foreach(watched_hosts_lib, cursor, key, host)
62   {
63     if(SIMIX_host_get_state((smx_host_t)host) == SURF_RESOURCE_ON){
64       XBT_INFO("Restart processes on host: %s", SIMIX_host_get_name((smx_host_t)host));
65       SIMIX_host_autorestart((smx_host_t)host);
66       xbt_dynar_push_as(hosts, char*, key);
67     }
68     else
69       XBT_DEBUG("See SURF_RESOURCE_OFF on host: %s",key);
70   }
71   xbt_dynar_map(hosts, remove_watched_host);
72   xbt_dynar_free(&hosts);
73 }*/
74
75 /* model_list_invoke contains only surf_workstation and surf_vm_workstation.
76  * The callback functions of cpu_model and network_model will be called from
77  * those of these workstation models. */
78 xbt_dynar_t model_list = NULL; /* for destroying all models correctly */
79 xbt_dynar_t model_list_invoke = NULL;  /* for invoking callbacks */
80
81 tmgr_history_t history = NULL;
82 lmm_system_t maxmin_system = NULL;
83 xbt_dynar_t surf_path = NULL;
84 xbt_dynar_t host_that_restart = NULL;
85 xbt_dict_t watched_hosts_lib;
86
87 /* Don't forget to update the option description in smx_config when you change this */
88 s_surf_model_description_t surf_network_model_description[] = {
89   {"LV08",
90    "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). ",
91    surf_network_model_init_LegrandVelho},
92   {"Constant",
93    "Simplistic network model where all communication take a constant time (one second). This model provides the lowest realism, but is (marginally) faster.",
94    surf_network_model_init_Constant},
95   {"SMPI",
96    "Realistic network model specifically tailored for HPC settings (accurate modeling of slow start with correction factors on three intervals: < 1KiB, < 64 KiB, >= 64 KiB)",
97    surf_network_model_init_SMPI},
98   {"CM02",
99    "Legacy network analytic model (Very similar to LV08, but without corrective factors. The timings of small messages are thus poorly modeled).",
100    surf_network_model_init_CM02},
101 #ifdef HAVE_GTNETS
102   {"GTNets",
103    "Network pseudo-model using the GTNets simulator instead of an analytic model",
104    surf_network_model_init_GTNETS},
105 #endif
106 #ifdef HAVE_NS3
107   {"NS3",
108    "Network pseudo-model using the NS3 tcp model instead of an analytic model",
109   surf_network_model_init_NS3},
110 #endif
111   {"Reno",
112    "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
113    surf_network_model_init_Reno},
114   {"Reno2",
115    "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
116    surf_network_model_init_Reno2},
117   {"Vegas",
118    "Model from Steven H. Low using lagrange_solve instead of lmm_solve (experts only; check the code for more info).",
119    surf_network_model_init_Vegas},
120   {NULL, NULL, NULL}      /* this array must be NULL terminated */
121 };
122
123 s_surf_model_description_t surf_cpu_model_description[] = {
124   {"Cas01",
125    "Simplistic CPU model (time=size/power).",
126    surf_cpu_model_init_Cas01},
127   {NULL, NULL,  NULL}      /* this array must be NULL terminated */
128 };
129
130 s_surf_model_description_t surf_workstation_model_description[] = {
131   {"default",
132    "Default workstation model. Currently, CPU:Cas01 and network:LV08 (with cross traffic enabled)",
133    surf_workstation_model_init_current_default},
134   {"compound",
135    "Workstation model that is automatically chosen if you change the network and CPU models",
136    surf_workstation_model_init_compound},
137   {"ptask_L07", "Workstation model somehow similar to Cas01+CM02 but allowing parallel tasks",
138    surf_workstation_model_init_ptask_L07},
139   {NULL, NULL, NULL}      /* this array must be NULL terminated */
140 };
141
142 s_surf_model_description_t surf_vm_workstation_model_description[] = {
143   {"default",
144    "Default vm workstation model.)",
145    surf_vm_workstation_model_init_current_default},
146   {NULL, NULL, NULL}      /* this array must be NULL terminated */
147 };
148
149 s_surf_model_description_t surf_optimization_mode_description[] = {
150   {"Lazy",
151    "Lazy action management (partial invalidation in lmm + heap in action remaining).",
152    NULL},
153   {"TI",
154    "Trace integration. Highly optimized mode when using availability traces (only available for the Cas01 CPU model for now).",
155     NULL},
156   {"Full",
157    "Full update of remaining and variables. Slow but may be useful when debugging.",
158    NULL},
159   {NULL, NULL, NULL}      /* this array must be NULL terminated */
160 };
161
162 s_surf_model_description_t surf_storage_model_description[] = {
163   {"default",
164    "Simplistic storage model.",
165    surf_storage_model_init_default},
166   {NULL, NULL,  NULL}      /* this array must be NULL terminated */
167 };
168
169 #ifdef CONTEXT_THREADS
170 static xbt_parmap_t surf_parmap = NULL; /* parallel map on models */
171 #endif
172
173 double NOW = 0;
174 double *surf_mins = NULL; /* return value of share_resources for each model */
175 int surf_min_index;       /* current index in surf_mins */
176 double surf_min;               /* duration determined by surf_solve */
177
178 double surf_get_clock(void)
179 {
180   return NOW;
181 }
182
183 #ifdef _XBT_WIN32
184 # define FILE_DELIM "\\"
185 #else
186 # define FILE_DELIM "/"         /* FIXME: move to better location */
187 #endif
188
189 FILE *surf_fopen(const char *name, const char *mode)
190 {
191   unsigned int cpt;
192   char *path_elm = NULL;
193   char *buff;
194   FILE *file = NULL;
195
196   xbt_assert(name);
197
198   if (__surf_is_absolute_file_path(name))       /* don't mess with absolute file names */
199     return fopen(name, mode);
200
201   /* search relative files in the path */
202   xbt_dynar_foreach(surf_path, cpt, path_elm) {
203     buff = bprintf("%s" FILE_DELIM "%s", path_elm, name);
204     file = fopen(buff, mode);
205     free(buff);
206
207     if (file)
208       return file;
209   }
210   return NULL;
211 }
212
213 /*
214  * Returns the initial path. On Windows the initial path is
215  * the current directory for the current process in the other
216  * case the function returns "./" that represents the current
217  * directory on Unix/Linux platforms.
218  */
219
220 const char *__surf_get_initial_path(void)
221 {
222
223 #ifdef _XBT_WIN32
224   unsigned i;
225   char current_directory[MAX_PATH + 1] = { 0 };
226   unsigned int len = GetCurrentDirectory(MAX_PATH + 1, current_directory);
227   char root[4] = { 0 };
228
229   if (!len)
230     return NULL;
231
232   strncpy(root, current_directory, 3);
233
234   for (i = 0; i < MAX_DRIVE; i++) {
235     if (toupper(root[0]) == disk_drives_letter_table[i][0])
236       return disk_drives_letter_table[i];
237   }
238
239   return NULL;
240 #else
241   return "./";
242 #endif
243 }
244
245 /* The __surf_is_absolute_file_path() returns 1 if
246  * file_path is a absolute file path, in the other
247  * case the function returns 0.
248  */
249 int __surf_is_absolute_file_path(const char *file_path)
250 {
251 #ifdef _XBT_WIN32
252   WIN32_FIND_DATA wfd = { 0 };
253   HANDLE hFile = FindFirstFile(file_path, &wfd);
254
255   if (INVALID_HANDLE_VALUE == hFile)
256     return 0;
257
258   FindClose(hFile);
259   return 1;
260 #else
261   return (file_path[0] == '/');
262 #endif
263 }
264
265 /** Displays the long description of all registered models, and quit */
266 void model_help(const char *category, s_surf_model_description_t * table)
267 {
268   int i;
269   printf("Long description of the %s models accepted by this simulator:\n",
270          category);
271   for (i = 0; table[i].name; i++)
272     printf("  %s: %s\n", table[i].name, table[i].description);
273 }
274
275 int find_model_description(s_surf_model_description_t * table,
276                            const char *name)
277 {
278   int i;
279   char *name_list = NULL;
280
281   for (i = 0; table[i].name; i++)
282     if (!strcmp(name, table[i].name)) {
283       return i;
284     }
285   name_list = strdup(table[0].name);
286   for (i = 1; table[i].name; i++) {
287     name_list = (char *) xbt_realloc(name_list, strlen(name_list) + strlen(table[i].name) + 3);
288     strcat(name_list, ", ");
289     strcat(name_list, table[i].name);
290   }
291   xbt_die("Model '%s' is invalid! Valid models are: %s.", name, name_list);
292   return -1;
293 }
294
295 static XBT_INLINE void routing_asr_host_free(void *p)
296 {
297   delete ((RoutingEdgePtr) p);
298 }
299
300 static XBT_INLINE void routing_asr_prop_free(void *p)
301 {
302   xbt_dict_t elm = (xbt_dict_t) p;
303   xbt_dict_free(&elm);
304 }
305
306 static XBT_INLINE void surf_cpu_free(void *r)
307 {
308   delete dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(r));
309 }
310
311 static XBT_INLINE void surf_link_free(void *r)
312 {
313   delete dynamic_cast<NetworkLinkPtr>(static_cast<ResourcePtr>(r));
314 }
315
316 static XBT_INLINE void surf_workstation_free(void *r)
317 {
318   delete dynamic_cast<WorkstationPtr>(static_cast<ResourcePtr>(r));
319 }
320
321
322 void sg_version(int *ver_major,int *ver_minor,int *ver_patch) {
323   *ver_major = SIMGRID_VERSION_MAJOR;
324   *ver_minor = SIMGRID_VERSION_MINOR;
325   *ver_patch = SIMGRID_VERSION_PATCH;
326 }
327
328 void surf_init(int *argc, char **argv)
329 {
330   XBT_DEBUG("Create all Libs");
331   host_lib = xbt_lib_new();
332   link_lib = xbt_lib_new();
333   as_router_lib = xbt_lib_new();
334   storage_lib = xbt_lib_new();
335   storage_type_lib = xbt_lib_new();
336   watched_hosts_lib = xbt_dict_new_homogeneous(NULL);
337
338   XBT_DEBUG("Add routing levels");
339   ROUTING_HOST_LEVEL = xbt_lib_add_level(host_lib,routing_asr_host_free);
340   ROUTING_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,routing_asr_host_free);
341   ROUTING_PROP_ASR_LEVEL = xbt_lib_add_level(as_router_lib,routing_asr_prop_free);
342
343   XBT_DEBUG("Add SURF levels");
344   SURF_CPU_LEVEL = xbt_lib_add_level(host_lib,surf_cpu_free);
345   SURF_WKS_LEVEL = xbt_lib_add_level(host_lib,surf_workstation_free);
346   SURF_LINK_LEVEL = xbt_lib_add_level(link_lib,surf_link_free);
347
348   xbt_init(argc, argv);
349   if (!model_list)
350     model_list = xbt_dynar_new(sizeof(ModelPtr), NULL);
351   if (!model_list_invoke)
352     model_list_invoke = xbt_dynar_new(sizeof(ModelPtr), NULL);
353   if (!history)
354     history = tmgr_history_new();
355
356 #ifdef HAVE_TRACING
357   TRACE_add_start_function(TRACE_surf_alloc);
358   TRACE_add_end_function(TRACE_surf_release);
359 #endif
360
361   sg_config_init(argc, argv);
362
363   if (MC_is_active())
364     MC_memory_init();
365 }
366
367 void surf_exit(void)
368 {
369   unsigned int iter;
370   ModelPtr model = NULL;
371
372 #ifdef HAVE_TRACING
373   TRACE_end();                  /* Just in case it was not called by the upper
374                                  * layer (or there is no upper layer) */
375 #endif
376
377   sg_config_finalize();
378
379   xbt_dynar_foreach(model_list, iter, model)
380     delete model;
381   xbt_dynar_free(&model_list);
382   xbt_dynar_free(&model_list_invoke);
383   routing_exit();
384
385   if (maxmin_system) {
386     lmm_system_free(maxmin_system);
387     maxmin_system = NULL;
388   }
389   if (history) {
390     tmgr_history_free(history);
391     history = NULL;
392   }
393
394 #ifdef CONTEXT_THREADS
395   xbt_parmap_destroy(surf_parmap);
396 #endif
397
398   xbt_free(surf_mins);
399   surf_mins = NULL;
400
401   xbt_dynar_free(&host_that_restart);
402   xbt_dynar_free(&surf_path);
403
404   xbt_lib_free(&host_lib);
405   xbt_lib_free(&link_lib);
406   xbt_lib_free(&as_router_lib);
407   xbt_lib_free(&storage_lib);
408   xbt_lib_free(&storage_type_lib);
409
410   xbt_dict_free(&watched_hosts_lib);
411
412   tmgr_finalize();
413   surf_parse_lex_destroy();
414   surf_parse_free_callbacks();
415
416   NOW = 0;                      /* Just in case the user plans to restart the simulation afterward */
417 }
418 /*********
419  * Model *
420  *********/
421
422 Model::Model(const char *name)
423   : p_maxminSystem(0),  p_name(name),
424     m_resOnCB(0), m_resOffCB(0),
425     m_actCancelCB(0), m_actSuspendCB(0), m_actResumeCB(0)
426 {
427   ActionPtr action = NULL;
428   p_readyActionSet = xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
429   p_runningActionSet = xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
430   p_failedActionSet = xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
431   p_doneActionSet = xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
432
433   p_modifiedSet = NULL;
434   p_actionHeap = NULL;
435   p_updateMechanism = UM_UNDEFINED;
436   m_selectiveUpdate = 0;
437 }
438
439 Model::~Model(){
440 xbt_swag_free(p_readyActionSet);
441 xbt_swag_free(p_runningActionSet);
442 xbt_swag_free(p_failedActionSet);
443 xbt_swag_free(p_doneActionSet);
444 }
445
446 double Model::shareResources(double now)
447 {
448   //FIXME: set the good function once and for all
449   if (p_updateMechanism == UM_LAZY)
450         return shareResourcesLazy(now);
451   else if (p_updateMechanism == UM_FULL)
452         return shareResourcesFull(now);
453   else
454         xbt_die("Invalid cpu update mechanism!");
455 }
456
457 double Model::shareResourcesLazy(double now)
458 {
459   ActionLmmPtr action = NULL;
460   double min = -1;
461   double value;
462
463   XBT_DEBUG
464       ("Before share resources, the size of modified actions set is %d",
465        xbt_swag_size(p_modifiedSet));
466
467   lmm_solve(p_maxminSystem);
468
469   XBT_DEBUG
470       ("After share resources, The size of modified actions set is %d",
471        xbt_swag_size(p_modifiedSet));
472
473   while((action = static_cast<ActionLmmPtr>(xbt_swag_extract(p_modifiedSet)))) {
474     int max_dur_flag = 0;
475
476     if (action->getStateSet() != p_runningActionSet)
477       continue;
478
479     /* bogus priority, skip it */
480     if (action->getPriority() <= 0)
481       continue;
482
483     action->updateRemainingLazy(now);
484
485     min = -1;
486     value = lmm_variable_getvalue(action->getVariable());
487     if (value > 0) {
488       if (action->getRemains() > 0) {
489         value = action->getRemains() / value;
490         min = now + value;
491       } else {
492         value = 0.0;
493         min = now;
494       }
495     }
496
497     if ((action->getMaxDuration() != NO_MAX_DURATION)
498         && (min == -1
499             || action->getStartTime() +
500             action->getMaxDuration() < min)) {
501       min = action->getStartTime() +
502           action->getMaxDuration();
503       max_dur_flag = 1;
504     }
505
506     XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
507         action->getStartTime(), now + value,
508         action->getMaxDuration());
509
510     if (min != -1) {
511       action->heapRemove(p_actionHeap);
512       action->heapInsert(p_actionHeap, min, max_dur_flag ? MAX_DURATION : NORMAL);
513       XBT_DEBUG("Insert at heap action(%p) min %lf now %lf", action, min,
514                 now);
515     } else DIE_IMPOSSIBLE;
516   }
517
518   //hereafter must have already the min value for this resource model
519   if (xbt_heap_size(p_actionHeap) > 0)
520     min = xbt_heap_maxkey(p_actionHeap) - now;
521   else
522     min = -1;
523
524   XBT_DEBUG("The minimum with the HEAP %lf", min);
525
526   return min;
527 }
528
529 double Model::shareResourcesFull(double /*now*/) {
530   THROW_UNIMPLEMENTED;
531 }
532
533
534 double Model::shareResourcesMaxMin(xbt_swag_t running_actions,
535                           lmm_system_t sys,
536                           void (*solve) (lmm_system_t))
537 {
538   void *_action = NULL;
539   ActionLmmPtr action = NULL;
540   double min = -1;
541   double value = -1;
542
543   solve(sys);
544
545   xbt_swag_foreach(_action, running_actions) {
546     action = dynamic_cast<ActionLmmPtr>(static_cast<ActionPtr>(_action));
547     value = lmm_variable_getvalue(action->getVariable());
548     if ((value > 0) || (action->getMaxDuration() >= 0))
549       break;
550   }
551
552   if (!_action)
553     return -1.0;
554
555   if (value > 0) {
556     if (action->getRemains() > 0)
557       min = action->getRemains() / value;
558     else
559       min = 0.0;
560     if ((action->getMaxDuration() >= 0) && (action->getMaxDuration() < min))
561       min = action->getMaxDuration();
562   } else
563     min = action->getMaxDuration();
564
565
566   for (_action = xbt_swag_getNext(static_cast<ActionPtr>(action), running_actions->offset);
567        _action;
568        _action = xbt_swag_getNext(static_cast<ActionPtr>(action), running_actions->offset)) {
569         action = dynamic_cast<ActionLmmPtr>(static_cast<ActionPtr>(_action));
570     value = lmm_variable_getvalue(action->getVariable());
571     if (value > 0) {
572       if (action->getRemains() > 0)
573         value = action->getRemains() / value;
574       else
575         value = 0.0;
576       if (value < min) {
577         min = value;
578         XBT_DEBUG("Updating min (value) with %p: %f", action, min);
579       }
580     }
581     if ((action->getMaxDuration() >= 0) && (action->getMaxDuration() < min)) {
582       min = action->getMaxDuration();
583       XBT_DEBUG("Updating min (duration) with %p: %f", action, min);
584     }
585   }
586   XBT_DEBUG("min value : %f", min);
587
588   return min;
589 }
590
591 void Model::updateActionsState(double now, double delta)
592 {
593   if (p_updateMechanism == UM_FULL)
594         updateActionsStateFull(now, delta);
595   else if (p_updateMechanism == UM_LAZY)
596         updateActionsStateLazy(now, delta);
597   else
598         xbt_die("Invalid cpu update mechanism!");
599 }
600
601 void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
602 {
603
604 }
605
606 void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
607 {
608
609 }
610
611
612 void Model::addTurnedOnCallback(ResourceCallback rc)
613 {
614   m_resOnCB = rc;
615 }
616
617 void Model::notifyResourceTurnedOn(ResourcePtr r)
618 {
619   m_resOnCB(r);
620 }
621
622 void Model::addTurnedOffCallback(ResourceCallback rc)
623 {
624   m_resOffCB = rc;
625 }
626
627 void Model::notifyResourceTurnedOff(ResourcePtr r)
628 {
629   m_resOffCB(r);
630 }
631
632 void Model::addActionCancelCallback(ActionCallback ac)
633 {
634   m_actCancelCB = ac;
635 }
636
637 void Model::notifyActionCancel(ActionPtr a)
638 {
639   m_actCancelCB(a);
640 }
641
642 void Model::addActionResumeCallback(ActionCallback ac)
643 {
644   m_actResumeCB = ac;
645 }
646
647 void Model::notifyActionResume(ActionPtr a)
648 {
649   m_actResumeCB(a);
650 }
651
652 void Model::addActionSuspendCallback(ActionCallback ac)
653 {
654   m_actSuspendCB = ac;
655 }
656
657 void Model::notifyActionSuspend(ActionPtr a)
658 {
659   m_actSuspendCB(a);
660 }
661
662
663 /************
664  * Resource *
665  ************/
666
667 Resource::Resource()
668 : p_name(NULL), p_properties(NULL), p_model(NULL)
669 {}
670
671 Resource::Resource(surf_model_t model, const char *name, xbt_dict_t props)
672  : p_name(xbt_strdup(name)), p_properties(props), p_model(model)
673  , m_running(true), m_stateCurrent(SURF_RESOURCE_ON)
674 {}
675
676 Resource::Resource(surf_model_t model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit)
677  : p_name(xbt_strdup(name)), p_properties(props), p_model(model)
678  , m_running(true), m_stateCurrent(stateInit)
679 {}
680
681 e_surf_resource_state_t Resource::getState()
682 {
683   return m_stateCurrent;
684 }
685
686 void Resource::setState(e_surf_resource_state_t state)
687 {
688   m_stateCurrent = state;
689 }
690
691 bool Resource::isOn()
692 {
693   return m_running;
694 }
695
696 void Resource::turnOn()
697 {
698   if (!m_running) {
699     m_running = true;
700     getModel()->notifyResourceTurnedOn(this);
701   }
702 }
703
704 void Resource::turnOff()
705 {
706   if (m_running) {
707     m_running = false;
708     getModel()->notifyResourceTurnedOff(this);
709   }
710 }
711
712 ResourceLmm::ResourceLmm()
713  : p_constraint(NULL)
714 {}
715
716 ResourceLmm::ResourceLmm(lmm_constraint_t constraint)
717  : p_constraint(constraint)
718 {}
719
720 /**********
721  * Action *
722  **********/
723
724 const char *surf_action_state_names[6] = {
725   "SURF_ACTION_READY",
726   "SURF_ACTION_RUNNING",
727   "SURF_ACTION_FAILED",
728   "SURF_ACTION_DONE",
729   "SURF_ACTION_TO_FREE",
730   "SURF_ACTION_NOT_IN_THE_SYSTEM"
731 };
732
733 Action::Action()
734 : m_refcount(1)
735 {}
736
737 Action::Action(ModelPtr model, double cost, bool failed)
738  : m_priority(1.0)
739  , m_failed(failed)
740  , m_start(surf_get_clock()), m_finish(-1.0)
741  , m_remains(cost)
742  , m_maxDuration(NO_MAX_DURATION)
743  , m_cost(cost)
744  , p_data(NULL)
745  , p_model(model)
746  , m_refcount(1)
747 {
748   #ifdef HAVE_TRACING
749     p_category = NULL;
750   #endif
751   p_stateHookup.prev = 0;
752   p_stateHookup.next = 0;
753   if (failed)
754     p_stateSet = getModel()->getFailedActionSet();
755   else
756     p_stateSet = getModel()->getRunningActionSet();
757
758   xbt_swag_insert(this, p_stateSet);
759 }
760
761 Action::~Action() {
762 #ifdef HAVE_TRACING
763   xbt_free(p_category);
764 #endif
765 }
766
767 void Action::finish() {
768     m_finish = surf_get_clock();
769 }
770
771 int Action::unref(){
772   DIE_IMPOSSIBLE;
773 }
774
775 void Action::cancel(){
776   DIE_IMPOSSIBLE;
777 }
778
779 void Action::recycle(){
780   DIE_IMPOSSIBLE;
781 }
782
783 e_surf_action_state_t Action::getState()
784 {
785   if (p_stateSet ==  getModel()->getReadyActionSet())
786     return SURF_ACTION_READY;
787   if (p_stateSet ==  getModel()->getRunningActionSet())
788     return SURF_ACTION_RUNNING;
789   if (p_stateSet ==  getModel()->getFailedActionSet())
790     return SURF_ACTION_FAILED;
791   if (p_stateSet ==  getModel()->getDoneActionSet())
792     return SURF_ACTION_DONE;
793   return SURF_ACTION_NOT_IN_THE_SYSTEM;
794 }
795
796 void Action::setState(e_surf_action_state_t state)
797 {
798   //surf_action_state_t action_state = &(action->model_type->states);
799   XBT_IN("(%p,%s)", this, surf_action_state_names[state]);
800   xbt_swag_remove(this, p_stateSet);
801
802   if (state == SURF_ACTION_READY)
803     p_stateSet = getModel()->getReadyActionSet();
804   else if (state == SURF_ACTION_RUNNING)
805     p_stateSet = getModel()->getRunningActionSet();
806   else if (state == SURF_ACTION_FAILED)
807     p_stateSet = getModel()->getFailedActionSet();
808   else if (state == SURF_ACTION_DONE)
809     p_stateSet = getModel()->getDoneActionSet();
810   else
811     p_stateSet = NULL;
812
813   if (p_stateSet)
814     xbt_swag_insert(this, p_stateSet);
815   XBT_OUT();
816 }
817
818 double Action::getStartTime()
819 {
820   return m_start;
821 }
822
823 double Action::getFinishTime()
824 {
825   /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
826   return m_remains == 0 ? m_finish : -1;
827 }
828
829 double Action::getRemains()
830 {
831   XBT_IN("(%p)", this);
832   XBT_OUT();
833   return m_remains;
834 }
835
836 void Action::setData(void* data)
837 {
838   p_data = data;
839 }
840
841 #ifdef HAVE_TRACING
842 void Action::setCategory(const char *category)
843 {
844   XBT_IN("(%p,%s)", this, category);
845   p_category = xbt_strdup(category);
846   XBT_OUT();
847 }
848 #endif
849
850 void Action::ref(){
851   m_refcount++;
852 }
853
854 void ActionLmm::setMaxDuration(double duration)
855 {
856   XBT_IN("(%p,%g)", this, duration);
857   m_maxDuration = duration;
858   if (getModel()->getUpdateMechanism() == UM_LAZY)      // remove action from the heap
859     heapRemove(getModel()->getActionHeap());
860   XBT_OUT();
861 }
862
863 void ActionLmm::gapRemove() {}
864
865 void ActionLmm::setPriority(double priority)
866 {
867   XBT_IN("(%p,%g)", this, priority);
868   m_priority = priority;
869   lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), priority);
870
871   if (getModel()->getUpdateMechanism() == UM_LAZY)
872         heapRemove(getModel()->getActionHeap());
873   XBT_OUT();
874 }
875
876 void ActionLmm::cancel(){
877   setState(SURF_ACTION_FAILED);
878   if (getModel()->getUpdateMechanism() == UM_LAZY) {
879     xbt_swag_remove(this, getModel()->getModifiedSet());
880     heapRemove(getModel()->getActionHeap());
881   }
882 }
883
884 int ActionLmm::unref(){
885   m_refcount--;
886   if (!m_refcount) {
887         xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
888         if (getVariable())
889           lmm_variable_free(getModel()->getMaxminSystem(), getVariable());
890         if (getModel()->getUpdateMechanism() == UM_LAZY) {
891           /* remove from heap */
892           heapRemove(getModel()->getActionHeap());
893           xbt_swag_remove(this, getModel()->getModifiedSet());
894     }
895         delete this;
896         return 1;
897   }
898   return 0;
899 }
900
901 void ActionLmm::suspend()
902 {
903   XBT_IN("(%p)", this);
904   if (m_suspended != 2) {
905     lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), 0.0);
906     m_suspended = 1;
907     if (getModel()->getUpdateMechanism() == UM_LAZY)
908       heapRemove(getModel()->getActionHeap());
909   }
910   XBT_OUT();
911 }
912
913 void ActionLmm::resume()
914 {
915   XBT_IN("(%p)", this);
916   if (m_suspended != 2) {
917     lmm_update_variable_weight(getModel()->getMaxminSystem(), getVariable(), m_priority);
918     m_suspended = 0;
919     if (getModel()->getUpdateMechanism() == UM_LAZY)
920       heapRemove(getModel()->getActionHeap());
921   }
922   XBT_OUT();
923 }
924
925 bool ActionLmm::isSuspended()
926 {
927   return m_suspended == 1;
928 }
929 /* insert action on heap using a given key and a hat (heap_action_type)
930  * a hat can be of three types for communications:
931  *
932  * NORMAL = this is a normal heap entry stating the date to finish transmitting
933  * LATENCY = this is a heap entry to warn us when the latency is payed
934  * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
935  */
936 void ActionLmm::heapInsert(xbt_heap_t heap, double key, enum heap_action_type hat)
937 {
938   m_hat = hat;
939   xbt_heap_push(heap, this, key);
940 }
941
942 void ActionLmm::heapRemove(xbt_heap_t heap)
943 {
944   m_hat = NOTSET;
945   if (m_indexHeap >= 0) {
946     xbt_heap_remove(heap, m_indexHeap);
947   }
948 }
949
950 /* added to manage the communication action's heap */
951 void surf_action_lmm_update_index_heap(void *action, int i) {
952   ((ActionLmmPtr)action)->updateIndexHeap(i);
953 }
954
955 void ActionLmm::updateIndexHeap(int i) {
956   m_indexHeap = i;
957 }
958
959 double ActionLmm::getRemains()
960 {
961   XBT_IN("(%p)", this);
962   /* update remains before return it */
963   if (getModel()->getUpdateMechanism() == UM_LAZY)      /* update remains before return it */
964     updateRemainingLazy(surf_get_clock());
965   XBT_OUT();
966   return m_remains;
967 }
968
969 //FIXME split code in the right places
970 void ActionLmm::updateRemainingLazy(double now)
971 {
972   double delta = 0.0;
973
974   if(getModel() == static_cast<ModelPtr>(surf_network_model))
975   {
976     if (m_suspended != 0)
977       return;
978   }
979   else
980   {
981     xbt_assert(p_stateSet == getModel()->getRunningActionSet(),
982         "You're updating an action that is not running.");
983
984       /* bogus priority, skip it */
985     xbt_assert(m_priority > 0,
986         "You're updating an action that seems suspended.");
987   }
988
989   delta = now - m_lastUpdate;
990
991   if (m_remains > 0) {
992     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
993     double_update(&m_remains, m_lastValue * delta);
994
995 #ifdef HAVE_TRACING
996     if (getModel() == static_cast<ModelPtr>(surf_cpu_model_pm) && TRACE_is_enabled()) {
997       ResourcePtr cpu = static_cast<ResourcePtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
998       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
999     }
1000 #endif
1001     XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
1002   }
1003
1004   if(getModel() == static_cast<ModelPtr>(surf_network_model))
1005   {
1006     if (m_maxDuration != NO_MAX_DURATION)
1007       double_update(&m_maxDuration, delta);
1008
1009     //FIXME: duplicated code
1010     if ((m_remains <= 0) &&
1011         (lmm_get_variable_weight(getVariable()) > 0)) {
1012       finish();
1013       setState(SURF_ACTION_DONE);
1014       heapRemove(getModel()->getActionHeap());
1015     } else if (((m_maxDuration != NO_MAX_DURATION)
1016         && (m_maxDuration <= 0))) {
1017       finish();
1018       setState(SURF_ACTION_DONE);
1019       heapRemove(getModel()->getActionHeap());
1020     }
1021   }
1022
1023   m_lastUpdate = now;
1024   m_lastValue = lmm_variable_getvalue(getVariable());
1025 }
1026
1027 /*void Action::cancel()
1028 {
1029   p_model->notifyActionCancel(this);
1030 }
1031
1032 void Action::suspend()
1033 {
1034   p_model->notifyActionSuspend(this);
1035 }
1036
1037 void Action::resume()
1038 {
1039   p_model->notifyActionResume(this);
1040 }
1041
1042 bool Action::isSuspended()
1043 {
1044   return false;
1045 }*/
1046