1 /* Copyright (c) 2006-2020. 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. */
6 #include "simgrid/Exception.hpp"
7 #include "simgrid/actor.h"
8 #include "simgrid/modelchecker.h"
9 #include "simgrid/s4u/Actor.hpp"
10 #include "simgrid/s4u/Exec.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/VirtualMachine.hpp"
13 #include "src/include/mc/mc.h"
14 #include "src/kernel/activity/ExecImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
16 #include "src/surf/HostImpl.hpp"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_actor, s4u, "S4U actors");
25 template class xbt::Extendable<s4u::Actor>;
29 xbt::signal<void(Actor&)> s4u::Actor::on_creation;
30 xbt::signal<void(Actor const&)> s4u::Actor::on_suspend;
31 xbt::signal<void(Actor const&)> s4u::Actor::on_resume;
32 xbt::signal<void(Actor const&)> s4u::Actor::on_sleep;
33 xbt::signal<void(Actor const&)> s4u::Actor::on_wake_up;
34 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_start; // deprecated
35 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_end; // deprecated
36 xbt::signal<void(Actor const&, Host const& previous_location)> s4u::Actor::on_host_change;
37 xbt::signal<void(Actor const&)> s4u::Actor::on_termination;
38 xbt::signal<void(Actor const&)> s4u::Actor::on_destruction;
40 // ***** Actor creation *****
43 kernel::context::Context* self_context = kernel::context::Context::self();
44 if (self_context == nullptr)
47 return self_context->get_actor()->ciface();
50 ActorPtr Actor::init(const std::string& name, s4u::Host* host)
52 kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
53 kernel::actor::ActorImpl* actor =
54 kernel::actor::simcall([self, &name, host] { return self->init(name, host).get(); });
55 return actor->iface();
58 ActorPtr Actor::start(const std::function<void()>& code)
60 simgrid::kernel::actor::simcall([this, &code] { pimpl_->start(code); });
64 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::function<void()>& code)
66 kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
67 kernel::actor::ActorImpl* actor =
68 kernel::actor::simcall([self, &name, host, &code] { return self->init(name, host)->start(code); });
70 return actor->iface();
73 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::string& function,
74 std::vector<std::string> args)
76 const simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
77 return create(name, host, factory(std::move(args)));
80 void intrusive_ptr_add_ref(const Actor* actor)
82 intrusive_ptr_add_ref(actor->pimpl_);
84 void intrusive_ptr_release(const Actor* actor)
86 intrusive_ptr_release(actor->pimpl_);
88 int Actor::get_refcount()
90 return pimpl_->get_refcount();
93 // ***** Actor methods *****
100 void Actor::join(double timeout)
102 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
103 kernel::actor::ActorImpl* target = pimpl_;
104 kernel::actor::simcall_blocking<void>([issuer, target, timeout] {
105 if (target->finished_) {
106 // The joined process is already finished, just wake up the issuer right away
107 issuer->simcall_answer();
109 smx_activity_t sync = issuer->join(target, timeout);
110 sync->register_simcall(&issuer->simcall);
115 void Actor::set_auto_restart(bool autorestart)
117 kernel::actor::simcall([this, autorestart]() {
118 xbt_assert(autorestart && not pimpl_->has_to_auto_restart()); // FIXME: handle all cases
119 pimpl_->set_auto_restart(autorestart);
121 kernel::actor::ProcessArg* arg = new kernel::actor::ProcessArg(pimpl_->get_host(), pimpl_);
122 XBT_DEBUG("Adding %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
123 pimpl_->get_host()->pimpl_->add_actor_at_boot(arg);
127 void Actor::on_exit(const std::function<void(bool /*failed*/)>& fun) const
129 kernel::actor::simcall([this, &fun] { pimpl_->on_exit->emplace_back(fun); });
132 void Actor::set_host(Host* new_host)
134 if (s4u::Actor::on_migration_start.get_slot_count() > 0) { // XBT_ATTRIB_DEPRECATED_v329
135 static bool already_warned = false;
136 if (not already_warned) {
137 XBT_INFO("Please use s4u::Actor::on_host_change instead of s4u::Actor::on_migration_start. This will be removed "
139 already_warned = true;
141 s4u::Actor::on_migration_start(*this);
144 const s4u::Host* previous_location = get_host();
146 kernel::actor::simcall([this, new_host]() {
147 if (pimpl_->waiting_synchro != nullptr) {
148 // The actor is blocked on an activity. If it's an exec, migrate it too.
149 // FIXME: implement the migration of other kinds of activities
150 kernel::activity::ExecImplPtr exec =
151 boost::dynamic_pointer_cast<kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
152 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
153 exec->migrate(new_host);
155 this->pimpl_->set_host(new_host);
158 if (s4u::Actor::on_migration_end.get_slot_count() > 0) { // XBT_ATTRIB_DEPRECATED_v329
159 static bool already_warned = false;
160 if (not already_warned) {
161 XBT_INFO("Please use s4u::Actor::on_host_change instead of s4u::Actor::on_migration_end. This will be removed in "
163 already_warned = true;
165 s4u::Actor::on_migration_end(*this);
168 s4u::Actor::on_host_change(*this, *previous_location);
171 s4u::Host* Actor::get_host() const
173 return this->pimpl_->get_host();
176 void Actor::daemonize()
178 kernel::actor::simcall([this]() { pimpl_->daemonize(); });
181 bool Actor::is_daemon() const
183 return this->pimpl_->is_daemon();
186 const simgrid::xbt::string& Actor::get_name() const
188 return this->pimpl_->get_name();
191 const char* Actor::get_cname() const
193 return this->pimpl_->get_cname();
196 aid_t Actor::get_pid() const
198 return this->pimpl_->get_pid();
201 aid_t Actor::get_ppid() const
203 return this->pimpl_->get_ppid();
206 void Actor::suspend()
208 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
209 kernel::actor::ActorImpl* target = pimpl_;
210 s4u::Actor::on_suspend(*this);
211 kernel::actor::simcall_blocking<void>([issuer, target]() {
213 if (target != issuer) {
214 /* If we are suspending ourselves, then just do not finish the simcall now */
215 issuer->simcall_answer();
222 kernel::actor::simcall([this] { pimpl_->resume(); });
223 s4u::Actor::on_resume(*this);
226 bool Actor::is_suspended()
228 return pimpl_->is_suspended();
231 void Actor::set_kill_time(double kill_time)
233 kernel::actor::simcall([this, kill_time] { pimpl_->set_kill_time(kill_time); });
236 /** @brief Get the kill time of an actor(or 0 if unset). */
237 double Actor::get_kill_time()
239 return pimpl_->get_kill_time();
244 kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
245 kernel::actor::simcall([this, self] { self->kill(pimpl_); });
248 // ***** Static functions *****
250 ActorPtr Actor::by_pid(aid_t pid)
252 kernel::actor::ActorImpl* actor = SIMIX_process_from_PID(pid);
253 if (actor != nullptr)
254 return actor->iface();
259 void Actor::kill_all()
261 kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
262 kernel::actor::simcall([self] { self->kill_all(); });
265 const std::unordered_map<std::string, std::string>* Actor::get_properties() const
267 return pimpl_->get_properties();
270 /** Retrieve the property value (or nullptr if not set) */
271 const char* Actor::get_property(const std::string& key) const
273 return pimpl_->get_property(key);
276 void Actor::set_property(const std::string& key, const std::string& value)
278 kernel::actor::simcall([this, &key, &value] { pimpl_->set_property(key, value); });
281 Actor* Actor::restart()
283 return kernel::actor::simcall([this]() { return pimpl_->restart(); });
286 // ***** this_actor *****
288 namespace this_actor {
290 /** Returns true if run from the kernel mode, and false if run from a real actor
292 * Everything that is run out of any actor (simulation setup before the engine is run,
293 * computing the model evolutions as a result to the actors' action, etc) is run in
294 * kernel mode, just as in any operating systems.
296 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
297 * because it is the one scheduling when the others should move or wait.
301 return SIMIX_is_maestro();
304 void sleep_for(double duration)
306 xbt_assert(std::isfinite(duration), "duration is not finite!");
309 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
310 Actor::on_sleep(*issuer->ciface());
312 kernel::actor::simcall_blocking<void>([issuer, duration]() {
313 if (MC_is_active() || MC_record_replay_is_active()) {
314 MC_process_clock_add(issuer, duration);
315 issuer->simcall_answer();
318 smx_activity_t sync = issuer->sleep(duration);
319 sync->register_simcall(&issuer->simcall);
322 Actor::on_wake_up(*issuer->ciface());
328 kernel::actor::simcall([] { /* do nothing*/ });
331 XBT_PUBLIC void sleep_until(double wakeup_time)
333 double now = SIMIX_get_clock();
334 if (wakeup_time > now)
335 sleep_for(wakeup_time - now);
338 void execute(double flops)
340 execute(flops, 1.0 /* priority */);
343 void execute(double flops, double priority)
345 exec_init(flops)->set_priority(priority)->start()->wait();
348 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
349 const std::vector<double>& bytes_amounts)
351 exec_init(hosts, flops_amounts, bytes_amounts)->wait();
354 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
355 const std::vector<double>& bytes_amounts, double timeout) // XBT_ATTRIB_DEPRECATED_v329
357 exec_init(hosts, flops_amounts, bytes_amounts)->wait_for(timeout);
360 ExecPtr exec_init(double flops_amount)
362 return ExecPtr(new ExecSeq(get_host(), flops_amount));
365 ExecPtr exec_init(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
366 const std::vector<double>& bytes_amounts)
368 xbt_assert(hosts.size() > 0, "Your parallel executions must span over at least one host.");
369 xbt_assert(hosts.size() == flops_amounts.size() || flops_amounts.empty(),
370 "Host count (%zu) does not match flops_amount count (%zu).", hosts.size(), flops_amounts.size());
371 xbt_assert(hosts.size() * hosts.size() == bytes_amounts.size() || bytes_amounts.empty(),
372 "bytes_amounts must be a matrix of size host_count * host_count (%zu*%zu), but it's of size %zu.",
373 hosts.size(), hosts.size(), flops_amounts.size());
374 /* Check that we are not mixing VMs and PMs in the parallel task */
375 bool is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(hosts.front()));
376 xbt_assert(std::all_of(hosts.begin(), hosts.end(),
377 [is_a_vm](s4u::Host* elm) {
378 bool tmp_is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(elm));
379 return is_a_vm == tmp_is_a_vm;
381 "parallel_execute: mixing VMs and PMs is not supported (yet).");
382 /* checking for infinite values */
383 xbt_assert(std::all_of(flops_amounts.begin(), flops_amounts.end(), [](double elm) { return std::isfinite(elm); }),
384 "flops_amounts comprises infinite values!");
385 xbt_assert(std::all_of(bytes_amounts.begin(), bytes_amounts.end(), [](double elm) { return std::isfinite(elm); }),
386 "flops_amounts comprises infinite values!");
388 return ExecPtr(new ExecPar(hosts, flops_amounts, bytes_amounts));
391 ExecPtr exec_async(double flops)
393 ExecPtr res = exec_init(flops);
400 return simgrid::kernel::actor::ActorImpl::self()->get_pid();
405 return simgrid::kernel::actor::ActorImpl::self()->get_ppid();
408 std::string get_name()
410 return simgrid::kernel::actor::ActorImpl::self()->get_name();
413 const char* get_cname()
415 return simgrid::kernel::actor::ActorImpl::self()->get_cname();
420 return simgrid::kernel::actor::ActorImpl::self()->get_host();
425 kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
426 s4u::Actor::on_suspend(*self->ciface());
427 kernel::actor::simcall_blocking<void>([self] { self->suspend(); });
432 kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
433 simgrid::kernel::actor::simcall([self] { self->exit(); });
436 void on_exit(const std::function<void(bool)>& fun)
438 simgrid::kernel::actor::ActorImpl::self()->iface()->on_exit(fun);
441 /** @brief Moves the current actor to another host
443 * @see simgrid::s4u::Actor::migrate() for more information
445 void set_host(Host* new_host)
447 simgrid::kernel::actor::ActorImpl::self()->iface()->set_host(new_host);
449 void migrate(Host* new_host) // deprecated
454 } // namespace this_actor
456 } // namespace simgrid
458 /* **************************** Public C interface *************************** */
460 sg_actor_t sg_actor_init(const char* name, sg_host_t host)
462 return simgrid::s4u::Actor::init(name, host).get();
465 void sg_actor_start(sg_actor_t actor, xbt_main_func_t code, int argc, char** argv)
467 simgrid::simix::ActorCode function;
469 function = simgrid::xbt::wrap_main(code, argc, static_cast<const char* const*>(argv));
470 actor->start(std::move(function));
473 /** @ingroup m_actor_management
474 * @brief Returns the process ID of @a actor.
476 * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
478 aid_t sg_actor_get_PID(const_sg_actor_t actor)
480 /* Do not raise an exception here: this function is called by the logs
481 * and the exceptions, so it would be called back again and again */
482 if (actor == nullptr || actor->get_impl() == nullptr)
484 return actor->get_pid();
487 /** @ingroup m_actor_management
488 * @brief Returns the process ID of the parent of @a actor.
490 * This function checks whether @a actor is a valid pointer and return its parent's PID.
491 * Returns -1 if the actor has not been created by any other actor.
493 aid_t sg_actor_get_PPID(const_sg_actor_t actor)
495 return actor->get_ppid();
498 /** @ingroup m_actor_management
500 * @brief Return a #sg_actor_t given its PID.
502 * This function search in the list of all the created sg_actor_t for a sg_actor_t whose PID is equal to @a PID.
503 * If none is found, @c nullptr is returned.
504 Note that the PID are unique in the whole simulation, not only on a given host.
506 sg_actor_t sg_actor_by_PID(aid_t pid)
508 return simgrid::s4u::Actor::by_pid(pid).get();
511 /** @ingroup m_actor_management
512 * @brief Return the name of an actor.
514 const char* sg_actor_get_name(const_sg_actor_t actor)
516 return actor->get_cname();
519 sg_host_t sg_actor_get_host(const_sg_actor_t actor)
521 return actor->get_host();
524 /** @ingroup m_actor_management
525 * @brief Returns the value of a given actor property
527 * @param actor an actor
528 * @param name a property name
529 * @return value of a property (or nullptr if the property is not set)
531 const char* sg_actor_get_property_value(const_sg_actor_t actor, const char* name)
533 return actor->get_property(name);
536 /** @ingroup m_actor_management
537 * @brief Return the list of properties
539 * This function returns all the parameters associated with an actor
541 xbt_dict_t sg_actor_get_properties(const_sg_actor_t actor)
543 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
544 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
545 const std::unordered_map<std::string, std::string>* props = actor->get_properties();
546 if (props == nullptr)
548 for (auto const& kv : *props) {
549 xbt_dict_set(as_dict, kv.first.c_str(), xbt_strdup(kv.second.c_str()));
554 /** @ingroup m_actor_management
555 * @brief Suspend the actor.
557 * This function suspends the actor by suspending the task on which it was waiting for the completion.
559 void sg_actor_suspend(sg_actor_t actor)
561 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
565 /** @ingroup m_actor_management
566 * @brief Resume a suspended actor.
568 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
570 void sg_actor_resume(sg_actor_t actor)
572 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
576 /** @ingroup m_actor_management
577 * @brief Returns true if the actor is suspended .
579 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
581 int sg_actor_is_suspended(sg_actor_t actor)
583 return actor->is_suspended();
587 * @ingroup m_actor_management
588 * @brief Restarts an actor from the beginning.
590 sg_actor_t sg_actor_restart(sg_actor_t actor)
592 return actor->restart();
596 * @ingroup m_actor_management
597 * @brief Sets the "auto-restart" flag of the actor.
598 * If the flag is set to 1, the actor will be automatically restarted when its host comes back up.
600 void sg_actor_set_auto_restart(sg_actor_t actor, int auto_restart)
602 actor->set_auto_restart(auto_restart);
605 /** @ingroup m_actor_management
606 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
608 void sg_actor_daemonize(sg_actor_t actor)
613 /** @ingroup m_actor_management
614 * @brief Migrates an actor to another location.
616 * This function changes the value of the #sg_host_t on which @a actor is running.
618 void sg_actor_set_host(sg_actor_t actor, sg_host_t host)
620 actor->set_host(host);
622 void sg_actor_migrate(sg_actor_t process, sg_host_t host) // deprecated
624 process->set_host(host);
627 /** @ingroup m_actor_management
628 * @brief Wait for the completion of a #sg_actor_t.
630 * @param actor the actor to wait for
631 * @param timeout wait until the actor is over, or the timeout expires
633 void sg_actor_join(sg_actor_t actor, double timeout)
635 actor->join(timeout);
638 void sg_actor_kill(sg_actor_t actor)
643 void sg_actor_kill_all()
645 simgrid::s4u::Actor::kill_all();
648 /** @ingroup m_actor_management
649 * @brief Set the kill time of an actor.
651 * @param actor an actor
652 * @param kill_time the time when the actor is killed.
654 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
656 actor->set_kill_time(kill_time);
659 /** Yield the current actor; let the other actors execute first */
660 void sg_actor_yield()
662 simgrid::s4u::this_actor::yield();
665 void sg_actor_sleep_for(double duration)
667 simgrid::s4u::this_actor::sleep_for(duration);
670 sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties)
672 xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
673 std::unordered_map<std::string, std::string> props;
674 xbt_dict_cursor_t cursor = nullptr;
677 xbt_dict_foreach (properties, cursor, key, value)
679 xbt_dict_free(&properties);
681 /* Let's create the process: SIMIX may decide to start it right now, even before returning the flow control to us */
682 smx_actor_t actor = nullptr;
684 actor = simgrid::kernel::actor::ActorImpl::attach(name, data, host, &props).get();
685 } catch (simgrid::HostFailureException const&) {
686 xbt_die("Could not attach");
689 simgrid::s4u::this_actor::yield();
690 return actor->ciface();
693 void sg_actor_detach()
695 simgrid::kernel::actor::ActorImpl::detach();
698 aid_t sg_actor_self_get_pid()
700 return simgrid::s4u::this_actor::get_pid();
703 aid_t sg_actor_self_get_ppid()
705 return simgrid::s4u::this_actor::get_ppid();
708 const char* sg_actor_self_get_name()
710 return simgrid::s4u::this_actor::get_cname();
713 void* sg_actor_self_data()
715 return simgrid::s4u::Actor::self()->get_data();
718 void sg_actor_self_data_set(void* userdata)
720 simgrid::s4u::Actor::self()->set_data(userdata);
723 sg_actor_t sg_actor_self()
725 return simgrid::s4u::Actor::self();
728 void sg_actor_self_execute(double flops)
730 simgrid::s4u::this_actor::execute(flops);
733 /** @brief Take an extra reference on that actor to prevent it to be garbage-collected */
734 void sg_actor_ref(const_sg_actor_t actor)
736 intrusive_ptr_add_ref(actor);
738 /** @brief Release a reference on that actor so that it can get be garbage-collected */
739 void sg_actor_unref(const_sg_actor_t actor)
741 intrusive_ptr_release(actor);
744 /** @brief Return the user data of a #sg_actor_t */
745 void* sg_actor_data(const_sg_actor_t actor)
747 return actor->get_data();
749 /** @brief Set the user data of a #sg_actor_t */
750 void sg_actor_data_set(sg_actor_t actor, void* userdata)
752 actor->set_data(userdata);