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"
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/include/private.hpp"
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
39 unsigned long simix_process_maxpid = 0;
42 * \brief Returns the current agent.
44 * This functions returns the currently running SIMIX process.
46 * \return The SIMIX process
48 smx_actor_t SIMIX_process_self()
50 smx_context_t self_context = SIMIX_context_self();
52 return (self_context != nullptr) ? self_context->process() : nullptr;
56 * \brief Returns whether a process has pending asynchronous communications.
57 * \return true if there are asynchronous communications in this process
59 int SIMIX_process_has_pending_comms(smx_actor_t process) {
61 return process->comms.size() > 0;
65 * \brief Moves a process to the list of processes to destroy.
67 void SIMIX_process_cleanup(smx_actor_t process)
69 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->name.c_str(), process,
70 process->waiting_synchro.get());
72 process->finished = true;
73 SIMIX_process_on_exit_runall(process);
75 /* Unregister from the kill timer if any */
76 if (process->kill_timer != nullptr)
77 SIMIX_timer_remove(process->kill_timer);
79 xbt_os_mutex_acquire(simix_global->mutex);
81 /* cancel non-blocking communications */
82 while (not process->comms.empty()) {
83 smx_activity_t synchro = process->comms.front();
84 process->comms.pop_front();
85 simgrid::kernel::activity::CommImplPtr comm =
86 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
88 /* make sure no one will finish the comm after this process is destroyed,
89 * because src_proc or dst_proc would be an invalid pointer */
91 if (comm->src_proc == process) {
92 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
93 comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
94 comm->src_proc = nullptr;
96 } else if (comm->dst_proc == process) {
97 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state,
98 comm->src_proc, comm->dst_proc);
99 comm->dst_proc = nullptr;
101 if (comm->detached && comm->src_proc != nullptr) {
102 /* the comm will be freed right now, remove it from the sender */
103 comm->src_proc->comms.remove(comm);
106 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
111 XBT_DEBUG("%p should not be run anymore",process);
112 simix_global->process_list.erase(process->pid);
114 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
115 xbt_swag_insert(process, simix_global->process_to_destroy);
116 process->context->iwannadie = 0;
118 xbt_os_mutex_release(simix_global->mutex);
124 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
126 void SIMIX_process_empty_trash()
128 smx_actor_t process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
131 XBT_DEBUG("Getting rid of %p",process);
132 intrusive_ptr_release(process);
133 process = static_cast<smx_actor_t>(xbt_swag_extract(simix_global->process_to_destroy));
140 ActorImpl::~ActorImpl()
142 delete this->context;
145 static int dying_daemon(void* exit_status, void* data)
147 std::vector<ActorImpl*>* vect = &simix_global->daemons;
149 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
150 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
152 /* Don't move the whole content since we don't really care about the order */
153 std::swap(*it, vect->back());
158 /** This process will be terminated automatically when the last non-daemon process finishes */
159 void ActorImpl::daemonize()
163 simix_global->daemons.push_back(this);
164 SIMIX_process_on_exit(this, dying_daemon, this);
168 simgrid::s4u::Actor* ActorImpl::restart()
170 XBT_DEBUG("Restarting process %s on %s", cname(), host->getCname());
172 // retrieve the arguments of the old process
173 // FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
174 simgrid::simix::ProcessArg arg;
178 arg.kill_time = SIMIX_timer_get_date(kill_timer);
180 arg.properties = nullptr;
181 arg.auto_restart = auto_restart;
183 // kill the old process
184 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
186 // start the new process
187 ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
188 arg.properties, nullptr);
189 if (arg.kill_time >= 0)
190 simcall_process_set_kill_time(actor, arg.kill_time);
191 if (arg.auto_restart)
192 actor->auto_restart = arg.auto_restart;
194 return actor->ciface();
197 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
200 XBT_DEBUG("Actor '%s' is already suspended", name.c_str());
206 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
207 if (this != issuer) {
209 waiting_synchro->suspend();
210 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
214 return SIMIX_execution_start(this, "suspend", 0.0, 1.0, 0.0);
218 void ActorImpl::resume()
220 XBT_IN("process = %p", this);
222 if (context->iwannadie) {
223 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
231 /* resume the synchronization that was blocking the resumed actor. */
233 waiting_synchro->resume();
238 smx_activity_t ActorImpl::sleep(double duration)
241 THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host->getCname());
243 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
244 synchro->host = host;
245 synchro->surf_sleep = host->pimpl_cpu->sleep(duration);
246 synchro->surf_sleep->setData(synchro);
247 XBT_DEBUG("Create sleep synchronization %p", synchro);
252 void create_maestro(std::function<void()> code)
254 smx_actor_t maestro = nullptr;
255 /* Create maestro process and initialize it */
256 maestro = new simgrid::simix::ActorImpl();
257 maestro->pid = simix_process_maxpid++;
259 maestro->userdata = nullptr;
262 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
264 if (not simix_global)
265 xbt_die("simix is not initialized, please call MSG_init first");
266 maestro->context = simix_global->context_factory->create_maestro(code, maestro);
269 maestro->simcall.issuer = maestro;
270 simix_global->maestro_process = maestro;
276 /** @brief Creates and runs the maestro process */
277 void SIMIX_maestro_create(void (*code)(void*), void* data)
279 simgrid::simix::create_maestro(std::bind(code, data));
283 * \brief Internal function to create a process.
285 * This function actually creates the process.
286 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
287 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
289 * \return the process created
291 smx_actor_t SIMIX_process_create(const char* name, std::function<void()> code, void* data, simgrid::s4u::Host* host,
292 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
295 XBT_DEBUG("Start process %s on host '%s'", name, host->getCname());
298 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->getCname());
302 smx_actor_t process = new simgrid::simix::ActorImpl();
304 xbt_assert(code && host != nullptr, "Invalid parameters");
306 process->pid = simix_process_maxpid++;
307 process->name = simgrid::xbt::string(name);
308 process->host = host;
309 process->userdata = data;
310 process->simcall.issuer = process;
312 if (parent_process != nullptr) {
313 process->ppid = parent_process->pid;
314 /* SMPI process have their own data segment and each other inherit from their father */
316 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
317 if (parent_process->pid != 0) {
318 process->segment_index = parent_process->segment_index;
320 process->segment_index = process->pid - 1;
326 process->code = code;
328 XBT_VERB("Create context %s", process->name.c_str());
329 process->context = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
332 if (properties != nullptr)
333 for (auto kv : *properties)
334 process->setProperty(kv.first, kv.second);
336 /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
337 if (host->extension<simgrid::simix::Host>() == nullptr)
338 host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
340 /* Add the process to its host process list */
341 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
343 XBT_DEBUG("Start context '%s'", process->name.c_str());
345 /* Now insert it in the global process list and in the process to run list */
346 simix_global->process_list[process->pid] = process;
347 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->getCname());
348 simix_global->process_to_run.push_back(process);
349 intrusive_ptr_add_ref(process);
351 /* Tracing the process creation */
352 TRACE_msg_process_create(process->cname(), process->pid, process->host);
357 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
358 std::map<std::string, std::string>* properties, smx_actor_t parent_process)
360 // This is mostly a copy/paste from SIMIX_process_new(),
361 // it'd be nice to share some code between those two functions.
363 sg_host_t host = sg_host_by_name(hostname);
364 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
367 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
371 smx_actor_t process = new simgrid::simix::ActorImpl();
373 process->pid = simix_process_maxpid++;
374 process->name = std::string(name);
375 process->host = host;
376 process->userdata = data;
377 process->simcall.issuer = process;
379 if (parent_process != nullptr) {
380 process->ppid = parent_process->pid;
381 /* SMPI process have their own data segment and each other inherit from their father */
383 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP) {
384 if (parent_process->pid != 0) {
385 process->segment_index = parent_process->segment_index;
387 process->segment_index = process->pid - 1;
393 /* Process data for auto-restart */
394 process->code = nullptr;
396 XBT_VERB("Create context %s", process->name.c_str());
397 if (not simix_global)
398 xbt_die("simix is not initialized, please call MSG_init first");
399 process->context = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
402 if (properties != nullptr)
403 for (auto kv : *properties)
404 process->setProperty(kv.first, kv.second);
406 /* Add the process to it's host process list */
407 xbt_swag_insert(process, host->extension<simgrid::simix::Host>()->process_list);
409 /* Now insert it in the global process list and in the process to run list */
410 simix_global->process_list[process->pid] = process;
411 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->cname(), host->getCname());
412 simix_global->process_to_run.push_back(process);
414 /* Tracing the process creation */
415 TRACE_msg_process_create(process->cname(), process->pid, process->host);
417 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context);
419 xbt_die("Not a suitable context");
421 context->attach_start();
425 void SIMIX_process_detach()
427 auto context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
429 xbt_die("Not a suitable context");
431 simix_global->cleanup_process_function(context->process());
433 // Let maestro ignore we are still alive:
434 // xbt_swag_remove(context->process(), simix_global->process_list);
436 // TODO, Remove from proces list:
437 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
439 context->attach_stop();
444 * \brief Executes the processes from simix_global->process_to_run.
446 * The processes of simix_global->process_to_run are run (in parallel if
447 * possible). On exit, simix_global->process_to_run is empty, and
448 * simix_global->process_that_ran contains the list of processes that just ran.
449 * The two lists are swapped so, be careful when using them before and after a
450 * call to this function.
452 void SIMIX_process_runall()
454 SIMIX_context_runall();
456 simix_global->process_to_run.swap(simix_global->process_that_ran);
457 simix_global->process_to_run.clear();
461 * \brief Internal function to kill a SIMIX process.
463 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
464 * or directly for SIMIX internal purposes.
466 * \param process poor victim
467 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
469 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
471 XBT_DEBUG("Killing process %s@%s", process->cname(), process->host->getCname());
473 process->context->iwannadie = 1;
474 process->blocked = 0;
475 process->suspended = 0;
476 process->exception = nullptr;
478 /* destroy the blocking synchro if any */
479 if (process->waiting_synchro != nullptr) {
481 simgrid::kernel::activity::ExecImplPtr exec =
482 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
483 simgrid::kernel::activity::CommImplPtr comm =
484 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
485 simgrid::kernel::activity::SleepImplPtr sleep =
486 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
487 simgrid::kernel::activity::RawImplPtr raw =
488 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
489 simgrid::kernel::activity::IoImplPtr io =
490 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
492 if (exec != nullptr) {
494 } else if (comm != nullptr) {
495 process->comms.remove(process->waiting_synchro);
497 // Remove first occurrence of &process->simcall:
498 auto i = boost::range::find(process->waiting_synchro->simcalls, &process->simcall);
499 if (i != process->waiting_synchro->simcalls.end())
500 process->waiting_synchro->simcalls.remove(&process->simcall);
501 } else if (sleep != nullptr) {
502 SIMIX_process_sleep_destroy(process->waiting_synchro);
504 } else if (raw != nullptr) {
505 SIMIX_synchro_stop_waiting(process, &process->simcall);
507 } else if (io != nullptr) {
508 SIMIX_io_destroy(process->waiting_synchro);
510 xbt_die("Unknown type of activity");
514 switch (process->waiting_synchro->type) {
515 case SIMIX_SYNC_JOIN:
516 SIMIX_process_sleep_destroy(process->waiting_synchro);
520 process->waiting_synchro = nullptr;
522 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
523 end(simix_global->process_to_run) &&
525 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
526 simix_global->process_to_run.push_back(process);
530 /** @brief Ask another process to raise the given exception
532 * @param process The process that should raise that exception
533 * @param cat category of exception
534 * @param value value associated to the exception
535 * @param msg string information associated to the exception
537 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
538 SMX_EXCEPTION(process, cat, value, msg);
540 if (process->suspended)
543 /* cancel the blocking synchro if any */
544 if (process->waiting_synchro) {
546 simgrid::kernel::activity::ExecImplPtr exec =
547 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
548 if (exec != nullptr && exec->surf_exec)
549 exec->surf_exec->cancel();
551 simgrid::kernel::activity::CommImplPtr comm =
552 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
553 if (comm != nullptr) {
554 process->comms.remove(comm);
558 simgrid::kernel::activity::SleepImplPtr sleep =
559 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
560 if (sleep != nullptr) {
561 SIMIX_process_sleep_destroy(process->waiting_synchro);
562 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
563 end(simix_global->process_to_run) &&
564 process != SIMIX_process_self()) {
565 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
566 simix_global->process_to_run.push_back(process);
570 simgrid::kernel::activity::RawImplPtr raw =
571 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
572 if (raw != nullptr) {
573 SIMIX_synchro_stop_waiting(process, &process->simcall);
576 simgrid::kernel::activity::IoImplPtr io =
577 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
579 SIMIX_io_destroy(process->waiting_synchro);
582 process->waiting_synchro = nullptr;
586 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
587 SIMIX_process_killall(simcall->issuer, reset_pid);
590 * \brief Kills all running processes.
591 * \param issuer this one will not be killed
593 void SIMIX_process_killall(smx_actor_t issuer, int reset_pid)
595 for (auto kv : simix_global->process_list)
596 if (kv.second != issuer)
597 SIMIX_process_kill(kv.second, issuer);
600 simix_process_maxpid = reset_pid;
602 SIMIX_context_runall();
604 SIMIX_process_empty_trash();
607 void SIMIX_process_change_host(smx_actor_t process, sg_host_t dest)
609 xbt_assert((process != nullptr), "Invalid parameters");
610 xbt_swag_remove(process, process->host->extension<simgrid::simix::Host>()->process_list);
611 process->host = dest;
612 xbt_swag_insert(process, dest->extension<simgrid::simix::Host>()->process_list);
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 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 static int SIMIX_process_join_finish(smx_process_exit_status_t status, void* synchro)
688 simgrid::kernel::activity::SleepImpl* sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(synchro);
690 if (sleep->surf_sleep) {
691 sleep->surf_sleep->cancel();
693 while (not sleep->simcalls.empty()) {
694 smx_simcall_t simcall = sleep->simcalls.front();
695 sleep->simcalls.pop_front();
696 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
697 simcall->issuer->waiting_synchro = nullptr;
698 if (simcall->issuer->suspended) {
699 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
700 simcall->issuer->suspended = 0;
701 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
703 SIMIX_simcall_answer(simcall);
706 sleep->surf_sleep->unref();
707 sleep->surf_sleep = nullptr;
709 // intrusive_ptr_release(process); // FIXME: We are leaking here. See comment in SIMIX_process_join()
713 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
715 smx_activity_t res = issuer->sleep(timeout);
716 intrusive_ptr_add_ref(res.get());
717 /* We are leaking the process here, but if we don't take the ref, we get a "use after free".
718 * The correct solution would be to derivate the type SynchroSleep into a SynchroProcessJoin,
719 * but the code is not clean enough for now for this.
720 * The C API should first be properly replaced with the C++ one, which is a fair amount of work.
722 intrusive_ptr_add_ref(process);
723 SIMIX_process_on_exit(process,
724 [](void*, void* arg) {
725 return simgrid::simix::kernelImmediate(
726 [&] { return SIMIX_process_join_finish(SMX_EXIT_SUCCESS, arg); });
732 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
734 if (MC_is_active() || MC_record_replay_is_active()) {
735 MC_process_clock_add(simcall->issuer, duration);
736 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
737 SIMIX_simcall_answer(simcall);
740 smx_activity_t sync = simcall->issuer->sleep(duration);
741 sync->simcalls.push_back(simcall);
742 simcall->issuer->waiting_synchro = sync;
745 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
747 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
748 simgrid::kernel::activity::SleepImplPtr sleep =
749 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
751 if (sleep->surf_sleep) {
752 sleep->surf_sleep->unref();
753 sleep->surf_sleep = nullptr;
758 * \brief Calling this function makes the process to yield.
760 * Only the current process can call this function, giving back the control to maestro.
762 * \param self the current process
764 void SIMIX_process_yield(smx_actor_t self)
766 XBT_DEBUG("Yield actor '%s'", self->cname());
768 /* Go into sleep and return control to maestro */
769 self->context->suspend();
771 /* Ok, maestro returned control to us */
772 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
774 if (self->new_host) {
775 SIMIX_process_change_host(self, self->new_host);
776 self->new_host = nullptr;
779 if (self->context->iwannadie){
780 XBT_DEBUG("I wanna die!");
781 self->finished = true;
782 /* execute the on_exit functions */
783 SIMIX_process_on_exit_runall(self);
784 /* Add the process to the list of process to restart, only if the host is down */
785 if (self->auto_restart && self->host->isOff()) {
786 SIMIX_host_add_auto_restart_process(self->host, self->cname(), self->code, self->userdata,
787 SIMIX_timer_get_date(self->kill_timer), self->getProperties(),
790 XBT_DEBUG("Process %s@%s is dead", self->cname(), self->host->getCname());
791 self->context->stop();
794 if (self->suspended) {
795 XBT_DEBUG("Hey! I'm suspended.");
796 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
801 if (self->exception != nullptr) {
802 XBT_DEBUG("Wait, maestro left me an exception");
803 std::exception_ptr exception = std::move(self->exception);
804 self->exception = nullptr;
805 std::rethrow_exception(std::move(exception));
808 if(SMPI_switch_data_segment && self->segment_index != -1){
809 SMPI_switch_data_segment(self->segment_index);
813 /* callback: termination */
814 void SIMIX_process_exception_terminate(xbt_ex_t * e)
820 /** @brief Returns the list of processes to run. */
821 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
823 return simix_global->process_to_run;
826 /** @brief Returns the process from PID. */
827 smx_actor_t SIMIX_process_from_PID(aid_t PID)
829 auto process = simix_global->process_list.find(PID);
830 return process == simix_global->process_list.end() ? nullptr : process->second;
833 void SIMIX_process_on_exit_runall(smx_actor_t process) {
834 smx_process_exit_status_t exit_status = (process->context->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
835 while (not process->on_exit.empty()) {
836 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
837 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
838 process->on_exit.pop_back();
842 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void *data) {
843 xbt_assert(process, "current process not found: are you in maestro context ?");
845 s_smx_process_exit_fun_t exit_fun = {fun, data};
847 process->on_exit.push_back(exit_fun);
851 * \brief Sets the auto-restart status of the process.
852 * If set to 1, the process will be automatically restarted when its host comes back.
854 void SIMIX_process_auto_restart_set(smx_actor_t process, int auto_restart) {
855 process->auto_restart = auto_restart;
858 /** @brief Restart a process, starting it again from the beginning. */
860 * \ingroup simix_process_management
861 * \brief Creates and runs a new SIMIX process.
863 * The structure and the corresponding thread are created and put in the list of ready processes.
865 * \param name a name for the process. It is for user-level information and can be nullptr.
866 * \param code the main function of the process
867 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
869 * It can be retrieved with the function \ref simcall_process_get_data.
870 * \param host where the new agent is executed.
871 * \param kill_time time when the process is killed
872 * \param argc first argument passed to \a code
873 * \param argv second argument passed to \a code
874 * \param properties the properties of the process
875 * \param auto_restart either it is autorestarting or not.
877 extern "C" smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host,
878 int argc, char** argv, std::map<std::string, std::string>* properties)
882 auto wrapped_code = simgrid::xbt::wrapMain(code, argc, argv);
883 for (int i = 0; i != argc; ++i)
886 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
890 smx_actor_t simcall_process_create(const char* name, std::function<void()> code, void* data, sg_host_t host,
891 std::map<std::string, std::string>* properties)
895 smx_actor_t self = SIMIX_process_self();
896 return simgrid::simix::kernelImmediate([name, code, data, host, properties, self] {
897 return SIMIX_process_create(name, std::move(code), data, host, properties, self);