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 */
75 SIMIX_comm_cancel(comm);
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");
86 SIMIX_comm_destroy(comm);
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->refcount == 1 && 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);
98 SIMIX_comm_destroy(comm);
100 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
104 XBT_DEBUG("%p should not be run anymore",process);
105 xbt_swag_remove(process, simix_global->process_list);
107 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
108 xbt_swag_insert(process, simix_global->process_to_destroy);
109 process->context->iwannadie = 0;
111 xbt_os_mutex_release(simix_global->mutex);
117 * Should be called some time to time to free the memory allocated for processes
118 * that have finished (or killed).
120 void SIMIX_process_empty_trash(void)
122 smx_process_t process = NULL;
124 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
125 XBT_DEBUG("Getting rid of %p",process);
127 delete process->context;
129 /* Free the exception allocated at creation time */
130 free(process->running_ctx);
131 xbt_dict_free(&process->properties);
133 xbt_fifo_free(process->comms);
135 xbt_dynar_free(&process->on_exit);
137 xbt_free(process->name);
145 void create_maestro(std::function<void()> code)
147 smx_process_t maestro = NULL;
148 /* Create maestro process and intilialize it */
149 maestro = xbt_new0(s_smx_process_t, 1);
150 maestro->pid = simix_process_maxpid++;
152 maestro->name = (char*) "";
153 maestro->data = nullptr;
154 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
155 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
158 maestro->context = SIMIX_context_new(NULL, 0, nullptr, NULL, maestro);
161 xbt_die("simix is not initialized, please call MSG_init first");
163 simix_global->context_factory->create_maestro(code, maestro);
166 maestro->simcall.issuer = maestro;
167 simix_global->maestro_process = maestro;
174 * \brief Creates and runs the maestro process
176 void SIMIX_maestro_create(void (*code)(void*), void* data)
178 simgrid::simix::create_maestro(std::bind(code, data));
182 * \brief Stops a process.
184 * Stops the process, execute all the registered on_exit functions,
185 * register it to the list of the process to restart if needed
186 * and stops its context.
188 void SIMIX_process_stop(smx_process_t arg) {
189 /* execute the on_exit functions */
190 SIMIX_process_on_exit_runall(arg);
191 /* Add the process to the list of process to restart, only if the host is down */
192 if (arg->auto_restart && arg->host->isOff()) {
193 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
194 sg_host_get_name(arg->host),
195 SIMIX_timer_get_date(arg->kill_timer),
196 arg->argc,arg->argv,arg->properties,
199 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
200 arg->context->stop();
204 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
205 * This function frees the argument.
206 * \return the process created
208 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
210 smx_process_t process = simix_global->create_process_function(
226 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
228 xbt_main_func_t code,
230 const char *hostname,
232 int argc, char **argv,
233 xbt_dict_t properties,
235 return (void*)SIMIX_process_create(name, code, data, hostname,
236 kill_time, argc, argv, properties, auto_restart,
240 static void kill_process(void* process)
242 simix_global->kill_process_function((smx_process_t) process);
246 * \brief Internal function to create a process.
248 * This function actually creates the process.
249 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
250 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
252 * \return the process created
254 smx_process_t SIMIX_process_create(
256 xbt_main_func_t code,
258 const char *hostname,
260 int argc, char **argv,
261 xbt_dict_t properties,
263 smx_process_t parent_process)
265 smx_process_t process = NULL;
266 sg_host_t host = sg_host_by_name(hostname);
268 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
272 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
274 for (i = 0; i < argc; i++)
279 process = xbt_new0(s_smx_process_t, 1);
281 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
283 process->pid = simix_process_maxpid++;
284 process->name = xbt_strdup(name);
285 process->host = host;
286 process->data = data;
287 process->comms = xbt_fifo_new();
288 process->simcall.issuer = process;
289 /* Initiliaze data segment to default value */
290 SIMIX_segment_index_set(process, -1);
292 if (parent_process != NULL) {
293 process->ppid = SIMIX_process_get_PID(parent_process);
294 /* SMPI process have their own data segment and
295 each other inherit from their father */
297 if(smpi_privatize_global_variables){
298 if( parent_process->pid != 0){
299 SIMIX_segment_index_set(process, parent_process->segment_index);
301 SIMIX_segment_index_set(process, process->pid - 1);
309 /* Process data for auto-restart */
310 process->auto_restart = auto_restart;
311 process->code = code;
312 process->argc = argc;
313 process->argv = argv;
316 XBT_VERB("Create context %s", process->name);
317 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
319 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
320 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
323 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
327 process->properties = properties;
329 /* Add the process to it's host process list */
330 xbt_swag_insert(process, sg_host_simix(host)->process_list);
332 XBT_DEBUG("Start context '%s'", process->name);
334 /* Now insert it in the global process list and in the process to run list */
335 xbt_swag_insert(process, simix_global->process_list);
336 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
337 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
339 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
340 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
341 sg_host_get_name(process->host), kill_time);
342 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
345 /* Tracing the process creation */
346 TRACE_msg_process_create(process->name, process->pid, process->host);
351 smx_process_t SIMIX_process_attach(
354 const char* hostname,
355 xbt_dict_t properties,
356 smx_process_t parent_process)
358 // This is mostly a copy/paste from SIMIX_process_new(),
359 // it'd be nice to share some code between those two functions.
361 sg_host_t host = sg_host_by_name(hostname);
362 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
365 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
370 smx_process_t process = xbt_new0(s_smx_process_t, 1);
372 process->pid = simix_process_maxpid++;
373 process->name = xbt_strdup(name);
374 process->host = host;
375 process->data = data;
376 process->comms = xbt_fifo_new();
377 process->simcall.issuer = process;
379 /* Initiliaze data segment to default value */
380 SIMIX_segment_index_set(process, -1);
381 if (parent_process != NULL) {
382 process->ppid = SIMIX_process_get_PID(parent_process);
383 /* SMPI process have their own data segment and
384 each other inherit from their father */
386 if(smpi_privatize_global_variables){
387 if(parent_process->pid != 0){
388 SIMIX_segment_index_set(process, parent_process->segment_index);
390 SIMIX_segment_index_set(process, process->pid - 1);
396 /* Process data for auto-restart */
397 process->auto_restart = false;
398 process->code = nullptr;
400 process->argv = nullptr;
402 XBT_VERB("Create context %s", process->name);
404 xbt_die("simix is not initialized, please call MSG_init first");
405 process->context = simix_global->context_factory->attach(
406 simix_global->cleanup_process_function, process);
408 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
409 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
412 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
416 process->properties = properties;
418 /* Add the process to it's host process list */
419 xbt_swag_insert(process, sg_host_simix(host)->process_list);
421 /* Now insert it in the global process list and in the process to run list */
422 xbt_swag_insert(process, simix_global->process_list);
423 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
424 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
426 /* Tracing the process creation */
427 TRACE_msg_process_create(process->name, process->pid, process->host);
429 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
431 xbt_die("Not a suitable context");
433 context->attach_start();
437 void SIMIX_process_detach(void)
439 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
441 xbt_die("Not a suitable context");
443 simix_global->cleanup_process_function(context->process());
445 // Let maestro ignore we are still alive:
446 // xbt_swag_remove(context->process(), simix_global->process_list);
448 // TODDO, Remove from proces list:
449 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
451 context->attach_stop();
456 * \brief Executes the processes from simix_global->process_to_run.
458 * The processes of simix_global->process_to_run are run (in parallel if
459 * possible). On exit, simix_global->process_to_run is empty, and
460 * simix_global->process_that_ran contains the list of processes that just ran.
461 * The two lists are swapped so, be careful when using them before and after a
462 * call to this function.
464 void SIMIX_process_runall(void)
466 SIMIX_context_runall();
468 xbt_dynar_t tmp = simix_global->process_that_ran;
469 simix_global->process_that_ran = simix_global->process_to_run;
470 simix_global->process_to_run = tmp;
471 xbt_dynar_reset(simix_global->process_to_run);
474 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
475 SIMIX_process_kill(process, simcall->issuer);
478 * \brief Internal function to kill a SIMIX process.
480 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
481 * or directly for SIMIX internal purposes.
483 * \param process poor victim
484 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
486 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
488 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
490 process->context->iwannadie = 1;
491 process->blocked = 0;
492 process->suspended = 0;
493 process->doexception = 0;
495 /* destroy the blocking synchro if any */
496 if (process->waiting_synchro) {
498 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
499 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
500 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
501 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
502 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
504 if (exec != nullptr) {
505 SIMIX_execution_destroy(process->waiting_synchro);
507 } else if (comm != nullptr) {
508 xbt_fifo_remove(process->comms, process->waiting_synchro);
509 SIMIX_comm_cancel(process->waiting_synchro);
510 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
511 SIMIX_comm_destroy(process->waiting_synchro);
513 } else if (sleep != nullptr) {
514 SIMIX_process_sleep_destroy(process->waiting_synchro);
516 } else if (raw != nullptr) {
517 SIMIX_synchro_stop_waiting(process, &process->simcall);
518 SIMIX_synchro_destroy(process->waiting_synchro);
520 } else if (io != nullptr) {
521 SIMIX_io_destroy(process->waiting_synchro);
525 switch (process->waiting_synchro->type) {
526 case SIMIX_SYNC_JOIN:
527 SIMIX_process_sleep_destroy(process->waiting_synchro);
531 process->waiting_synchro = NULL;
533 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
534 XBT_DEBUG("Inserting %s in the to_run list", process->name);
535 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
540 /** @brief Ask another process to raise the given exception
542 * @param cat category of exception
543 * @param value value associated to the exception
544 * @param msg string information associated to the exception
546 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
547 SMX_EXCEPTION(process, cat, value, msg);
549 if (process->suspended)
550 SIMIX_process_resume(process,SIMIX_process_self());
552 /* cancel the blocking synchro if any */
553 if (process->waiting_synchro) {
555 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
556 if (exec != nullptr) {
557 SIMIX_execution_cancel(process->waiting_synchro);
560 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
561 if (comm != nullptr) {
562 xbt_fifo_remove(process->comms, process->waiting_synchro);
563 SIMIX_comm_cancel(process->waiting_synchro);
566 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
567 if (sleep != nullptr) {
568 SIMIX_process_sleep_destroy(process->waiting_synchro);
569 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
570 XBT_DEBUG("Inserting %s in the to_run list", process->name);
571 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
575 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
576 if (raw != nullptr) {
577 SIMIX_synchro_stop_waiting(process, &process->simcall);
580 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
582 SIMIX_io_destroy(process->waiting_synchro);
585 process->waiting_synchro = NULL;
589 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
590 SIMIX_process_killall(simcall->issuer, reset_pid);
593 * \brief Kills all running processes.
594 * \param issuer this one will not be killed
596 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
598 smx_process_t p = NULL;
600 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
602 SIMIX_process_kill(p,issuer);
607 simix_process_maxpid = reset_pid;
609 SIMIX_context_runall();
611 SIMIX_process_empty_trash();
614 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
616 process->new_host = dest;
618 void SIMIX_process_change_host(smx_process_t process,
621 xbt_assert((process != NULL), "Invalid parameters");
622 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
623 process->host = dest;
624 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
628 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
630 smx_synchro_t sync_suspend =
631 SIMIX_process_suspend(process, simcall->issuer);
633 if (process != simcall->issuer) {
634 SIMIX_simcall_answer(simcall);
636 xbt_fifo_push(sync_suspend->simcalls, simcall);
637 process->waiting_synchro = sync_suspend;
638 SIMIX_execution_suspend(process->waiting_synchro);
640 /* If we are suspending ourselves, then just do not finish the simcall now */
643 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
645 if (process->suspended) {
646 XBT_DEBUG("Process '%s' is already suspended", process->name);
650 process->suspended = 1;
652 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
653 if (process != issuer) {
655 if (process->waiting_synchro)
656 process->waiting_synchro->suspend();
657 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
661 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
662 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
666 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
667 SIMIX_process_resume(process, simcall->issuer);
670 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
672 XBT_IN("process = %p, issuer = %p", process, issuer);
674 if(process->context->iwannadie) {
675 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
679 if(!process->suspended) return;
680 process->suspended = 0;
682 /* If we are resuming another process, resume the synchronization it was waiting for
683 if any. Otherwise add it to the list of process to run in the next round. */
684 if (process != issuer) {
686 if (process->waiting_synchro) {
687 process->waiting_synchro->resume();
689 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
694 int SIMIX_process_get_maxpid(void) {
695 return simix_process_maxpid;
698 int SIMIX_process_count(void)
700 return xbt_swag_size(simix_global->process_list);
703 int SIMIX_process_get_PID(smx_process_t self){
710 int SIMIX_process_get_PPID(smx_process_t self){
717 void* SIMIX_process_self_get_data()
719 smx_process_t self = SIMIX_process_self();
724 return SIMIX_process_get_data(self);
727 void SIMIX_process_self_set_data(void *data)
729 smx_process_t self = SIMIX_process_self();
731 SIMIX_process_set_data(self, data);
734 void* SIMIX_process_get_data(smx_process_t process)
736 return process->data;
739 void SIMIX_process_set_data(smx_process_t process, void *data)
741 process->data = data;
744 sg_host_t SIMIX_process_get_host(smx_process_t process)
746 return process->host;
749 xbt_main_func_t SIMIX_process_get_code(void){
750 return SIMIX_process_self()->code;
753 /* needs to be public and without simcall because it is called
754 by exceptions and logging events */
755 const char* SIMIX_process_self_get_name(void) {
757 smx_process_t process = SIMIX_process_self();
758 if (process == NULL || process == simix_global->maestro_process)
761 return SIMIX_process_get_name(process);
764 const char* SIMIX_process_get_name(smx_process_t process)
766 return process->name;
769 smx_process_t SIMIX_process_get_by_name(const char* name)
773 xbt_swag_foreach(proc, simix_global->process_list)
775 if(!strcmp(name, proc->name))
781 int SIMIX_process_is_suspended(smx_process_t process)
783 return process->suspended;
786 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
788 return process->properties;
791 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
793 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
794 xbt_fifo_push(sync->simcalls, simcall);
795 simcall->issuer->waiting_synchro = sync;
798 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
799 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
801 if (sleep->surf_sleep) {
802 sleep->surf_sleep->cancel();
804 smx_simcall_t simcall;
805 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sleep->simcalls))) {
806 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
807 simcall->issuer->waiting_synchro = NULL;
808 if (simcall->issuer->suspended) {
809 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
810 simcall->issuer->suspended = 0;
811 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
813 SIMIX_simcall_answer(simcall);
816 sleep->surf_sleep->unref();
817 sleep->surf_sleep = NULL;
823 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
825 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
826 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
830 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
832 if (MC_is_active() || MC_record_replay_is_active()) {
833 MC_process_clock_add(simcall->issuer, duration);
834 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
835 SIMIX_simcall_answer(simcall);
838 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
839 xbt_fifo_push(sync->simcalls, simcall);
840 simcall->issuer->waiting_synchro = sync;
843 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
845 sg_host_t host = process->host;
847 /* check if the host is active */
849 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
851 simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
852 synchro->name = NULL;
854 synchro->host = host;
855 synchro->surf_sleep = surf_host_sleep(host, duration);
856 synchro->surf_sleep->setData(synchro);
857 XBT_DEBUG("Create sleep synchronization %p", synchro);
862 void SIMIX_post_process_sleep(smx_synchro_t synchro)
864 smx_simcall_t simcall;
866 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
868 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
870 switch (sleep->surf_sleep->getState()){
871 case simgrid::surf::Action::State::failed:
872 simcall->issuer->context->iwannadie = 1;
873 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
874 state = SIMIX_SRC_HOST_FAILURE;
877 case simgrid::surf::Action::State::done:
885 if (simcall->issuer->host->isOff()) {
886 simcall->issuer->context->iwannadie = 1;
888 simcall_process_sleep__set__result(simcall, state);
889 simcall->issuer->waiting_synchro = NULL;
890 if (simcall->issuer->suspended) {
891 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
892 simcall->issuer->suspended = 0;
893 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
895 SIMIX_simcall_answer(simcall);
899 SIMIX_process_sleep_destroy(synchro);
902 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
904 XBT_DEBUG("Destroy synchro %p", synchro);
905 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
907 if (sleep->surf_sleep) {
908 sleep->surf_sleep->unref();
909 sleep->surf_sleep = NULL;
914 * \brief Calling this function makes the process to yield.
916 * Only the current process can call this function, giving back the control to
919 * \param self the current process
921 void SIMIX_process_yield(smx_process_t self)
923 XBT_DEBUG("Yield process '%s'", self->name);
925 /* Go into sleep and return control to maestro */
926 self->context->suspend();
928 /* Ok, maestro returned control to us */
929 XBT_DEBUG("Control returned to me: '%s'", self->name);
931 if (self->new_host) {
932 SIMIX_process_change_host(self, self->new_host);
933 self->new_host = NULL;
936 if (self->context->iwannadie){
937 XBT_DEBUG("I wanna die!");
938 SIMIX_process_stop(self);
941 if (self->suspended) {
942 XBT_DEBUG("Hey! I'm suspended.");
943 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
945 SIMIX_process_suspend(self, self);
948 if (self->doexception) {
949 XBT_DEBUG("Wait, maestro left me an exception");
950 self->doexception = 0;
954 if(SMPI_switch_data_segment && self->segment_index != -1){
955 SMPI_switch_data_segment(self->segment_index);
959 /* callback: context fetching */
960 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
962 smx_process_t process = SIMIX_process_self();
964 return process->running_ctx;
969 /* callback: termination */
970 void SIMIX_process_exception_terminate(xbt_ex_t * e)
976 smx_context_t SIMIX_process_get_context(smx_process_t p) {
980 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
985 * \brief Returns the list of processes to run.
987 xbt_dynar_t SIMIX_process_get_runnable(void)
989 return simix_global->process_to_run;
993 * \brief Returns the process from PID.
995 smx_process_t SIMIX_process_from_PID(int PID)
998 xbt_swag_foreach(proc, simix_global->process_list) {
999 if (proc->pid == (unsigned long) PID)
1005 /** @brief returns a dynar containg all currently existing processes */
1006 xbt_dynar_t SIMIX_processes_as_dynar(void) {
1008 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
1009 xbt_swag_foreach(proc, simix_global->process_list) {
1010 xbt_dynar_push(res,&proc);
1016 void SIMIX_process_on_exit_runall(smx_process_t process) {
1017 s_smx_process_exit_fun_t exit_fun;
1018 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
1019 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
1020 while (!xbt_dynar_is_empty(process->on_exit)) {
1021 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
1022 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
1026 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
1027 xbt_assert(process, "current process not found: are you in maestro context ?");
1029 if (!process->on_exit) {
1030 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
1033 s_smx_process_exit_fun_t exit_fun = {fun, data};
1035 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
1039 * \brief Sets the auto-restart status of the process.
1040 * If set to 1, the process will be automatically restarted when its host
1043 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
1044 process->auto_restart = auto_restart;
1047 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
1048 return SIMIX_process_restart(process, simcall->issuer);
1050 /** @brief Restart a process, starting it again from the beginning. */
1051 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
1052 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
1053 //retrieve the arguments of the old process
1054 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
1055 s_smx_process_arg_t arg;
1056 arg.code = process->code;
1057 arg.hostname = sg_host_get_name(process->host);
1058 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1059 arg.argc = process->argc;
1060 arg.data = process->data;
1062 arg.argv = xbt_new(char*,process->argc + 1);
1063 for (i = 0; i < arg.argc; i++) {
1064 arg.argv[i] = xbt_strdup(process->argv[i]);
1066 arg.argv[process->argc] = NULL;
1067 arg.properties = NULL;
1068 arg.auto_restart = process->auto_restart;
1069 //kill the old process
1070 SIMIX_process_kill(process,issuer);
1071 //start the new process
1072 smx_process_t new_process;
1073 if (simix_global->create_process_function) {
1074 new_process = simix_global->create_process_function(
1086 new_process = simcall_process_create(
1101 void SIMIX_segment_index_set(smx_process_t proc, int index){
1102 proc->segment_index = index;