1 /* Copyright (c) 2007-2015. 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 "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
9 #include "xbt/sysdep.h"
13 #include "src/mc/mc_replay.h"
14 #include "src/mc/Client.hpp"
15 #include "src/msg/msg_private.h"
17 #include "src/simix/SynchroSleep.hpp"
18 #include "src/simix/SynchroRaw.hpp"
19 #include "src/simix/SynchroIo.hpp"
22 #include "src/smpi/private.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
27 unsigned long simix_process_maxpid = 0;
30 * \brief Returns the current agent.
32 * This functions returns the currently running SIMIX process.
34 * \return The SIMIX process
36 smx_process_t SIMIX_process_self(void)
38 smx_context_t self_context = SIMIX_context_self();
40 return self_context ? self_context->process() : NULL;
44 * \brief Returns whether a process has pending asynchronous communications.
45 * \return true if there are asynchronous communications in this process
47 int SIMIX_process_has_pending_comms(smx_process_t process) {
49 return xbt_fifo_size(process->comms) > 0;
53 * \brief Moves a process to the list of processes to destroy.
55 void SIMIX_process_cleanup(smx_process_t process)
57 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
58 process->name, process, process->waiting_synchro);
60 SIMIX_process_on_exit_runall(process);
62 /* Unregister from the kill timer if any */
63 if (process->kill_timer != NULL)
64 SIMIX_timer_remove(process->kill_timer);
66 xbt_os_mutex_acquire(simix_global->mutex);
68 /* cancel non-blocking communications */
69 smx_synchro_t synchro;
70 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
71 simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
73 /* make sure no one will finish the comm after this process is destroyed,
74 * because src_proc or dst_proc would be an invalid pointer */
77 if (comm->src_proc == process) {
78 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
79 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
80 comm->src_proc = NULL;
82 /* I'm not supposed to destroy a detached comm from the sender side, */
84 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
89 else if (comm->dst_proc == process){
90 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
91 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
92 comm->dst_proc = NULL;
94 if (comm->detached && comm->src_proc != NULL) {
95 /* the comm will be freed right now, remove it from the sender */
96 xbt_fifo_remove(comm->src_proc->comms, comm);
101 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
105 XBT_DEBUG("%p should not be run anymore",process);
106 xbt_swag_remove(process, simix_global->process_list);
108 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
109 xbt_swag_insert(process, simix_global->process_to_destroy);
110 process->context->iwannadie = 0;
112 xbt_os_mutex_release(simix_global->mutex);
118 * Should be called some time to time to free the memory allocated for processes
119 * that have finished (or killed).
121 void SIMIX_process_empty_trash(void)
123 smx_process_t process = NULL;
125 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
126 XBT_DEBUG("Getting rid of %p",process);
128 delete process->context;
130 /* Free the exception allocated at creation time */
131 free(process->running_ctx);
132 xbt_dict_free(&process->properties);
134 xbt_fifo_free(process->comms);
136 xbt_dynar_free(&process->on_exit);
138 xbt_free(process->name);
146 void create_maestro(std::function<void()> code)
148 smx_process_t maestro = NULL;
149 /* Create maestro process and intilialize it */
150 maestro = new simgrid::simix::Process();
151 maestro->pid = simix_process_maxpid++;
153 maestro->name = (char*) "";
154 maestro->data = nullptr;
155 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
156 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
159 maestro->context = SIMIX_context_new(std::function<void()>(), NULL, maestro);
162 xbt_die("simix is not initialized, please call MSG_init first");
164 simix_global->context_factory->create_maestro(code, maestro);
167 maestro->simcall.issuer = maestro;
168 simix_global->maestro_process = maestro;
175 * \brief Creates and runs the maestro process
177 void SIMIX_maestro_create(void (*code)(void*), void* data)
179 simgrid::simix::create_maestro(std::bind(code, data));
183 * \brief Stops a process.
185 * Stops the process, execute all the registered on_exit functions,
186 * register it to the list of the process to restart if needed
187 * and stops its context.
189 void SIMIX_process_stop(smx_process_t arg) {
190 /* execute the on_exit functions */
191 SIMIX_process_on_exit_runall(arg);
192 /* Add the process to the list of process to restart, only if the host is down */
193 if (arg->auto_restart && arg->host->isOff()) {
194 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
195 sg_host_get_name(arg->host),
196 SIMIX_timer_get_date(arg->kill_timer),
197 arg->argc,arg->argv,arg->properties,
200 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
201 arg->context->stop();
205 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
206 * This function frees the argument.
207 * \return the process created
209 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
211 smx_process_t process = simix_global->create_process_function(
227 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
229 xbt_main_func_t code,
231 const char *hostname,
233 int argc, char **argv,
234 xbt_dict_t properties,
236 return (void*)SIMIX_process_create(name, code, data, hostname,
237 kill_time, argc, argv, properties, auto_restart,
241 static void kill_process(void* process)
243 simix_global->kill_process_function((smx_process_t) process);
247 * \brief Internal function to create a process.
249 * This function actually creates the process.
250 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
251 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
253 * \return the process created
255 smx_process_t SIMIX_process_create(
257 xbt_main_func_t code,
259 const char *hostname,
261 int argc, char **argv,
262 xbt_dict_t properties,
264 smx_process_t parent_process)
266 smx_process_t process = NULL;
267 sg_host_t host = sg_host_by_name(hostname);
269 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
273 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
275 for (i = 0; i < argc; i++)
280 process = new simgrid::simix::Process();
282 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
284 process->pid = simix_process_maxpid++;
285 process->name = xbt_strdup(name);
286 process->host = host;
287 process->data = data;
288 process->comms = xbt_fifo_new();
289 process->simcall.issuer = process;
290 /* Initiliaze data segment to default value */
291 SIMIX_segment_index_set(process, -1);
293 if (parent_process != NULL) {
294 process->ppid = SIMIX_process_get_PID(parent_process);
295 /* SMPI process have their own data segment and
296 each other inherit from their father */
298 if(smpi_privatize_global_variables){
299 if( parent_process->pid != 0){
300 SIMIX_segment_index_set(process, parent_process->segment_index);
302 SIMIX_segment_index_set(process, process->pid - 1);
310 /* Process data for auto-restart */
311 process->auto_restart = auto_restart;
312 process->code = code;
313 process->argc = argc;
314 process->argv = argv;
317 XBT_VERB("Create context %s", process->name);
318 process->context = SIMIX_context_new(
319 simgrid::simix::wrap_main(code, argc, argv),
320 simix_global->cleanup_process_function, process);
322 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
323 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
326 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
330 process->properties = properties;
332 /* Add the process to it's host process list */
333 xbt_swag_insert(process, sg_host_simix(host)->process_list);
335 XBT_DEBUG("Start context '%s'", process->name);
337 /* Now insert it in the global process list and in the process to run list */
338 xbt_swag_insert(process, simix_global->process_list);
339 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
340 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
342 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
343 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
344 sg_host_get_name(process->host), kill_time);
345 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
348 /* Tracing the process creation */
349 TRACE_msg_process_create(process->name, process->pid, process->host);
354 smx_process_t SIMIX_process_attach(
357 const char* hostname,
358 xbt_dict_t properties,
359 smx_process_t parent_process)
361 // This is mostly a copy/paste from SIMIX_process_new(),
362 // it'd be nice to share some code between those two functions.
364 sg_host_t host = sg_host_by_name(hostname);
365 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
368 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
373 smx_process_t process = new simgrid::simix::Process();
375 process->pid = simix_process_maxpid++;
376 process->name = xbt_strdup(name);
377 process->host = host;
378 process->data = data;
379 process->comms = xbt_fifo_new();
380 process->simcall.issuer = process;
382 /* Initiliaze data segment to default value */
383 SIMIX_segment_index_set(process, -1);
384 if (parent_process != NULL) {
385 process->ppid = SIMIX_process_get_PID(parent_process);
386 /* SMPI process have their own data segment and
387 each other inherit from their father */
389 if(smpi_privatize_global_variables){
390 if(parent_process->pid != 0){
391 SIMIX_segment_index_set(process, parent_process->segment_index);
393 SIMIX_segment_index_set(process, process->pid - 1);
399 /* Process data for auto-restart */
400 process->auto_restart = false;
401 process->code = nullptr;
403 process->argv = nullptr;
405 XBT_VERB("Create context %s", process->name);
407 xbt_die("simix is not initialized, please call MSG_init first");
408 process->context = simix_global->context_factory->attach(
409 simix_global->cleanup_process_function, process);
411 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
412 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
415 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
419 process->properties = properties;
421 /* Add the process to it's host process list */
422 xbt_swag_insert(process, sg_host_simix(host)->process_list);
424 /* Now insert it in the global process list and in the process to run list */
425 xbt_swag_insert(process, simix_global->process_list);
426 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
427 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
429 /* Tracing the process creation */
430 TRACE_msg_process_create(process->name, process->pid, process->host);
432 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
434 xbt_die("Not a suitable context");
436 context->attach_start();
440 void SIMIX_process_detach(void)
442 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
444 xbt_die("Not a suitable context");
446 simix_global->cleanup_process_function(context->process());
448 // Let maestro ignore we are still alive:
449 // xbt_swag_remove(context->process(), simix_global->process_list);
451 // TODDO, Remove from proces list:
452 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
454 context->attach_stop();
459 * \brief Executes the processes from simix_global->process_to_run.
461 * The processes of simix_global->process_to_run are run (in parallel if
462 * possible). On exit, simix_global->process_to_run is empty, and
463 * simix_global->process_that_ran contains the list of processes that just ran.
464 * The two lists are swapped so, be careful when using them before and after a
465 * call to this function.
467 void SIMIX_process_runall(void)
469 SIMIX_context_runall();
471 xbt_dynar_t tmp = simix_global->process_that_ran;
472 simix_global->process_that_ran = simix_global->process_to_run;
473 simix_global->process_to_run = tmp;
474 xbt_dynar_reset(simix_global->process_to_run);
477 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
478 SIMIX_process_kill(process, simcall->issuer);
481 * \brief Internal function to kill a SIMIX process.
483 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
484 * or directly for SIMIX internal purposes.
486 * \param process poor victim
487 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
489 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
491 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
493 process->context->iwannadie = 1;
494 process->blocked = 0;
495 process->suspended = 0;
496 process->doexception = 0;
498 /* destroy the blocking synchro if any */
499 if (process->waiting_synchro) {
501 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
502 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
503 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
504 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
505 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
507 if (exec != nullptr) {
510 } else if (comm != nullptr) {
511 xbt_fifo_remove(process->comms, process->waiting_synchro);
513 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
516 } else if (sleep != nullptr) {
517 SIMIX_process_sleep_destroy(process->waiting_synchro);
519 } else if (raw != nullptr) {
520 SIMIX_synchro_stop_waiting(process, &process->simcall);
521 delete process->waiting_synchro;
523 } else if (io != nullptr) {
524 SIMIX_io_destroy(process->waiting_synchro);
528 switch (process->waiting_synchro->type) {
529 case SIMIX_SYNC_JOIN:
530 SIMIX_process_sleep_destroy(process->waiting_synchro);
534 process->waiting_synchro = NULL;
536 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
537 XBT_DEBUG("Inserting %s in the to_run list", process->name);
538 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
543 /** @brief Ask another process to raise the given exception
545 * @param cat category of exception
546 * @param value value associated to the exception
547 * @param msg string information associated to the exception
549 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
550 SMX_EXCEPTION(process, cat, value, msg);
552 if (process->suspended)
553 SIMIX_process_resume(process,SIMIX_process_self());
555 /* cancel the blocking synchro if any */
556 if (process->waiting_synchro) {
558 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
559 if (exec != nullptr) {
560 SIMIX_execution_cancel(process->waiting_synchro);
563 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
564 if (comm != nullptr) {
565 xbt_fifo_remove(process->comms, comm);
569 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
570 if (sleep != nullptr) {
571 SIMIX_process_sleep_destroy(process->waiting_synchro);
572 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
573 XBT_DEBUG("Inserting %s in the to_run list", process->name);
574 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
578 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
579 if (raw != nullptr) {
580 SIMIX_synchro_stop_waiting(process, &process->simcall);
583 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
585 SIMIX_io_destroy(process->waiting_synchro);
588 process->waiting_synchro = NULL;
592 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
593 SIMIX_process_killall(simcall->issuer, reset_pid);
596 * \brief Kills all running processes.
597 * \param issuer this one will not be killed
599 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
601 smx_process_t p = NULL;
603 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
605 SIMIX_process_kill(p,issuer);
610 simix_process_maxpid = reset_pid;
612 SIMIX_context_runall();
614 SIMIX_process_empty_trash();
617 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
619 process->new_host = dest;
621 void SIMIX_process_change_host(smx_process_t process,
624 xbt_assert((process != NULL), "Invalid parameters");
625 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
626 process->host = dest;
627 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
631 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
633 smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
635 if (process != simcall->issuer) {
636 SIMIX_simcall_answer(simcall);
638 xbt_fifo_push(sync_suspend->simcalls, simcall);
639 process->waiting_synchro = sync_suspend;
640 process->waiting_synchro->suspend();
642 /* If we are suspending ourselves, then just do not finish the simcall now */
645 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
647 if (process->suspended) {
648 XBT_DEBUG("Process '%s' is already suspended", process->name);
652 process->suspended = 1;
654 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
655 if (process != issuer) {
657 if (process->waiting_synchro)
658 process->waiting_synchro->suspend();
659 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
663 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
664 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
668 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
669 SIMIX_process_resume(process, simcall->issuer);
672 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
674 XBT_IN("process = %p, issuer = %p", process, issuer);
676 if(process->context->iwannadie) {
677 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
681 if(!process->suspended) return;
682 process->suspended = 0;
684 /* If we are resuming another process, resume the synchronization it was waiting for
685 if any. Otherwise add it to the list of process to run in the next round. */
686 if (process != issuer) {
688 if (process->waiting_synchro) {
689 process->waiting_synchro->resume();
691 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
696 int SIMIX_process_get_maxpid(void) {
697 return simix_process_maxpid;
700 int SIMIX_process_count(void)
702 return xbt_swag_size(simix_global->process_list);
705 int SIMIX_process_get_PID(smx_process_t self){
712 int SIMIX_process_get_PPID(smx_process_t self){
719 void* SIMIX_process_self_get_data()
721 smx_process_t self = SIMIX_process_self();
726 return SIMIX_process_get_data(self);
729 void SIMIX_process_self_set_data(void *data)
731 smx_process_t self = SIMIX_process_self();
733 SIMIX_process_set_data(self, data);
736 void* SIMIX_process_get_data(smx_process_t process)
738 return process->data;
741 void SIMIX_process_set_data(smx_process_t process, void *data)
743 process->data = data;
746 sg_host_t SIMIX_process_get_host(smx_process_t process)
748 return process->host;
751 xbt_main_func_t SIMIX_process_get_code(void){
752 return SIMIX_process_self()->code;
755 /* needs to be public and without simcall because it is called
756 by exceptions and logging events */
757 const char* SIMIX_process_self_get_name(void) {
759 smx_process_t process = SIMIX_process_self();
760 if (process == NULL || process == simix_global->maestro_process)
763 return SIMIX_process_get_name(process);
766 const char* SIMIX_process_get_name(smx_process_t process)
768 return process->name;
771 smx_process_t SIMIX_process_get_by_name(const char* name)
775 xbt_swag_foreach(proc, simix_global->process_list)
777 if(!strcmp(name, proc->name))
783 int SIMIX_process_is_suspended(smx_process_t process)
785 return process->suspended;
788 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
790 return process->properties;
793 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
795 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
796 xbt_fifo_push(sync->simcalls, simcall);
797 simcall->issuer->waiting_synchro = sync;
800 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
801 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
803 if (sleep->surf_sleep) {
804 sleep->surf_sleep->cancel();
806 smx_simcall_t simcall;
807 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sleep->simcalls))) {
808 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
809 simcall->issuer->waiting_synchro = NULL;
810 if (simcall->issuer->suspended) {
811 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
812 simcall->issuer->suspended = 0;
813 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
815 SIMIX_simcall_answer(simcall);
818 sleep->surf_sleep->unref();
819 sleep->surf_sleep = NULL;
825 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
827 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
828 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
832 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
834 if (MC_is_active() || MC_record_replay_is_active()) {
835 MC_process_clock_add(simcall->issuer, duration);
836 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
837 SIMIX_simcall_answer(simcall);
840 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
841 xbt_fifo_push(sync->simcalls, simcall);
842 simcall->issuer->waiting_synchro = sync;
845 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
847 sg_host_t host = process->host;
849 /* check if the host is active */
851 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
853 simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
854 synchro->name = NULL;
856 synchro->host = host;
857 synchro->surf_sleep = surf_host_sleep(host, duration);
858 synchro->surf_sleep->setData(synchro);
859 XBT_DEBUG("Create sleep synchronization %p", synchro);
864 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
866 XBT_DEBUG("Destroy synchro %p", synchro);
867 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
869 if (sleep->surf_sleep) {
870 sleep->surf_sleep->unref();
871 sleep->surf_sleep = NULL;
876 * \brief Calling this function makes the process to yield.
878 * Only the current process can call this function, giving back the control to
881 * \param self the current process
883 void SIMIX_process_yield(smx_process_t self)
885 XBT_DEBUG("Yield process '%s'", self->name);
887 /* Go into sleep and return control to maestro */
888 self->context->suspend();
890 /* Ok, maestro returned control to us */
891 XBT_DEBUG("Control returned to me: '%s'", self->name);
893 if (self->new_host) {
894 SIMIX_process_change_host(self, self->new_host);
895 self->new_host = NULL;
898 if (self->context->iwannadie){
899 XBT_DEBUG("I wanna die!");
900 SIMIX_process_stop(self);
903 if (self->suspended) {
904 XBT_DEBUG("Hey! I'm suspended.");
905 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
907 SIMIX_process_suspend(self, self);
910 if (self->doexception) {
911 XBT_DEBUG("Wait, maestro left me an exception");
912 self->doexception = 0;
916 if(SMPI_switch_data_segment && self->segment_index != -1){
917 SMPI_switch_data_segment(self->segment_index);
921 /* callback: context fetching */
922 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
924 smx_process_t process = SIMIX_process_self();
926 return process->running_ctx;
931 /* callback: termination */
932 void SIMIX_process_exception_terminate(xbt_ex_t * e)
938 smx_context_t SIMIX_process_get_context(smx_process_t p) {
942 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
947 * \brief Returns the list of processes to run.
949 xbt_dynar_t SIMIX_process_get_runnable(void)
951 return simix_global->process_to_run;
955 * \brief Returns the process from PID.
957 smx_process_t SIMIX_process_from_PID(int PID)
960 xbt_swag_foreach(proc, simix_global->process_list) {
961 if (proc->pid == (unsigned long) PID)
967 /** @brief returns a dynar containg all currently existing processes */
968 xbt_dynar_t SIMIX_processes_as_dynar(void) {
970 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
971 xbt_swag_foreach(proc, simix_global->process_list) {
972 xbt_dynar_push(res,&proc);
978 void SIMIX_process_on_exit_runall(smx_process_t process) {
979 s_smx_process_exit_fun_t exit_fun;
980 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
981 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
982 while (!xbt_dynar_is_empty(process->on_exit)) {
983 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
984 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
988 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
989 xbt_assert(process, "current process not found: are you in maestro context ?");
991 if (!process->on_exit) {
992 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
995 s_smx_process_exit_fun_t exit_fun = {fun, data};
997 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
1001 * \brief Sets the auto-restart status of the process.
1002 * If set to 1, the process will be automatically restarted when its host
1005 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
1006 process->auto_restart = auto_restart;
1009 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
1010 return SIMIX_process_restart(process, simcall->issuer);
1012 /** @brief Restart a process, starting it again from the beginning. */
1013 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
1014 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
1015 //retrieve the arguments of the old process
1016 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
1017 s_smx_process_arg_t arg;
1018 arg.code = process->code;
1019 arg.hostname = sg_host_get_name(process->host);
1020 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1021 arg.argc = process->argc;
1022 arg.data = process->data;
1024 arg.argv = xbt_new(char*,process->argc + 1);
1025 for (i = 0; i < arg.argc; i++) {
1026 arg.argv[i] = xbt_strdup(process->argv[i]);
1028 arg.argv[process->argc] = NULL;
1029 arg.properties = NULL;
1030 arg.auto_restart = process->auto_restart;
1031 //kill the old process
1032 SIMIX_process_kill(process,issuer);
1033 //start the new process
1034 smx_process_t new_process;
1035 if (simix_global->create_process_function) {
1036 new_process = simix_global->create_process_function(
1048 new_process = simcall_process_create(
1063 void SIMIX_segment_index_set(smx_process_t proc, int index){
1064 proc->segment_index = index;