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. */
12 #include <boost/range/algorithm.hpp>
14 #include <xbt/functional.hpp>
16 #include <xbt/sysdep.h>
20 #include <simgrid/s4u/host.hpp>
24 #include "smx_private.h"
25 #include "src/kernel/activity/SynchroIo.hpp"
26 #include "src/kernel/activity/SynchroRaw.hpp"
27 #include "src/kernel/activity/SynchroSleep.hpp"
28 #include "src/mc/Client.hpp"
29 #include "src/mc/mc_replay.h"
30 #include "src/msg/msg_private.h"
31 #include "src/surf/cpu_interface.hpp"
32 #include "src/surf/surf_interface.hpp"
35 #include "src/smpi/private.h"
38 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
40 unsigned long simix_process_maxpid = 0;
42 /** Increase the refcount for this process */
43 smx_actor_t SIMIX_process_ref(smx_actor_t process)
45 if (process != nullptr)
46 intrusive_ptr_add_ref(process);
50 /** Decrease the refcount for this process */
51 void SIMIX_process_unref(smx_actor_t process)
53 if (process != nullptr)
54 intrusive_ptr_release(process);
58 * \brief Returns the current agent.
60 * This functions returns the currently running SIMIX process.
62 * \return The SIMIX process
64 smx_actor_t SIMIX_process_self()
66 smx_context_t self_context = SIMIX_context_self();
68 return (self_context != nullptr) ? self_context->process() : nullptr;
72 * \brief Returns whether a process has pending asynchronous communications.
73 * \return true if there are asynchronous communications in this process
75 int SIMIX_process_has_pending_comms(smx_actor_t process) {
77 return xbt_fifo_size(process->comms) > 0;
81 * \brief Moves a process to the list of processes to destroy.
83 void SIMIX_process_cleanup(smx_actor_t process)
85 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
86 process->name.c_str(), process, process->waiting_synchro);
88 process->finished = true;
89 SIMIX_process_on_exit_runall(process);
91 /* Unregister from the kill timer if any */
92 if (process->kill_timer != nullptr)
93 SIMIX_timer_remove(process->kill_timer);
95 xbt_os_mutex_acquire(simix_global->mutex);
97 /* cancel non-blocking communications */
98 smx_activity_t synchro = static_cast<smx_activity_t>(xbt_fifo_pop(process->comms));
99 while (synchro != nullptr) {
100 simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
102 /* make sure no one will finish the comm after this process is destroyed,
103 * because src_proc or dst_proc would be an invalid pointer */
106 if (comm->src_proc == process) {
107 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
108 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
109 comm->src_proc = nullptr;
111 /* I'm not supposed to destroy a detached comm from the sender side, */
113 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
118 else if (comm->dst_proc == process){
119 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
120 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
121 comm->dst_proc = nullptr;
123 if (comm->detached && comm->src_proc != nullptr) {
124 /* the comm will be freed right now, remove it from the sender */
125 xbt_fifo_remove(comm->src_proc->comms, comm);
130 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
132 synchro = static_cast<smx_activity_t>(xbt_fifo_pop(process->comms));
135 XBT_DEBUG("%p should not be run anymore",process);
136 xbt_swag_remove(process, simix_global->process_list);
138 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
139 xbt_swag_insert(process, simix_global->process_to_destroy);
140 process->context->iwannadie = 0;
142 xbt_os_mutex_release(simix_global->mutex);
148 * Should be called some time to time to free the memory allocated for processes
149 * that have finished (or killed).
151 void SIMIX_process_empty_trash()
153 smx_actor_t process = nullptr;
155 while ((process = (smx_actor_t) xbt_swag_extract(simix_global->process_to_destroy))) {
156 XBT_DEBUG("Getting rid of %p",process);
157 intrusive_ptr_release(process);
164 ActorImpl::~ActorImpl()
166 delete this->context;
167 if (this->properties)
168 xbt_dict_free(&this->properties);
169 if (this->comms != nullptr)
170 xbt_fifo_free(this->comms);
172 xbt_dynar_free(&this->on_exit);
175 void create_maestro(std::function<void()> code)
177 smx_actor_t maestro = nullptr;
178 /* Create maestro process and intilialize it */
179 maestro = new simgrid::simix::ActorImpl();
180 maestro->pid = simix_process_maxpid++;
183 maestro->data = nullptr;
186 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
189 xbt_die("simix is not initialized, please call MSG_init first");
191 simix_global->context_factory->create_maestro(code, maestro);
194 maestro->simcall.issuer = maestro;
195 simix_global->maestro_process = maestro;
201 /** @brief Creates and runs the maestro process */
202 void SIMIX_maestro_create(void (*code)(void*), void* data)
204 simgrid::simix::create_maestro(std::bind(code, data));
208 * \brief Internal function to create a process.
210 * This function actually creates the process.
211 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
212 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
214 * \return the process created
216 smx_actor_t SIMIX_process_create(
218 std::function<void()> code,
222 xbt_dict_t properties,
224 smx_actor_t parent_process)
226 smx_actor_t process = nullptr;
228 XBT_DEBUG("Start process %s on host '%s'", name, host->cname());
231 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->cname());
235 process = new simgrid::simix::ActorImpl();
237 xbt_assert(code && host != nullptr, "Invalid parameters");
239 process->pid = simix_process_maxpid++;
240 process->name = simgrid::xbt::string(name);
241 process->host = host;
242 process->data = data;
243 process->comms = xbt_fifo_new();
244 process->simcall.issuer = process;
245 /* Initiliaze data segment to default value */
246 SIMIX_segment_index_set(process, -1);
248 if (parent_process != nullptr) {
249 process->ppid = parent_process->pid;
250 /* SMPI process have their own data segment and each other inherit from their father */
252 if( smpi_privatize_global_variables) {
253 if (parent_process->pid != 0) {
254 SIMIX_segment_index_set(process, parent_process->segment_index);
256 SIMIX_segment_index_set(process, process->pid - 1);
264 /* Process data for auto-restart */
265 process->auto_restart = auto_restart;
266 process->code = code;
268 XBT_VERB("Create context %s", process->name.c_str());
269 process->context = SIMIX_context_new(
271 simix_global->cleanup_process_function, process);
274 process->properties = properties;
276 /* Add the process to it's host process list */
277 xbt_swag_insert(process, sg_host_simix(host)->process_list);
279 XBT_DEBUG("Start context '%s'", process->name.c_str());
281 /* Now insert it in the global process list and in the process to run list */
282 xbt_swag_insert(process, simix_global->process_list);
283 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
284 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
286 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
287 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->cname(), process->host->cname(), kill_time);
288 process->kill_timer = SIMIX_timer_set(kill_time, [=]() {
289 simix_global->kill_process_function(process);
293 /* Tracing the process creation */
294 TRACE_msg_process_create(process->cname(), process->pid, process->host);
299 smx_actor_t SIMIX_process_attach(
302 const char* hostname,
303 xbt_dict_t properties,
304 smx_actor_t parent_process)
306 // This is mostly a copy/paste from SIMIX_process_new(),
307 // it'd be nice to share some code between those two functions.
309 sg_host_t host = sg_host_by_name(hostname);
310 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
313 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
318 smx_actor_t process = new simgrid::simix::ActorImpl();
320 process->pid = simix_process_maxpid++;
321 process->name = std::string(name);
322 process->host = host;
323 process->data = data;
324 process->comms = xbt_fifo_new();
325 process->simcall.issuer = process;
327 /* Initiliaze data segment to default value */
328 SIMIX_segment_index_set(process, -1);
329 if (parent_process != nullptr) {
330 process->ppid = parent_process->pid;
331 /* SMPI process have their own data segment and each other inherit from their father */
333 if (smpi_privatize_global_variables) {
334 if (parent_process->pid != 0) {
335 SIMIX_segment_index_set(process, parent_process->segment_index);
337 SIMIX_segment_index_set(process, process->pid - 1);
343 /* Process data for auto-restart */
344 process->auto_restart = false;
345 process->code = nullptr;
347 XBT_VERB("Create context %s", process->name.c_str());
349 xbt_die("simix is not initialized, please call MSG_init first");
350 process->context = simix_global->context_factory->attach(
351 simix_global->cleanup_process_function, process);
354 process->properties = properties;
356 /* Add the process to it's host process list */
357 xbt_swag_insert(process, sg_host_simix(host)->process_list);
359 /* Now insert it in the global process list and in the process to run list */
360 xbt_swag_insert(process, simix_global->process_list);
361 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
362 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
364 /* Tracing the process creation */
365 TRACE_msg_process_create(process->cname(), process->pid, process->host);
367 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
369 xbt_die("Not a suitable context");
371 context->attach_start();
375 void SIMIX_process_detach()
377 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
379 xbt_die("Not a suitable context");
381 simix_global->cleanup_process_function(context->process());
383 // Let maestro ignore we are still alive:
384 // xbt_swag_remove(context->process(), simix_global->process_list);
386 // TODO, Remove from proces list:
387 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
389 context->attach_stop();
394 * \brief Executes the processes from simix_global->process_to_run.
396 * The processes of simix_global->process_to_run are run (in parallel if
397 * possible). On exit, simix_global->process_to_run is empty, and
398 * simix_global->process_that_ran contains the list of processes that just ran.
399 * The two lists are swapped so, be careful when using them before and after a
400 * call to this function.
402 void SIMIX_process_runall()
404 SIMIX_context_runall();
406 xbt_dynar_t tmp = simix_global->process_that_ran;
407 simix_global->process_that_ran = simix_global->process_to_run;
408 simix_global->process_to_run = tmp;
409 xbt_dynar_reset(simix_global->process_to_run);
412 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_actor_t process) {
413 SIMIX_process_kill(process, simcall->issuer);
416 * \brief Internal function to kill a SIMIX process.
418 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
419 * or directly for SIMIX internal purposes.
421 * \param process poor victim
422 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
424 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
426 XBT_DEBUG("Killing process %s@%s", process->cname(), process->host->cname());
428 process->context->iwannadie = 1;
429 process->blocked = 0;
430 process->suspended = 0;
431 process->exception = nullptr;
433 /* destroy the blocking synchro if any */
434 if (process->waiting_synchro) {
436 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
437 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
438 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
439 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
440 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
442 if (exec != nullptr) {
445 } else if (comm != nullptr) {
446 xbt_fifo_remove(process->comms, process->waiting_synchro);
449 // Remove first occurrence of &process->simcall:
450 auto i = boost::range::find(
451 process->waiting_synchro->simcalls,
453 if (i != process->waiting_synchro->simcalls.end())
454 process->waiting_synchro->simcalls.remove(&process->simcall);
458 } else if (sleep != nullptr) {
459 SIMIX_process_sleep_destroy(process->waiting_synchro);
461 } else if (raw != nullptr) {
462 SIMIX_synchro_stop_waiting(process, &process->simcall);
463 delete process->waiting_synchro;
465 } else if (io != nullptr) {
466 SIMIX_io_destroy(process->waiting_synchro);
470 switch (process->waiting_synchro->type) {
471 case SIMIX_SYNC_JOIN:
472 SIMIX_process_sleep_destroy(process->waiting_synchro);
476 process->waiting_synchro = nullptr;
478 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
479 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
480 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
484 /** @brief Ask another process to raise the given exception
486 * @param cat category of exception
487 * @param value value associated to the exception
488 * @param msg string information associated to the exception
490 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
491 SMX_EXCEPTION(process, cat, value, msg);
493 if (process->suspended)
494 SIMIX_process_resume(process);
496 /* cancel the blocking synchro if any */
497 if (process->waiting_synchro) {
499 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec*>(process->waiting_synchro);
500 if (exec != nullptr) {
501 SIMIX_execution_cancel(process->waiting_synchro);
504 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm*>(process->waiting_synchro);
505 if (comm != nullptr) {
506 xbt_fifo_remove(process->comms, comm);
510 simgrid::kernel::activity::Sleep *sleep = dynamic_cast<simgrid::kernel::activity::Sleep*>(process->waiting_synchro);
511 if (sleep != nullptr) {
512 SIMIX_process_sleep_destroy(process->waiting_synchro);
513 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
514 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
515 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
519 simgrid::kernel::activity::Raw *raw = dynamic_cast<simgrid::kernel::activity::Raw*>(process->waiting_synchro);
520 if (raw != nullptr) {
521 SIMIX_synchro_stop_waiting(process, &process->simcall);
524 simgrid::kernel::activity::Io *io = dynamic_cast<simgrid::kernel::activity::Io*>(process->waiting_synchro);
526 SIMIX_io_destroy(process->waiting_synchro);
529 process->waiting_synchro = nullptr;
533 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
534 SIMIX_process_killall(simcall->issuer, reset_pid);
537 * \brief Kills all running processes.
538 * \param issuer this one will not be killed
540 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
542 smx_actor_t p = nullptr;
544 while ((p = (smx_actor_t) xbt_swag_extract(simix_global->process_list))) {
546 SIMIX_process_kill(p,issuer);
551 simix_process_maxpid = reset_pid;
553 SIMIX_context_runall();
555 SIMIX_process_empty_trash();
558 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_actor_t process, sg_host_t dest)
560 process->new_host = dest;
562 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
564 xbt_assert((process != nullptr), "Invalid parameters");
565 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
566 process->host = dest;
567 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
571 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
573 smx_activity_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
575 if (process != simcall->issuer) {
576 SIMIX_simcall_answer(simcall);
578 sync_suspend->simcalls.push_back(simcall);
579 process->waiting_synchro = sync_suspend;
580 process->waiting_synchro->suspend();
582 /* If we are suspending ourselves, then just do not finish the simcall now */
585 smx_activity_t SIMIX_process_suspend(smx_actor_t process, smx_actor_t issuer)
587 if (process->suspended) {
588 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
592 process->suspended = 1;
594 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
595 if (process != issuer) {
597 if (process->waiting_synchro)
598 process->waiting_synchro->suspend();
599 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
603 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0);
607 void SIMIX_process_resume(smx_actor_t process)
609 XBT_IN("process = %p", process);
611 if(process->context->iwannadie) {
612 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
616 if(!process->suspended) return;
617 process->suspended = 0;
619 /* resume the synchronization that was blocking the resumed process. */
620 if (process->waiting_synchro)
621 process->waiting_synchro->resume();
626 int SIMIX_process_get_maxpid() {
627 return simix_process_maxpid;
630 int SIMIX_process_count()
632 return xbt_swag_size(simix_global->process_list);
635 int SIMIX_process_get_PID(smx_actor_t self)
643 void* SIMIX_process_self_get_data()
645 smx_actor_t self = SIMIX_process_self();
650 return SIMIX_process_get_data(self);
653 void SIMIX_process_self_set_data(void *data)
655 smx_actor_t self = SIMIX_process_self();
657 SIMIX_process_set_data(self, data);
660 void* SIMIX_process_get_data(smx_actor_t process)
662 return process->data;
665 void SIMIX_process_set_data(smx_actor_t process, void *data)
667 process->data = data;
670 /* needs to be public and without simcall because it is called
671 by exceptions and logging events */
672 const char* SIMIX_process_self_get_name() {
674 smx_actor_t process = SIMIX_process_self();
675 if (process == nullptr || process == simix_global->maestro_process)
678 return process->name.c_str();
681 smx_actor_t SIMIX_process_get_by_name(const char* name)
684 xbt_swag_foreach(proc, simix_global->process_list) {
685 if (proc->name == name)
691 int SIMIX_process_is_suspended(smx_actor_t process)
693 return process->suspended;
696 xbt_dict_t SIMIX_process_get_properties(smx_actor_t process)
698 return process->properties;
701 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
703 if (process->finished) {
704 // The joined process is already finished, just wake up the issuer process right away
705 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
706 SIMIX_simcall_answer(simcall);
709 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
710 sync->simcalls.push_back(simcall);
711 simcall->issuer->waiting_synchro = sync;
714 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_activity_t synchro){
715 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
717 if (sleep->surf_sleep) {
718 sleep->surf_sleep->cancel();
720 while (!sleep->simcalls.empty()) {
721 smx_simcall_t simcall = sleep->simcalls.front();
722 sleep->simcalls.pop_front();
723 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
724 simcall->issuer->waiting_synchro = nullptr;
725 if (simcall->issuer->suspended) {
726 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
727 simcall->issuer->suspended = 0;
728 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
730 SIMIX_simcall_answer(simcall);
733 sleep->surf_sleep->unref();
734 sleep->surf_sleep = nullptr;
740 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
742 smx_activity_t res = SIMIX_process_sleep(issuer, timeout);
743 static_cast<simgrid::kernel::activity::ActivityImpl*>(res)->ref();
744 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
748 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
750 if (MC_is_active() || MC_record_replay_is_active()) {
751 MC_process_clock_add(simcall->issuer, duration);
752 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
753 SIMIX_simcall_answer(simcall);
756 smx_activity_t sync = SIMIX_process_sleep(simcall->issuer, duration);
757 sync->simcalls.push_back(simcall);
758 simcall->issuer->waiting_synchro = sync;
761 smx_activity_t SIMIX_process_sleep(smx_actor_t process, double duration)
763 sg_host_t host = process->host;
766 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->cname());
768 simgrid::kernel::activity::Sleep *synchro = new simgrid::kernel::activity::Sleep();
769 synchro->host = host;
770 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
771 synchro->surf_sleep->setData(synchro);
772 XBT_DEBUG("Create sleep synchronization %p", synchro);
777 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
779 XBT_DEBUG("Destroy synchro %p", synchro);
780 simgrid::kernel::activity::Sleep *sleep = static_cast<simgrid::kernel::activity::Sleep*>(synchro);
782 if (sleep->surf_sleep) {
783 sleep->surf_sleep->unref();
784 sleep->surf_sleep = nullptr;
790 * \brief Calling this function makes the process to yield.
792 * Only the current process can call this function, giving back the control to
795 * \param self the current process
797 void SIMIX_process_yield(smx_actor_t self)
799 XBT_DEBUG("Yield process '%s'", self->name.c_str());
801 /* Go into sleep and return control to maestro */
802 self->context->suspend();
804 /* Ok, maestro returned control to us */
805 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
807 if (self->new_host) {
808 SIMIX_process_change_host(self, self->new_host);
809 self->new_host = nullptr;
812 if (self->context->iwannadie){
813 XBT_DEBUG("I wanna die!");
814 self->finished = true;
815 /* execute the on_exit functions */
816 SIMIX_process_on_exit_runall(self);
817 /* Add the process to the list of process to restart, only if the host is down */
818 if (self->auto_restart && self->host->isOff()) {
819 SIMIX_host_add_auto_restart_process(self->host, self->cname(),
820 self->code, self->data,
821 SIMIX_timer_get_date(self->kill_timer),
825 XBT_DEBUG("Process %s@%s is dead", self->cname(), self->host->cname());
826 self->context->stop();
829 if (self->suspended) {
830 XBT_DEBUG("Hey! I'm suspended.");
831 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
833 SIMIX_process_suspend(self, self);
836 if (self->exception != nullptr) {
837 XBT_DEBUG("Wait, maestro left me an exception");
838 std::exception_ptr exception = std::move(self->exception);
839 self->exception = nullptr;
840 std::rethrow_exception(std::move(exception));
843 if(SMPI_switch_data_segment && self->segment_index != -1){
844 SMPI_switch_data_segment(self->segment_index);
848 /* callback: termination */
849 void SIMIX_process_exception_terminate(xbt_ex_t * e)
855 smx_context_t SIMIX_process_get_context(smx_actor_t p) {
859 void SIMIX_process_set_context(smx_actor_t p,smx_context_t c) {
864 * \brief Returns the list of processes to run.
866 xbt_dynar_t SIMIX_process_get_runnable()
868 return simix_global->process_to_run;
872 * \brief Returns the process from PID.
874 smx_actor_t SIMIX_process_from_PID(int PID)
877 xbt_swag_foreach(proc, simix_global->process_list) {
878 if (proc->pid == static_cast<unsigned long> (PID))
884 /** @brief returns a dynar containing all currently existing processes */
885 xbt_dynar_t SIMIX_processes_as_dynar() {
887 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_actor_t),nullptr);
888 xbt_swag_foreach(proc, simix_global->process_list) {
889 xbt_dynar_push(res,&proc);
894 void SIMIX_process_on_exit_runall(smx_actor_t process) {
895 s_smx_process_exit_fun_t exit_fun;
896 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
897 while (!xbt_dynar_is_empty(process->on_exit)) {
898 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
899 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
903 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
904 xbt_assert(process, "current process not found: are you in maestro context ?");
906 if (!process->on_exit) {
907 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), nullptr);
910 s_smx_process_exit_fun_t exit_fun = {fun, data};
912 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
916 * \brief Sets the auto-restart status of the process.
917 * If set to 1, the process will be automatically restarted when its host
920 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
921 process->auto_restart = auto_restart;
924 smx_actor_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_actor_t process) {
925 return SIMIX_process_restart(process, simcall->issuer);
927 /** @brief Restart a process, starting it again from the beginning. */
928 smx_actor_t SIMIX_process_restart(smx_actor_t process, smx_actor_t issuer) {
929 XBT_DEBUG("Restarting process %s on %s", process->cname(), process->host->cname());
931 //retrieve the arguments of the old process
932 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
933 simgrid::simix::ProcessArg arg;
934 arg.name = process->name;
935 arg.code = process->code;
936 arg.host = process->host;
937 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
938 arg.data = process->data;
939 arg.properties = nullptr;
940 arg.auto_restart = process->auto_restart;
942 //kill the old process
943 SIMIX_process_kill(process, issuer);
945 //start the new process
946 return simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
947 arg.kill_time, arg.properties, arg.auto_restart, nullptr);
950 void SIMIX_segment_index_set(smx_actor_t proc, int index){
951 proc->segment_index = index;
955 * \ingroup simix_process_management
956 * \brief Creates and runs a new SIMIX process.
958 * The structure and the corresponding thread are created and put in the list of ready processes.
960 * \param name a name for the process. It is for user-level information and can be nullptr.
961 * \param code the main function of the process
962 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can be nullptr.
963 * It can be retrieved with the function \ref simcall_process_get_data.
964 * \param hostname name of the host where the new agent is executed.
965 * \param kill_time time when the process is killed
966 * \param argc first argument passed to \a code
967 * \param argv second argument passed to \a code
968 * \param properties the properties of the process
969 * \param auto_restart either it is autorestarting or not.
971 smx_actor_t simcall_process_create(const char *name,
972 xbt_main_func_t code,
976 int argc, char **argv,
977 xbt_dict_t properties,
982 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
983 for (int i = 0; i != argc; ++i)
986 smx_actor_t res = simcall_process_create(name,
987 std::move(wrapped_code),
988 data, host, kill_time, properties, auto_restart);
992 smx_actor_t simcall_process_create(
993 const char *name, std::function<void()> code, void *data,
994 sg_host_t host, double kill_time,
995 xbt_dict_t properties, int auto_restart)
999 smx_actor_t self = SIMIX_process_self();
1000 return simgrid::simix::kernelImmediate([&] {
1001 return SIMIX_process_create(name, std::move(code), data, host, kill_time, properties, auto_restart, self);