Logo AND Algorithmique Numérique Distribuée

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