Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add the load capping feature
[simgrid.git] / src / simix / smx_process.c
1 /* Copyright (c) 2007-2012. 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_process, simix,
14                                 "Logging specific to SIMIX (process)");
15
16 unsigned long simix_process_maxpid = 0;
17
18 /**
19  * \brief Returns the current agent.
20  *
21  * This functions returns the currently running SIMIX process.
22  *
23  * \return The SIMIX process
24  */
25 XBT_INLINE smx_process_t SIMIX_process_self(void)
26 {
27   smx_context_t self_context = SIMIX_context_self();
28
29   return self_context ? SIMIX_context_get_data(self_context) : NULL;
30 }
31
32 /**
33  * \brief Returns whether a process has pending asynchronous communications.
34  * \return true if there are asynchronous communications in this process
35  */
36 int SIMIX_process_has_pending_comms(smx_process_t process) {
37
38   return xbt_fifo_size(process->comms) > 0;
39 }
40
41 void SIMIX_pre_process_cleanup(smx_simcall_t simcall, smx_process_t process) {
42   SIMIX_process_cleanup(process);
43 }
44 /**
45  * \brief Moves a process to the list of processes to destroy.
46  */
47 void SIMIX_process_cleanup(smx_process_t process)
48 {
49   XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
50       process->name, process, process->waiting_action);
51
52   /* cancel non-blocking communications */
53   smx_action_t action;
54   while ((action = xbt_fifo_pop(process->comms))) {
55
56     /* make sure no one will finish the comm after this process is destroyed,
57      * because src_proc or dst_proc would be an invalid pointer */
58     SIMIX_comm_cancel(action);
59
60     if (action->comm.src_proc == process) {
61       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
62           action, action->comm.detached, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
63       action->comm.src_proc = NULL;
64
65       if (action->comm.detached) {
66          if (action->comm.refcount == 0) {
67            XBT_DEBUG("Increase the refcount before destroying it since it's detached");
68            /* I'm not supposed to destroy a detached comm from the sender side,
69             * unless there is no receiver matching the rdv */
70            action->comm.refcount++;
71            SIMIX_comm_destroy(action);
72          }
73          else {
74            XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
75          }
76       } else {
77         SIMIX_comm_destroy(action);
78       }
79     }
80     else if (action->comm.dst_proc == process){
81       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
82           action, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
83       action->comm.dst_proc = NULL;
84
85       if (action->comm.detached && action->comm.refcount == 1
86           && action->comm.src_proc != NULL) {
87         /* the comm will be freed right now, remove it from the sender */
88         xbt_fifo_remove(action->comm.src_proc->comms, action);
89       }
90       SIMIX_comm_destroy(action);
91     }
92     else {
93       xbt_die("Communication action %p is in my list but I'm not the sender "
94           "or the receiver", action);
95     }
96   }
97
98   xbt_swag_remove(process, simix_global->process_list);
99   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
100   xbt_swag_insert(process, simix_global->process_to_destroy);
101   process->context->iwannadie = 0;
102 }
103
104 /** 
105  * Garbage collection
106  *
107  * Should be called some time to time to free the memory allocated for processes
108  * that have finished (or killed).
109  */
110 void SIMIX_process_empty_trash(void)
111 {
112   smx_process_t process = NULL;
113
114   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
115     SIMIX_context_free(process->context);
116
117     /* Free the exception allocated at creation time */
118     free(process->running_ctx);
119     xbt_dict_free(&process->properties);
120
121     xbt_fifo_free(process->comms);
122
123     xbt_dynar_free(&process->on_exit);
124
125     free(process->name);
126     free(process);
127   }
128 }
129
130 /**
131  * \brief Creates and runs the maestro process
132  */
133 void SIMIX_create_maestro_process()
134 {
135   smx_process_t maestro = NULL;
136
137   /* Create maestro process and intilialize it */
138   maestro = xbt_new0(s_smx_process_t, 1);
139   maestro->pid = simix_process_maxpid++;
140   maestro->name = (char *) "";
141   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
142   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
143   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
144   maestro->simcall.issuer = maestro;
145
146   if (SIMIX_process_self()) {
147     maestro->ppid = SIMIX_process_get_PID(SIMIX_process_self());
148   } else {
149     maestro->ppid = -1;
150   }
151
152   simix_global->maestro_process = maestro;
153   return;
154 }
155 /**
156  * \brief Stops a process.
157  *
158  * Stops the process, execute all the registered on_exit functions,
159  * register it to the list of the process to restart if needed
160  * and stops its context.
161  */
162 void SIMIX_process_stop(smx_process_t arg) {
163   /* execute the on_exit functions */
164   SIMIX_process_on_exit_runall(arg);
165   /* Add the process to the list of process to restart, only if
166    * the host is down
167    */
168   if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
169     SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
170                                         sg_host_name(arg->smx_host),
171                                         arg->kill_time,
172                                         arg->argc,arg->argv,arg->properties,
173                                         arg->auto_restart);
174   }
175   XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->smx_host));
176   /* stop the context */
177   SIMIX_context_stop(arg->context);
178 }
179
180 /**
181  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
182  * This function frees the argument.
183  * \return the process created
184  */
185 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
186
187   smx_process_t process;
188   simix_global->create_process_function(
189       &process,
190       args->name,
191       args->code,
192       args->data,
193       args->hostname,
194       args->kill_time,
195       args->argc,
196       args->argv,
197       args->properties,
198       args->auto_restart);
199   xbt_free(args);
200   return process;
201 }
202
203
204 void SIMIX_pre_process_create(smx_simcall_t simcall,
205                           smx_process_t *process,
206                           const char *name,
207                           xbt_main_func_t code,
208                           void *data,
209                           const char *hostname,
210                           double kill_time,
211                           int argc, char **argv,
212                           xbt_dict_t properties,
213                           int auto_restart){
214   return SIMIX_process_create(process, name, code, data, hostname,
215                               kill_time, argc, argv, properties, auto_restart);
216 }
217 /**
218  * \brief Internal function to create a process.
219  *
220  * This function actually creates the process.
221  * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
222  * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
223  *
224  * \return the process created
225  */
226 void SIMIX_process_create(smx_process_t *process,
227                           const char *name,
228                           xbt_main_func_t code,
229                           void *data,
230                           const char *hostname,
231                           double kill_time,
232                           int argc, char **argv,
233                           xbt_dict_t properties,
234                           int auto_restart) {
235
236   *process = NULL;
237   smx_host_t host = SIMIX_host_get_by_name(hostname);
238
239   XBT_DEBUG("Start process %s on host '%s'", name, hostname);
240
241   if (!SIMIX_host_get_state(host)) {
242     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
243           hostname);
244   }
245   else {
246     *process = xbt_new0(s_smx_process_t, 1);
247
248     xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
249     /* Process data */
250     (*process)->pid = simix_process_maxpid++;
251     (*process)->name = xbt_strdup(name);
252     (*process)->smx_host = host;
253     (*process)->data = data;
254     (*process)->comms = xbt_fifo_new();
255     (*process)->simcall.issuer = *process;
256     
257      if (SIMIX_process_self()) {
258        (*process)->ppid = SIMIX_process_get_PID(SIMIX_process_self());
259      } else {
260        (*process)->ppid = -1;
261      }
262
263     /* Process data for auto-restart */
264     (*process)->auto_restart = auto_restart;
265     (*process)->code = code;
266     (*process)->argc = argc;
267     (*process)->argv = argv;
268     (*process)->kill_time = kill_time;
269
270
271     XBT_VERB("Create context %s", (*process)->name);
272     (*process)->context = SIMIX_context_new(code, argc, argv,
273       simix_global->cleanup_process_function, *process);
274
275     (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
276     XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
277
278     if(MC_is_active()){
279       MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
280     }
281
282     /* Add properties */
283     (*process)->properties = properties;
284
285     /* Add the process to it's host process list */
286     xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
287
288     XBT_DEBUG("Start context '%s'", (*process)->name);
289
290     /* Now insert it in the global process list and in the process to run list */
291     xbt_swag_insert(*process, simix_global->process_list);
292     XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
293     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
294   }
295
296   if (kill_time > SIMIX_get_clock()) {
297     if (simix_global->kill_process_function) {
298       XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
299           sg_host_name((*process)->smx_host), kill_time);
300       SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
301     }
302   }
303 }
304
305 /**
306  * \brief Executes the processes from simix_global->process_to_run.
307  *
308  * The processes of simix_global->process_to_run are run (in parallel if
309  * possible).  On exit, simix_global->process_to_run is empty, and
310  * simix_global->process_that_ran contains the list of processes that just ran.
311  * The two lists are swapped so, be careful when using them before and after a
312  * call to this function.
313  */
314 void SIMIX_process_runall(void)
315 {
316   SIMIX_context_runall();
317
318   xbt_dynar_t tmp = simix_global->process_that_ran;
319   simix_global->process_that_ran = simix_global->process_to_run;
320   simix_global->process_to_run = tmp;
321   xbt_dynar_reset(simix_global->process_to_run);
322 }
323
324 void SIMIX_pre_process_kill(smx_simcall_t simcall, smx_process_t process) {
325   SIMIX_process_kill(process, simcall->issuer);
326 }
327 /**
328  * \brief Internal function to kill a SIMIX process.
329  *
330  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
331  * or directly for SIMIX internal purposes.
332  *
333  * \param process poor victim
334  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
335  */
336 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
337
338   XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
339
340   process->context->iwannadie = 1;
341   process->blocked = 0;
342   process->suspended = 0;
343   /* FIXME: set doexception to 0 also? */
344
345   /* destroy the blocking action if any */
346   if (process->waiting_action) {
347
348     switch (process->waiting_action->type) {
349
350     case SIMIX_ACTION_EXECUTE:
351     case SIMIX_ACTION_PARALLEL_EXECUTE:
352       SIMIX_host_execution_destroy(process->waiting_action);
353       break;
354
355     case SIMIX_ACTION_COMMUNICATE:
356       xbt_fifo_remove(process->comms, process->waiting_action);
357       SIMIX_comm_cancel(process->waiting_action);
358       SIMIX_comm_destroy(process->waiting_action);
359       break;
360
361     case SIMIX_ACTION_SLEEP:
362       SIMIX_process_sleep_destroy(process->waiting_action);
363       break;
364
365     case SIMIX_ACTION_SYNCHRO:
366       SIMIX_synchro_stop_waiting(process, &process->simcall);
367       SIMIX_synchro_destroy(process->waiting_action);
368       break;
369
370     case SIMIX_ACTION_IO:
371       SIMIX_io_destroy(process->waiting_action);
372       break;
373
374       /* **************************************/
375       /* TUTORIAL: New API                    */
376     case SIMIX_ACTION_NEW_API:
377       SIMIX_new_api_destroy(process->waiting_action);
378       break;
379       /* **************************************/
380
381     }
382   }
383   if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
384     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
385   }
386
387 }
388
389 void SIMIX_pre_process_killall(smx_simcall_t simcall, int reset_pid) {
390   SIMIX_process_killall(simcall->issuer, reset_pid);
391 }
392 /**
393  * \brief Kills all running processes.
394  * \param issuer this one will not be killed
395  */
396 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
397 {
398   smx_process_t p = NULL;
399
400   while ((p = xbt_swag_extract(simix_global->process_list))) {
401     if (p != issuer) {
402       SIMIX_process_kill(p,issuer);
403     }
404   }
405
406   if (reset_pid > 0)
407     simix_process_maxpid = reset_pid;
408
409   SIMIX_context_runall();
410
411   SIMIX_process_empty_trash();
412 }
413
414 void SIMIX_pre_process_change_host(smx_simcall_t simcall, smx_process_t process,
415                                    smx_host_t dest)
416 {
417   process->new_host = dest;
418 }
419 void SIMIX_process_change_host(smx_process_t process,
420              smx_host_t dest)
421 {
422   xbt_assert((process != NULL), "Invalid parameters");
423   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
424   process->smx_host = dest;
425   xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
426 }
427
428
429 void SIMIX_pre_process_suspend(smx_simcall_t simcall, smx_process_t process)
430 {
431   smx_action_t action_suspend =
432       SIMIX_process_suspend(process, simcall->issuer);
433
434   if (process != simcall->issuer) {
435     SIMIX_simcall_answer(simcall);
436   } else {
437     xbt_fifo_push(action_suspend->simcalls, simcall);
438     process->waiting_action = action_suspend;
439     SIMIX_host_execution_suspend(process->waiting_action);
440   }
441   /* If we are suspending ourselves, then just do not finish the simcall now */
442 }
443
444 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
445 {
446   xbt_assert((process != NULL), "Invalid parameters");
447
448   if (process->suspended) {
449     XBT_DEBUG("Process '%s' is already suspended", process->name);
450     return NULL;
451   }
452
453   process->suspended = 1;
454
455   /* If we are suspending another process, and it is waiting on an action,
456      suspend its action. */
457   if (process != issuer) {
458
459     if (process->waiting_action) {
460
461       switch (process->waiting_action->type) {
462
463         case SIMIX_ACTION_EXECUTE:
464         case SIMIX_ACTION_PARALLEL_EXECUTE:
465           SIMIX_host_execution_suspend(process->waiting_action);
466           break;
467
468         case SIMIX_ACTION_COMMUNICATE:
469           SIMIX_comm_suspend(process->waiting_action);
470           break;
471
472         case SIMIX_ACTION_SLEEP:
473           SIMIX_process_sleep_suspend(process->waiting_action);
474           break;
475
476         case SIMIX_ACTION_SYNCHRO:
477           /* Suspension is delayed to when the process is rescheduled. */
478           break;
479
480         default:
481           xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
482               (int)process->waiting_action->type);
483       }
484       return NULL;
485     } else {
486       /* Suspension is delayed to when the process is rescheduled. */
487       return NULL;
488     }
489   } else {
490     /* FIXME: computation size is zero. Is it okay that bound is zero ? */
491     return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0);
492   }
493 }
494
495 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
496   SIMIX_process_resume(process, simcall->issuer);
497 }
498
499 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
500 {
501   xbt_assert((process != NULL), "Invalid parameters");
502
503   XBT_IN("process = %p, issuer = %p", process, issuer);
504
505   if(process->context->iwannadie) {
506     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
507     return;
508   }
509
510   if(!process->suspended) return;
511   process->suspended = 0;
512
513   /* If we are resuming another process, resume the action it was waiting for
514      if any. Otherwise add it to the list of process to run in the next round. */
515   if (process != issuer) {
516
517     if (process->waiting_action) {
518
519       switch (process->waiting_action->type) {
520
521         case SIMIX_ACTION_EXECUTE:          
522         case SIMIX_ACTION_PARALLEL_EXECUTE:
523           SIMIX_host_execution_resume(process->waiting_action);
524           break;
525
526         case SIMIX_ACTION_COMMUNICATE:
527           SIMIX_comm_resume(process->waiting_action);
528           break;
529
530         case SIMIX_ACTION_SLEEP:
531           SIMIX_process_sleep_resume(process->waiting_action);
532           break;
533
534         case SIMIX_ACTION_SYNCHRO:
535           /* I cannot resume it now. This is delayed to when the process is rescheduled at
536            * the end of the synchro. */
537           break;
538
539         default:
540           xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
541               (int)process->waiting_action->type);
542       }
543     }
544   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
545
546   XBT_OUT();
547 }
548
549 int SIMIX_process_get_maxpid(void) {
550   return simix_process_maxpid;
551 }
552
553 int SIMIX_pre_process_count(smx_simcall_t simcall){
554   return SIMIX_process_count();
555 }
556 int SIMIX_process_count(void)
557 {
558   return xbt_swag_size(simix_global->process_list);
559 }
560
561 int SIMIX_pre_process_get_PID(smx_simcall_t simcall, smx_process_t self){
562    return SIMIX_process_get_PID(self);
563 }
564
565 int SIMIX_process_get_PID(smx_process_t self){
566   if (self == NULL)
567     return 0;
568   else
569     return self->pid;
570 }
571
572 int SIMIX_pre_process_get_PPID(smx_simcall_t simcall, smx_process_t self){
573   return SIMIX_process_get_PPID(self);
574 }
575
576 int SIMIX_process_get_PPID(smx_process_t self){
577   if (self == NULL)
578     return 0;
579   else
580     return self->ppid;
581 }
582
583 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
584   return SIMIX_process_self_get_data(self);     
585 }
586
587 void* SIMIX_process_self_get_data(smx_process_t self)
588 {
589   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
590
591   if (!self) {
592     return NULL;
593   }
594   return SIMIX_process_get_data(self);
595 }
596
597 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
598                                 void *data){
599   SIMIX_process_set_data(process, data);
600 }
601 void SIMIX_process_self_set_data(smx_process_t self, void *data)
602 {
603   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
604
605   SIMIX_process_set_data(self, data);
606 }
607
608 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
609   return SIMIX_process_get_data(process);
610 }
611 void* SIMIX_process_get_data(smx_process_t process)
612 {
613   return process->data;
614 }
615
616 void SIMIX_process_set_data(smx_process_t process, void *data)
617 {
618   process->data = data;
619 }
620
621 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
622   return SIMIX_process_get_host(process);
623 }
624 smx_host_t SIMIX_process_get_host(smx_process_t process)
625 {
626   return process->smx_host;
627 }
628
629 /* needs to be public and without simcall because it is called
630    by exceptions and logging events */
631 const char* SIMIX_process_self_get_name(void) {
632
633   smx_process_t process = SIMIX_process_self();
634   if (process == NULL || process == simix_global->maestro_process)
635     return "";
636
637   return SIMIX_process_get_name(process);
638 }
639
640 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
641   return SIMIX_process_get_name(process);
642 }
643 const char* SIMIX_process_get_name(smx_process_t process)
644 {
645   return process->name;
646 }
647
648 smx_process_t SIMIX_process_get_by_name(const char* name)
649 {
650   smx_process_t proc;
651
652   xbt_swag_foreach(proc, simix_global->process_list)
653   {
654     if(!strcmp(name, proc->name))
655       return proc;
656   }
657   return NULL;
658 }
659
660 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
661   return SIMIX_process_is_suspended(process);
662 }
663 int SIMIX_process_is_suspended(smx_process_t process)
664 {
665   return process->suspended;
666 }
667
668 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
669   return SIMIX_process_get_properties(process);
670 }
671 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
672 {
673   return process->properties;
674 }
675
676 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
677 {
678   if (MC_is_active()) {
679     MC_process_clock_add(simcall->issuer, duration);
680     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
681     SIMIX_simcall_answer(simcall);
682     return;
683   }
684   smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
685   xbt_fifo_push(action->simcalls, simcall);
686   simcall->issuer->waiting_action = action;
687 }
688
689 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
690 {
691   smx_action_t action;
692   smx_host_t host = process->smx_host;
693   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
694
695   /* check if the host is active */
696   if (ws_model->extension.
697       workstation.get_state(host) != SURF_RESOURCE_ON) {
698     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
699            sg_host_name(host));
700   }
701
702   action = xbt_mallocator_get(simix_global->action_mallocator);
703   action->type = SIMIX_ACTION_SLEEP;
704   action->name = NULL;
705 #ifdef HAVE_TRACING
706   action->category = NULL;
707 #endif
708
709   action->sleep.host = host;
710   action->sleep.surf_sleep =
711       ws_model->extension.workstation.sleep(host, duration);
712
713   ws_model->action_data_set(action->sleep.surf_sleep, action);
714   XBT_DEBUG("Create sleep action %p", action);
715
716   return action;
717 }
718
719 void SIMIX_post_process_sleep(smx_action_t action)
720 {
721   smx_simcall_t simcall;
722   e_smx_state_t state;
723
724   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
725   smx_host_t host = action->sleep.host;
726   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
727
728   while ((simcall = xbt_fifo_shift(action->simcalls))) {
729
730     switch(ws_model->action_state_get(action->sleep.surf_sleep)){
731       case SURF_ACTION_FAILED:
732         simcall->issuer->context->iwannadie = 1;
733         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
734         state = SIMIX_SRC_HOST_FAILURE;
735         break;
736
737       case SURF_ACTION_DONE:
738         state = SIMIX_DONE;
739         break;
740
741       default:
742         THROW_IMPOSSIBLE;
743         break;
744     }
745     if (ws_model->extension.
746         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
747       simcall->issuer->context->iwannadie = 1;
748     }
749     simcall_process_sleep__set__result(simcall, state);
750     simcall->issuer->waiting_action = NULL;
751     SIMIX_simcall_answer(simcall);
752
753   }
754   SIMIX_process_sleep_destroy(action);
755 }
756
757 void SIMIX_process_sleep_destroy(smx_action_t action)
758 {
759   XBT_DEBUG("Destroy action %p", action);
760   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
761   smx_host_t host = action->sleep.host;
762   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
763
764   if (action->sleep.surf_sleep)
765     action->sleep.surf_sleep->model_obj->action_unref(action->sleep.surf_sleep);
766   xbt_mallocator_release(simix_global->action_mallocator, action);
767 }
768
769 void SIMIX_process_sleep_suspend(smx_action_t action)
770 {
771   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
772   smx_host_t host = action->sleep.host;
773   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
774
775   ws_model->suspend(action->sleep.surf_sleep);
776 }
777
778 void SIMIX_process_sleep_resume(smx_action_t action)
779 {
780   xbt_assert(action->type == SIMIX_ACTION_SLEEP);
781   smx_host_t host = action->sleep.host;
782   surf_model_t ws_model = surf_resource_model(host, SURF_WKS_LEVEL);
783   XBT_DEBUG("%p ws_model", ws_model);
784
785   ws_model->resume(action->sleep.surf_sleep);
786 }
787
788 /** 
789  * \brief Calling this function makes the process to yield.
790  *
791  * Only the current process can call this function, giving back the control to
792  * maestro.
793  *
794  * \param self the current process
795  */
796 void SIMIX_process_yield(smx_process_t self)
797 {
798   XBT_DEBUG("Yield process '%s'", self->name);
799
800   /* Go into sleep and return control to maestro */
801   SIMIX_context_suspend(self->context);
802
803   /* Ok, maestro returned control to us */
804   XBT_DEBUG("Control returned to me: '%s'", self->name);
805
806   if (self->new_host) {
807     SIMIX_process_change_host(self, self->new_host);
808     self->new_host = NULL;
809   }
810
811   if (self->context->iwannadie){
812     XBT_DEBUG("I wanna die!");
813     SIMIX_process_stop(self);
814   }
815
816   if(self->suspended) {
817     xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
818     self->suspended = 0;
819     SIMIX_process_suspend(self,self);
820   }
821
822   if (self->doexception) {
823     XBT_DEBUG("Wait, maestro left me an exception");
824     self->doexception = 0;
825     SMX_THROW();
826   }
827
828 }
829
830 /* callback: context fetching */
831 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
832 {
833   return SIMIX_process_self()->running_ctx;
834 }
835
836 /* callback: termination */
837 void SIMIX_process_exception_terminate(xbt_ex_t * e)
838 {
839   xbt_ex_display(e);
840   xbt_abort();
841 }
842
843 smx_context_t SIMIX_process_get_context(smx_process_t p) {
844   return p->context;
845 }
846
847 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
848   p->context = c;
849 }
850
851 /**
852  * \brief Returns the list of processes to run.
853  */
854 xbt_dynar_t SIMIX_process_get_runnable(void)
855 {
856   return simix_global->process_to_run;
857 }
858
859 /**
860  * \brief Returns the process from PID.
861  */
862 smx_process_t SIMIX_process_from_PID(int PID)
863 {
864   smx_process_t proc;
865   xbt_swag_foreach(proc, simix_global->process_list)
866   {
867    if(proc->pid == PID)
868    return proc;
869   }
870   return NULL;
871 }
872
873 /** @brief returns a dynar containg all currently existing processes */
874 xbt_dynar_t SIMIX_processes_as_dynar(void) {
875   smx_process_t proc;
876   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
877   xbt_swag_foreach(proc, simix_global->process_list) {
878     xbt_dynar_push(res,&proc);
879   }
880   return res;
881 }
882
883
884 void SIMIX_process_on_exit_runall(smx_process_t process) {
885   s_smx_process_exit_fun_t exit_fun;
886
887   while (!xbt_dynar_is_empty(process->on_exit)) {
888     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
889     (exit_fun.fun)(exit_fun.arg);
890   }
891 }
892
893 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
894                                int_f_pvoid_t fun, void *data) {
895   SIMIX_process_on_exit(process, fun, data);
896 }
897
898 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
899   xbt_assert(process, "current process not found: are you in maestro context ?");
900
901   if (!process->on_exit) {
902     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
903   }
904
905   s_smx_process_exit_fun_t exit_fun = {fun, data};
906
907   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
908 }
909
910 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
911                                         int auto_restart) {
912   SIMIX_process_auto_restart_set(process, auto_restart);        
913 }
914 /**
915  * \brief Sets the auto-restart status of the process.
916  * If set to 1, the process will be automatically restarted when its host
917  * comes back.
918  */
919 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
920   process->auto_restart = auto_restart;
921 }
922
923 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
924   return SIMIX_process_restart(process, simcall->issuer);       
925 }
926 /**
927  * \brief Restart a process.
928  * Restart a process, starting it again from the beginning.
929  */
930 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
931   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
932   //retrieve the arguments of the old process
933   //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
934   s_smx_process_arg_t arg;
935   arg.code = process->code;
936   arg.hostname = sg_host_name(process->smx_host);
937   arg.kill_time = process->kill_time;
938   arg.argc = process->argc;
939   arg.data = process->data;
940   int i;
941   arg.argv = xbt_new(char*,process->argc + 1);
942   for (i = 0; i < arg.argc; i++) {
943     arg.argv[i] = xbt_strdup(process->argv[i]);
944   }
945   arg.argv[process->argc] = NULL;
946   arg.properties = NULL;
947   arg.auto_restart = process->auto_restart;
948   //kill the old process
949   SIMIX_process_kill(process,issuer);
950   //start the new process
951   smx_process_t new_process;
952   if (simix_global->create_process_function) {
953     simix_global->create_process_function(&new_process,
954                                           arg.argv[0],
955                                           arg.code,
956                                           arg.data,
957                                           arg.hostname,
958                                           arg.kill_time,
959                                           arg.argc,
960                                           arg.argv,
961                                           arg.properties,
962                                           arg.auto_restart);
963   }
964   else {
965     simcall_process_create(&new_process,
966                                           arg.argv[0],
967                                           arg.code,
968                                           arg.data,
969                                           arg.hostname,
970                                           arg.kill_time,
971                                           arg.argc,
972                                           arg.argv,
973                                           arg.properties,
974                                           arg.auto_restart);
975
976   }
977   return new_process;
978 }