Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix initialization order.
[simgrid.git] / src / simix / smx_host.c
1 /* Copyright (c) 2007-2013. 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 "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix,
14                                 "Logging specific to SIMIX (hosts)");
15
16 static void SIMIX_execution_finish(smx_action_t action);
17
18 /**
19  * \brief Internal function to create a SIMIX host.
20  * \param name name of the host to create
21  * \param workstation the SURF workstation to encapsulate
22  * \param data some user data (may be NULL)
23  */
24 smx_host_t SIMIX_host_create(const char *name,
25                                void *workstation, void *data)
26 {
27   smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
28   s_smx_process_t proc;
29
30   /* Host structure */
31   smx_host->data = data;
32   smx_host->process_list =
33       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
34
35   /* Update global variables */
36   xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
37
38   return xbt_lib_get_or_null(host_lib, name, SIMIX_HOST_LEVEL);
39 }
40
41 void SIMIX_pre_host_on(smx_simcall_t simcall, smx_host_t h)
42 {
43   SIMIX_host_on(h);
44 }
45
46 /**
47  * \brief Start the host if it is off
48  *
49  */
50 void SIMIX_host_on(smx_host_t h)
51 {
52   smx_host_priv_t host = SIMIX_host_priv(h);
53
54   xbt_assert((host != NULL), "Invalid parameters");
55
56   if (surf_resource_get_state(surf_workstation_resource_priv(h))==SURF_RESOURCE_OFF) {
57         surf_resource_set_state(surf_workstation_resource_priv(h), SURF_RESOURCE_ON);
58     unsigned int cpt;
59     smx_process_arg_t arg;
60     xbt_dynar_foreach(host->boot_processes,cpt,arg) {
61       smx_process_t process;
62
63       XBT_DEBUG("Booting Process %s(%s) right now", arg->argv[0], arg->hostname);
64       if (simix_global->create_process_function) {
65         simix_global->create_process_function(&process,
66                                               arg->argv[0],
67                                               arg->code,
68                                               NULL,
69                                               arg->hostname,
70                                               arg->kill_time,
71                                               arg->argc,
72                                               arg->argv,
73                                               arg->properties,
74                                               arg->auto_restart);
75       }
76       else {
77         simcall_process_create(&process,
78                                               arg->argv[0],
79                                               arg->code,
80                                               NULL,
81                                               arg->hostname,
82                                               arg->kill_time,
83                                               arg->argc,
84                                               arg->argv,
85                                               arg->properties,
86                                               arg->auto_restart);
87       }
88     }
89   }
90 }
91
92 void SIMIX_pre_host_off(smx_simcall_t simcall, smx_host_t h)
93 {
94   SIMIX_host_off(h, simcall->issuer);
95 }
96
97 /**
98  * \brief Stop the host if it is on
99  *
100  */
101 void SIMIX_host_off(smx_host_t h, smx_process_t issuer)
102 {
103   smx_host_priv_t host = SIMIX_host_priv(h);
104
105   xbt_assert((host != NULL), "Invalid parameters");
106   
107   if (surf_resource_get_state(surf_workstation_resource_priv(h))==SURF_RESOURCE_OFF) {
108         surf_resource_set_state(surf_workstation_resource_priv(h), SURF_RESOURCE_ON);
109
110     /* Clean Simulator data */
111     if (xbt_swag_size(host->process_list) != 0) {
112       smx_process_t process = NULL;
113       xbt_swag_foreach(process, host->process_list) {
114         SIMIX_process_kill(process, issuer);
115         XBT_DEBUG("Killing %s on %s by %s", process->name,  sg_host_name(process->smx_host), issuer->name);
116       }
117     }
118   }
119 }
120
121 /**
122  * \brief Internal function to destroy a SIMIX host.
123  *
124  * \param h the host to destroy (a smx_host_t)
125  */
126 void SIMIX_host_destroy(void *h)
127 {
128   smx_host_priv_t host = (smx_host_priv_t) h;
129
130   xbt_assert((host != NULL), "Invalid parameters");
131
132   /* Clean Simulator data */
133   if (xbt_swag_size(host->process_list) != 0) {
134     char *msg = xbt_strdup("Shutting down host, but it's not empty:");
135     char *tmp;
136     smx_process_t process = NULL;
137
138     xbt_swag_foreach(process, host->process_list) {
139       tmp = bprintf("%s\n\t%s", msg, process->name);
140       free(msg);
141       msg = tmp;
142     }
143     SIMIX_display_process_status();
144     THROWF(arg_error, 0, "%s", msg);
145   }
146   xbt_dynar_free(&host->auto_restart_processes);
147   xbt_dynar_free(&host->boot_processes);
148   xbt_swag_free(host->process_list);
149
150   /* Clean host structure */
151   free(host); 
152   return;
153 }
154
155 ///**
156 // * \brief Returns a dict of all hosts.
157 // *
158 // * \return List of all hosts (as a #xbt_dict_t)
159 // */
160 //xbt_dict_t SIMIX_host_get_dict(void)
161 //{
162 //  xbt_dict_t host_dict = xbt_dict_new_homogeneous(NULL);
163 //  xbt_lib_cursor_t cursor = NULL;
164 //  char *name = NULL;
165 //  void **host = NULL;
166 //
167 //  xbt_lib_foreach(host_lib, cursor, name, host){
168 //    if(host[SIMIX_HOST_LEVEL])
169 //            xbt_dict_set(host_dict,name,host[SIMIX_HOST_LEVEL], NULL);
170 //  }
171 //  return host_dict;
172 //}
173 smx_host_t SIMIX_pre_host_get_by_name(smx_simcall_t simcall, const char *name){
174    return SIMIX_host_get_by_name(name);
175 }
176 smx_host_t SIMIX_host_get_by_name(const char *name){
177   xbt_assert(((simix_global != NULL)
178                && (host_lib != NULL)),
179               "Environment not set yet");
180
181   return xbt_lib_get_elm_or_null(host_lib, name);
182 }
183
184 smx_host_t SIMIX_host_self(void)
185 {
186   smx_process_t process = SIMIX_process_self();
187   return (process == NULL) ? NULL : SIMIX_process_get_host(process);
188 }
189
190 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
191    return SIMIX_host_self_get_name();
192 }
193 /* needs to be public and without simcall because it is called
194    by exceptions and logging events */
195 const char* SIMIX_host_self_get_name(void)
196 {
197   smx_host_t host = SIMIX_host_self();
198   if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
199     return "";
200
201   return SIMIX_host_get_name(host);
202 }
203
204 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
205    return SIMIX_host_get_name(host);
206 }
207 const char* SIMIX_host_get_name(smx_host_t host){
208   xbt_assert((host != NULL), "Invalid parameters");
209
210   return sg_host_name(host);
211 }
212
213 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
214   return SIMIX_host_get_properties(host);
215 }
216 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
217   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
218
219   return surf_resource_get_properties(surf_workstation_resource_priv(host));
220 }
221
222 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
223   return SIMIX_host_get_speed(host);
224 }
225 double SIMIX_host_get_speed(smx_host_t host){
226   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
227   return surf_workstation_get_speed(host, 1.0);
228 }
229
230 int SIMIX_pre_host_get_core(smx_simcall_t simcall, smx_host_t host){
231   return SIMIX_host_get_core(host);
232 }
233 int SIMIX_host_get_core(smx_host_t host){
234   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
235
236   return surf_workstation_get_core(host);
237 }
238
239 xbt_swag_t SIMIX_pre_host_get_process_list(smx_simcall_t simcall, smx_host_t host){
240   return SIMIX_host_get_process_list(host);
241 }
242
243 xbt_swag_t SIMIX_host_get_process_list(smx_host_t host){
244   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
245   smx_host_priv_t host_priv = SIMIX_host_priv(host);
246
247   return host_priv->process_list;
248 }
249
250
251 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
252   return SIMIX_host_get_available_speed(host);
253 }
254 double SIMIX_host_get_available_speed(smx_host_t host){
255   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
256
257   return surf_workstation_get_available_speed(host);
258 }
259
260 double SIMIX_pre_host_get_current_power_peak(smx_simcall_t simcall, smx_host_t host){
261   return SIMIX_host_get_current_power_peak(host);
262 }
263 double SIMIX_host_get_current_power_peak(smx_host_t host) {
264           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
265           return surf_workstation_get_current_power_peak(host);
266 }
267
268 double SIMIX_pre_host_get_power_peak_at(smx_simcall_t simcall, smx_host_t host, int pstate_index){
269   return SIMIX_host_get_power_peak_at(host, pstate_index);
270 }
271 double SIMIX_host_get_power_peak_at(smx_host_t host, int pstate_index) {
272           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
273
274           return surf_workstation_get_power_peak_at(host, pstate_index);
275 }
276
277 int SIMIX_pre_host_get_nb_pstates(smx_simcall_t simcall, smx_host_t host){
278   return SIMIX_host_get_nb_pstates(host);
279 }
280 int SIMIX_host_get_nb_pstates(smx_host_t host) {
281           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
282
283           return surf_workstation_get_nb_pstates(host);
284 }
285
286
287 void SIMIX_pre_host_set_power_peak_at(smx_simcall_t simcall, smx_host_t host, int pstate_index){
288   SIMIX_host_set_power_peak_at(host, pstate_index);
289 }
290 void SIMIX_host_set_power_peak_at(smx_host_t host, int pstate_index) {
291           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
292
293           surf_workstation_set_power_peak_at(host, pstate_index);
294 }
295
296 double SIMIX_pre_host_get_consumed_energy(smx_simcall_t simcall, smx_host_t host){
297   return SIMIX_host_get_consumed_energy(host);
298 }
299 double SIMIX_host_get_consumed_energy(smx_host_t host) {
300           xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
301           return surf_workstation_get_consumed_energy(host);
302 }
303
304 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
305   return SIMIX_host_get_state(host);
306 }
307 int SIMIX_host_get_state(smx_host_t host){
308   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
309
310   return surf_resource_get_state(surf_workstation_resource_priv(host));
311 }
312
313 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
314   return SIMIX_host_self_get_data();
315 }
316 void* SIMIX_host_self_get_data(void)
317 {
318   smx_host_t self = SIMIX_host_self();
319   return SIMIX_host_get_data(self);
320 }
321
322 void SIMIX_host_self_set_data(void *data)
323 {
324   smx_host_t self = SIMIX_host_self();
325   SIMIX_host_set_data(self, data);
326 }
327
328 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
329   return SIMIX_host_get_data(host);
330 }
331 void* SIMIX_host_get_data(smx_host_t host){
332   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
333
334   return SIMIX_host_priv(host)->data;
335 }
336
337 void _SIMIX_host_free_process_arg(void *data)
338 {
339   smx_process_arg_t arg = *(void**)data;
340   int i;
341   for (i = 0; i < arg->argc; i++)
342     xbt_free(arg->argv[i]);
343   xbt_free(arg->argv);
344   xbt_free(arg->name);
345   xbt_free(arg);
346 }
347 /**
348  * \brief Add a process to the list of the processes that the host will restart when it comes back
349  * This function add a process to the list of the processes that will be restarted when the host comes
350  * back. It is expected that this function is called when the host is down.
351  * The processes will only be restarted once, meaning that you will have to register the process
352  * again to restart the process again.
353  */
354 void SIMIX_host_add_auto_restart_process(smx_host_t host,
355                                          const char *name,
356                                          xbt_main_func_t code,
357                                          void *data,
358                                          const char *hostname,
359                                          double kill_time,
360                                          int argc, char **argv,
361                                          xbt_dict_t properties,
362                                          int auto_restart)
363 {
364   if (!SIMIX_host_priv(host)->auto_restart_processes) {
365     SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
366   }
367   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
368   arg->name = xbt_strdup(name);
369   arg->code = code;
370   arg->data = data;
371   arg->hostname = hostname;
372   arg->kill_time = kill_time;
373   arg->argc = argc;
374
375   arg->argv = xbt_new(char*,argc + 1);
376
377   int i;
378   for (i = 0; i < argc; i++) {
379     arg->argv[i] = xbt_strdup(argv[i]);
380   }
381   arg->argv[argc] = NULL;
382
383   arg->properties = properties;
384   arg->auto_restart = auto_restart;
385
386   if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
387       && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
388     xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
389     XBT_DEBUG("Have pushed host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
390   }
391   xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
392 }
393 /**
394  * \brief Restart the list of processes that have been registered to the host
395  */
396 void SIMIX_host_restart_processes(smx_host_t host)
397 {
398   unsigned int cpt;
399   smx_process_arg_t arg;
400   xbt_dynar_t process_list = SIMIX_host_priv(host)->auto_restart_processes;
401   if (!process_list)
402     return;
403
404   xbt_dynar_foreach (process_list, cpt, arg) {
405
406     smx_process_t process;
407
408     XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
409     if (simix_global->create_process_function) {
410       simix_global->create_process_function(&process,
411                                             arg->argv[0],
412                                             arg->code,
413                                             NULL,
414                                             arg->hostname,
415                                             arg->kill_time,
416                                             arg->argc,
417                                             arg->argv,
418                                             arg->properties,
419                                             arg->auto_restart);
420     } else {
421       simcall_process_create(&process,
422                              arg->argv[0],
423                              arg->code,
424                              NULL,
425                              arg->hostname,
426                              arg->kill_time,
427                              arg->argc,
428                              arg->argv,
429                              arg->properties,
430                              arg->auto_restart);
431
432     }
433     /* arg->argv is used by the process created above.  Hide it to
434      * _SIMIX_host_free_process_arg() which is called by xbt_dynar_reset()
435      * below. */
436     arg->argc = 0;
437     arg->argv = NULL;
438   }
439   xbt_dynar_reset(process_list);
440 }
441
442 void SIMIX_host_autorestart(smx_host_t host)
443 {
444   if(simix_global->autorestart)
445     simix_global->autorestart(host);
446   else
447     xbt_die("No function for simix_global->autorestart");
448 }
449
450 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
451   SIMIX_host_set_data(host, data);
452 }
453 void SIMIX_host_set_data(smx_host_t host, void *data){
454   xbt_assert((host != NULL), "Invalid parameters");
455   xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
456
457   SIMIX_host_priv(host)->data = data;
458 }
459
460 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
461     smx_host_t host, double computation_amount, double priority, double bound, unsigned long affinity_mask){
462   return SIMIX_host_execute(name, host, computation_amount, priority, bound, affinity_mask);
463 }
464 smx_action_t SIMIX_host_execute(const char *name,
465     smx_host_t host, double computation_amount, double priority, double bound, unsigned long affinity_mask){
466
467   /* alloc structures and initialize */
468   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
469   action->type = SIMIX_ACTION_EXECUTE;
470   action->name = xbt_strdup(name);
471   action->state = SIMIX_RUNNING;
472   action->execution.host = host;
473
474 #ifdef HAVE_TRACING
475   action->category = NULL;
476 #endif
477
478   /* set surf's action */
479   if (!MC_is_active()) {
480
481     action->execution.surf_exec = surf_workstation_execute(host, computation_amount);
482     surf_action_set_data(action->execution.surf_exec, action);
483     surf_action_set_priority(action->execution.surf_exec, priority);
484
485     /* Note (hypervisor): for multicore, the bound value being passed to the
486      * surf layer should not be zero (i.e., unlimited). It should be the
487      * capacity of a CPU core. */
488     if (bound == 0)
489       surf_cpu_action_set_bound(action->execution.surf_exec, SIMIX_host_get_speed(host));
490     else
491       surf_cpu_action_set_bound(action->execution.surf_exec, bound);
492
493     if (affinity_mask != 0) {
494       /* just a double check to confirm that this host is the host where this task is running. */
495       xbt_assert(action->execution.host == host);
496       surf_cpu_action_set_affinity(action->execution.surf_exec, host, affinity_mask);
497     }
498   }
499
500   XBT_DEBUG("Create execute action %p", action);
501
502   return action;
503 }
504
505 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
506     int host_nb, smx_host_t *host_list,
507     double *computation_amount, double *communication_amount,
508     double amount, double rate){
509   return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
510                                      communication_amount, amount, rate);
511 }
512 smx_action_t SIMIX_host_parallel_execute(const char *name,
513     int host_nb, smx_host_t *host_list,
514     double *computation_amount, double *communication_amount,
515     double amount, double rate){
516
517   void **workstation_list = NULL;
518   int i;
519
520   /* alloc structures and initialize */
521   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
522   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
523   action->name = xbt_strdup(name);
524   action->state = SIMIX_RUNNING;
525   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
526
527 #ifdef HAVE_TRACING
528   action->category = NULL;
529 #endif
530
531   /* set surf's action */
532   workstation_list = xbt_new0(void *, host_nb);
533   for (i = 0; i < host_nb; i++)
534     workstation_list[i] = surf_workstation_resource_priv(host_list[i]);
535
536
537   /* FIXME: what happens if host_list contains VMs and PMs. If
538    * execute_parallel_task() does not change the state of the model, we can mix
539    * them. */
540   surf_model_t ws_model = surf_resource_model(host_list[0], SURF_WKS_LEVEL);
541   for (i = 1; i < host_nb; i++) {
542     surf_model_t ws_model_tmp = surf_resource_model(host_list[i], SURF_WKS_LEVEL);
543     if (ws_model_tmp != ws_model) {
544       XBT_CRITICAL("mixing VMs and PMs is not supported");
545       DIE_IMPOSSIBLE;
546     }
547   }
548
549   /* set surf's action */
550   if (!MC_is_active()) {
551     action->execution.surf_exec =
552       surf_workstation_model_execute_parallel_task((surf_workstation_model_t)surf_workstation_model,
553                   host_nb, workstation_list, computation_amount, communication_amount, rate);
554
555     surf_action_set_data(action->execution.surf_exec, action);
556   }
557   XBT_DEBUG("Create parallel execute action %p", action);
558
559   return action;
560 }
561
562 //FIXME: REMOVE not used
563 static surf_model_t get_ws_model_from_action(smx_action_t action)
564 {
565   xbt_assert(action->type == SIMIX_ACTION_EXECUTE);
566   smx_host_t host = action->execution.host;
567   surf_model_t model = surf_resource_model(host, SURF_WKS_LEVEL);
568   return model;
569 }
570
571 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
572   SIMIX_host_execution_destroy(action);
573 }
574 void SIMIX_host_execution_destroy(smx_action_t action){
575   XBT_DEBUG("Destroy action %p", action);
576
577   if (action->execution.surf_exec) {
578     surf_action_unref(action->execution.surf_exec);
579     action->execution.surf_exec = NULL;
580   }
581   xbt_free(action->name);
582   xbt_mallocator_release(simix_global->action_mallocator, action);
583 }
584
585 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
586   SIMIX_host_execution_cancel(action);
587 }
588 void SIMIX_host_execution_cancel(smx_action_t action){
589   XBT_DEBUG("Cancel action %p", action);
590
591   if (action->execution.surf_exec)
592     surf_action_cancel(action->execution.surf_exec);
593 }
594
595 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
596   return SIMIX_host_execution_get_remains(action);
597 }
598 double SIMIX_host_execution_get_remains(smx_action_t action){
599   double result = 0.0;
600
601   if (action->state == SIMIX_RUNNING)
602     result = surf_action_get_remains(action->execution.surf_exec);
603
604   return result;
605 }
606
607 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
608   return SIMIX_host_execution_get_state(action);
609 }
610 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
611   return action->state;
612 }
613
614 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
615                                         double priority){
616   SIMIX_host_execution_set_priority(action, priority);
617 }
618 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
619
620   if(action->execution.surf_exec)
621         surf_action_set_priority(action->execution.surf_exec, priority);
622 }
623
624 void SIMIX_pre_host_execution_set_bound(smx_simcall_t simcall, smx_action_t action,
625                                         double bound){
626   SIMIX_host_execution_set_bound(action, bound);
627 }
628 void SIMIX_host_execution_set_bound(smx_action_t action, double bound){
629
630   if(action->execution.surf_exec)
631         surf_cpu_action_set_bound(action->execution.surf_exec, bound);
632 }
633
634 void SIMIX_pre_host_execution_set_affinity(smx_simcall_t simcall,
635     smx_action_t action, smx_host_t host, unsigned long mask){
636   SIMIX_host_execution_set_affinity(action, host, mask);
637 }
638 void SIMIX_host_execution_set_affinity(smx_action_t action, smx_host_t host, unsigned long mask){
639   xbt_assert(action->type == SIMIX_ACTION_EXECUTE);
640
641   if (action->execution.surf_exec) {
642     /* just a double check to confirm that this host is the host where this task is running. */
643     xbt_assert(action->execution.host == host);
644     surf_cpu_action_set_affinity(action->execution.surf_exec, host, mask);
645   }
646 }
647
648 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
649
650   XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
651
652   /* Associate this simcall to the action */
653   xbt_fifo_push(action->simcalls, simcall);
654   simcall->issuer->waiting_action = action;
655
656   /* set surf's action */
657   if (MC_is_active()) {
658     action->state = SIMIX_DONE;
659     SIMIX_execution_finish(action);
660     return;
661   }
662
663   /* If the action is already finished then perform the error handling */
664   if (action->state != SIMIX_RUNNING)
665     SIMIX_execution_finish(action);
666 }
667
668 void SIMIX_host_execution_suspend(smx_action_t action)
669 {
670   if(action->execution.surf_exec)
671     surf_action_suspend(action->execution.surf_exec);
672 }
673
674 void SIMIX_host_execution_resume(smx_action_t action)
675 {
676   if(action->execution.surf_exec)
677     surf_action_resume(action->execution.surf_exec);
678 }
679
680 void SIMIX_execution_finish(smx_action_t action)
681 {
682   xbt_fifo_item_t item;
683   smx_simcall_t simcall;
684
685   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
686
687     switch (action->state) {
688
689       case SIMIX_DONE:
690         /* do nothing, action done */
691   XBT_DEBUG("SIMIX_execution_finished: execution successful");
692         break;
693
694       case SIMIX_FAILED:
695         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
696         simcall->issuer->context->iwannadie = 1;
697         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
698         break;
699
700       case SIMIX_CANCELED:
701         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
702         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
703         break;
704
705       default:
706         xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
707             (int)action->state);
708     }
709     /* check if the host is down */
710     if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
711       simcall->issuer->context->iwannadie = 1;
712     }
713
714     simcall->issuer->waiting_action =    NULL;
715     simcall_host_execution_wait__set__result(simcall, action->state);
716     SIMIX_simcall_answer(simcall);
717   }
718
719   /* We no longer need it */
720   SIMIX_host_execution_destroy(action);
721 }
722
723
724 void SIMIX_post_host_execute(smx_action_t action)
725 {
726   if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
727                                                * for parallel tasks too */
728       surf_resource_get_state(surf_workstation_resource_priv(action->execution.host)) == SURF_RESOURCE_OFF) {
729     /* If the host running the action failed, notice it so that the asking
730      * process can be killed if it runs on that host itself */
731     action->state = SIMIX_FAILED;
732   } else if (surf_action_get_state(action->execution.surf_exec) == SURF_ACTION_FAILED) {
733     /* If the host running the action didn't fail, then the action was
734      * canceled */
735     action->state = SIMIX_CANCELED;
736   } else {
737     action->state = SIMIX_DONE;
738   }
739
740   if (action->execution.surf_exec) {
741     surf_action_unref(action->execution.surf_exec);
742     action->execution.surf_exec = NULL;
743   }
744
745   /* If there are simcalls associated with the action, then answer them */
746   if (xbt_fifo_size(action->simcalls)) {
747     SIMIX_execution_finish(action);
748   }
749 }
750
751
752 #ifdef HAVE_TRACING
753 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
754                             const char *category){
755   SIMIX_set_category(action, category);
756 }
757 void SIMIX_set_category(smx_action_t action, const char *category)
758 {
759   if (action->state != SIMIX_RUNNING) return;
760   if (action->type == SIMIX_ACTION_EXECUTE){
761     surf_action_set_category(action->execution.surf_exec, category);
762   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
763     surf_action_set_category(action->comm.surf_comm, category);
764   }
765 }
766 #endif
767
768 /**
769  * \brief Function to get the parameters of the given the SIMIX host.
770  *
771  * \param host the host to get_phys_host (a smx_host_t)
772  * \param param the parameter object space to be overwritten (a ws_params_t)
773  */
774 void SIMIX_host_get_params(smx_host_t ind_vm, ws_params_t params)
775 {
776   /* jump to ws_get_params(). */
777   surf_workstation_get_params(ind_vm, params);
778 }
779
780 void SIMIX_pre_host_get_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
781 {
782   SIMIX_host_get_params(ind_vm, params);
783 }
784
785 void SIMIX_host_set_params(smx_host_t ind_vm, ws_params_t params)
786 {
787   /* jump to ws_set_params(). */
788   surf_workstation_set_params(ind_vm, params);
789 }
790
791 void SIMIX_pre_host_set_params(smx_simcall_t simcall, smx_host_t ind_vm, ws_params_t params)
792 {
793   SIMIX_host_set_params(ind_vm, params);
794 }
795
796 xbt_dict_t SIMIX_pre_host_get_storage_list(smx_simcall_t simcall, smx_host_t host){
797   return SIMIX_host_get_storage_list(host);
798 }
799
800 xbt_dict_t SIMIX_host_get_storage_list(smx_host_t host){
801   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
802
803   return surf_workstation_get_storage_list(host);
804 }