1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include <boost/range/algorithm.hpp>
15 #include "xbt/functional.hpp"
17 #include "xbt/sysdep.h"
18 #include "xbt/utility.hpp"
20 #include "simgrid/s4u/Host.hpp"
24 #include "smx_private.hpp"
25 #include "src/kernel/activity/SleepImpl.hpp"
26 #include "src/kernel/activity/SynchroIo.hpp"
27 #include "src/kernel/activity/SynchroRaw.hpp"
28 #include "src/mc/mc_replay.hpp"
29 #include "src/mc/remote/Client.hpp"
30 #include "src/msg/msg_private.hpp"
31 #include "src/surf/cpu_interface.hpp"
32 #include "src/surf/surf_interface.hpp"
35 #include "src/smpi/include/private.hpp"
38 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
40 unsigned long simix_process_maxpid = 0;
43 * \brief Returns the current agent.
45 * This functions returns the currently running SIMIX process.
47 * \return The SIMIX process
49 smx_actor_t SIMIX_process_self()
51 smx_context_t self_context = SIMIX_context_self();
53 return (self_context != nullptr) ? self_context->process() : nullptr;
57 * \brief Returns whether a process has pending asynchronous communications.
58 * \return true if there are asynchronous communications in this process
60 int SIMIX_process_has_pending_comms(smx_actor_t process) {
62 return process->comms.size() > 0;
66 * \brief Moves a process to the list of processes to destroy.
68 void SIMIX_process_cleanup(smx_actor_t process)
70 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->name.c_str(), process,
71 process->waiting_synchro.get());
73 process->finished = true;
74 SIMIX_process_on_exit_runall(process);
76 /* Unregister from the kill timer if any */
77 if (process->kill_timer != nullptr) {
78 SIMIX_timer_remove(process->kill_timer);
79 process->kill_timer = nullptr;
82 xbt_os_mutex_acquire(simix_global->mutex);
84 /* cancel non-blocking communications */
85 while (not process->comms.empty()) {
86 smx_activity_t synchro = process->comms.front();
87 process->comms.pop_front();
88 simgrid::kernel::activity::CommImplPtr comm =
89 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
91 /* make sure no one will finish the comm after this process is destroyed,
92 * because src_proc or dst_proc would be an invalid pointer */
94 if (comm->src_proc == process) {
95 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
96 comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
97 comm->src_proc = nullptr;
99 } else if (comm->dst_proc == process) {
100 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state,
101 comm->src_proc, comm->dst_proc);
102 comm->dst_proc = nullptr;
104 if (comm->detached && comm->src_proc != nullptr) {
105 /* the comm will be freed right now, remove it from the sender */
106 comm->src_proc->comms.remove(comm);
109 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
114 XBT_DEBUG("%p should not be run anymore",process);
115 simix_global->process_list.erase(process->pid);
116 if (process->host && process->host_process_list_hook.is_linked())
117 simgrid::xbt::intrusive_erase(process->host->extension<simgrid::simix::Host>()->process_list, *process);
118 if (not xbt_swag_belongs(process, simix_global->process_to_destroy)) {
120 xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
122 xbt_swag_insert(process, simix_global->process_to_destroy);
124 process->context->iwannadie = 0;
126 xbt_os_mutex_release(simix_global->mutex);
132 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
134 void SIMIX_process_empty_trash()
136 smx_actor_t process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
139 XBT_DEBUG("Getting rid of %p",process);
140 intrusive_ptr_release(process);
141 process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
144 xbt_dynar_reset(simix_global->dead_actors_vector);
151 ActorImpl::~ActorImpl()
153 delete this->context;
156 static int dying_daemon(void* exit_status, void* data)
158 std::vector<ActorImpl*>* vect = &simix_global->daemons;
160 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
161 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
163 /* Don't move the whole content since we don't really care about the order */
164 std::swap(*it, vect->back());
169 /** This process will be terminated automatically when the last non-daemon process finishes */
170 void ActorImpl::daemonize()
174 simix_global->daemons.push_back(this);
175 SIMIX_process_on_exit(this, dying_daemon, this);
179 simgrid::s4u::Actor* ActorImpl::restart()
181 XBT_DEBUG("Restarting process %s on %s", getCname(), host->getCname());
183 // retrieve the arguments of the old process
184 // FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
185 simgrid::simix::ProcessArg arg;
189 arg.kill_time = SIMIX_timer_get_date(kill_timer);
191 arg.properties = nullptr;
192 arg.auto_restart = auto_restart;
194 // kill the old process
195 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
197 // start the new process
198 ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
199 arg.properties.get(), nullptr);
200 if (arg.kill_time >= 0)
201 simcall_process_set_kill_time(actor, arg.kill_time);
202 if (arg.auto_restart)
203 actor->auto_restart = arg.auto_restart;
205 return actor->ciface();
208 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
211 XBT_DEBUG("Actor '%s' is already suspended", name.c_str());
217 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
218 if (this != issuer) {
220 waiting_synchro->suspend();
221 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
225 return SIMIX_execution_start(this, "suspend", 0.0, 1.0, 0.0);
229 void ActorImpl::resume()
231 XBT_IN("process = %p", this);
233 if (context->iwannadie) {
234 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
242 /* resume the synchronization that was blocking the resumed actor. */
244 waiting_synchro->resume();
249 smx_activity_t ActorImpl::sleep(double duration)
252 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->getCname());
254 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
255 synchro->host = host;
256 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
257 synchro->surf_sleep->setData(synchro);
258 XBT_DEBUG("Create sleep synchronization %p", synchro);
263 void create_maestro(std::function<void()> code)
265 smx_actor_t maestro = nullptr;
266 /* Create maestro process and initialize it */
267 maestro = new simgrid::simix::ActorImpl();
268 maestro->pid = simix_process_maxpid++;
270 maestro->userdata = nullptr;
273 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
275 if (not simix_global)
276 xbt_die("simix is not initialized, please call MSG_init first");
277 maestro->context = simix_global->context_factory->create_maestro(code, maestro);
280 maestro->simcall.issuer = maestro;
281 simix_global->maestro_process = maestro;
287 /** @brief Creates and runs the maestro process */
288 void SIMIX_maestro_create(void (*code)(void*), void* data)
290 simgrid::simix::create_maestro(std::bind(code, data));
294 * \brief Internal function to create a process.
296 * This function actually creates the process.
297 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
298 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
300 * \return the process created
302 smx_actor_t SIMIX_process_create(const char* name, std::function<void()> code, void* data, simgrid::s4u::Host* host,
303 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
306 XBT_DEBUG("Start process %s on host '%s'", name, host->getCname());
309 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->getCname());
313 smx_actor_t process = new simgrid::simix::ActorImpl();
315 xbt_assert(code && host != nullptr, "Invalid parameters");
317 process->pid = simix_process_maxpid++;
318 process->name = simgrid::xbt::string(name);
319 process->host = host;
320 process->userdata = data;
321 process->simcall.issuer = process;
323 if (parent_process != nullptr) {
324 process->ppid = parent_process->pid;
325 /* SMPI process have their own data segment and each other inherit from their father */
327 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
328 if (parent_process->pid != 0) {
329 process->segment_index = parent_process->segment_index;
331 process->segment_index = process->pid - 1;
337 process->code = code;
339 XBT_VERB("Create context %s", process->name.c_str());
340 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
343 if (properties != nullptr)
344 for (auto const& kv : *properties)
345 process->setProperty(kv.first, kv.second);
347 /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
348 if (host->extension<simgrid::simix::Host>() == nullptr)
349 host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
351 /* Add the process to its host process list */
352 host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
354 XBT_DEBUG("Start context '%s'", process->name.c_str());
356 /* Now insert it in the global process list and in the process to run list */
357 simix_global->process_list[process->pid] = process;
358 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->getCname(), host->getCname());
359 simix_global->process_to_run.push_back(process);
360 intrusive_ptr_add_ref(process);
362 /* Tracing the process creation */
363 TRACE_msg_process_create(process->getName(), process->pid, process->host);
368 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
369 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
371 // This is mostly a copy/paste from SIMIX_process_new(),
372 // it'd be nice to share some code between those two functions.
374 sg_host_t host = sg_host_by_name(hostname);
375 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
378 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
382 smx_actor_t process = new simgrid::simix::ActorImpl();
384 process->pid = simix_process_maxpid++;
385 process->name = std::string(name);
386 process->host = host;
387 process->userdata = data;
388 process->simcall.issuer = process;
390 if (parent_process != nullptr) {
391 process->ppid = parent_process->pid;
392 /* SMPI process have their own data segment and each other inherit from their father */
394 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
395 if (parent_process->pid != 0) {
396 process->segment_index = parent_process->segment_index;
398 process->segment_index = process->pid - 1;
404 /* Process data for auto-restart */
405 process->code = nullptr;
407 XBT_VERB("Create context %s", process->name.c_str());
408 if (not simix_global)
409 xbt_die("simix is not initialized, please call MSG_init first");
410 process->context = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
413 if (properties != nullptr)
414 for (auto const& kv : *properties)
415 process->setProperty(kv.first, kv.second);
417 /* Add the process to it's host process list */
418 host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
420 /* Now insert it in the global process list and in the process to run list */
421 simix_global->process_list[process->pid] = process;
422 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->getCname(), host->getCname());
423 simix_global->process_to_run.push_back(process);
425 /* Tracing the process creation */
426 TRACE_msg_process_create(process->getName(), process->pid, process->host);
428 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
430 xbt_die("Not a suitable context");
432 context->attach_start();
436 void SIMIX_process_detach()
438 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
440 xbt_die("Not a suitable context");
442 auto process = context->process();
443 simix_global->cleanup_process_function(process);
444 context->attach_stop();
449 * \brief Executes the processes from simix_global->process_to_run.
451 * The processes of simix_global->process_to_run are run (in parallel if
452 * possible). On exit, simix_global->process_to_run is empty, and
453 * simix_global->process_that_ran contains the list of processes that just ran.
454 * The two lists are swapped so, be careful when using them before and after a
455 * call to this function.
457 void SIMIX_process_runall()
459 SIMIX_context_runall();
461 simix_global->process_to_run.swap(simix_global->process_that_ran);
462 simix_global->process_to_run.clear();
466 * \brief Internal function to kill a SIMIX process.
468 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
469 * or directly for SIMIX internal purposes.
471 * \param process poor victim
472 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
474 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
476 if (process->finished) {
477 XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", process->getCname(),
478 process->host->getCname());
482 XBT_DEBUG("Killing process %s@%s", process->getCname(), process->host->getCname());
484 process->context->iwannadie = 1;
485 process->blocked = 0;
486 process->suspended = 0;
487 process->exception = nullptr;
489 /* destroy the blocking synchro if any */
490 if (process->waiting_synchro != nullptr) {
492 simgrid::kernel::activity::ExecImplPtr exec =
493 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
494 simgrid::kernel::activity::CommImplPtr comm =
495 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
496 simgrid::kernel::activity::SleepImplPtr sleep =
497 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
498 simgrid::kernel::activity::RawImplPtr raw =
499 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
500 simgrid::kernel::activity::IoImplPtr io =
501 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
503 if (exec != nullptr) {
505 } else if (comm != nullptr) {
506 process->comms.remove(process->waiting_synchro);
508 // Remove first occurrence of &process->simcall:
509 auto i = boost::range::find(process->waiting_synchro->simcalls, &process->simcall);
510 if (i != process->waiting_synchro->simcalls.end())
511 process->waiting_synchro->simcalls.remove(&process->simcall);
512 } else if (sleep != nullptr) {
513 SIMIX_process_sleep_destroy(process->waiting_synchro);
515 } else if (raw != nullptr) {
516 SIMIX_synchro_stop_waiting(process, &process->simcall);
518 } else if (io != nullptr) {
519 SIMIX_io_destroy(process->waiting_synchro);
521 xbt_die("Unknown type of activity");
524 process->waiting_synchro = nullptr;
526 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
527 end(simix_global->process_to_run) &&
529 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
530 simix_global->process_to_run.push_back(process);
534 /** @brief Ask another process to raise the given exception
536 * @param process The process that should raise that exception
537 * @param cat category of exception
538 * @param value value associated to the exception
539 * @param msg string information associated to the exception
541 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
542 SMX_EXCEPTION(process, cat, value, msg);
544 if (process->suspended)
547 /* cancel the blocking synchro if any */
548 if (process->waiting_synchro) {
550 simgrid::kernel::activity::ExecImplPtr exec =
551 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
552 if (exec != nullptr && exec->surf_exec)
553 exec->surf_exec->cancel();
555 simgrid::kernel::activity::CommImplPtr comm =
556 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
557 if (comm != nullptr) {
558 process->comms.remove(comm);
562 simgrid::kernel::activity::SleepImplPtr sleep =
563 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
564 if (sleep != nullptr) {
565 SIMIX_process_sleep_destroy(process->waiting_synchro);
566 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
567 end(simix_global->process_to_run) &&
568 process != SIMIX_process_self()) {
569 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
570 simix_global->process_to_run.push_back(process);
574 simgrid::kernel::activity::RawImplPtr raw =
575 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
576 if (raw != nullptr) {
577 SIMIX_synchro_stop_waiting(process, &process->simcall);
580 simgrid::kernel::activity::IoImplPtr io =
581 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
583 SIMIX_io_destroy(process->waiting_synchro);
586 process->waiting_synchro = nullptr;
590 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
591 SIMIX_process_killall(simcall->issuer, reset_pid);
594 * \brief Kills all running processes.
595 * \param issuer this one will not be killed
597 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
599 for (auto const& kv : simix_global->process_list)
600 if (kv.second != issuer)
601 SIMIX_process_kill(kv.second, issuer);
604 simix_process_maxpid = reset_pid;
607 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
609 xbt_assert((process != nullptr), "Invalid parameters");
610 simgrid::xbt::intrusive_erase(process->host->extension<simgrid::simix::Host>()->process_list, *process);
611 process->host = dest;
612 dest->extension<simgrid::simix::Host>()->process_list.push_back(*process);
615 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
617 smx_activity_t sync_suspend = process->suspend(simcall->issuer);
619 if (process != simcall->issuer) {
620 SIMIX_simcall_answer(simcall);
622 sync_suspend->simcalls.push_back(simcall);
623 process->waiting_synchro = sync_suspend;
624 process->waiting_synchro->suspend();
626 /* If we are suspending ourselves, then just do not finish the simcall now */
629 int SIMIX_process_get_maxpid() {
630 return simix_process_maxpid;
633 int SIMIX_process_count()
635 return simix_global->process_list.size();
638 void* SIMIX_process_self_get_data()
640 smx_actor_t self = SIMIX_process_self();
645 return self->getUserData();
648 void SIMIX_process_self_set_data(void *data)
650 SIMIX_process_self()->setUserData(data);
654 /* needs to be public and without simcall because it is called
655 by exceptions and logging events */
656 const char* SIMIX_process_self_get_name() {
658 smx_actor_t process = SIMIX_process_self();
659 if (process == nullptr || process == simix_global->maestro_process)
662 return process->name.c_str();
665 smx_actor_t SIMIX_process_get_by_name(const char* name)
667 for (auto const& kv : simix_global->process_list)
668 if (kv.second->name == name)
673 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
675 if (process->finished) {
676 // The joined process is already finished, just wake up the issuer process right away
677 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
678 SIMIX_simcall_answer(simcall);
681 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
682 sync->simcalls.push_back(simcall);
683 simcall->issuer->waiting_synchro = sync;
686 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
688 smx_activity_t res = issuer->sleep(timeout);
689 intrusive_ptr_add_ref(res.get());
690 SIMIX_process_on_exit(process,
691 [](void*, void* arg) {
692 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
693 if (sleep->surf_sleep)
694 sleep->surf_sleep->finish(simgrid::surf::Action::State::done);
695 intrusive_ptr_release(sleep);
702 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
704 if (MC_is_active() || MC_record_replay_is_active()) {
705 MC_process_clock_add(simcall->issuer, duration);
706 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
707 SIMIX_simcall_answer(simcall);
710 smx_activity_t sync = simcall->issuer->sleep(duration);
711 sync->simcalls.push_back(simcall);
712 simcall->issuer->waiting_synchro = sync;
715 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
717 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
718 simgrid::kernel::activity::SleepImplPtr sleep =
719 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
721 if (sleep->surf_sleep) {
722 sleep->surf_sleep->unref();
723 sleep->surf_sleep = nullptr;
728 * \brief Calling this function makes the process to yield.
730 * Only the current process can call this function, giving back the control to maestro.
732 * \param self the current process
734 void SIMIX_process_yield(smx_actor_t self)
736 XBT_DEBUG("Yield actor '%s'", self->getCname());
738 /* Go into sleep and return control to maestro */
739 self->context->suspend();
741 /* Ok, maestro returned control to us */
742 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
744 if (self->new_host) {
745 SIMIX_process_change_host(self, self->new_host);
746 self->new_host = nullptr;
749 if (self->context->iwannadie){
750 XBT_DEBUG("I wanna die!");
751 self->finished = true;
752 /* execute the on_exit functions */
753 SIMIX_process_on_exit_runall(self);
754 /* Add the process to the list of process to restart, only if the host is down */
755 if (self->auto_restart && self->host->isOff()) {
756 SIMIX_host_add_auto_restart_process(self->host, self->getCname(), self->code, self->userdata,
757 SIMIX_timer_get_date(self->kill_timer), self->getProperties(),
760 XBT_DEBUG("Process %s@%s is dead", self->getCname(), self->host->getCname());
761 self->context->stop();
764 if (self->suspended) {
765 XBT_DEBUG("Hey! I'm suspended.");
766 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
771 if (self->exception != nullptr) {
772 XBT_DEBUG("Wait, maestro left me an exception");
773 std::exception_ptr exception = std::move(self->exception);
774 self->exception = nullptr;
775 std::rethrow_exception(std::move(exception));
778 if(SMPI_switch_data_segment && self->segment_index != -1){
779 SMPI_switch_data_segment(self->segment_index);
783 /* callback: termination */
784 void SIMIX_process_exception_terminate(xbt_ex_t * e)
790 /** @brief Returns the list of processes to run. */
791 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
793 return simix_global->process_to_run;
796 /** @brief Returns the process from PID. */
797 smx_actor_t SIMIX_process_from_PID(aid_t PID)
799 auto process = simix_global->process_list.find(PID);
800 return process == simix_global->process_list.end() ? nullptr : process->second;
803 void SIMIX_process_on_exit_runall(smx_actor_t process) {
804 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
805 while (not process->on_exit.empty()) {
806 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
807 process->on_exit.pop_back();
808 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
812 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
813 xbt_assert(process, "current process not found: are you in maestro context ?");
815 s_smx_process_exit_fun_t exit_fun = {fun, data};
817 process->on_exit.push_back(exit_fun);
821 * \brief Sets the auto-restart status of the process.
822 * If set to 1, the process will be automatically restarted when its host comes back.
824 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
825 process->auto_restart = auto_restart;
828 /** @brief Restart a process, starting it again from the beginning. */
830 * \ingroup simix_process_management
831 * \brief Creates and runs a new SIMIX process.
833 * The structure and the corresponding thread are created and put in the list of ready processes.
835 * \param name a name for the process. It is for user-level information and can be nullptr.
836 * \param code the main function of the process
837 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
839 * It can be retrieved with the function \ref simcall_process_get_data.
840 * \param host where the new agent is executed.
841 * \param kill_time time when the process is killed
842 * \param argc first argument passed to \a code
843 * \param argv second argument passed to \a code
844 * \param properties the properties of the process
845 * \param auto_restart either it is autorestarting or not.
847 extern "C" smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host,
848 int argc, char** argv, std::map<std::string, std::string>* properties)
852 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
853 for (int i = 0; i != argc; ++i)
856 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
860 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
861 std::map<std::string, std::string>* properties)
865 smx_actor_t self = SIMIX_process_self();
866 return simgrid::simix::kernelImmediate([name, code, data, host, properties, self] {
867 return SIMIX_process_create(name, std::move(code), data, host, properties, self);