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/s4u/Actor.hpp"
9 #include "simgrid/s4u/Exec.hpp"
10 #include "simgrid/s4u/Host.hpp"
11 #include "src/kernel/activity/ExecImpl.hpp"
12 #include "src/simix/smx_host_private.hpp"
13 #include "src/simix/smx_private.hpp"
14 #include "src/surf/HostImpl.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
23 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_creation;
24 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_suspend;
25 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_resume;
26 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_sleep;
27 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_wake_up;
28 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_migration_start;
29 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_migration_end;
30 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_destruction;
32 // ***** Actor creation *****
33 ActorPtr Actor::self()
35 smx_context_t self_context = simgrid::kernel::context::Context::self();
36 if (self_context == nullptr)
37 return simgrid::s4u::ActorPtr();
39 return self_context->get_actor()->iface();
42 ActorPtr Actor::create(std::string name, s4u::Host* host, std::function<void()> code)
44 simgrid::kernel::actor::ActorImpl* actor =
45 simcall_process_create(std::move(name), std::move(code), nullptr, host, nullptr);
46 return actor->iface();
49 ActorPtr Actor::create(std::string name, s4u::Host* host, const std::string& function, std::vector<std::string> args)
51 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
52 return create(std::move(name), host, factory(std::move(args)));
55 void intrusive_ptr_add_ref(Actor* actor)
57 intrusive_ptr_add_ref(actor->pimpl_);
59 void intrusive_ptr_release(Actor* actor)
61 intrusive_ptr_release(actor->pimpl_);
64 // ***** Actor methods *****
68 simcall_process_join(this->pimpl_, -1);
71 void Actor::join(double timeout)
73 simcall_process_join(this->pimpl_, timeout);
76 void Actor::set_auto_restart(bool autorestart)
78 simgrid::simix::simcall([this, autorestart]() {
79 xbt_assert(autorestart && not pimpl_->has_to_auto_restart()); // FIXME: handle all cases
80 pimpl_->set_auto_restart(autorestart);
82 simgrid::kernel::actor::ProcessArg* arg = new simgrid::kernel::actor::ProcessArg(pimpl_->get_host(), pimpl_);
83 XBT_DEBUG("Adding Process %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
84 pimpl_->get_host()->pimpl_->actors_at_boot_.emplace_back(arg);
88 void Actor::on_exit(int_f_pvoid_pvoid_t fun, void* data) /* deprecated */
90 simgrid::simix::simcall([this, fun, data] { SIMIX_process_on_exit(pimpl_, fun, data); });
93 void Actor::on_exit(std::function<void(int, void*)> fun, void* data)
95 simgrid::simix::simcall([this, fun, data] { SIMIX_process_on_exit(pimpl_, fun, data); });
98 void Actor::migrate(Host* new_host)
100 s4u::Actor::on_migration_start(this);
102 simgrid::simix::simcall([this, new_host]() {
103 if (pimpl_->waiting_synchro != nullptr) {
104 // The actor is blocked on an activity. If it's an exec, migrate it too.
105 // FIXME: implement the migration of other kind of activities
106 simgrid::kernel::activity::ExecImplPtr exec =
107 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
108 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
109 exec->migrate(new_host);
111 this->pimpl_->set_host(new_host);
114 s4u::Actor::on_migration_end(this);
117 s4u::Host* Actor::get_host()
119 return this->pimpl_->get_host();
122 void Actor::daemonize()
124 simgrid::simix::simcall([this]() { pimpl_->daemonize(); });
127 bool Actor::is_daemon() const
129 return this->pimpl_->is_daemon();
132 const simgrid::xbt::string& Actor::get_name() const
134 return this->pimpl_->get_name();
137 const char* Actor::get_cname() const
139 return this->pimpl_->get_cname();
142 aid_t Actor::get_pid() const
144 return this->pimpl_->get_pid();
147 aid_t Actor::get_ppid() const
149 return this->pimpl_->get_ppid();
152 void Actor::suspend()
154 s4u::Actor::on_suspend(this);
155 simcall_process_suspend(pimpl_);
160 simgrid::simix::simcall([this] { pimpl_->resume(); });
161 s4u::Actor::on_resume(this);
164 bool Actor::is_suspended()
166 return simgrid::simix::simcall([this] { return pimpl_->is_suspended(); });
169 void Actor::set_kill_time(double kill_time)
171 simgrid::simix::simcall([this, kill_time] { pimpl_->set_kill_time(kill_time); });
174 /** @brief Get the kill time of an actor(or 0 if unset). */
175 double Actor::get_kill_time()
177 return pimpl_->get_kill_time();
180 void Actor::kill(aid_t pid) // deprecated
182 smx_actor_t killer = SIMIX_process_self();
183 smx_actor_t victim = SIMIX_process_from_PID(pid);
184 if (victim != nullptr) {
185 simgrid::simix::simcall([killer, victim] { killer->kill(victim); });
187 std::ostringstream oss;
188 oss << "kill: (" << pid << ") - No such actor" << std::endl;
189 throw std::runtime_error(oss.str());
195 smx_actor_t process = SIMIX_process_self();
196 simgrid::simix::simcall([this, process] {
197 if (pimpl_ == simix_global->maestro_process)
200 process->kill(pimpl_);
204 smx_actor_t Actor::get_impl()
209 // ***** Static functions *****
211 ActorPtr Actor::by_pid(aid_t pid)
213 smx_actor_t process = SIMIX_process_from_PID(pid);
214 if (process != nullptr)
215 return process->iface();
220 void Actor::kill_all()
222 smx_actor_t self = SIMIX_process_self();
223 simgrid::simix::simcall([self] { self->kill_all(); });
226 std::unordered_map<std::string, std::string>* Actor::get_properties()
228 return simgrid::simix::simcall([this] { return this->pimpl_->get_properties(); });
231 /** Retrieve the property value (or nullptr if not set) */
232 const char* Actor::get_property(const std::string& key)
234 return simgrid::simix::simcall([this, key] { return pimpl_->get_property(key); });
237 void Actor::set_property(const std::string& key, std::string value)
239 simgrid::simix::simcall([this, key, value] { pimpl_->set_property(key, std::move(value)); });
242 Actor* Actor::restart()
244 return simgrid::simix::simcall([this]() { return pimpl_->restart(); });
247 // ***** this_actor *****
249 namespace this_actor {
251 /** Returns true if run from the kernel mode, and false if run from a real actor
253 * Everything that is run out of any actor (simulation setup before the engine is run,
254 * computing the model evolutions as a result to the actors' action, etc) is run in
255 * kernel mode, just as in any operating systems.
257 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
258 * because it is the one scheduling when the others should move or wait.
262 smx_actor_t process = SIMIX_process_self();
263 return process == nullptr || process == simix_global->maestro_process;
266 void sleep_for(double duration)
269 smx_actor_t actor = SIMIX_process_self();
270 simgrid::s4u::Actor::on_sleep(actor->iface());
272 simcall_process_sleep(duration);
274 simgrid::s4u::Actor::on_wake_up(actor->iface());
280 simgrid::simix::simcall([] { /* do nothing*/ });
283 XBT_PUBLIC void sleep_until(double timeout)
285 double now = SIMIX_get_clock();
287 sleep_for(timeout - now);
290 void execute(double flops)
292 execute(flops, 1.0 /* priority */);
295 void execute(double flops, double priority)
297 exec_init(flops)->set_priority(priority)->start()->wait();
300 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
301 const std::vector<double>& bytes_amounts)
303 parallel_execute(hosts, flops_amounts, bytes_amounts, -1);
306 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
307 const std::vector<double>& bytes_amounts, double timeout)
309 xbt_assert(hosts.size() > 0, "Your parallel executions must span over at least one host.");
310 xbt_assert(hosts.size() == flops_amounts.size() || flops_amounts.empty(),
311 "Host count (%zu) does not match flops_amount count (%zu).", hosts.size(), flops_amounts.size());
312 xbt_assert(hosts.size() * hosts.size() == bytes_amounts.size() || bytes_amounts.empty(),
313 "bytes_amounts must be a matrix of size host_count * host_count (%zu*%zu), but it's of size %zu.",
314 hosts.size(), hosts.size(), flops_amounts.size());
316 /* The vectors live as parameter of parallel_execute. No copy is created for simcall_execution_parallel_start(),
317 * but that's OK because simcall_execution_wait() is called from here too.
319 smx_activity_t s = simcall_execution_parallel_start("", hosts.size(), hosts.data(),
320 (flops_amounts.empty() ? nullptr : flops_amounts.data()),
321 (bytes_amounts.empty() ? nullptr : bytes_amounts.data()),
322 /* rate */ -1, timeout);
323 simcall_execution_wait(s);
327 void parallel_execute(int host_nb, s4u::Host* const* host_list, const double* flops_amount, const double* bytes_amount,
331 simcall_execution_parallel_start("", host_nb, host_list, flops_amount, bytes_amount, /* rate */ -1, timeout);
332 simcall_execution_wait(s);
333 delete[] flops_amount;
334 delete[] bytes_amount;
338 void parallel_execute(int host_nb, s4u::Host* const* host_list, const double* flops_amount, const double* bytes_amount)
340 smx_activity_t s = simcall_execution_parallel_start("", host_nb, host_list, flops_amount, bytes_amount,
341 /* rate */ -1, /*timeout*/ -1);
342 simcall_execution_wait(s);
343 delete[] flops_amount;
344 delete[] bytes_amount;
347 ExecPtr exec_init(double flops_amount)
349 return ExecPtr(new Exec(get_host(), flops_amount));
352 ExecPtr exec_async(double flops)
354 ExecPtr res = exec_init(flops);
361 return SIMIX_process_self()->get_pid();
366 return SIMIX_process_self()->get_ppid();
369 std::string get_name()
371 return SIMIX_process_self()->get_name();
374 const char* get_cname()
376 return SIMIX_process_self()->get_cname();
381 return SIMIX_process_self()->get_host();
386 smx_actor_t actor = SIMIX_process_self();
387 simgrid::s4u::Actor::on_suspend(actor->iface());
389 simcall_process_suspend(actor);
394 smx_actor_t process = SIMIX_process_self();
395 simgrid::simix::simcall([process] { process->resume(); });
396 simgrid::s4u::Actor::on_resume(process->iface());
401 smx_actor_t actor = SIMIX_process_self();
402 simgrid::simix::simcall([actor] { actor->exit(); });
405 void on_exit(std::function<void(int, void*)> fun, void* data)
407 SIMIX_process_self()->iface()->on_exit(fun, data);
410 /** @brief Moves the current actor to another host
412 * @see simgrid::s4u::Actor::migrate() for more information
414 void migrate(Host* new_host)
416 SIMIX_process_self()->iface()->migrate(new_host);
419 std::string getName() /* deprecated */
423 const char* getCname() /* deprecated */
427 bool isMaestro() /* deprecated */
431 aid_t getPid() /* deprecated */
435 aid_t getPpid() /* deprecated */
439 Host* getHost() /* deprecated */
443 void on_exit(int_f_pvoid_pvoid_t fun, void* data) /* deprecated */
445 SIMIX_process_self()->iface()->on_exit([fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
447 void onExit(int_f_pvoid_pvoid_t fun, void* data) /* deprecated */
449 on_exit([fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
451 void kill() /* deprecated */
456 } // namespace this_actor
458 } // namespace simgrid
460 /* **************************** Public C interface *************************** */
462 /** @ingroup m_actor_management
463 * @brief Returns the process ID of @a actor.
465 * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
467 aid_t sg_actor_get_PID(sg_actor_t actor)
469 /* Do not raise an exception here: this function is called by the logs
470 * and the exceptions, so it would be called back again and again */
471 if (actor == nullptr || actor->get_impl() == nullptr)
473 return actor->get_pid();
476 /** @ingroup m_actor_management
477 * @brief Returns the process ID of the parent of @a actor.
479 * This function checks whether @a actor is a valid pointer and return its parent's PID.
480 * Returns -1 if the actor has not been created by any other actor.
482 aid_t sg_actor_get_PPID(sg_actor_t actor)
484 return actor->get_ppid();
487 /** @ingroup m_actor_management
489 * @brief Return a #sg_actor_t given its PID.
491 * 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.
492 * If none is found, @c nullptr is returned.
493 Note that the PID are unique in the whole simulation, not only on a given host.
495 sg_actor_t sg_actor_by_PID(aid_t pid)
497 return simgrid::s4u::Actor::by_pid(pid).get();
500 /** @ingroup m_actor_management
501 * @brief Return the name of an actor.
503 const char* sg_actor_get_name(sg_actor_t actor)
505 return actor->get_cname();
508 sg_host_t sg_actor_get_host(sg_actor_t actor)
510 return actor->get_host();
513 /** @ingroup m_actor_management
514 * @brief Returns the value of a given actor property
516 * @param actor an actor
517 * @param name a property name
518 * @return value of a property (or nullptr if the property is not set)
520 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
522 return actor->get_property(name);
525 /** @ingroup m_actor_management
526 * @brief Return the list of properties
528 * This function returns all the parameters associated with an actor
530 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
532 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
533 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
534 std::unordered_map<std::string, std::string>* props = actor->get_properties();
535 if (props == nullptr)
537 for (auto const& kv : *props) {
538 xbt_dict_set(as_dict, kv.first.c_str(), xbt_strdup(kv.second.c_str()), nullptr);
543 /** @ingroup m_actor_management
544 * @brief Suspend the actor.
546 * This function suspends the actor by suspending the task on which it was waiting for the completion.
548 void sg_actor_suspend(sg_actor_t actor)
550 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
554 /** @ingroup m_actor_management
555 * @brief Resume a suspended actor.
557 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
559 void sg_actor_resume(sg_actor_t actor)
561 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
565 /** @ingroup m_actor_management
566 * @brief Returns true if the actor is suspended .
568 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
570 int sg_actor_is_suspended(sg_actor_t actor)
572 return actor->is_suspended();
576 * @ingroup m_actor_management
577 * @brief Restarts an actor from the beginning.
579 sg_actor_t sg_actor_restart(sg_actor_t actor)
581 return actor->restart();
585 * @ingroup m_actor_management
586 * @brief Sets the "auto-restart" flag of the actor.
587 * If the flag is set to 1, the actor will be automatically restarted when its host comes back up.
589 void sg_actor_set_auto_restart(sg_actor_t actor, int auto_restart)
591 actor->set_auto_restart(auto_restart);
594 /** @ingroup m_actor_management
595 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
597 void sg_actor_daemonize(sg_actor_t actor)
602 /** @ingroup m_actor_management
603 * @brief Migrates an actor to another location.
605 * This function changes the value of the #sg_host_t on which @a actor is running.
607 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
609 process->migrate(host);
612 /** @ingroup m_actor_management
613 * @brief Wait for the completion of a #sg_actor_t.
615 * @param actor the actor to wait for
616 * @param timeout wait until the actor is over, or the timeout expires
618 void sg_actor_join(sg_actor_t actor, double timeout)
620 actor->join(timeout);
623 void sg_actor_kill(sg_actor_t actor)
628 void sg_actor_kill_all()
630 simgrid::s4u::Actor::kill_all();
633 /** @ingroup m_actor_management
634 * @brief Set the kill time of an actor.
636 * @param actor an actor
637 * @param kill_time the time when the actor is killed.
639 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
641 actor->set_kill_time(kill_time);
644 /** Yield the current actor; let the other actors execute first */
645 void sg_actor_yield()
647 simgrid::s4u::this_actor::yield();
650 void sg_actor_sleep_for(double duration)
652 simgrid::s4u::this_actor::sleep_for(duration);
655 sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties)
657 xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
658 std::unordered_map<std::string, std::string> props;
659 xbt_dict_cursor_t cursor = nullptr;
662 xbt_dict_foreach (properties, cursor, key, value)
664 xbt_dict_free(&properties);
666 /* Let's create the process: SIMIX may decide to start it right now, even before returning the flow control to us */
667 smx_actor_t actor = nullptr;
669 actor = simgrid::kernel::actor::ActorImpl::attach(name, data, host, &props).get();
670 } catch (simgrid::HostFailureException const&) {
671 xbt_die("Could not attach");
674 simgrid::s4u::this_actor::yield();
675 return actor->ciface();
678 void sg_actor_detach()
680 simgrid::kernel::actor::ActorImpl::detach();