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.c_str(), 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);
145 void create_maestro(std::function<void()> code)
147 smx_process_t maestro = NULL;
148 /* Create maestro process and intilialize it */
149 maestro = new simgrid::simix::Process();
150 maestro->pid = simix_process_maxpid++;
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(std::function<void()>(), 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.c_str(),
194 arg->code, arg->data,
195 sg_host_get_name(arg->host),
196 SIMIX_timer_get_date(arg->kill_timer),
197 arg->args.argc(), arg->args.argv(), arg->properties,
200 XBT_DEBUG("Process %s (%s) is dead",
201 arg->name.c_str(), sg_host_get_name(arg->host));
202 arg->context->stop();
205 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
207 xbt_main_func_t code,
209 const char *hostname,
211 int argc, char **argv,
212 xbt_dict_t properties,
215 simgrid::simix::args args(argc, argv);
216 void* res = SIMIX_process_create(name, code, data, hostname,
217 kill_time, std::move(args), properties, auto_restart,
219 for (int i = 0; i != argc; ++i)
225 static void kill_process(void* process)
227 simix_global->kill_process_function((smx_process_t) process);
231 * \brief Internal function to create a process.
233 * This function actually creates the process.
234 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
235 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
237 * \return the process created
239 smx_process_t SIMIX_process_create(
241 xbt_main_func_t code,
243 const char *hostname,
245 simgrid::simix::args args,
246 xbt_dict_t properties,
248 smx_process_t parent_process)
250 smx_process_t process = NULL;
251 sg_host_t host = sg_host_by_name(hostname);
253 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
256 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
261 process = new simgrid::simix::Process();
263 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
265 process->pid = simix_process_maxpid++;
266 process->name = std::string(name);
267 process->host = host;
268 process->data = data;
269 process->comms = xbt_fifo_new();
270 process->simcall.issuer = process;
271 /* Initiliaze data segment to default value */
272 SIMIX_segment_index_set(process, -1);
274 if (parent_process != NULL) {
275 process->ppid = SIMIX_process_get_PID(parent_process);
276 /* SMPI process have their own data segment and
277 each other inherit from their father */
279 if(smpi_privatize_global_variables){
280 if( parent_process->pid != 0){
281 SIMIX_segment_index_set(process, parent_process->segment_index);
283 SIMIX_segment_index_set(process, process->pid - 1);
291 /* Process data for auto-restart */
292 process->auto_restart = auto_restart;
293 process->code = code;
294 process->args = args;
296 XBT_VERB("Create context %s", process->name.c_str());
297 process->context = SIMIX_context_new(
298 simgrid::simix::wrap_main(code, std::move(args)),
299 simix_global->cleanup_process_function, process);
301 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
302 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
305 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
309 process->properties = properties;
311 /* Add the process to it's host process list */
312 xbt_swag_insert(process, sg_host_simix(host)->process_list);
314 XBT_DEBUG("Start context '%s'", process->name.c_str());
316 /* Now insert it in the global process list and in the process to run list */
317 xbt_swag_insert(process, simix_global->process_list);
318 XBT_DEBUG("Inserting %s(%s) in the to_run list",
319 process->name.c_str(), sg_host_get_name(host));
320 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
322 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
323 XBT_DEBUG("Process %s(%s) will be kill at time %f",
324 process->name.c_str(), sg_host_get_name(process->host), kill_time);
325 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
328 /* Tracing the process creation */
329 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
334 smx_process_t SIMIX_process_attach(
337 const char* hostname,
338 xbt_dict_t properties,
339 smx_process_t parent_process)
341 // This is mostly a copy/paste from SIMIX_process_new(),
342 // it'd be nice to share some code between those two functions.
344 sg_host_t host = sg_host_by_name(hostname);
345 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
348 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
353 smx_process_t process = new simgrid::simix::Process();
355 process->pid = simix_process_maxpid++;
356 process->name = std::string(name);
357 process->host = host;
358 process->data = data;
359 process->comms = xbt_fifo_new();
360 process->simcall.issuer = process;
362 /* Initiliaze data segment to default value */
363 SIMIX_segment_index_set(process, -1);
364 if (parent_process != NULL) {
365 process->ppid = SIMIX_process_get_PID(parent_process);
366 /* SMPI process have their own data segment and
367 each other inherit from their father */
369 if(smpi_privatize_global_variables){
370 if(parent_process->pid != 0){
371 SIMIX_segment_index_set(process, parent_process->segment_index);
373 SIMIX_segment_index_set(process, process->pid - 1);
379 /* Process data for auto-restart */
380 process->auto_restart = false;
381 process->code = nullptr;
383 XBT_VERB("Create context %s", process->name.c_str());
385 xbt_die("simix is not initialized, please call MSG_init first");
386 process->context = simix_global->context_factory->attach(
387 simix_global->cleanup_process_function, process);
389 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
390 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
393 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
397 process->properties = properties;
399 /* Add the process to it's host process list */
400 xbt_swag_insert(process, sg_host_simix(host)->process_list);
402 /* Now insert it in the global process list and in the process to run list */
403 xbt_swag_insert(process, simix_global->process_list);
404 XBT_DEBUG("Inserting %s(%s) in the to_run list",
405 process->name.c_str(), sg_host_get_name(host));
406 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
408 /* Tracing the process creation */
409 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
411 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
413 xbt_die("Not a suitable context");
415 context->attach_start();
419 void SIMIX_process_detach(void)
421 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
423 xbt_die("Not a suitable context");
425 simix_global->cleanup_process_function(context->process());
427 // Let maestro ignore we are still alive:
428 // xbt_swag_remove(context->process(), simix_global->process_list);
430 // TODDO, Remove from proces list:
431 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
433 context->attach_stop();
438 * \brief Executes the processes from simix_global->process_to_run.
440 * The processes of simix_global->process_to_run are run (in parallel if
441 * possible). On exit, simix_global->process_to_run is empty, and
442 * simix_global->process_that_ran contains the list of processes that just ran.
443 * The two lists are swapped so, be careful when using them before and after a
444 * call to this function.
446 void SIMIX_process_runall(void)
448 SIMIX_context_runall();
450 xbt_dynar_t tmp = simix_global->process_that_ran;
451 simix_global->process_that_ran = simix_global->process_to_run;
452 simix_global->process_to_run = tmp;
453 xbt_dynar_reset(simix_global->process_to_run);
456 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
457 SIMIX_process_kill(process, simcall->issuer);
460 * \brief Internal function to kill a SIMIX process.
462 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
463 * or directly for SIMIX internal purposes.
465 * \param process poor victim
466 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
468 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
470 XBT_DEBUG("Killing process %s on %s",
471 process->name.c_str(), sg_host_get_name(process->host));
473 process->context->iwannadie = 1;
474 process->blocked = 0;
475 process->suspended = 0;
476 process->doexception = 0;
478 /* destroy the blocking synchro if any */
479 if (process->waiting_synchro) {
481 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
482 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
483 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
484 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
485 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
487 if (exec != nullptr) {
490 } else if (comm != nullptr) {
491 xbt_fifo_remove(process->comms, process->waiting_synchro);
493 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
496 } else if (sleep != nullptr) {
497 SIMIX_process_sleep_destroy(process->waiting_synchro);
499 } else if (raw != nullptr) {
500 SIMIX_synchro_stop_waiting(process, &process->simcall);
501 delete process->waiting_synchro;
503 } else if (io != nullptr) {
504 SIMIX_io_destroy(process->waiting_synchro);
508 switch (process->waiting_synchro->type) {
509 case SIMIX_SYNC_JOIN:
510 SIMIX_process_sleep_destroy(process->waiting_synchro);
514 process->waiting_synchro = NULL;
516 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
517 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
518 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
523 /** @brief Ask another process to raise the given exception
525 * @param cat category of exception
526 * @param value value associated to the exception
527 * @param msg string information associated to the exception
529 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
530 SMX_EXCEPTION(process, cat, value, msg);
532 if (process->suspended)
533 SIMIX_process_resume(process,SIMIX_process_self());
535 /* cancel the blocking synchro if any */
536 if (process->waiting_synchro) {
538 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
539 if (exec != nullptr) {
540 SIMIX_execution_cancel(process->waiting_synchro);
543 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
544 if (comm != nullptr) {
545 xbt_fifo_remove(process->comms, comm);
549 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
550 if (sleep != nullptr) {
551 SIMIX_process_sleep_destroy(process->waiting_synchro);
552 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
553 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
554 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
558 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
559 if (raw != nullptr) {
560 SIMIX_synchro_stop_waiting(process, &process->simcall);
563 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
565 SIMIX_io_destroy(process->waiting_synchro);
568 process->waiting_synchro = NULL;
572 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
573 SIMIX_process_killall(simcall->issuer, reset_pid);
576 * \brief Kills all running processes.
577 * \param issuer this one will not be killed
579 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
581 smx_process_t p = NULL;
583 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
585 SIMIX_process_kill(p,issuer);
590 simix_process_maxpid = reset_pid;
592 SIMIX_context_runall();
594 SIMIX_process_empty_trash();
597 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
599 process->new_host = dest;
601 void SIMIX_process_change_host(smx_process_t process,
604 xbt_assert((process != NULL), "Invalid parameters");
605 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
606 process->host = dest;
607 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
611 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
613 smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
615 if (process != simcall->issuer) {
616 SIMIX_simcall_answer(simcall);
618 xbt_fifo_push(sync_suspend->simcalls, simcall);
619 process->waiting_synchro = sync_suspend;
620 process->waiting_synchro->suspend();
622 /* If we are suspending ourselves, then just do not finish the simcall now */
625 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
627 if (process->suspended) {
628 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
632 process->suspended = 1;
634 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
635 if (process != issuer) {
637 if (process->waiting_synchro)
638 process->waiting_synchro->suspend();
639 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
643 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
644 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
648 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
649 SIMIX_process_resume(process, simcall->issuer);
652 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
654 XBT_IN("process = %p, issuer = %p", process, issuer);
656 if(process->context->iwannadie) {
657 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
661 if(!process->suspended) return;
662 process->suspended = 0;
664 /* If we are resuming another process, resume the synchronization it was waiting for
665 if any. Otherwise add it to the list of process to run in the next round. */
666 if (process != issuer) {
668 if (process->waiting_synchro) {
669 process->waiting_synchro->resume();
671 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
676 int SIMIX_process_get_maxpid(void) {
677 return simix_process_maxpid;
680 int SIMIX_process_count(void)
682 return xbt_swag_size(simix_global->process_list);
685 int SIMIX_process_get_PID(smx_process_t self){
692 int SIMIX_process_get_PPID(smx_process_t self){
699 void* SIMIX_process_self_get_data()
701 smx_process_t self = SIMIX_process_self();
706 return SIMIX_process_get_data(self);
709 void SIMIX_process_self_set_data(void *data)
711 smx_process_t self = SIMIX_process_self();
713 SIMIX_process_set_data(self, data);
716 void* SIMIX_process_get_data(smx_process_t process)
718 return process->data;
721 void SIMIX_process_set_data(smx_process_t process, void *data)
723 process->data = data;
726 sg_host_t SIMIX_process_get_host(smx_process_t process)
728 return process->host;
731 xbt_main_func_t SIMIX_process_get_code(void){
732 return SIMIX_process_self()->code;
735 /* needs to be public and without simcall because it is called
736 by exceptions and logging events */
737 const char* SIMIX_process_self_get_name(void) {
739 smx_process_t process = SIMIX_process_self();
740 if (process == NULL || process == simix_global->maestro_process)
743 return SIMIX_process_get_name(process);
746 const char* SIMIX_process_get_name(smx_process_t process)
748 return process->name.c_str();
751 smx_process_t SIMIX_process_get_by_name(const char* name)
754 xbt_swag_foreach(proc, simix_global->process_list) {
755 if (proc->name == name)
761 int SIMIX_process_is_suspended(smx_process_t process)
763 return process->suspended;
766 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
768 return process->properties;
771 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
773 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
774 xbt_fifo_push(sync->simcalls, simcall);
775 simcall->issuer->waiting_synchro = sync;
778 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
779 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
781 if (sleep->surf_sleep) {
782 sleep->surf_sleep->cancel();
784 smx_simcall_t simcall;
785 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sleep->simcalls))) {
786 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
787 simcall->issuer->waiting_synchro = NULL;
788 if (simcall->issuer->suspended) {
789 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
790 simcall->issuer->suspended = 0;
791 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
793 SIMIX_simcall_answer(simcall);
796 sleep->surf_sleep->unref();
797 sleep->surf_sleep = NULL;
803 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
805 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
806 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
810 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
812 if (MC_is_active() || MC_record_replay_is_active()) {
813 MC_process_clock_add(simcall->issuer, duration);
814 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
815 SIMIX_simcall_answer(simcall);
818 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
819 xbt_fifo_push(sync->simcalls, simcall);
820 simcall->issuer->waiting_synchro = sync;
823 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
825 sg_host_t host = process->host;
827 /* check if the host is active */
829 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
831 simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
832 synchro->name = NULL;
834 synchro->host = host;
835 synchro->surf_sleep = surf_host_sleep(host, duration);
836 synchro->surf_sleep->setData(synchro);
837 XBT_DEBUG("Create sleep synchronization %p", synchro);
842 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
844 XBT_DEBUG("Destroy synchro %p", synchro);
845 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
847 if (sleep->surf_sleep) {
848 sleep->surf_sleep->unref();
849 sleep->surf_sleep = NULL;
854 * \brief Calling this function makes the process to yield.
856 * Only the current process can call this function, giving back the control to
859 * \param self the current process
861 void SIMIX_process_yield(smx_process_t self)
863 XBT_DEBUG("Yield process '%s'", self->name.c_str());
865 /* Go into sleep and return control to maestro */
866 self->context->suspend();
868 /* Ok, maestro returned control to us */
869 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
871 if (self->new_host) {
872 SIMIX_process_change_host(self, self->new_host);
873 self->new_host = NULL;
876 if (self->context->iwannadie){
877 XBT_DEBUG("I wanna die!");
878 SIMIX_process_stop(self);
881 if (self->suspended) {
882 XBT_DEBUG("Hey! I'm suspended.");
883 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
885 SIMIX_process_suspend(self, self);
888 if (self->doexception) {
889 XBT_DEBUG("Wait, maestro left me an exception");
890 self->doexception = 0;
894 if(SMPI_switch_data_segment && self->segment_index != -1){
895 SMPI_switch_data_segment(self->segment_index);
899 /* callback: context fetching */
900 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
902 smx_process_t process = SIMIX_process_self();
904 return process->running_ctx;
909 /* callback: termination */
910 void SIMIX_process_exception_terminate(xbt_ex_t * e)
916 smx_context_t SIMIX_process_get_context(smx_process_t p) {
920 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
925 * \brief Returns the list of processes to run.
927 xbt_dynar_t SIMIX_process_get_runnable(void)
929 return simix_global->process_to_run;
933 * \brief Returns the process from PID.
935 smx_process_t SIMIX_process_from_PID(int PID)
938 xbt_swag_foreach(proc, simix_global->process_list) {
939 if (proc->pid == (unsigned long) PID)
945 /** @brief returns a dynar containg all currently existing processes */
946 xbt_dynar_t SIMIX_processes_as_dynar(void) {
948 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
949 xbt_swag_foreach(proc, simix_global->process_list) {
950 xbt_dynar_push(res,&proc);
956 void SIMIX_process_on_exit_runall(smx_process_t process) {
957 s_smx_process_exit_fun_t exit_fun;
958 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
959 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
960 while (!xbt_dynar_is_empty(process->on_exit)) {
961 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
962 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
966 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
967 xbt_assert(process, "current process not found: are you in maestro context ?");
969 if (!process->on_exit) {
970 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
973 s_smx_process_exit_fun_t exit_fun = {fun, data};
975 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
979 * \brief Sets the auto-restart status of the process.
980 * If set to 1, the process will be automatically restarted when its host
983 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
984 process->auto_restart = auto_restart;
987 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
988 return SIMIX_process_restart(process, simcall->issuer);
990 /** @brief Restart a process, starting it again from the beginning. */
991 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
992 XBT_DEBUG("Restarting process %s on %s",
993 process->name.c_str(), sg_host_get_name(process->host));
995 //retrieve the arguments of the old process
996 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
997 simgrid::simix::ProcessArg arg;
998 arg.name = process->name;
999 arg.code = process->code;
1000 arg.hostname = sg_host_get_name(process->host);
1001 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1002 arg.data = process->data;
1003 arg.properties = NULL;
1004 arg.auto_restart = process->auto_restart;
1005 arg.args = process->args;
1007 //kill the old process
1008 SIMIX_process_kill(process, issuer);
1010 //start the new process
1011 if (simix_global->create_process_function)
1012 return simix_global->create_process_function(
1013 arg.name.c_str(), arg.code, arg.data,
1014 arg.hostname, arg.kill_time,
1016 arg.properties, arg.auto_restart,
1019 return simcall_process_create(
1020 arg.name.c_str(), arg.code, arg.data,
1021 arg.hostname, arg.kill_time,
1022 arg.args.argc(), arg.args.to_argv(),
1023 arg.properties, arg.auto_restart);
1026 void SIMIX_segment_index_set(smx_process_t proc, int index){
1027 proc->segment_index = index;