1 /* Copyright (c) 2007-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14 "Logging specific to SIMIX (process)");
16 unsigned long simix_process_maxpid = 0;
19 * \brief Returns the current agent.
21 * This functions returns the currently running SIMIX process.
23 * \return The SIMIX process
25 XBT_INLINE smx_process_t SIMIX_process_self(void)
27 smx_context_t self_context = SIMIX_context_self();
29 return self_context ? SIMIX_context_get_process(self_context) : NULL;
33 * \brief Returns whether a process has pending asynchronous communications.
34 * \return true if there are asynchronous communications in this process
36 int SIMIX_process_has_pending_comms(smx_process_t process) {
38 return xbt_fifo_size(process->comms) > 0;
41 void SIMIX_pre_process_cleanup(smx_simcall_t simcall, smx_process_t process) {
42 SIMIX_process_cleanup(process);
45 * \brief Moves a process to the list of processes to destroy.
47 void SIMIX_process_cleanup(smx_process_t process)
49 XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
50 process->name, process, process->waiting_action);
52 /* cancel non-blocking communications */
54 while ((action = xbt_fifo_pop(process->comms))) {
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);
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;
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);
74 XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
77 SIMIX_comm_destroy(action);
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;
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);
90 SIMIX_comm_destroy(action);
93 xbt_die("Communication action %p is in my list but I'm not the sender "
94 "or the receiver", action);
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;
107 * Should be called some time to time to free the memory allocated for processes
108 * that have finished (or killed).
110 void SIMIX_process_empty_trash(void)
112 smx_process_t process = NULL;
114 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
115 SIMIX_context_free(process->context);
117 /* Free the exception allocated at creation time */
118 free(process->running_ctx);
119 xbt_dict_free(&process->properties);
121 xbt_fifo_free(process->comms);
123 xbt_dynar_free(&process->on_exit);
125 xbt_free(process->name);
131 * \brief Creates and runs the maestro process
133 void SIMIX_create_maestro_process()
135 smx_process_t maestro = NULL;
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;
146 if (SIMIX_process_self()) {
147 maestro->ppid = SIMIX_process_get_PID(SIMIX_process_self());
152 simix_global->maestro_process = maestro;
156 * \brief Stops a process.
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.
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
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),
172 arg->argc,arg->argv,arg->properties,
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);
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
185 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
187 smx_process_t process;
188 simix_global->create_process_function(
204 void SIMIX_pre_process_create(smx_simcall_t simcall,
205 smx_process_t *process,
207 xbt_main_func_t code,
209 const char *hostname,
211 int argc, char **argv,
212 xbt_dict_t properties,
214 return SIMIX_process_create(process, name, code, data, hostname,
215 kill_time, argc, argv, properties, auto_restart);
218 * \brief Internal function to create a process.
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.
224 * \return the process created
226 void SIMIX_process_create(smx_process_t *process,
228 xbt_main_func_t code,
230 const char *hostname,
232 int argc, char **argv,
233 xbt_dict_t properties,
237 smx_host_t host = SIMIX_host_get_by_name(hostname);
239 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
241 if (!SIMIX_host_get_state(host)) {
243 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
245 for (i = 0; i < argc; i++)
250 *process = xbt_new0(s_smx_process_t, 1);
252 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
254 (*process)->pid = simix_process_maxpid++;
255 (*process)->name = xbt_strdup(name);
256 (*process)->smx_host = host;
257 (*process)->data = data;
258 (*process)->comms = xbt_fifo_new();
259 (*process)->simcall.issuer = *process;
261 if (SIMIX_process_self()) {
262 (*process)->ppid = SIMIX_process_get_PID(SIMIX_process_self());
264 (*process)->ppid = -1;
267 /* Process data for auto-restart */
268 (*process)->auto_restart = auto_restart;
269 (*process)->code = code;
270 (*process)->argc = argc;
271 (*process)->argv = argv;
272 (*process)->kill_time = kill_time;
275 XBT_VERB("Create context %s", (*process)->name);
276 (*process)->context = SIMIX_context_new(code, argc, argv,
277 simix_global->cleanup_process_function, *process);
279 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
280 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
283 MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
287 (*process)->properties = properties;
289 /* Add the process to it's host process list */
290 xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
292 XBT_DEBUG("Start context '%s'", (*process)->name);
294 /* Now insert it in the global process list and in the process to run list */
295 xbt_swag_insert(*process, simix_global->process_list);
296 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
297 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
300 if (kill_time > SIMIX_get_clock()) {
301 if (simix_global->kill_process_function) {
302 XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
303 sg_host_name((*process)->smx_host), kill_time);
304 SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
310 * \brief Executes the processes from simix_global->process_to_run.
312 * The processes of simix_global->process_to_run are run (in parallel if
313 * possible). On exit, simix_global->process_to_run is empty, and
314 * simix_global->process_that_ran contains the list of processes that just ran.
315 * The two lists are swapped so, be careful when using them before and after a
316 * call to this function.
318 void SIMIX_process_runall(void)
320 SIMIX_context_runall();
322 xbt_dynar_t tmp = simix_global->process_that_ran;
323 simix_global->process_that_ran = simix_global->process_to_run;
324 simix_global->process_to_run = tmp;
325 xbt_dynar_reset(simix_global->process_to_run);
328 void SIMIX_pre_process_kill(smx_simcall_t simcall, smx_process_t process) {
329 SIMIX_process_kill(process, simcall->issuer);
332 * \brief Internal function to kill a SIMIX process.
334 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
335 * or directly for SIMIX internal purposes.
337 * \param process poor victim
338 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
340 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
342 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
344 process->context->iwannadie = 1;
345 process->blocked = 0;
346 process->suspended = 0;
347 /* FIXME: set doexception to 0 also? */
349 /* destroy the blocking action if any */
350 if (process->waiting_action) {
352 switch (process->waiting_action->type) {
354 case SIMIX_ACTION_EXECUTE:
355 case SIMIX_ACTION_PARALLEL_EXECUTE:
356 SIMIX_host_execution_destroy(process->waiting_action);
359 case SIMIX_ACTION_COMMUNICATE:
360 xbt_fifo_remove(process->comms, process->waiting_action);
361 SIMIX_comm_cancel(process->waiting_action);
362 SIMIX_comm_destroy(process->waiting_action);
365 case SIMIX_ACTION_SLEEP:
366 SIMIX_process_sleep_destroy(process->waiting_action);
369 case SIMIX_ACTION_SYNCHRO:
370 SIMIX_synchro_stop_waiting(process, &process->simcall);
371 SIMIX_synchro_destroy(process->waiting_action);
374 case SIMIX_ACTION_IO:
375 SIMIX_io_destroy(process->waiting_action);
378 /* **************************************/
379 /* TUTORIAL: New API */
380 case SIMIX_ACTION_NEW_API:
381 SIMIX_new_api_destroy(process->waiting_action);
383 /* **************************************/
387 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
388 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
393 void SIMIX_pre_process_killall(smx_simcall_t simcall, int reset_pid) {
394 SIMIX_process_killall(simcall->issuer, reset_pid);
397 * \brief Kills all running processes.
398 * \param issuer this one will not be killed
400 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
402 smx_process_t p = NULL;
404 while ((p = xbt_swag_extract(simix_global->process_list))) {
406 SIMIX_process_kill(p,issuer);
411 simix_process_maxpid = reset_pid;
413 SIMIX_context_runall();
415 SIMIX_process_empty_trash();
418 void SIMIX_pre_process_change_host(smx_simcall_t simcall, smx_process_t process,
421 process->new_host = dest;
423 void SIMIX_process_change_host(smx_process_t process,
426 xbt_assert((process != NULL), "Invalid parameters");
427 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
428 process->smx_host = dest;
429 xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
433 void SIMIX_pre_process_suspend(smx_simcall_t simcall, smx_process_t process)
435 smx_action_t action_suspend =
436 SIMIX_process_suspend(process, simcall->issuer);
438 if (process != simcall->issuer) {
439 SIMIX_simcall_answer(simcall);
441 xbt_fifo_push(action_suspend->simcalls, simcall);
442 process->waiting_action = action_suspend;
443 SIMIX_host_execution_suspend(process->waiting_action);
445 /* If we are suspending ourselves, then just do not finish the simcall now */
448 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
450 xbt_assert((process != NULL), "Invalid parameters");
452 if (process->suspended) {
453 XBT_DEBUG("Process '%s' is already suspended", process->name);
457 process->suspended = 1;
459 /* If we are suspending another process, and it is waiting on an action,
460 suspend its action. */
461 if (process != issuer) {
463 if (process->waiting_action) {
465 switch (process->waiting_action->type) {
467 case SIMIX_ACTION_EXECUTE:
468 case SIMIX_ACTION_PARALLEL_EXECUTE:
469 SIMIX_host_execution_suspend(process->waiting_action);
472 case SIMIX_ACTION_COMMUNICATE:
473 SIMIX_comm_suspend(process->waiting_action);
476 case SIMIX_ACTION_SLEEP:
477 SIMIX_process_sleep_suspend(process->waiting_action);
480 case SIMIX_ACTION_SYNCHRO:
481 /* Suspension is delayed to when the process is rescheduled. */
485 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
486 (int)process->waiting_action->type);
490 /* Suspension is delayed to when the process is rescheduled. */
494 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
495 return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
499 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
500 SIMIX_process_resume(process, simcall->issuer);
503 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
505 xbt_assert((process != NULL), "Invalid parameters");
507 XBT_IN("process = %p, issuer = %p", process, issuer);
509 if(process->context->iwannadie) {
510 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
514 if(!process->suspended) return;
515 process->suspended = 0;
517 /* If we are resuming another process, resume the action it was waiting for
518 if any. Otherwise add it to the list of process to run in the next round. */
519 if (process != issuer) {
521 if (process->waiting_action) {
523 switch (process->waiting_action->type) {
525 case SIMIX_ACTION_EXECUTE:
526 case SIMIX_ACTION_PARALLEL_EXECUTE:
527 SIMIX_host_execution_resume(process->waiting_action);
530 case SIMIX_ACTION_COMMUNICATE:
531 SIMIX_comm_resume(process->waiting_action);
534 case SIMIX_ACTION_SLEEP:
535 SIMIX_process_sleep_resume(process->waiting_action);
538 case SIMIX_ACTION_SYNCHRO:
539 /* I cannot resume it now. This is delayed to when the process is rescheduled at
540 * the end of the synchro. */
544 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
545 (int)process->waiting_action->type);
548 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
553 int SIMIX_process_get_maxpid(void) {
554 return simix_process_maxpid;
557 int SIMIX_pre_process_count(smx_simcall_t simcall){
558 return SIMIX_process_count();
560 int SIMIX_process_count(void)
562 return xbt_swag_size(simix_global->process_list);
565 int SIMIX_pre_process_get_PID(smx_simcall_t simcall, smx_process_t self){
566 return SIMIX_process_get_PID(self);
569 int SIMIX_process_get_PID(smx_process_t self){
576 int SIMIX_pre_process_get_PPID(smx_simcall_t simcall, smx_process_t self){
577 return SIMIX_process_get_PPID(self);
580 int SIMIX_process_get_PPID(smx_process_t self){
587 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
588 return SIMIX_process_self_get_data(self);
591 void* SIMIX_process_self_get_data(smx_process_t self)
593 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
598 return SIMIX_process_get_data(self);
601 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
603 SIMIX_process_set_data(process, data);
605 void SIMIX_process_self_set_data(smx_process_t self, void *data)
607 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
609 SIMIX_process_set_data(self, data);
612 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
613 return SIMIX_process_get_data(process);
615 void* SIMIX_process_get_data(smx_process_t process)
617 return process->data;
620 void SIMIX_process_set_data(smx_process_t process, void *data)
622 process->data = data;
625 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
626 return SIMIX_process_get_host(process);
628 smx_host_t SIMIX_process_get_host(smx_process_t process)
630 return process->smx_host;
633 /* needs to be public and without simcall because it is called
634 by exceptions and logging events */
635 const char* SIMIX_process_self_get_name(void) {
637 smx_process_t process = SIMIX_process_self();
638 if (process == NULL || process == simix_global->maestro_process)
641 return SIMIX_process_get_name(process);
644 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
645 return SIMIX_process_get_name(process);
647 const char* SIMIX_process_get_name(smx_process_t process)
649 return process->name;
652 smx_process_t SIMIX_process_get_by_name(const char* name)
656 xbt_swag_foreach(proc, simix_global->process_list)
658 if(!strcmp(name, proc->name))
664 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
665 return SIMIX_process_is_suspended(process);
667 int SIMIX_process_is_suspended(smx_process_t process)
669 return process->suspended;
672 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
673 return SIMIX_process_get_properties(process);
675 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
677 return process->properties;
680 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
682 if (MC_is_active()) {
683 MC_process_clock_add(simcall->issuer, duration);
684 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
685 SIMIX_simcall_answer(simcall);
688 smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
689 xbt_fifo_push(action->simcalls, simcall);
690 simcall->issuer->waiting_action = action;
693 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
696 smx_host_t host = process->smx_host;
698 /* check if the host is active */
699 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
700 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
704 action = xbt_mallocator_get(simix_global->action_mallocator);
705 action->type = SIMIX_ACTION_SLEEP;
708 action->category = NULL;
711 action->sleep.host = host;
712 action->sleep.surf_sleep =
713 surf_workstation_sleep(host, duration);
715 surf_action_set_data(action->sleep.surf_sleep, action);
716 XBT_DEBUG("Create sleep action %p", action);
721 void SIMIX_post_process_sleep(smx_action_t action)
723 smx_simcall_t simcall;
726 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
728 while ((simcall = xbt_fifo_shift(action->simcalls))) {
730 switch(surf_action_get_state(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;
737 case SURF_ACTION_DONE:
745 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
746 simcall->issuer->context->iwannadie = 1;
748 simcall_process_sleep__set__result(simcall, state);
749 simcall->issuer->waiting_action = NULL;
750 SIMIX_simcall_answer(simcall);
753 SIMIX_process_sleep_destroy(action);
756 void SIMIX_process_sleep_destroy(smx_action_t action)
758 XBT_DEBUG("Destroy action %p", action);
759 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
761 if (action->sleep.surf_sleep)
762 surf_action_unref(action->sleep.surf_sleep);
763 xbt_mallocator_release(simix_global->action_mallocator, action);
766 void SIMIX_process_sleep_suspend(smx_action_t action)
768 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
769 surf_action_suspend(action->sleep.surf_sleep);
772 void SIMIX_process_sleep_resume(smx_action_t action)
774 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
775 surf_action_resume(action->sleep.surf_sleep);
779 * \brief Calling this function makes the process to yield.
781 * Only the current process can call this function, giving back the control to
784 * \param self the current process
786 void SIMIX_process_yield(smx_process_t self)
788 XBT_DEBUG("Yield process '%s'", self->name);
790 /* Go into sleep and return control to maestro */
791 SIMIX_context_suspend(self->context);
793 /* Ok, maestro returned control to us */
794 XBT_DEBUG("Control returned to me: '%s'", self->name);
796 if (self->new_host) {
797 SIMIX_process_change_host(self, self->new_host);
798 self->new_host = NULL;
801 if (self->context->iwannadie){
802 XBT_DEBUG("I wanna die!");
803 SIMIX_process_stop(self);
806 if(self->suspended) {
807 xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
809 SIMIX_process_suspend(self,self);
812 if (self->doexception) {
813 XBT_DEBUG("Wait, maestro left me an exception");
814 self->doexception = 0;
820 /* callback: context fetching */
821 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
823 return SIMIX_process_self()->running_ctx;
826 /* callback: termination */
827 void SIMIX_process_exception_terminate(xbt_ex_t * e)
833 smx_context_t SIMIX_process_get_context(smx_process_t p) {
837 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
842 * \brief Returns the list of processes to run.
844 xbt_dynar_t SIMIX_process_get_runnable(void)
846 return simix_global->process_to_run;
850 * \brief Returns the process from PID.
852 smx_process_t SIMIX_process_from_PID(int PID)
855 xbt_swag_foreach(proc, simix_global->process_list)
863 /** @brief returns a dynar containg all currently existing processes */
864 xbt_dynar_t SIMIX_processes_as_dynar(void) {
866 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
867 xbt_swag_foreach(proc, simix_global->process_list) {
868 xbt_dynar_push(res,&proc);
874 void SIMIX_process_on_exit_runall(smx_process_t process) {
875 s_smx_process_exit_fun_t exit_fun;
877 while (!xbt_dynar_is_empty(process->on_exit)) {
878 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
879 (exit_fun.fun)(exit_fun.arg);
883 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
884 int_f_pvoid_t fun, void *data) {
885 SIMIX_process_on_exit(process, fun, data);
888 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
889 xbt_assert(process, "current process not found: are you in maestro context ?");
891 if (!process->on_exit) {
892 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
895 s_smx_process_exit_fun_t exit_fun = {fun, data};
897 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
900 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
902 SIMIX_process_auto_restart_set(process, auto_restart);
905 * \brief Sets the auto-restart status of the process.
906 * If set to 1, the process will be automatically restarted when its host
909 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
910 process->auto_restart = auto_restart;
913 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
914 return SIMIX_process_restart(process, simcall->issuer);
917 * \brief Restart a process.
918 * Restart a process, starting it again from the beginning.
920 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
921 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
922 //retrieve the arguments of the old process
923 //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
924 s_smx_process_arg_t arg;
925 arg.code = process->code;
926 arg.hostname = sg_host_name(process->smx_host);
927 arg.kill_time = process->kill_time;
928 arg.argc = process->argc;
929 arg.data = process->data;
931 arg.argv = xbt_new(char*,process->argc + 1);
932 for (i = 0; i < arg.argc; i++) {
933 arg.argv[i] = xbt_strdup(process->argv[i]);
935 arg.argv[process->argc] = NULL;
936 arg.properties = NULL;
937 arg.auto_restart = process->auto_restart;
938 //kill the old process
939 SIMIX_process_kill(process,issuer);
940 //start the new process
941 smx_process_t new_process;
942 if (simix_global->create_process_function) {
943 simix_global->create_process_function(&new_process,
955 simcall_process_create(&new_process,