Logo AND Algorithmique Numérique Distribuée

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