Logo AND Algorithmique Numérique Distribuée

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