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. */
11 #include <boost/range/algorithm.hpp>
15 #include "xbt/functional.hpp"
17 #include "xbt/sysdep.h"
19 #include "simgrid/s4u/Host.hpp"
23 #include "smx_private.h"
24 #include "src/kernel/activity/SleepImpl.hpp"
25 #include "src/kernel/activity/SynchroIo.hpp"
26 #include "src/kernel/activity/SynchroRaw.hpp"
27 #include "src/mc/mc_replay.h"
28 #include "src/mc/remote/Client.hpp"
29 #include "src/msg/msg_private.h"
30 #include "src/surf/cpu_interface.hpp"
31 #include "src/surf/surf_interface.hpp"
34 #include "src/smpi/private.h"
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
39 unsigned long simix_process_maxpid = 0;
41 /** Increase the refcount for this process */
42 smx_actor_t SIMIX_process_ref(smx_actor_t process)
44 if (process != nullptr)
45 intrusive_ptr_add_ref(process);
49 /** Decrease the refcount for this process */
50 void SIMIX_process_unref(smx_actor_t process)
52 if (process != nullptr)
53 intrusive_ptr_release(process);
57 * \brief Returns the current agent.
59 * This functions returns the currently running SIMIX process.
61 * \return The SIMIX process
63 smx_actor_t SIMIX_process_self()
65 smx_context_t self_context = SIMIX_context_self();
67 return (self_context != nullptr) ? self_context->process() : nullptr;
71 * \brief Returns whether a process has pending asynchronous communications.
72 * \return true if there are asynchronous communications in this process
74 int SIMIX_process_has_pending_comms(smx_actor_t process) {
76 return process->comms.size() > 0;
80 * \brief Moves a process to the list of processes to destroy.
82 void SIMIX_process_cleanup(smx_actor_t process)
84 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->name.c_str(), process,
85 process->waiting_synchro.get());
87 process->finished = true;
88 SIMIX_process_on_exit_runall(process);
90 /* Unregister from the kill timer if any */
91 if (process->kill_timer != nullptr)
92 SIMIX_timer_remove(process->kill_timer);
94 xbt_os_mutex_acquire(simix_global->mutex);
96 /* cancel non-blocking communications */
97 smx_activity_t synchro = process->comms.front();
98 while (not process->comms.empty()) {
99 simgrid::kernel::activity::CommImplPtr comm =
100 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(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 */
105 if (comm->src_proc == process) {
106 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
107 comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
108 comm->src_proc = nullptr;
110 } else if (comm->dst_proc == process) {
111 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state,
112 comm->src_proc, comm->dst_proc);
113 comm->dst_proc = nullptr;
115 if (comm->detached && comm->src_proc != nullptr) {
116 /* the comm will be freed right now, remove it from the sender */
117 comm->src_proc->comms.remove(comm);
120 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
122 process->comms.pop_front();
123 synchro = process->comms.front();
127 XBT_DEBUG("%p should not be run anymore",process);
128 simix_global->process_list.erase(process->pid);
130 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
131 xbt_swag_insert(process, simix_global->process_to_destroy);
132 process->context->iwannadie = 0;
134 xbt_os_mutex_release(simix_global->mutex);
140 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
142 void SIMIX_process_empty_trash()
144 smx_actor_t process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
147 XBT_DEBUG("Getting rid of %p",process);
148 intrusive_ptr_release(process);
149 process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
156 ActorImpl::~ActorImpl()
158 delete this->context;
159 xbt_dict_free(&this->properties);
162 static int dying_daemon(void* exit_status, void* data)
164 std::vector<ActorImpl*>* vect = &simix_global->daemons;
166 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
167 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
169 /* Don't move the whole content since we don't really care about the order */
170 std::swap(*it, vect->back());
175 /** This process will be terminated automatically when the last non-daemon process finishes */
176 void ActorImpl::daemonize()
180 simix_global->daemons.push_back(this);
181 SIMIX_process_on_exit(this, dying_daemon, this);
185 /** Whether this process is daemonized */
186 bool ActorImpl::isDaemon()
191 void create_maestro(std::function<void()> code)
193 smx_actor_t maestro = nullptr;
194 /* Create maestro process and initialize it */
195 maestro = new simgrid::simix::ActorImpl();
196 maestro->pid = simix_process_maxpid++;
198 maestro->data = nullptr;
201 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
203 if (not simix_global)
204 xbt_die("simix is not initialized, please call MSG_init first");
206 simix_global->context_factory->create_maestro(code, maestro);
209 maestro->simcall.issuer = maestro;
210 simix_global->maestro_process = maestro;
216 /** @brief Creates and runs the maestro process */
217 void SIMIX_maestro_create(void (*code)(void*), void* data)
219 simgrid::simix::create_maestro(std::bind(code, data));
223 * \brief Internal function to create a process.
225 * This function actually creates the process.
226 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
227 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
229 * \return the process created
231 smx_actor_t SIMIX_process_create(const char* name, std::function<void()> code, void* data, simgrid::s4u::Host* host,
232 xbt_dict_t properties, smx_actor_t parent_process)
235 XBT_DEBUG("Start process %s on host '%s'", name, host->cname());
238 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->cname());
242 smx_actor_t process = new simgrid::simix::ActorImpl();
244 xbt_assert(code && host != nullptr, "Invalid parameters");
246 process->pid = simix_process_maxpid++;
247 process->name = simgrid::xbt::string(name);
248 process->host = host;
249 process->data = data;
250 process->simcall.issuer = process;
252 if (parent_process != nullptr) {
253 process->ppid = parent_process->pid;
254 /* SMPI process have their own data segment and each other inherit from their father */
256 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
257 if (parent_process->pid != 0) {
258 SIMIX_segment_index_set(process, parent_process->segment_index);
260 SIMIX_segment_index_set(process, process->pid - 1);
266 process->code = code;
268 XBT_VERB("Create context %s", process->name.c_str());
269 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
272 process->properties = properties;
274 /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
275 if (host->extension<simgrid::simix::Host>() == nullptr)
276 host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
278 /* Add the process to its host process list */
279 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
281 XBT_DEBUG("Start context '%s'", process->name.c_str());
283 /* Now insert it in the global process list and in the process to run list */
284 simix_global->process_list[process->pid] = process;
285 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
286 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
287 intrusive_ptr_add_ref(process);
289 /* Tracing the process creation */
290 TRACE_msg_process_create(process->cname(), process->pid, process->host);
295 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname, xbt_dict_t properties,
296 smx_actor_t parent_process)
298 // This is mostly a copy/paste from SIMIX_process_new(),
299 // it'd be nice to share some code between those two functions.
301 sg_host_t host = sg_host_by_name(hostname);
302 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
305 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
310 smx_actor_t process = new simgrid::simix::ActorImpl();
312 process->pid = simix_process_maxpid++;
313 process->name = std::string(name);
314 process->host = host;
315 process->data = data;
316 process->simcall.issuer = process;
318 if (parent_process != nullptr) {
319 process->ppid = parent_process->pid;
320 /* SMPI process have their own data segment and each other inherit from their father */
322 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
323 if (parent_process->pid != 0) {
324 SIMIX_segment_index_set(process, parent_process->segment_index);
326 SIMIX_segment_index_set(process, process->pid - 1);
332 /* Process data for auto-restart */
333 process->code = nullptr;
335 XBT_VERB("Create context %s", process->name.c_str());
336 if (not simix_global)
337 xbt_die("simix is not initialized, please call MSG_init first");
338 process->context = simix_global->context_factory->attach(
339 simix_global->cleanup_process_function, process);
342 process->properties = properties;
344 /* Add the process to it's host process list */
345 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
347 /* Now insert it in the global process list and in the process to run list */
348 simix_global->process_list[process->pid] = process;
349 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->cname());
350 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
352 /* Tracing the process creation */
353 TRACE_msg_process_create(process->cname(), process->pid, process->host);
355 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
357 xbt_die("Not a suitable context");
359 context->attach_start();
363 void SIMIX_process_detach()
365 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
367 xbt_die("Not a suitable context");
369 simix_global->cleanup_process_function(context->process());
371 // Let maestro ignore we are still alive:
372 // xbt_swag_remove(context->process(), simix_global->process_list);
374 // TODO, Remove from proces list:
375 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
377 context->attach_stop();
382 * \brief Executes the processes from simix_global->process_to_run.
384 * The processes of simix_global->process_to_run are run (in parallel if
385 * possible). On exit, simix_global->process_to_run is empty, and
386 * simix_global->process_that_ran contains the list of processes that just ran.
387 * The two lists are swapped so, be careful when using them before and after a
388 * call to this function.
390 void SIMIX_process_runall()
392 SIMIX_context_runall();
394 xbt_dynar_t tmp = simix_global->process_that_ran;
395 simix_global->process_that_ran = simix_global->process_to_run;
396 simix_global->process_to_run = tmp;
397 xbt_dynar_reset(simix_global->process_to_run);
400 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_actor_t process) {
401 SIMIX_process_kill(process, simcall->issuer);
404 * \brief Internal function to kill a SIMIX process.
406 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
407 * or directly for SIMIX internal purposes.
409 * \param process poor victim
410 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
412 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
414 XBT_DEBUG("Killing process %s@%s", process->cname(), process->host->cname());
416 process->context->iwannadie = 1;
417 process->blocked = 0;
418 process->suspended = 0;
419 process->exception = nullptr;
421 /* destroy the blocking synchro if any */
422 if (process->waiting_synchro != nullptr) {
424 simgrid::kernel::activity::ExecImplPtr exec =
425 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
426 simgrid::kernel::activity::CommImplPtr comm =
427 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
428 simgrid::kernel::activity::SleepImplPtr sleep =
429 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
430 simgrid::kernel::activity::RawImplPtr raw =
431 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
432 simgrid::kernel::activity::IoImplPtr io =
433 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
435 if (exec != nullptr) {
437 } else if (comm != nullptr) {
438 process->comms.remove(process->waiting_synchro);
440 // Remove first occurrence of &process->simcall:
441 auto i = boost::range::find(process->waiting_synchro->simcalls, &process->simcall);
442 if (i != process->waiting_synchro->simcalls.end())
443 process->waiting_synchro->simcalls.remove(&process->simcall);
444 } else if (sleep != nullptr) {
445 SIMIX_process_sleep_destroy(process->waiting_synchro);
447 } else if (raw != nullptr) {
448 SIMIX_synchro_stop_waiting(process, &process->simcall);
450 } else if (io != nullptr) {
451 SIMIX_io_destroy(process->waiting_synchro);
453 xbt_die("Unknown type of activity");
457 switch (process->waiting_synchro->type) {
458 case SIMIX_SYNC_JOIN:
459 SIMIX_process_sleep_destroy(process->waiting_synchro);
463 process->waiting_synchro = nullptr;
465 if (not xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
466 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
467 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
471 /** @brief Ask another process to raise the given exception
473 * @param process The process that should raise that exception
474 * @param cat category of exception
475 * @param value value associated to the exception
476 * @param msg string information associated to the exception
478 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
479 SMX_EXCEPTION(process, cat, value, msg);
481 if (process->suspended)
482 SIMIX_process_resume(process);
484 /* cancel the blocking synchro if any */
485 if (process->waiting_synchro) {
487 simgrid::kernel::activity::ExecImplPtr exec =
488 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
489 if (exec != nullptr) {
490 SIMIX_execution_cancel(process->waiting_synchro);
493 simgrid::kernel::activity::CommImplPtr comm =
494 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
495 if (comm != nullptr) {
496 process->comms.remove(comm);
500 simgrid::kernel::activity::SleepImplPtr sleep =
501 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
502 if (sleep != nullptr) {
503 SIMIX_process_sleep_destroy(process->waiting_synchro);
504 if (not xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
505 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
506 xbt_dynar_push_as(simix_global->process_to_run, smx_actor_t, process);
510 simgrid::kernel::activity::RawImplPtr raw =
511 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
512 if (raw != nullptr) {
513 SIMIX_synchro_stop_waiting(process, &process->simcall);
516 simgrid::kernel::activity::IoImplPtr io =
517 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
519 SIMIX_io_destroy(process->waiting_synchro);
522 process->waiting_synchro = nullptr;
526 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
527 SIMIX_process_killall(simcall->issuer, reset_pid);
530 * \brief Kills all running processes.
531 * \param issuer this one will not be killed
533 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
535 for (auto kv : simix_global->process_list)
536 if (kv.second != issuer)
537 SIMIX_process_kill(kv.second, issuer);
540 simix_process_maxpid = reset_pid;
542 SIMIX_context_runall();
544 SIMIX_process_empty_trash();
547 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_actor_t process, sg_host_t dest)
549 process->new_host = dest;
552 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
554 xbt_assert((process != nullptr), "Invalid parameters");
555 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
556 process->host = dest;
557 xbt_swag_insert(process, dest->extension<simgrid::simix::Host>()->process_list);
561 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
563 smx_activity_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
565 if (process != simcall->issuer) {
566 SIMIX_simcall_answer(simcall);
568 sync_suspend->simcalls.push_back(simcall);
569 process->waiting_synchro = sync_suspend;
570 process->waiting_synchro->suspend();
572 /* If we are suspending ourselves, then just do not finish the simcall now */
575 smx_activity_t SIMIX_process_suspend(smx_actor_t process, smx_actor_t issuer)
577 if (process->suspended) {
578 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
582 process->suspended = 1;
584 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
585 if (process != issuer) {
587 if (process->waiting_synchro)
588 process->waiting_synchro->suspend();
589 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
593 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0);
597 void SIMIX_process_resume(smx_actor_t process)
599 XBT_IN("process = %p", process);
601 if (process->context->iwannadie) {
602 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
606 if (not process->suspended)
608 process->suspended = 0;
610 /* resume the synchronization that was blocking the resumed process. */
611 if (process->waiting_synchro)
612 process->waiting_synchro->resume();
617 int SIMIX_process_get_maxpid() {
618 return simix_process_maxpid;
621 int SIMIX_process_count()
623 return simix_global->process_list.size();
626 int SIMIX_process_get_PID(smx_actor_t self)
634 void* SIMIX_process_self_get_data()
636 smx_actor_t self = SIMIX_process_self();
644 void SIMIX_process_self_set_data(void *data)
646 smx_actor_t self = SIMIX_process_self();
648 SIMIX_process_set_data(self, data);
651 void SIMIX_process_set_data(smx_actor_t process, void *data)
653 process->data = data;
656 /* needs to be public and without simcall because it is called
657 by exceptions and logging events */
658 const char* SIMIX_process_self_get_name() {
660 smx_actor_t process = SIMIX_process_self();
661 if (process == nullptr || process == simix_global->maestro_process)
664 return process->name.c_str();
667 smx_actor_t SIMIX_process_get_by_name(const char* name)
669 for (auto kv : simix_global->process_list)
670 if (kv.second->name == name)
675 int SIMIX_process_is_suspended(smx_actor_t process)
677 return process->suspended;
680 xbt_dict_t SIMIX_process_get_properties(smx_actor_t process)
682 return process->properties;
685 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
687 if (process->finished) {
688 // The joined process is already finished, just wake up the issuer process right away
689 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
690 SIMIX_simcall_answer(simcall);
693 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
694 sync->simcalls.push_back(simcall);
695 simcall->issuer->waiting_synchro = sync;
698 static int SIMIX_process_join_finish(smx_process_exit_status_t status, void* synchro)
700 simgrid::kernel::activity::SleepImpl* sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(synchro);
702 if (sleep->surf_sleep) {
703 sleep->surf_sleep->cancel();
705 while (not sleep->simcalls.empty()) {
706 smx_simcall_t simcall = sleep->simcalls.front();
707 sleep->simcalls.pop_front();
708 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
709 simcall->issuer->waiting_synchro = nullptr;
710 if (simcall->issuer->suspended) {
711 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
712 simcall->issuer->suspended = 0;
713 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
715 SIMIX_simcall_answer(simcall);
718 sleep->surf_sleep->unref();
719 sleep->surf_sleep = nullptr;
721 // intrusive_ptr_release(process); // FIXME: We are leaking here. See comment in SIMIX_process_join()
725 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
727 smx_activity_t res = SIMIX_process_sleep(issuer, timeout);
728 intrusive_ptr_add_ref(res.get());
729 /* We are leaking the process here, but if we don't take the ref, we get a "use after free".
730 * The correct solution would be to derivate the type SynchroSleep into a SynchroProcessJoin,
731 * but the code is not clean enough for now for this.
732 * The C API should first be properly replaced with the C++ one, which is a fair amount of work.
734 intrusive_ptr_add_ref(process);
735 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, &*res);
739 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
741 if (MC_is_active() || MC_record_replay_is_active()) {
742 MC_process_clock_add(simcall->issuer, duration);
743 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
744 SIMIX_simcall_answer(simcall);
747 smx_activity_t sync = SIMIX_process_sleep(simcall->issuer, duration);
748 sync->simcalls.push_back(simcall);
749 simcall->issuer->waiting_synchro = sync;
752 smx_activity_t SIMIX_process_sleep(smx_actor_t process, double duration)
754 sg_host_t host = process->host;
757 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->cname());
759 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
760 synchro->host = host;
761 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
762 synchro->surf_sleep->setData(synchro);
763 XBT_DEBUG("Create sleep synchronization %p", synchro);
768 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
770 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
771 simgrid::kernel::activity::SleepImplPtr sleep =
772 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
774 if (sleep->surf_sleep) {
775 sleep->surf_sleep->unref();
776 sleep->surf_sleep = nullptr;
781 * \brief Calling this function makes the process to yield.
783 * Only the current process can call this function, giving back the control to
786 * \param self the current process
788 void SIMIX_process_yield(smx_actor_t self)
790 XBT_DEBUG("Yield actor '%s'", self->cname());
792 /* Go into sleep and return control to maestro */
793 self->context->suspend();
795 /* Ok, maestro returned control to us */
796 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
798 if (self->new_host) {
799 SIMIX_process_change_host(self, self->new_host);
800 self->new_host = nullptr;
803 if (self->context->iwannadie){
804 XBT_DEBUG("I wanna die!");
805 self->finished = true;
806 /* execute the on_exit functions */
807 SIMIX_process_on_exit_runall(self);
808 /* Add the process to the list of process to restart, only if the host is down */
809 if (self->auto_restart && self->host->isOff()) {
810 SIMIX_host_add_auto_restart_process(self->host, self->cname(),
811 self->code, self->data,
812 SIMIX_timer_get_date(self->kill_timer),
816 XBT_DEBUG("Process %s@%s is dead", self->cname(), self->host->cname());
817 self->context->stop();
820 if (self->suspended) {
821 XBT_DEBUG("Hey! I'm suspended.");
822 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
824 SIMIX_process_suspend(self, self);
827 if (self->exception != nullptr) {
828 XBT_DEBUG("Wait, maestro left me an exception");
829 std::exception_ptr exception = std::move(self->exception);
830 self->exception = nullptr;
831 std::rethrow_exception(std::move(exception));
834 if(SMPI_switch_data_segment && self->segment_index != -1){
835 SMPI_switch_data_segment(self->segment_index);
839 /* callback: termination */
840 void SIMIX_process_exception_terminate(xbt_ex_t * e)
846 smx_context_t SIMIX_process_get_context(smx_actor_t p) {
850 void SIMIX_process_set_context(smx_actor_t p,smx_context_t c) {
855 * \brief Returns the list of processes to run.
857 xbt_dynar_t SIMIX_process_get_runnable()
859 return simix_global->process_to_run;
863 * \brief Returns the process from PID.
865 smx_actor_t SIMIX_process_from_PID(aid_t PID)
867 if (simix_global->process_list.find(PID) == simix_global->process_list.end())
869 return simix_global->process_list.at(PID);
872 /** @brief returns a dynar containing all currently existing processes */
873 xbt_dynar_t SIMIX_processes_as_dynar() {
874 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_actor_t),nullptr);
875 for (auto kv : simix_global->process_list) {
876 smx_actor_t proc = kv.second;
877 xbt_dynar_push(res,&proc);
882 void SIMIX_process_on_exit_runall(smx_actor_t process) {
883 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
884 while (not process->on_exit.empty()) {
885 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
886 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
887 process->on_exit.pop_back();
891 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
892 xbt_assert(process, "current process not found: are you in maestro context ?");
894 s_smx_process_exit_fun_t exit_fun = {fun, data};
896 process->on_exit.push_back(exit_fun);
900 * \brief Sets the auto-restart status of the process.
901 * If set to 1, the process will be automatically restarted when its host
904 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
905 process->auto_restart = auto_restart;
908 smx_actor_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_actor_t process) {
909 return SIMIX_process_restart(process, simcall->issuer);
911 /** @brief Restart a process, starting it again from the beginning. */
912 smx_actor_t SIMIX_process_restart(smx_actor_t process, smx_actor_t issuer) {
913 XBT_DEBUG("Restarting process %s on %s", process->cname(), process->host->cname());
915 //retrieve the arguments of the old process
916 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
917 simgrid::simix::ProcessArg arg;
918 arg.name = process->name;
919 arg.code = process->code;
920 arg.host = process->host;
921 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
922 arg.data = process->data;
923 arg.properties = nullptr;
924 arg.auto_restart = process->auto_restart;
926 //kill the old process
927 SIMIX_process_kill(process, issuer);
929 //start the new process
930 smx_actor_t actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
931 arg.properties, nullptr);
932 if (arg.kill_time >= 0)
933 simcall_process_set_kill_time(actor, arg.kill_time);
934 if (arg.auto_restart)
935 simcall_process_auto_restart_set(actor, arg.auto_restart);
940 void SIMIX_segment_index_set(smx_actor_t proc, int index){
941 proc->segment_index = index;
945 * \ingroup simix_process_management
946 * \brief Creates and runs a new SIMIX process.
948 * The structure and the corresponding thread are created and put in the list of ready processes.
950 * \param name a name for the process. It is for user-level information and can be nullptr.
951 * \param code the main function of the process
952 * \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.
953 * It can be retrieved with the function \ref simcall_process_get_data.
954 * \param host where the new agent is executed.
955 * \param kill_time time when the process is killed
956 * \param argc first argument passed to \a code
957 * \param argv second argument passed to \a code
958 * \param properties the properties of the process
959 * \param auto_restart either it is autorestarting or not.
962 smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host, int argc,
963 char** argv, xbt_dict_t properties)
967 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
968 for (int i = 0; i != argc; ++i)
971 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
975 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
976 xbt_dict_t properties)
980 smx_actor_t self = SIMIX_process_self();
981 return simgrid::simix::kernelImmediate([name, code, data, host, properties, self] {
982 return SIMIX_process_create(name, std::move(code), data, host, properties, self);