Logo AND Algorithmique Numérique Distribuée

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