Logo AND Algorithmique Numérique Distribuée

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