Logo AND Algorithmique Numérique Distribuée

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