1 /* Copyright (c) 2006-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. */
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/simix/smx_private.hpp"
17 #include "src/surf/HostImpl.hpp"
22 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
27 xbt::signal<void(Actor&)> s4u::Actor::on_creation;
28 xbt::signal<void(Actor const&)> s4u::Actor::on_suspend;
29 xbt::signal<void(Actor const&)> s4u::Actor::on_resume;
30 xbt::signal<void(Actor const&)> s4u::Actor::on_sleep;
31 xbt::signal<void(Actor const&)> s4u::Actor::on_wake_up;
32 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_start;
33 xbt::signal<void(Actor const&)> s4u::Actor::on_migration_end;
34 xbt::signal<void(Actor const&)> s4u::Actor::on_termination;
35 xbt::signal<void(Actor const&)> s4u::Actor::on_destruction;
37 // ***** Actor creation *****
40 kernel::context::Context* self_context = kernel::context::Context::self();
41 if (self_context == nullptr)
44 return self_context->get_actor()->ciface();
46 ActorPtr Actor::init(const std::string& name, s4u::Host* host)
48 smx_actor_t self = SIMIX_process_self();
49 kernel::actor::ActorImpl* actor =
50 kernel::actor::simcall([self, &name, host] { return self->init(name, host).get(); });
51 return actor->iface();
54 ActorPtr Actor::start(const std::function<void()>& code)
56 simgrid::kernel::actor::simcall([this, &code] { pimpl_->start(code); });
60 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::function<void()>& code)
62 smx_actor_t self = SIMIX_process_self();
63 kernel::actor::ActorImpl* actor =
64 kernel::actor::simcall([self, &name, host, &code] { return self->init(name, host)->start(code); });
66 return actor->iface();
69 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::string& function,
70 std::vector<std::string> args)
72 simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
73 return create(name, host, factory(std::move(args)));
76 void intrusive_ptr_add_ref(Actor* actor)
78 intrusive_ptr_add_ref(actor->pimpl_);
80 void intrusive_ptr_release(Actor* actor)
82 intrusive_ptr_release(actor->pimpl_);
84 int Actor::get_refcount()
86 return pimpl_->get_refcount();
89 // ***** Actor methods *****
96 void Actor::join(double timeout)
98 auto issuer = SIMIX_process_self();
100 kernel::actor::simcall_blocking<void>([issuer, target, timeout] {
101 if (target->finished_) {
102 // The joined process is already finished, just wake up the issuer right away
103 issuer->simcall_answer();
105 smx_activity_t sync = issuer->join(target, timeout);
106 sync->register_simcall(&issuer->simcall);
111 void Actor::set_auto_restart(bool autorestart)
113 kernel::actor::simcall([this, autorestart]() {
114 xbt_assert(autorestart && not pimpl_->has_to_auto_restart()); // FIXME: handle all cases
115 pimpl_->set_auto_restart(autorestart);
117 kernel::actor::ProcessArg* arg = new kernel::actor::ProcessArg(pimpl_->get_host(), pimpl_);
118 XBT_DEBUG("Adding Process %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
119 pimpl_->get_host()->pimpl_->actors_at_boot_.emplace_back(arg);
123 void Actor::on_exit(const std::function<void(bool /*failed*/)>& fun) const
125 kernel::actor::simcall([this, &fun] { SIMIX_process_on_exit(pimpl_, fun); });
128 void Actor::migrate(Host* new_host)
130 s4u::Actor::on_migration_start(*this);
132 kernel::actor::simcall([this, new_host]() {
133 if (pimpl_->waiting_synchro != nullptr) {
134 // The actor is blocked on an activity. If it's an exec, migrate it too.
135 // FIXME: implement the migration of other kinds of activities
136 kernel::activity::ExecImplPtr exec =
137 boost::dynamic_pointer_cast<kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
138 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
139 exec->migrate(new_host);
141 this->pimpl_->set_host(new_host);
144 s4u::Actor::on_migration_end(*this);
147 s4u::Host* Actor::get_host() const
149 return this->pimpl_->get_host();
152 void Actor::daemonize()
154 kernel::actor::simcall([this]() { pimpl_->daemonize(); });
157 bool Actor::is_daemon() const
159 return this->pimpl_->is_daemon();
162 const simgrid::xbt::string& Actor::get_name() const
164 return this->pimpl_->get_name();
167 const char* Actor::get_cname() const
169 return this->pimpl_->get_cname();
172 aid_t Actor::get_pid() const
174 return this->pimpl_->get_pid();
177 aid_t Actor::get_ppid() const
179 return this->pimpl_->get_ppid();
182 void Actor::suspend()
184 auto issuer = SIMIX_process_self();
185 auto target = pimpl_;
186 s4u::Actor::on_suspend(*this);
187 kernel::actor::simcall_blocking<void>([issuer, target]() {
189 if (target != issuer) {
190 /* If we are suspending ourselves, then just do not finish the simcall now */
191 issuer->simcall_answer();
198 kernel::actor::simcall([this] { pimpl_->resume(); });
199 s4u::Actor::on_resume(*this);
202 bool Actor::is_suspended()
204 return pimpl_->is_suspended();
207 void Actor::set_kill_time(double kill_time)
209 kernel::actor::simcall([this, kill_time] { pimpl_->set_kill_time(kill_time); });
212 /** @brief Get the kill time of an actor(or 0 if unset). */
213 double Actor::get_kill_time()
215 return pimpl_->get_kill_time();
220 kernel::actor::ActorImpl* process = SIMIX_process_self();
221 kernel::actor::simcall([this, process] {
222 xbt_assert(pimpl_ != simix_global->maestro_process, "Killing maestro is a rather bad idea");
223 process->kill(pimpl_);
227 // ***** Static functions *****
229 ActorPtr Actor::by_pid(aid_t pid)
231 kernel::actor::ActorImpl* process = SIMIX_process_from_PID(pid);
232 if (process != nullptr)
233 return process->iface();
238 void Actor::kill_all()
240 kernel::actor::ActorImpl* self = SIMIX_process_self();
241 kernel::actor::simcall([self] { self->kill_all(); });
244 const std::unordered_map<std::string, std::string>* Actor::get_properties() const
246 return pimpl_->get_properties();
249 /** Retrieve the property value (or nullptr if not set) */
250 const char* Actor::get_property(const std::string& key) const
252 return pimpl_->get_property(key);
255 void Actor::set_property(const std::string& key, const std::string& value)
257 kernel::actor::simcall([this, &key, &value] { pimpl_->set_property(key, value); });
260 Actor* Actor::restart()
262 return kernel::actor::simcall([this]() { return pimpl_->restart(); });
265 // ***** this_actor *****
267 namespace this_actor {
269 /** Returns true if run from the kernel mode, and false if run from a real actor
271 * Everything that is run out of any actor (simulation setup before the engine is run,
272 * computing the model evolutions as a result to the actors' action, etc) is run in
273 * kernel mode, just as in any operating systems.
275 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
276 * because it is the one scheduling when the others should move or wait.
280 kernel::actor::ActorImpl* process = SIMIX_process_self();
281 return process == nullptr || process == simix_global->maestro_process;
284 void sleep_for(double duration)
286 xbt_assert(std::isfinite(duration), "duration is not finite!");
289 kernel::actor::ActorImpl* issuer = SIMIX_process_self();
290 Actor::on_sleep(*issuer->ciface());
292 kernel::actor::simcall_blocking<void>([issuer, duration]() {
293 if (MC_is_active() || MC_record_replay_is_active()) {
294 MC_process_clock_add(issuer, duration);
295 issuer->simcall_answer();
298 smx_activity_t sync = issuer->sleep(duration);
299 sync->register_simcall(&issuer->simcall);
302 Actor::on_wake_up(*issuer->ciface());
308 kernel::actor::simcall([] { /* do nothing*/ });
311 XBT_PUBLIC void sleep_until(double wakeup_time)
313 double now = SIMIX_get_clock();
314 if (wakeup_time > now)
315 sleep_for(wakeup_time - now);
318 void execute(double flops)
320 execute(flops, 1.0 /* priority */);
323 void execute(double flops, double priority)
325 exec_init(flops)->set_priority(priority)->start()->wait();
328 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
329 const std::vector<double>& bytes_amounts)
331 parallel_execute(hosts, flops_amounts, bytes_amounts, -1);
334 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
335 const std::vector<double>& bytes_amounts, double timeout)
337 xbt_assert(hosts.size() > 0, "Your parallel executions must span over at least one host.");
338 xbt_assert(hosts.size() == flops_amounts.size() || flops_amounts.empty(),
339 "Host count (%zu) does not match flops_amount count (%zu).", hosts.size(), flops_amounts.size());
340 xbt_assert(hosts.size() * hosts.size() == bytes_amounts.size() || bytes_amounts.empty(),
341 "bytes_amounts must be a matrix of size host_count * host_count (%zu*%zu), but it's of size %zu.",
342 hosts.size(), hosts.size(), flops_amounts.size());
343 /* Check that we are not mixing VMs and PMs in the parallel task */
344 bool is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(hosts.front()));
345 xbt_assert(std::all_of(hosts.begin(), hosts.end(),
346 [is_a_vm](s4u::Host* elm) {
347 bool tmp_is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(elm));
348 return is_a_vm == tmp_is_a_vm;
350 "parallel_execute: mixing VMs and PMs is not supported (yet).");
351 /* checking for infinite values */
352 xbt_assert(std::all_of(flops_amounts.begin(), flops_amounts.end(), [](double elm) { return std::isfinite(elm); }),
353 "flops_amounts comprises infinite values!");
354 xbt_assert(std::all_of(bytes_amounts.begin(), bytes_amounts.end(), [](double elm) { return std::isfinite(elm); }),
355 "flops_amounts comprises infinite values!");
357 exec_init(hosts, flops_amounts, bytes_amounts)->set_timeout(timeout)->wait();
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 return ExecPtr(new ExecPar(hosts, flops_amounts, bytes_amounts));
371 ExecPtr exec_async(double flops)
373 ExecPtr res = exec_init(flops);
380 return SIMIX_process_self()->get_pid();
385 return SIMIX_process_self()->get_ppid();
388 std::string get_name()
390 return SIMIX_process_self()->get_name();
393 const char* get_cname()
395 return SIMIX_process_self()->get_cname();
400 return SIMIX_process_self()->get_host();
405 kernel::actor::ActorImpl* self = SIMIX_process_self();
406 s4u::Actor::on_suspend(*self->ciface());
407 kernel::actor::simcall_blocking<void>([self] { self->suspend(); });
412 kernel::actor::ActorImpl* self = SIMIX_process_self();
413 kernel::actor::simcall([self] { self->resume(); });
414 Actor::on_resume(*self->ciface());
419 kernel::actor::ActorImpl* self = SIMIX_process_self();
420 simgrid::kernel::actor::simcall([self] { self->exit(); });
423 void on_exit(const std::function<void(bool)>& fun)
425 SIMIX_process_self()->iface()->on_exit(fun);
428 /** @brief Moves the current actor to another host
430 * @see simgrid::s4u::Actor::migrate() for more information
432 void migrate(Host* new_host)
434 SIMIX_process_self()->iface()->migrate(new_host);
437 } // namespace this_actor
439 } // namespace simgrid
441 /* **************************** Public C interface *************************** */
443 /** @ingroup m_actor_management
444 * @brief Returns the process ID of @a actor.
446 * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
448 aid_t sg_actor_get_PID(sg_actor_t actor)
450 /* Do not raise an exception here: this function is called by the logs
451 * and the exceptions, so it would be called back again and again */
452 if (actor == nullptr || actor->get_impl() == nullptr)
454 return actor->get_pid();
457 /** @ingroup m_actor_management
458 * @brief Returns the process ID of the parent of @a actor.
460 * This function checks whether @a actor is a valid pointer and return its parent's PID.
461 * Returns -1 if the actor has not been created by any other actor.
463 aid_t sg_actor_get_PPID(sg_actor_t actor)
465 return actor->get_ppid();
468 /** @ingroup m_actor_management
470 * @brief Return a #sg_actor_t given its PID.
472 * 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.
473 * If none is found, @c nullptr is returned.
474 Note that the PID are unique in the whole simulation, not only on a given host.
476 sg_actor_t sg_actor_by_PID(aid_t pid)
478 return simgrid::s4u::Actor::by_pid(pid).get();
481 /** @ingroup m_actor_management
482 * @brief Return the name of an actor.
484 const char* sg_actor_get_name(sg_actor_t actor)
486 return actor->get_cname();
489 sg_host_t sg_actor_get_host(sg_actor_t actor)
491 return actor->get_host();
494 /** @ingroup m_actor_management
495 * @brief Returns the value of a given actor property
497 * @param actor an actor
498 * @param name a property name
499 * @return value of a property (or nullptr if the property is not set)
501 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
503 return actor->get_property(name);
506 /** @ingroup m_actor_management
507 * @brief Return the list of properties
509 * This function returns all the parameters associated with an actor
511 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
513 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
514 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
515 const std::unordered_map<std::string, std::string>* props = actor->get_properties();
516 if (props == nullptr)
518 for (auto const& kv : *props) {
519 xbt_dict_set(as_dict, kv.first.c_str(), xbt_strdup(kv.second.c_str()), nullptr);
524 /** @ingroup m_actor_management
525 * @brief Suspend the actor.
527 * This function suspends the actor by suspending the task on which it was waiting for the completion.
529 void sg_actor_suspend(sg_actor_t actor)
531 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
535 /** @ingroup m_actor_management
536 * @brief Resume a suspended actor.
538 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
540 void sg_actor_resume(sg_actor_t actor)
542 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
546 /** @ingroup m_actor_management
547 * @brief Returns true if the actor is suspended .
549 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
551 int sg_actor_is_suspended(sg_actor_t actor)
553 return actor->is_suspended();
557 * @ingroup m_actor_management
558 * @brief Restarts an actor from the beginning.
560 sg_actor_t sg_actor_restart(sg_actor_t actor)
562 return actor->restart();
566 * @ingroup m_actor_management
567 * @brief Sets the "auto-restart" flag of the actor.
568 * If the flag is set to 1, the actor will be automatically restarted when its host comes back up.
570 void sg_actor_set_auto_restart(sg_actor_t actor, int auto_restart)
572 actor->set_auto_restart(auto_restart);
575 /** @ingroup m_actor_management
576 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
578 void sg_actor_daemonize(sg_actor_t actor)
583 /** @ingroup m_actor_management
584 * @brief Migrates an actor to another location.
586 * This function changes the value of the #sg_host_t on which @a actor is running.
588 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
590 process->migrate(host);
593 /** @ingroup m_actor_management
594 * @brief Wait for the completion of a #sg_actor_t.
596 * @param actor the actor to wait for
597 * @param timeout wait until the actor is over, or the timeout expires
599 void sg_actor_join(sg_actor_t actor, double timeout)
601 actor->join(timeout);
604 void sg_actor_kill(sg_actor_t actor)
609 void sg_actor_kill_all()
611 simgrid::s4u::Actor::kill_all();
614 /** @ingroup m_actor_management
615 * @brief Set the kill time of an actor.
617 * @param actor an actor
618 * @param kill_time the time when the actor is killed.
620 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
622 actor->set_kill_time(kill_time);
625 /** Yield the current actor; let the other actors execute first */
626 void sg_actor_yield()
628 simgrid::s4u::this_actor::yield();
631 void sg_actor_sleep_for(double duration)
633 simgrid::s4u::this_actor::sleep_for(duration);
636 sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties)
638 xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
639 std::unordered_map<std::string, std::string> props;
640 xbt_dict_cursor_t cursor = nullptr;
643 xbt_dict_foreach (properties, cursor, key, value)
645 xbt_dict_free(&properties);
647 /* Let's create the process: SIMIX may decide to start it right now, even before returning the flow control to us */
648 smx_actor_t actor = nullptr;
650 actor = simgrid::kernel::actor::ActorImpl::attach(name, data, host, &props).get();
651 } catch (simgrid::HostFailureException const&) {
652 xbt_die("Could not attach");
655 simgrid::s4u::this_actor::yield();
656 return actor->ciface();
659 void sg_actor_detach()
661 simgrid::kernel::actor::ActorImpl::detach();
664 aid_t sg_actor_self_get_pid()
666 return simgrid::s4u::this_actor::get_pid();
669 aid_t sg_actor_self_get_ppid()
671 return simgrid::s4u::this_actor::get_ppid();
674 const char* sg_actor_self_get_name()
676 return simgrid::s4u::this_actor::get_cname();
679 sg_actor_t sg_actor_self()
681 return simgrid::s4u::Actor::self();
684 void sg_actor_self_execute(double flops)
686 simgrid::s4u::this_actor::execute(flops);
689 /** @brief Take an extra reference on that actor to prevent it to be garbage-collected */
690 void sg_actor_ref(sg_actor_t actor)
692 intrusive_ptr_add_ref(actor);
694 /** @brief Release a reference on that actor so that it can get be garbage-collected */
695 void sg_actor_unref(sg_actor_t actor)
697 intrusive_ptr_release(actor);
700 /** @brief Return the user data of a #sg_actor_t */
701 void* sg_actor_data(sg_actor_t actor)
703 return actor->get_data();
705 /** @brief Set the user data of a #sg_actor_t */
706 void sg_actor_data_set(sg_actor_t actor, void* userdata)
708 actor->set_data(userdata);