1 /* Copyright (c) 2007-2018. 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. */
7 #include "simgrid/Exception.hpp"
8 #include "smx_private.hpp"
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/kernel/activity/IoImpl.hpp"
12 #include "src/kernel/activity/SleepImpl.hpp"
13 #include "src/kernel/activity/SynchroRaw.hpp"
14 #include "src/mc/mc_replay.hpp"
15 #include "src/mc/remote/Client.hpp"
16 #include "src/simix/smx_host_private.hpp"
17 #include "src/simix/smx_io_private.hpp"
18 #include "src/simix/smx_synchro_private.hpp"
19 #include "src/surf/HostImpl.hpp"
20 #include "src/surf/cpu_interface.hpp"
23 #include "src/smpi/include/private.hpp"
26 #include <boost/range/algorithm.hpp>
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
30 unsigned long simix_process_maxpid = 0;
33 * @brief Returns the current agent.
35 * This functions returns the currently running SIMIX process.
37 * @return The SIMIX process
39 smx_actor_t SIMIX_process_self()
41 smx_context_t self_context = SIMIX_context_self();
43 return (self_context != nullptr) ? self_context->process() : nullptr;
47 * @brief Returns whether a process has pending asynchronous communications.
48 * @return true if there are asynchronous communications in this process
50 int SIMIX_process_has_pending_comms(smx_actor_t process) {
52 return process->comms.size() > 0;
56 * @brief Moves a process to the list of processes to destroy.
58 void SIMIX_process_cleanup(smx_actor_t process)
60 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->get_cname(), process,
61 process->waiting_synchro.get());
63 process->finished_ = true;
64 SIMIX_process_on_exit_runall(process);
66 /* Unregister from the kill timer if any */
67 if (process->kill_timer != nullptr) {
68 SIMIX_timer_remove(process->kill_timer);
69 process->kill_timer = nullptr;
72 simix_global->mutex.lock();
74 /* cancel non-blocking communications */
75 while (not process->comms.empty()) {
76 smx_activity_t synchro = process->comms.front();
77 process->comms.pop_front();
78 simgrid::kernel::activity::CommImplPtr comm =
79 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
81 /* make sure no one will finish the comm after this process is destroyed,
82 * because src_proc or dst_proc would be an invalid pointer */
84 if (comm->src_proc == process) {
85 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
86 comm->detached, (int)comm->state_, comm->src_proc, comm->dst_proc);
87 comm->src_proc = nullptr;
89 } else if (comm->dst_proc == process) {
90 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state_,
91 comm->src_proc, comm->dst_proc);
92 comm->dst_proc = nullptr;
94 if (comm->detached && comm->src_proc != nullptr) {
95 /* the comm will be freed right now, remove it from the sender */
96 comm->src_proc->comms.remove(comm);
99 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
104 XBT_DEBUG("%p should not be run anymore",process);
105 simix_global->process_list.erase(process->pid_);
106 if (process->host_ && process->host_process_list_hook.is_linked())
107 simgrid::xbt::intrusive_erase(process->host_->pimpl_->process_list_, *process);
108 if (not process->smx_destroy_list_hook.is_linked()) {
110 xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
112 simix_global->process_to_destroy.push_back(*process);
114 process->context_->iwannadie = false;
116 simix_global->mutex.unlock();
122 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
124 void SIMIX_process_empty_trash()
126 while (not simix_global->process_to_destroy.empty()) {
127 smx_actor_t process = &simix_global->process_to_destroy.front();
128 simix_global->process_to_destroy.pop_front();
129 XBT_DEBUG("Getting rid of %p",process);
130 intrusive_ptr_release(process);
133 xbt_dynar_reset(simix_global->dead_actors_vector);
142 ActorImpl::~ActorImpl()
144 delete this->context_;
147 static void dying_daemon(int /*exit_status*/, void* data)
149 std::vector<ActorImpl*>* vect = &simix_global->daemons;
151 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
152 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
154 /* Don't move the whole content since we don't really care about the order */
155 std::swap(*it, vect->back());
159 /** This process will be terminated automatically when the last non-daemon process finishes */
160 void ActorImpl::daemonize()
164 simix_global->daemons.push_back(this);
165 SIMIX_process_on_exit(this, dying_daemon, this);
169 simgrid::s4u::Actor* ActorImpl::restart()
171 XBT_DEBUG("Restarting process %s on %s", get_cname(), host_->get_cname());
173 // retrieve the arguments of the old process
174 simgrid::kernel::actor::ProcessArg arg = ProcessArg(host_, this);
176 // kill the old process
177 SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
179 // start the new process
180 ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
181 arg.properties.get(), nullptr);
182 simcall_process_set_kill_time(actor, arg.kill_time);
183 actor->set_auto_restart(arg.auto_restart);
185 return actor->ciface();
188 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
191 XBT_DEBUG("Actor '%s' is already suspended", get_cname());
197 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
198 if (this != issuer) {
200 waiting_synchro->suspend();
201 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
205 return SIMIX_execution_start("suspend", "", 0.0, 1.0, 0.0, this->host_);
209 void ActorImpl::resume()
211 XBT_IN("process = %p", this);
213 if (context_->iwannadie) {
214 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
222 /* resume the synchronization that was blocking the resumed actor. */
224 waiting_synchro->resume();
229 smx_activity_t ActorImpl::sleep(double duration)
232 throw_exception(std::make_exception_ptr(simgrid::HostFailureException(
233 XBT_THROW_POINT, std::string("Host ") + std::string(host_->get_cname()) + " failed, you cannot sleep there.")));
235 simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
236 synchro->host = host_;
237 synchro->surf_sleep = host_->pimpl_cpu->sleep(duration);
238 synchro->surf_sleep->set_data(synchro);
239 XBT_DEBUG("Create sleep synchronization %p", synchro);
244 void ActorImpl::throw_exception(std::exception_ptr e)
251 /* cancel the blocking synchro if any */
252 if (waiting_synchro) {
254 simgrid::kernel::activity::ExecImplPtr exec =
255 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(waiting_synchro);
256 if (exec != nullptr && exec->surf_action_)
257 exec->surf_action_->cancel();
259 simgrid::kernel::activity::CommImplPtr comm =
260 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(waiting_synchro);
261 if (comm != nullptr) {
266 simgrid::kernel::activity::SleepImplPtr sleep =
267 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(waiting_synchro);
268 if (sleep != nullptr) {
269 SIMIX_process_sleep_destroy(waiting_synchro);
270 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), this) ==
271 end(simix_global->process_to_run) &&
272 this != SIMIX_process_self()) {
273 XBT_DEBUG("Inserting %s in the to_run list", get_cname());
274 simix_global->process_to_run.push_back(this);
278 simgrid::kernel::activity::RawImplPtr raw =
279 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(waiting_synchro);
280 if (raw != nullptr) {
281 SIMIX_synchro_stop_waiting(this, &simcall);
284 simgrid::kernel::activity::IoImplPtr io =
285 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(waiting_synchro);
290 waiting_synchro = nullptr;
293 void create_maestro(simgrid::simix::ActorCode code)
295 smx_actor_t maestro = nullptr;
296 /* Create maestro process and initialize it */
297 maestro = new simgrid::kernel::actor::ActorImpl();
298 maestro->pid_ = simix_process_maxpid++;
300 maestro->set_user_data(nullptr);
303 maestro->context_ = SIMIX_context_new(simgrid::simix::ActorCode(), nullptr, maestro);
305 if (not simix_global)
306 xbt_die("simix is not initialized, please call MSG_init first");
307 maestro->context_ = simix_global->context_factory->create_maestro(code, maestro);
310 maestro->simcall.issuer = maestro;
311 simix_global->maestro_process = maestro;
318 /** @brief Creates and runs the maestro process */
319 void SIMIX_maestro_create(void (*code)(void*), void* data)
321 simgrid::kernel::actor::create_maestro(std::bind(code, data));
325 * @brief Internal function to create a process.
327 * This function actually creates the process.
328 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
329 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
331 * @return the process created
333 smx_actor_t SIMIX_process_create(std::string name, simgrid::simix::ActorCode code, void* data, simgrid::s4u::Host* host,
334 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
337 XBT_DEBUG("Start process %s on host '%s'", name.c_str(), host->get_cname());
339 if (host->is_off()) {
340 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name.c_str(), host->get_cname());
344 smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
346 xbt_assert(code && host != nullptr, "Invalid parameters");
348 process->pid_ = simix_process_maxpid++;
349 process->name_ = simgrid::xbt::string(name);
350 process->host_ = host;
351 process->set_user_data(data);
352 process->simcall.issuer = process;
354 if (parent_process != nullptr) {
355 process->ppid_ = parent_process->pid_;
358 process->code = code;
360 XBT_VERB("Create context %s", process->get_cname());
361 process->context_ = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
364 if (properties != nullptr)
365 for (auto const& kv : *properties)
366 process->set_property(kv.first, kv.second);
368 /* Add the process to its host's process list */
369 host->pimpl_->process_list_.push_back(*process);
371 XBT_DEBUG("Start context '%s'", process->get_cname());
373 /* Now insert it in the global process list and in the process to run list */
374 simix_global->process_list[process->pid_] = process;
375 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
376 simix_global->process_to_run.push_back(process);
377 intrusive_ptr_add_ref(process);
379 /* The onCreation() signal must be delayed until there, where the pid and everything is set */
380 simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
381 simgrid::s4u::Actor::on_creation(tmp);
386 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
387 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
389 // This is mostly a copy/paste from SIMIX_process_new(),
390 // it'd be nice to share some code between those two functions.
392 sg_host_t host = sg_host_by_name(hostname);
393 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
395 if (host->is_off()) {
396 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
400 smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
402 process->pid_ = simix_process_maxpid++;
403 process->name_ = std::string(name);
404 process->host_ = host;
405 process->set_user_data(data);
406 process->simcall.issuer = process;
408 if (parent_process != nullptr) {
409 process->ppid_ = parent_process->pid_;
412 /* Process data for auto-restart */
413 process->code = nullptr;
415 XBT_VERB("Create context %s", process->get_cname());
416 if (not simix_global)
417 xbt_die("simix is not initialized, please call MSG_init first");
418 process->context_ = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
421 if (properties != nullptr)
422 for (auto const& kv : *properties)
423 process->set_property(kv.first, kv.second);
425 /* Add the process to it's host process list */
426 host->pimpl_->process_list_.push_back(*process);
428 /* Now insert it in the global process list and in the process to run list */
429 simix_global->process_list[process->pid_] = process;
430 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
431 simix_global->process_to_run.push_back(process);
432 intrusive_ptr_add_ref(process);
434 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context_);
436 xbt_die("Not a suitable context");
438 context->attach_start();
440 /* The onCreation() signal must be delayed until there, where the pid and everything is set */
441 simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
442 simgrid::s4u::Actor::on_creation(tmp);
447 void SIMIX_process_detach()
449 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
451 xbt_die("Not a suitable context");
453 auto process = context->process();
454 simix_global->cleanup_process_function(process);
455 context->attach_stop();
459 * @brief Executes the processes from simix_global->process_to_run.
461 * The processes of simix_global->process_to_run are run (in parallel if
462 * possible). On exit, simix_global->process_to_run is empty, and
463 * simix_global->process_that_ran contains the list of processes that just ran.
464 * The two lists are swapped so, be careful when using them before and after a
465 * call to this function.
467 void SIMIX_process_runall()
469 SIMIX_context_runall();
471 simix_global->process_to_run.swap(simix_global->process_that_ran);
472 simix_global->process_to_run.clear();
476 * @brief Internal function to kill a SIMIX process.
478 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
479 * or directly for SIMIX internal purposes.
481 * @param process poor victim
482 * @param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
484 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
486 if (process->finished_) {
487 XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", process->get_cname(),
488 process->host_->get_cname());
492 XBT_DEBUG("Actor '%s'@%s is killing actor '%s'@%s", issuer->get_cname(),
493 (issuer->host_ == nullptr ? "(null)" : issuer->host_->get_cname()), process->get_cname(),
494 process->host_->get_cname());
496 process->context_->iwannadie = true;
497 process->blocked_ = false;
498 process->suspended_ = false;
499 process->exception = nullptr;
501 if (process->host_->is_off())
502 process->throw_exception(std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed")));
504 /* destroy the blocking synchro if any */
505 if (process->waiting_synchro != nullptr) {
507 simgrid::kernel::activity::ExecImplPtr exec =
508 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
509 simgrid::kernel::activity::CommImplPtr comm =
510 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
511 simgrid::kernel::activity::SleepImplPtr sleep =
512 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
513 simgrid::kernel::activity::RawImplPtr raw =
514 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
515 simgrid::kernel::activity::IoImplPtr io =
516 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
518 if (exec != nullptr) {
519 if (exec->surf_action_) {
520 exec->surf_action_->cancel();
521 exec->surf_action_->unref();
522 exec->surf_action_ = nullptr;
524 } else if (comm != nullptr) {
525 process->comms.remove(process->waiting_synchro);
527 // Remove first occurrence of &process->simcall:
528 auto i = boost::range::find(process->waiting_synchro->simcalls_, &process->simcall);
529 if (i != process->waiting_synchro->simcalls_.end())
530 process->waiting_synchro->simcalls_.remove(&process->simcall);
531 } else if (sleep != nullptr) {
532 if (sleep->surf_sleep)
533 sleep->surf_sleep->cancel();
535 } else if (raw != nullptr) {
536 SIMIX_synchro_stop_waiting(process, &process->simcall);
538 } else if (io != nullptr) {
541 simgrid::kernel::activity::ActivityImplPtr activity = process->waiting_synchro;
542 xbt_die("Activity %s is of unknown type %s", activity->name_.c_str(),
543 simgrid::xbt::demangle(typeid(activity).name()).get());
546 process->waiting_synchro = nullptr;
548 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
549 end(simix_global->process_to_run) &&
551 XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
552 simix_global->process_to_run.push_back(process);
556 /** @deprecated When this function gets removed, also remove the xbt_ex class, that is only there to help users to
558 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
559 SMX_EXCEPTION(process, cat, value, msg);
561 if (process->suspended_)
564 /* cancel the blocking synchro if any */
565 if (process->waiting_synchro) {
567 simgrid::kernel::activity::ExecImplPtr exec =
568 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
569 if (exec != nullptr && exec->surf_action_)
570 exec->surf_action_->cancel();
572 simgrid::kernel::activity::CommImplPtr comm =
573 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
574 if (comm != nullptr) {
575 process->comms.remove(comm);
579 simgrid::kernel::activity::SleepImplPtr sleep =
580 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
581 if (sleep != nullptr) {
582 SIMIX_process_sleep_destroy(process->waiting_synchro);
583 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
584 end(simix_global->process_to_run) &&
585 process != SIMIX_process_self()) {
586 XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
587 simix_global->process_to_run.push_back(process);
591 simgrid::kernel::activity::RawImplPtr raw =
592 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
593 if (raw != nullptr) {
594 SIMIX_synchro_stop_waiting(process, &process->simcall);
597 simgrid::kernel::activity::IoImplPtr io =
598 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
603 process->waiting_synchro = nullptr;
608 * @brief Kills all running processes.
609 * @param issuer this one will not be killed
611 void SIMIX_process_killall(smx_actor_t issuer)
613 for (auto const& kv : simix_global->process_list)
614 if (kv.second != issuer)
615 SIMIX_process_kill(kv.second, issuer);
618 void SIMIX_process_change_host(smx_actor_t actor, sg_host_t dest)
620 xbt_assert((actor != nullptr), "Invalid parameters");
621 simgrid::xbt::intrusive_erase(actor->host_->pimpl_->process_list_, *actor);
623 dest->pimpl_->process_list_.push_back(*actor);
626 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
628 smx_activity_t sync_suspend = process->suspend(simcall->issuer);
630 if (process != simcall->issuer) {
631 SIMIX_simcall_answer(simcall);
633 sync_suspend->simcalls_.push_back(simcall);
634 process->waiting_synchro = sync_suspend;
635 process->waiting_synchro->suspend();
637 /* If we are suspending ourselves, then just do not finish the simcall now */
640 int SIMIX_process_get_maxpid() {
641 return simix_process_maxpid;
644 int SIMIX_process_count()
646 return simix_global->process_list.size();
649 void* SIMIX_process_self_get_data()
651 smx_actor_t self = SIMIX_process_self();
656 return self->get_user_data();
659 void SIMIX_process_self_set_data(void *data)
661 SIMIX_process_self()->set_user_data(data);
665 /* needs to be public and without simcall because it is called
666 by exceptions and logging events */
667 const char* SIMIX_process_self_get_name() {
669 smx_actor_t process = SIMIX_process_self();
670 if (process == nullptr || process == simix_global->maestro_process)
673 return process->get_cname();
676 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
678 if (process->finished_) {
679 // The joined process is already finished, just wake up the issuer process right away
680 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
681 SIMIX_simcall_answer(simcall);
684 smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
685 sync->simcalls_.push_back(simcall);
686 simcall->issuer->waiting_synchro = sync;
689 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
691 smx_activity_t res = issuer->sleep(timeout);
692 intrusive_ptr_add_ref(res.get());
693 SIMIX_process_on_exit(process,
695 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
696 if (sleep->surf_sleep)
697 sleep->surf_sleep->finish(simgrid::kernel::resource::Action::State::FINISHED);
698 intrusive_ptr_release(sleep);
704 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
706 if (MC_is_active() || MC_record_replay_is_active()) {
707 MC_process_clock_add(simcall->issuer, duration);
708 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
709 SIMIX_simcall_answer(simcall);
712 smx_activity_t sync = simcall->issuer->sleep(duration);
713 sync->simcalls_.push_back(simcall);
714 simcall->issuer->waiting_synchro = sync;
717 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
719 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
720 simgrid::kernel::activity::SleepImplPtr sleep =
721 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
723 if (sleep->surf_sleep) {
724 sleep->surf_sleep->unref();
725 sleep->surf_sleep = nullptr;
730 * @brief Calling this function makes the process to yield.
732 * Only the current process can call this function, giving back the control to maestro.
734 * @param self the current process
736 void SIMIX_process_yield(smx_actor_t self)
738 XBT_DEBUG("Yield actor '%s'", self->get_cname());
740 /* Go into sleep and return control to maestro */
741 self->context_->suspend();
743 /* Ok, maestro returned control to us */
744 XBT_DEBUG("Control returned to me: '%s'", self->get_cname());
746 if (self->context_->iwannadie) {
747 XBT_DEBUG("I wanna die!");
748 self->finished_ = true;
749 /* execute the on_exit functions */
750 SIMIX_process_on_exit_runall(self);
752 if (self->auto_restart_ && self->host_->is_off() &&
753 watched_hosts.find(self->host_->get_cname()) == watched_hosts.end()) {
754 XBT_DEBUG("Push host %s to watched_hosts because it's off and %s needs to restart", self->host_->get_cname(),
756 watched_hosts.insert(self->host_->get_cname());
759 XBT_DEBUG("Process %s@%s is dead", self->get_cname(), self->host_->get_cname());
760 self->context_->stop();
763 if (self->suspended_) {
764 XBT_DEBUG("Hey! I'm suspended.");
765 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
766 self->suspended_ = false;
770 if (self->exception != nullptr) {
771 XBT_DEBUG("Wait, maestro left me an exception");
772 std::exception_ptr exception = std::move(self->exception);
773 self->exception = nullptr;
774 std::rethrow_exception(std::move(exception));
777 if (SMPI_switch_data_segment && not self->finished_) {
778 SMPI_switch_data_segment(self->iface());
782 /** @brief Returns the list of processes to run. */
783 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
785 return simix_global->process_to_run;
788 /** @brief Returns the process from PID. */
789 smx_actor_t SIMIX_process_from_PID(aid_t PID)
791 auto process = simix_global->process_list.find(PID);
792 return process == simix_global->process_list.end() ? nullptr : process->second;
795 void SIMIX_process_on_exit_runall(smx_actor_t process) {
796 simgrid::s4u::Actor::on_destruction(process->iface());
797 smx_process_exit_status_t exit_status = (process->context_->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
798 while (not process->on_exit.empty()) {
799 s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
800 process->on_exit.pop_back();
801 (exit_fun.fun)(exit_status, exit_fun.arg);
805 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void* data)
807 SIMIX_process_on_exit(process, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
810 void SIMIX_process_on_exit(smx_actor_t process, std::function<void(int, void*)> fun, void* data)
812 xbt_assert(process, "current process not found: are you in maestro context ?");
814 process->on_exit.emplace_back(s_smx_process_exit_fun_t{fun, data});
817 /** @brief Restart a process, starting it again from the beginning. */
819 * @ingroup simix_process_management
820 * @brief Creates and runs a new SIMIX process.
822 * The structure and the corresponding thread are created and put in the list of ready processes.
824 * @param name a name for the process. It is for user-level information and can be nullptr.
825 * @param code the main function of the process
826 * @param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
828 * It can be retrieved with the method ActorImpl::getUserData().
829 * @param host where the new agent is executed.
830 * @param argc first argument passed to @a code
831 * @param argv second argument passed to @a code
832 * @param properties the properties of the process
834 smx_actor_t simcall_process_create(std::string name, xbt_main_func_t code, void* data, sg_host_t host, int argc,
835 char** argv, std::unordered_map<std::string, std::string>* properties)
837 auto wrapped_code = simgrid::xbt::wrap_main(code, argc, argv);
838 for (int i = 0; i != argc; ++i)
841 smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
845 smx_actor_t simcall_process_create(std::string name, simgrid::simix::ActorCode code, void* data, sg_host_t host,
846 std::unordered_map<std::string, std::string>* properties)
848 smx_actor_t self = SIMIX_process_self();
849 return simgrid::simix::simcall([name, code, data, host, properties, self] {
850 return SIMIX_process_create(name, std::move(code), data, host, properties, self);