1 /* Copyright (c) 2007-2019. 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 "simgrid/s4u/Actor.hpp"
9 #include "simgrid/s4u/Exec.hpp"
10 #include "smx_private.hpp"
11 #include "src/kernel/activity/CommImpl.hpp"
12 #include "src/kernel/activity/ExecImpl.hpp"
13 #include "src/kernel/activity/IoImpl.hpp"
14 #include "src/kernel/activity/SleepImpl.hpp"
15 #include "src/kernel/activity/SynchroRaw.hpp"
16 #include "src/mc/mc_replay.hpp"
17 #include "src/mc/remote/Client.hpp"
18 #include "src/simix/smx_host_private.hpp"
19 #include "src/simix/smx_io_private.hpp"
20 #include "src/simix/smx_synchro_private.hpp"
21 #include "src/surf/HostImpl.hpp"
22 #include "src/surf/cpu_interface.hpp"
24 #include <boost/range/algorithm.hpp>
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
28 static unsigned long simix_process_maxpid = 0;
31 * @brief Returns the current agent.
33 * This functions returns the currently running SIMIX process.
35 * @return The SIMIX process
37 smx_actor_t SIMIX_process_self()
39 smx_context_t self_context = simgrid::kernel::context::Context::self();
41 return (self_context != nullptr) ? self_context->get_actor() : nullptr;
45 * @brief Returns whether a process has pending asynchronous communications.
46 * @return true if there are asynchronous communications in this process
48 int SIMIX_process_has_pending_comms(smx_actor_t process) {
50 return process->comms.size() > 0;
54 * @brief Moves a process to the list of processes to destroy.
56 void SIMIX_process_cleanup(smx_actor_t process)
58 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->get_cname(), process,
59 process->waiting_synchro.get());
61 simix_global->mutex.lock();
63 simix_global->process_list.erase(process->pid_);
64 if (process->host_ && process->host_process_list_hook.is_linked())
65 simgrid::xbt::intrusive_erase(process->host_->pimpl_->process_list_, *process);
66 if (not process->smx_destroy_list_hook.is_linked()) {
68 xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
70 simix_global->process_to_destroy.push_back(*process);
72 process->context_->iwannadie = false;
74 simix_global->mutex.unlock();
80 * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
82 void SIMIX_process_empty_trash()
84 while (not simix_global->process_to_destroy.empty()) {
85 smx_actor_t process = &simix_global->process_to_destroy.front();
86 simix_global->process_to_destroy.pop_front();
87 XBT_DEBUG("Getting rid of %p",process);
88 intrusive_ptr_release(process);
91 xbt_dynar_reset(simix_global->dead_actors_vector);
99 ActorImpl::ActorImpl(simgrid::xbt::string name, simgrid::s4u::Host* host) : name_(name), host_(host), piface_(this)
101 pid_ = simix_process_maxpid++;
102 simcall.issuer = this;
105 ActorImpl::~ActorImpl()
107 delete this->context_;
110 void ActorImpl::exit()
112 context_->iwannadie = true;
117 // Forcefully kill the actor if its host is turned off. Not a HostFailureException because you should not survive that
118 if (not host_->is_on())
119 this->throw_exception(std::make_exception_ptr(simgrid::kernel::context::StopRequest("host failed")));
121 /* destroy the blocking synchro if any */
122 if (waiting_synchro != nullptr) {
124 activity::ExecImplPtr exec = boost::dynamic_pointer_cast<activity::ExecImpl>(waiting_synchro);
125 activity::CommImplPtr comm = boost::dynamic_pointer_cast<activity::CommImpl>(waiting_synchro);
126 activity::SleepImplPtr sleep = boost::dynamic_pointer_cast<activity::SleepImpl>(waiting_synchro);
127 activity::RawImplPtr raw = boost::dynamic_pointer_cast<activity::RawImpl>(waiting_synchro);
128 activity::IoImplPtr io = boost::dynamic_pointer_cast<activity::IoImpl>(waiting_synchro);
130 if (exec != nullptr && exec->surf_action_) {
132 exec->surf_action_->unref();
133 exec->surf_action_ = nullptr;
134 } else if (comm != nullptr) {
135 comms.remove(waiting_synchro);
137 // Remove first occurrence of &process->simcall:
138 auto i = boost::range::find(waiting_synchro->simcalls_, &simcall);
139 if (i != waiting_synchro->simcalls_.end())
140 waiting_synchro->simcalls_.remove(&simcall);
141 } else if (sleep != nullptr) {
142 if (sleep->surf_action_)
143 sleep->surf_action_->cancel();
145 } else if (raw != nullptr) {
146 SIMIX_synchro_stop_waiting(this, &simcall);
147 } else if (io != nullptr) {
150 simgrid::kernel::activity::ActivityImplPtr activity = waiting_synchro;
151 xbt_die("Activity %s is of unknown type %s", activity->get_cname(),
152 simgrid::xbt::demangle(typeid(activity).name()).get());
155 waiting_synchro = nullptr;
159 void ActorImpl::kill(smx_actor_t actor)
161 if (actor->finished_) {
162 XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", actor->get_cname(),
163 actor->host_->get_cname());
167 XBT_DEBUG("Actor '%s'@%s is killing actor '%s'@%s", get_cname(), host_->get_cname(), actor->get_cname(),
168 actor->host_->get_cname());
172 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), actor) ==
173 end(simix_global->process_to_run) &&
175 XBT_DEBUG("Inserting %s in the to_run list", actor->get_cname());
176 simix_global->process_to_run.push_back(actor);
180 void ActorImpl::kill_all()
182 for (auto const& kv : simix_global->process_list)
183 if (kv.second != this)
184 this->kill(kv.second);
187 void ActorImpl::set_kill_time(double kill_time)
189 if (kill_time <= SIMIX_get_clock())
191 XBT_DEBUG("Set kill time %f for process %s@%s", kill_time, get_cname(), host_->get_cname());
192 kill_timer = SIMIX_timer_set(kill_time, [this] {
194 kill_timer = nullptr;
198 static void dying_daemon(int /*exit_status*/, void* data)
200 std::vector<ActorImpl*>* vect = &simix_global->daemons;
202 auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
203 xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
205 /* Don't move the whole content since we don't really care about the order */
206 std::swap(*it, vect->back());
210 /** This process will be terminated automatically when the last non-daemon process finishes */
211 void ActorImpl::daemonize()
215 simix_global->daemons.push_back(this);
216 SIMIX_process_on_exit(this, dying_daemon, this);
220 simgrid::s4u::Actor* ActorImpl::restart()
222 XBT_DEBUG("Restarting process %s on %s", get_cname(), host_->get_cname());
224 // retrieve the arguments of the old process
225 simgrid::kernel::actor::ProcessArg arg = ProcessArg(host_, this);
227 // kill the old process
228 (this == simix_global->maestro_process) ? this->exit() : SIMIX_process_self()->kill(this);
230 // start the new process
232 ActorImpl::create(arg.name, std::move(arg.code), arg.data, arg.host, arg.properties.get(), nullptr);
233 actor->set_kill_time(arg.kill_time);
234 actor->set_auto_restart(arg.auto_restart);
236 return actor->ciface();
239 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
242 XBT_DEBUG("Actor '%s' is already suspended", get_cname());
248 /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
249 if (this != issuer) {
251 waiting_synchro->suspend();
252 /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
256 return activity::ExecImplPtr(new activity::ExecImpl("suspend", "", nullptr, this->host_))->start(0.0, 1.0, 0.0);
260 void ActorImpl::resume()
262 XBT_IN("actor = %p", this);
264 if (context_->iwannadie) {
265 XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
273 /* resume the synchronization that was blocking the resumed actor. */
275 waiting_synchro->resume();
280 smx_activity_t ActorImpl::join(smx_actor_t actor, double timeout)
282 smx_activity_t res = this->sleep(timeout);
283 intrusive_ptr_add_ref(res.get());
284 SIMIX_process_on_exit(actor,
286 auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
287 if (sleep->surf_action_)
288 sleep->surf_action_->finish(simgrid::kernel::resource::Action::State::FINISHED);
289 intrusive_ptr_release(sleep);
294 smx_activity_t ActorImpl::sleep(double duration)
296 if (not host_->is_on())
297 throw_exception(std::make_exception_ptr(simgrid::HostFailureException(
298 XBT_THROW_POINT, std::string("Host ") + host_->get_cname() + " failed, you cannot sleep there.")));
300 return simgrid::kernel::activity::SleepImplPtr(new simgrid::kernel::activity::SleepImpl("sleep", host_))
304 void ActorImpl::throw_exception(std::exception_ptr e)
311 /* cancel the blocking synchro if any */
312 if (waiting_synchro) {
314 simgrid::kernel::activity::ExecImplPtr exec =
315 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(waiting_synchro);
319 simgrid::kernel::activity::CommImplPtr comm =
320 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(waiting_synchro);
321 if (comm != nullptr) {
326 simgrid::kernel::activity::SleepImplPtr sleep =
327 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(waiting_synchro);
328 if (sleep != nullptr) {
329 SIMIX_process_sleep_destroy(waiting_synchro);
330 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), this) ==
331 end(simix_global->process_to_run) &&
332 this != SIMIX_process_self()) {
333 XBT_DEBUG("Inserting [%p] %s in the to_run list", this, get_cname());
334 simix_global->process_to_run.push_back(this);
338 simgrid::kernel::activity::RawImplPtr raw =
339 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(waiting_synchro);
340 if (raw != nullptr) {
341 SIMIX_synchro_stop_waiting(this, &simcall);
344 simgrid::kernel::activity::IoImplPtr io =
345 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(waiting_synchro);
350 waiting_synchro = nullptr;
353 void ActorImpl::set_host(sg_host_t dest)
355 simgrid::xbt::intrusive_erase(host_->pimpl_->process_list_, *this);
357 dest->pimpl_->process_list_.push_back(*this);
360 ActorImplPtr ActorImpl::create(std::string name, simgrid::simix::ActorCode code, void* data, simgrid::s4u::Host* host,
361 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_actor)
364 XBT_DEBUG("Start actor %s@'%s'", name.c_str(), host->get_cname());
366 if (not host->is_on()) {
367 XBT_WARN("Cannot launch actor '%s' on failed host '%s'", name.c_str(), host->get_cname());
371 ActorImpl* actor = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host);
373 xbt_assert(code && host != nullptr, "Invalid parameters");
375 actor->set_user_data(data);
378 if (parent_actor != nullptr)
379 actor->ppid_ = parent_actor->pid_;
381 XBT_VERB("Create context %s", actor->get_cname());
382 actor->context_ = SIMIX_context_new(std::move(code), &SIMIX_process_cleanup, actor);
385 if (properties != nullptr)
386 for (auto const& kv : *properties)
387 actor->set_property(kv.first, kv.second);
389 /* Add the process to its host's process list */
390 host->pimpl_->process_list_.push_back(*actor);
392 XBT_DEBUG("Start context '%s'", actor->get_cname());
394 /* Now insert it in the global process list and in the process to run list */
395 simix_global->process_list[actor->pid_] = actor;
396 XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), host->get_cname());
397 simix_global->process_to_run.push_back(actor);
398 intrusive_ptr_add_ref(actor);
400 /* The on_creation() signal must be delayed until there, where the pid and everything is set */
401 simgrid::s4u::Actor::on_creation(actor->iface());
403 return ActorImplPtr(actor);
406 void create_maestro(simgrid::simix::ActorCode code)
408 /* Create maestro process and initialize it */
409 smx_actor_t maestro = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(""), /*host*/ nullptr);
412 maestro->context_ = SIMIX_context_new(simgrid::simix::ActorCode(), nullptr, maestro);
414 maestro->context_ = simix_global->context_factory->create_maestro(code, maestro);
417 maestro->simcall.issuer = maestro;
418 simix_global->maestro_process = maestro;
422 } // namespace kernel
425 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
426 std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
428 // This is mostly a copy/paste from SIMIX_process_new(),
429 // it'd be nice to share some code between those two functions.
431 sg_host_t host = sg_host_by_name(hostname);
432 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
434 if (not host->is_on()) {
435 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
439 smx_actor_t actor = new simgrid::kernel::actor::ActorImpl(simgrid::xbt::string(name), host);
441 actor->set_user_data(data);
442 actor->code = nullptr;
444 if (parent_process != nullptr)
445 actor->ppid_ = parent_process->pid_;
447 XBT_VERB("Create context %s", actor->get_cname());
448 xbt_assert(simix_global != nullptr, "simix is not initialized, please call MSG_init first");
449 actor->context_ = simix_global->context_factory->attach(&SIMIX_process_cleanup, actor);
452 if (properties != nullptr)
453 for (auto const& kv : *properties)
454 actor->set_property(kv.first, kv.second);
456 /* Add the process to it's host process list */
457 host->pimpl_->process_list_.push_back(*actor);
459 /* Now insert it in the global process list and in the process to run list */
460 simix_global->process_list[actor->pid_] = actor;
461 XBT_DEBUG("Inserting [%p] %s(%s) in the to_run list", actor, actor->get_cname(), host->get_cname());
462 simix_global->process_to_run.push_back(actor);
463 intrusive_ptr_add_ref(actor);
465 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(actor->context_);
466 xbt_assert(nullptr != context, "Not a suitable context");
467 context->attach_start();
469 /* The on_creation() signal must be delayed until there, where the pid and everything is set */
470 simgrid::s4u::ActorPtr tmp = actor->iface(); // Passing this directly to on_creation will lead to crashes
471 simgrid::s4u::Actor::on_creation(tmp);
476 void SIMIX_process_detach()
478 auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(simgrid::kernel::context::Context::self());
479 if (context == nullptr)
480 xbt_die("Not a suitable context");
482 SIMIX_process_cleanup(context->get_actor());
483 context->attach_stop();
487 * @brief Executes the processes from simix_global->process_to_run.
489 * The processes of simix_global->process_to_run are run (in parallel if
490 * possible). On exit, simix_global->process_to_run is empty, and
491 * simix_global->process_that_ran contains the list of processes that just ran.
492 * The two lists are swapped so, be careful when using them before and after a
493 * call to this function.
495 void SIMIX_process_runall()
497 SIMIX_context_runall();
499 simix_global->process_to_run.swap(simix_global->process_that_ran);
500 simix_global->process_to_run.clear();
503 /** @deprecated When this function gets removed, also remove the xbt_ex class, that is only there to help users to
505 void SIMIX_process_throw(smx_actor_t actor, xbt_errcat_t cat, int value, const char* msg)
507 SMX_EXCEPTION(actor, cat, value, msg);
509 if (actor->suspended_)
512 /* cancel the blocking synchro if any */
513 if (actor->waiting_synchro) {
515 simgrid::kernel::activity::ExecImplPtr exec =
516 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(actor->waiting_synchro);
520 simgrid::kernel::activity::CommImplPtr comm =
521 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(actor->waiting_synchro);
522 if (comm != nullptr) {
523 actor->comms.remove(comm);
527 simgrid::kernel::activity::SleepImplPtr sleep =
528 boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(actor->waiting_synchro);
529 if (sleep != nullptr) {
530 SIMIX_process_sleep_destroy(actor->waiting_synchro);
531 if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), actor) ==
532 end(simix_global->process_to_run) &&
533 actor != SIMIX_process_self()) {
534 XBT_DEBUG("Inserting [%p] %s in the to_run list", actor, actor->get_cname());
535 simix_global->process_to_run.push_back(actor);
539 simgrid::kernel::activity::RawImplPtr raw =
540 boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(actor->waiting_synchro);
541 if (raw != nullptr) {
542 SIMIX_synchro_stop_waiting(actor, &actor->simcall);
545 simgrid::kernel::activity::IoImplPtr io =
546 boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(actor->waiting_synchro);
551 actor->waiting_synchro = nullptr;
554 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t actor)
556 smx_activity_t sync_suspend = actor->suspend(simcall->issuer);
558 if (actor != simcall->issuer) {
559 SIMIX_simcall_answer(simcall);
561 sync_suspend->simcalls_.push_back(simcall);
562 actor->waiting_synchro = sync_suspend;
563 actor->waiting_synchro->suspend();
565 /* If we are suspending ourselves, then just do not finish the simcall now */
568 int SIMIX_process_get_maxpid() {
569 return simix_process_maxpid;
572 int SIMIX_process_count()
574 return simix_global->process_list.size();
577 void* SIMIX_process_self_get_data()
579 smx_actor_t self = SIMIX_process_self();
581 if (self == nullptr) {
584 return self->get_user_data();
587 void SIMIX_process_self_set_data(void *data)
589 SIMIX_process_self()->set_user_data(data);
593 /* needs to be public and without simcall because it is called
594 by exceptions and logging events */
595 const char* SIMIX_process_self_get_name() {
597 smx_actor_t process = SIMIX_process_self();
598 if (process == nullptr || process == simix_global->maestro_process)
601 return process->get_cname();
604 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
606 if (process->finished_) {
607 // The joined process is already finished, just wake up the issuer process right away
608 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
609 SIMIX_simcall_answer(simcall);
612 smx_activity_t sync = simcall->issuer->join(process, timeout);
613 sync->simcalls_.push_back(simcall);
614 simcall->issuer->waiting_synchro = sync;
617 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
619 if (MC_is_active() || MC_record_replay_is_active()) {
620 MC_process_clock_add(simcall->issuer, duration);
621 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
622 SIMIX_simcall_answer(simcall);
625 smx_activity_t sync = simcall->issuer->sleep(duration);
626 sync->simcalls_.push_back(simcall);
627 simcall->issuer->waiting_synchro = sync;
630 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
632 XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
633 simgrid::kernel::activity::SleepImplPtr sleep =
634 boost::static_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
636 if (sleep->surf_action_) {
637 sleep->surf_action_->unref();
638 sleep->surf_action_ = nullptr;
643 * @brief Calling this function makes the process to yield.
645 * Only the current process can call this function, giving back the control to maestro.
647 * @param self the current process
649 void SIMIX_process_yield(smx_actor_t self)
651 XBT_DEBUG("Yield actor '%s'", self->get_cname());
653 /* Go into sleep and return control to maestro */
654 self->context_->suspend();
656 /* Ok, maestro returned control to us */
657 XBT_DEBUG("Control returned to me: '%s'", self->get_cname());
659 if (self->context_->iwannadie) {
661 XBT_DEBUG("Process %s@%s is dead", self->get_cname(), self->host_->get_cname());
662 // throw simgrid::kernel::context::StopRequest(); Does not seem to properly kill the actor
663 self->context_->stop();
667 if (self->suspended_) {
668 XBT_DEBUG("Hey! I'm suspended.");
669 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
670 self->suspended_ = false;
674 if (self->exception != nullptr) {
675 XBT_DEBUG("Wait, maestro left me an exception");
676 std::exception_ptr exception = std::move(self->exception);
677 self->exception = nullptr;
678 std::rethrow_exception(std::move(exception));
681 if (SMPI_switch_data_segment && not self->finished_) {
682 SMPI_switch_data_segment(self->iface());
686 /** @brief Returns the list of processes to run.
689 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
691 return simix_global->process_to_run;
694 /** @brief Returns the process from PID. */
695 smx_actor_t SIMIX_process_from_PID(aid_t PID)
697 auto actor = simix_global->process_list.find(PID);
698 return actor == simix_global->process_list.end() ? nullptr : actor->second;
701 void SIMIX_process_on_exit(smx_actor_t actor, int_f_pvoid_pvoid_t fun, void* data)
703 SIMIX_process_on_exit(actor, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
706 void SIMIX_process_on_exit(smx_actor_t actor, std::function<void(int, void*)> fun, void* data)
708 xbt_assert(actor, "current process not found: are you in maestro context ?");
710 actor->on_exit.emplace_back(s_smx_process_exit_fun_t{fun, data});
713 /** @brief Restart a process, starting it again from the beginning. */
715 * @ingroup simix_process_management
716 * @brief Creates and runs a new SIMIX process.
718 * The structure and the corresponding thread are created and put in the list of ready processes.
720 * @param name a name for the process. It is for user-level information and can be nullptr.
721 * @param code the main function of the process
722 * @param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
724 * It can be retrieved with the method ActorImpl::getUserData().
725 * @param host where the new agent is executed.
726 * @param properties the properties of the process
728 smx_actor_t simcall_process_create(std::string name, simgrid::simix::ActorCode code, void* data, sg_host_t host,
729 std::unordered_map<std::string, std::string>* properties)
731 smx_actor_t self = SIMIX_process_self();
732 return simgrid::simix::simcall([name, code, data, host, properties, self] {
733 return simgrid::kernel::actor::ActorImpl::create(std::move(name), std::move(code), data, host, properties, self)
738 void simcall_process_set_data(smx_actor_t process, void* data)
740 simgrid::simix::simcall([process, data] { process->set_user_data(data); });