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/simix/smx_private.hpp"
16 #include "src/msg/msg_private.h"
18 #include "src/simix/SynchroSleep.hpp"
19 #include "src/simix/SynchroRaw.hpp"
20 #include "src/simix/SynchroIo.hpp"
23 #include "src/smpi/private.h"
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
28 unsigned long simix_process_maxpid = 0;
31 * \brief Returns the current agent.
33 * This functions returns the currently running SIMIX process.
35 * \return The SIMIX process
37 smx_process_t SIMIX_process_self(void)
39 smx_context_t self_context = SIMIX_context_self();
41 return self_context ? SIMIX_context_get_process(self_context) : NULL;
45 * \brief Returns whether a process has pending asynchronous communications.
46 * \return true if there are asynchronous communications in this process
48 int SIMIX_process_has_pending_comms(smx_process_t process) {
50 return xbt_fifo_size(process->comms) > 0;
54 * \brief Moves a process to the list of processes to destroy.
56 void SIMIX_process_cleanup(smx_process_t process)
58 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
59 process->name, process, process->waiting_synchro);
61 SIMIX_process_on_exit_runall(process);
63 /* Unregister from the kill timer if any */
64 if (process->kill_timer != NULL)
65 SIMIX_timer_remove(process->kill_timer);
67 xbt_os_mutex_acquire(simix_global->mutex);
69 /* cancel non-blocking communications */
70 smx_synchro_t synchro;
71 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
72 simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
74 /* make sure no one will finish the comm after this process is destroyed,
75 * because src_proc or dst_proc would be an invalid pointer */
76 SIMIX_comm_cancel(comm);
78 if (comm->src_proc == process) {
79 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
80 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
81 comm->src_proc = NULL;
83 /* I'm not supposed to destroy a detached comm from the sender side, */
85 SIMIX_comm_destroy(comm);
87 XBT_DEBUG("Don't destroy it since it's a detached comm");
90 else if (comm->dst_proc == process){
91 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
92 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
93 comm->dst_proc = NULL;
95 if (comm->detached && comm->refcount == 1 && comm->src_proc != NULL) {
96 /* the comm will be freed right now, remove it from the sender */
97 xbt_fifo_remove(comm->src_proc->comms, comm);
99 SIMIX_comm_destroy(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 SIMIX_context_free(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 = xbt_new0(s_smx_process_t, 1);
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(NULL, 0, nullptr, 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 /* stop the context */
202 SIMIX_context_stop(arg->context);
206 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
207 * This function frees the argument.
208 * \return the process created
210 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
212 smx_process_t process = simix_global->create_process_function(
228 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
230 xbt_main_func_t code,
232 const char *hostname,
234 int argc, char **argv,
235 xbt_dict_t properties,
237 return (void*)SIMIX_process_create(name, code, data, hostname,
238 kill_time, argc, argv, properties, auto_restart,
242 static void kill_process(void* process)
244 simix_global->kill_process_function((smx_process_t) process);
248 * \brief Internal function to create a process.
250 * This function actually creates the process.
251 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
252 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
254 * \return the process created
256 smx_process_t SIMIX_process_create(
258 xbt_main_func_t code,
260 const char *hostname,
262 int argc, char **argv,
263 xbt_dict_t properties,
265 smx_process_t parent_process)
267 smx_process_t process = NULL;
268 sg_host_t host = sg_host_by_name(hostname);
270 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
274 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
276 for (i = 0; i < argc; i++)
281 process = xbt_new0(s_smx_process_t, 1);
283 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
285 process->pid = simix_process_maxpid++;
286 process->name = xbt_strdup(name);
287 process->host = host;
288 process->data = data;
289 process->comms = xbt_fifo_new();
290 process->simcall.issuer = process;
291 /* Initiliaze data segment to default value */
292 SIMIX_segment_index_set(process, -1);
294 if (parent_process != NULL) {
295 process->ppid = SIMIX_process_get_PID(parent_process);
296 /* SMPI process have their own data segment and
297 each other inherit from their father */
299 if(smpi_privatize_global_variables){
300 if( parent_process->pid != 0){
301 SIMIX_segment_index_set(process, parent_process->segment_index);
303 SIMIX_segment_index_set(process, process->pid - 1);
311 /* Process data for auto-restart */
312 process->auto_restart = auto_restart;
313 process->code = code;
314 process->argc = argc;
315 process->argv = argv;
318 XBT_VERB("Create context %s", process->name);
319 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
321 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
322 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
325 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
329 process->properties = properties;
331 /* Add the process to it's host process list */
332 xbt_swag_insert(process, sg_host_simix(host)->process_list);
334 XBT_DEBUG("Start context '%s'", process->name);
336 /* Now insert it in the global process list and in the process to run list */
337 xbt_swag_insert(process, simix_global->process_list);
338 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
339 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
341 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
342 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
343 sg_host_get_name(process->host), kill_time);
344 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
347 /* Tracing the process creation */
348 TRACE_msg_process_create(process->name, process->pid, process->host);
353 smx_process_t SIMIX_process_attach(
356 const char* hostname,
357 xbt_dict_t properties,
358 smx_process_t parent_process)
360 // This is mostly a copy/paste from SIMIX_process_new(),
361 // it'd be nice to share some code between those two functions.
363 sg_host_t host = sg_host_by_name(hostname);
364 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
367 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
372 smx_process_t process = xbt_new0(s_smx_process_t, 1);
374 process->pid = simix_process_maxpid++;
375 process->name = xbt_strdup(name);
376 process->host = host;
377 process->data = data;
378 process->comms = xbt_fifo_new();
379 process->simcall.issuer = process;
381 /* Initiliaze data segment to default value */
382 SIMIX_segment_index_set(process, -1);
383 if (parent_process != NULL) {
384 process->ppid = SIMIX_process_get_PID(parent_process);
385 /* SMPI process have their own data segment and
386 each other inherit from their father */
388 if(smpi_privatize_global_variables){
389 if(parent_process->pid != 0){
390 SIMIX_segment_index_set(process, parent_process->segment_index);
392 SIMIX_segment_index_set(process, process->pid - 1);
398 /* Process data for auto-restart */
399 process->auto_restart = false;
400 process->code = nullptr;
402 process->argv = nullptr;
404 XBT_VERB("Create context %s", process->name);
406 xbt_die("simix is not initialized, please call MSG_init first");
407 process->context = simix_global->context_factory->attach(
408 simix_global->cleanup_process_function, process);
410 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
411 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
414 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
418 process->properties = properties;
420 /* Add the process to it's host process list */
421 xbt_swag_insert(process, sg_host_simix(host)->process_list);
423 /* Now insert it in the global process list and in the process to run list */
424 xbt_swag_insert(process, simix_global->process_list);
425 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
426 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
428 /* Tracing the process creation */
429 TRACE_msg_process_create(process->name, process->pid, process->host);
431 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
433 xbt_die("Not a suitable context");
435 context->attach_start();
439 void SIMIX_process_detach(void)
441 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
443 xbt_die("Not a suitable context");
445 simix_global->cleanup_process_function(context->process());
447 // Let maestro ignore we are still alive:
448 // xbt_swag_remove(context->process(), simix_global->process_list);
450 // TODDO, Remove from proces list:
451 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
453 context->attach_stop();
458 * \brief Executes the processes from simix_global->process_to_run.
460 * The processes of simix_global->process_to_run are run (in parallel if
461 * possible). On exit, simix_global->process_to_run is empty, and
462 * simix_global->process_that_ran contains the list of processes that just ran.
463 * The two lists are swapped so, be careful when using them before and after a
464 * call to this function.
466 void SIMIX_process_runall(void)
468 SIMIX_context_runall();
470 xbt_dynar_t tmp = simix_global->process_that_ran;
471 simix_global->process_that_ran = simix_global->process_to_run;
472 simix_global->process_to_run = tmp;
473 xbt_dynar_reset(simix_global->process_to_run);
476 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
477 SIMIX_process_kill(process, simcall->issuer);
480 * \brief Internal function to kill a SIMIX process.
482 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
483 * or directly for SIMIX internal purposes.
485 * \param process poor victim
486 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
488 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
490 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
492 process->context->iwannadie = 1;
493 process->blocked = 0;
494 process->suspended = 0;
495 process->doexception = 0;
497 /* destroy the blocking synchro if any */
498 if (process->waiting_synchro) {
500 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
501 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
502 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
503 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
504 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
506 if (exec != nullptr) {
507 SIMIX_execution_destroy(process->waiting_synchro);
509 } else if (comm != nullptr) {
510 xbt_fifo_remove(process->comms, process->waiting_synchro);
511 SIMIX_comm_cancel(process->waiting_synchro);
512 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
513 SIMIX_comm_destroy(process->waiting_synchro);
515 } else if (sleep != nullptr) {
516 SIMIX_process_sleep_destroy(process->waiting_synchro);
518 } else if (raw != nullptr) {
519 SIMIX_synchro_stop_waiting(process, &process->simcall);
520 SIMIX_synchro_destroy(process->waiting_synchro);
522 } else if (io != nullptr) {
523 SIMIX_io_destroy(process->waiting_synchro);
527 switch (process->waiting_synchro->type) {
528 case SIMIX_SYNC_JOIN:
529 SIMIX_process_sleep_destroy(process->waiting_synchro);
533 process->waiting_synchro = NULL;
535 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
536 XBT_DEBUG("Inserting %s in the to_run list", process->name);
537 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
542 /** @brief Ask another process to raise the given exception
544 * @param cat category of exception
545 * @param value value associated to the exception
546 * @param msg string information associated to the exception
548 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
549 SMX_EXCEPTION(process, cat, value, msg);
551 if (process->suspended)
552 SIMIX_process_resume(process,SIMIX_process_self());
554 /* cancel the blocking synchro if any */
555 if (process->waiting_synchro) {
557 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
558 if (exec != nullptr) {
559 SIMIX_execution_cancel(process->waiting_synchro);
562 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
563 if (comm != nullptr) {
564 xbt_fifo_remove(process->comms, process->waiting_synchro);
565 SIMIX_comm_cancel(process->waiting_synchro);
568 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
569 if (sleep != nullptr) {
570 SIMIX_process_sleep_destroy(process->waiting_synchro);
571 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
572 XBT_DEBUG("Inserting %s in the to_run list", process->name);
573 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
577 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
578 if (raw != nullptr) {
579 SIMIX_synchro_stop_waiting(process, &process->simcall);
582 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
584 SIMIX_io_destroy(process->waiting_synchro);
587 process->waiting_synchro = NULL;
591 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
592 SIMIX_process_killall(simcall->issuer, reset_pid);
595 * \brief Kills all running processes.
596 * \param issuer this one will not be killed
598 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
600 smx_process_t p = NULL;
602 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
604 SIMIX_process_kill(p,issuer);
609 simix_process_maxpid = reset_pid;
611 SIMIX_context_runall();
613 SIMIX_process_empty_trash();
616 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
618 process->new_host = dest;
620 void SIMIX_process_change_host(smx_process_t process,
623 xbt_assert((process != NULL), "Invalid parameters");
624 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
625 process->host = dest;
626 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
630 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
632 smx_synchro_t sync_suspend =
633 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 SIMIX_execution_suspend(process->waiting_synchro);
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_post_process_sleep(smx_synchro_t synchro)
866 smx_simcall_t simcall;
868 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
870 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
872 switch (sleep->surf_sleep->getState()){
873 case simgrid::surf::Action::State::failed:
874 simcall->issuer->context->iwannadie = 1;
875 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
876 state = SIMIX_SRC_HOST_FAILURE;
879 case simgrid::surf::Action::State::done:
887 if (simcall->issuer->host->isOff()) {
888 simcall->issuer->context->iwannadie = 1;
890 simcall_process_sleep__set__result(simcall, state);
891 simcall->issuer->waiting_synchro = NULL;
892 if (simcall->issuer->suspended) {
893 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
894 simcall->issuer->suspended = 0;
895 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
897 SIMIX_simcall_answer(simcall);
901 SIMIX_process_sleep_destroy(synchro);
904 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
906 XBT_DEBUG("Destroy synchro %p", synchro);
907 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
909 if (sleep->surf_sleep) {
910 sleep->surf_sleep->unref();
911 sleep->surf_sleep = NULL;
915 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
917 synchro->suspend(); //FIXME: USELESS
920 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
922 synchro->resume(); //FIXME: USELESS
926 * \brief Calling this function makes the process to yield.
928 * Only the current process can call this function, giving back the control to
931 * \param self the current process
933 void SIMIX_process_yield(smx_process_t self)
935 XBT_DEBUG("Yield process '%s'", self->name);
937 /* Go into sleep and return control to maestro */
938 SIMIX_context_suspend(self->context);
940 /* Ok, maestro returned control to us */
941 XBT_DEBUG("Control returned to me: '%s'", self->name);
943 if (self->new_host) {
944 SIMIX_process_change_host(self, self->new_host);
945 self->new_host = NULL;
948 if (self->context->iwannadie){
949 XBT_DEBUG("I wanna die!");
950 SIMIX_process_stop(self);
953 if (self->suspended) {
954 XBT_DEBUG("Hey! I'm suspended.");
955 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
957 SIMIX_process_suspend(self, self);
960 if (self->doexception) {
961 XBT_DEBUG("Wait, maestro left me an exception");
962 self->doexception = 0;
966 if(SMPI_switch_data_segment && self->segment_index != -1){
967 SMPI_switch_data_segment(self->segment_index);
971 /* callback: context fetching */
972 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
974 smx_process_t process = SIMIX_process_self();
976 return process->running_ctx;
981 /* callback: termination */
982 void SIMIX_process_exception_terminate(xbt_ex_t * e)
988 smx_context_t SIMIX_process_get_context(smx_process_t p) {
992 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
997 * \brief Returns the list of processes to run.
999 xbt_dynar_t SIMIX_process_get_runnable(void)
1001 return simix_global->process_to_run;
1005 * \brief Returns the process from PID.
1007 smx_process_t SIMIX_process_from_PID(int PID)
1010 xbt_swag_foreach(proc, simix_global->process_list) {
1011 if (proc->pid == (unsigned long) PID)
1017 /** @brief returns a dynar containg all currently existing processes */
1018 xbt_dynar_t SIMIX_processes_as_dynar(void) {
1020 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
1021 xbt_swag_foreach(proc, simix_global->process_list) {
1022 xbt_dynar_push(res,&proc);
1028 void SIMIX_process_on_exit_runall(smx_process_t process) {
1029 s_smx_process_exit_fun_t exit_fun;
1030 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
1031 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
1032 while (!xbt_dynar_is_empty(process->on_exit)) {
1033 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
1034 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
1038 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
1039 xbt_assert(process, "current process not found: are you in maestro context ?");
1041 if (!process->on_exit) {
1042 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
1045 s_smx_process_exit_fun_t exit_fun = {fun, data};
1047 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
1051 * \brief Sets the auto-restart status of the process.
1052 * If set to 1, the process will be automatically restarted when its host
1055 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
1056 process->auto_restart = auto_restart;
1059 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
1060 return SIMIX_process_restart(process, simcall->issuer);
1062 /** @brief Restart a process, starting it again from the beginning. */
1063 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
1064 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
1065 //retrieve the arguments of the old process
1066 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
1067 s_smx_process_arg_t arg;
1068 arg.code = process->code;
1069 arg.hostname = sg_host_get_name(process->host);
1070 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1071 arg.argc = process->argc;
1072 arg.data = process->data;
1074 arg.argv = xbt_new(char*,process->argc + 1);
1075 for (i = 0; i < arg.argc; i++) {
1076 arg.argv[i] = xbt_strdup(process->argv[i]);
1078 arg.argv[process->argc] = NULL;
1079 arg.properties = NULL;
1080 arg.auto_restart = process->auto_restart;
1081 //kill the old process
1082 SIMIX_process_kill(process,issuer);
1083 //start the new process
1084 smx_process_t new_process;
1085 if (simix_global->create_process_function) {
1086 new_process = simix_global->create_process_function(
1098 new_process = simcall_process_create(
1113 void SIMIX_segment_index_set(smx_process_t proc, int index){
1114 proc->segment_index = index;