1 /* Copyright (c) 2006-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. */
8 #include "simgrid/s4u/Actor.hpp"
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Exec.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
14 #include "src/kernel/context/Context.hpp"
15 #include "src/simix/smx_private.hpp"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
24 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onCreation;
25 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onDestruction;
27 // ***** Actor creation *****
28 ActorPtr Actor::self()
30 smx_context_t self_context = SIMIX_context_self();
31 if (self_context == nullptr)
32 return simgrid::s4u::ActorPtr();
34 return self_context->process()->iface();
37 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
39 simgrid::kernel::actor::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
40 return actor->iface();
43 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
45 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
46 simgrid::simix::ActorCode code = factory(std::move(args));
47 simgrid::kernel::actor::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
48 return actor->iface();
51 void intrusive_ptr_add_ref(Actor* actor)
53 intrusive_ptr_add_ref(actor->pimpl_);
55 void intrusive_ptr_release(Actor* actor)
57 intrusive_ptr_release(actor->pimpl_);
60 // ***** Actor methods *****
63 simcall_process_join(this->pimpl_, -1);
66 void Actor::join(double timeout)
68 simcall_process_join(this->pimpl_, timeout);
71 void Actor::setAutoRestart(bool autorestart) {
72 simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
75 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
77 simcall_process_on_exit(pimpl_, fun, data);
80 /** @brief Moves the actor to another host
82 * If the actor is currently blocked on an execution activity, the activity is also
83 * migrated to the new host. If it's blocked on another kind of activity, an error is
84 * raised as the mandated code is not written yet. Please report that bug if you need it.
86 * Asynchronous activities started by the actor are not migrated automatically, so you have
87 * to take care of this yourself (only you knows which ones should be migrated).
89 void Actor::migrate(Host* new_host)
92 simgrid::instr::LinkType* link = nullptr;
93 if (TRACE_actor_is_enabled()) {
94 static long long int counter = 0;
96 key = std::to_string(counter);
100 container_t actor_container = simgrid::instr::Container::byName(instr_pid(this));
101 link = simgrid::instr::Container::getRoot()->getLink("ACTOR_LINK");
102 link->startEvent(actor_container, "M", key);
104 // destroy existing container of this process
105 actor_container->removeFromParent();
108 simgrid::simix::kernelImmediate([this, new_host]() {
109 if (pimpl_->waiting_synchro != nullptr) {
110 // The actor is blocked on an activity. If it's an exec, migrate it too.
111 // FIXME: implement the migration of other kind of activities
112 simgrid::kernel::activity::ExecImplPtr exec =
113 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
114 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
115 exec->migrate(new_host);
117 SIMIX_process_change_host(this->pimpl_, new_host);
120 if (TRACE_actor_is_enabled()) {
121 // create new container on the new_host location
122 simgrid::instr::Container::byName(new_host->get_name())->createChild(instr_pid(this), "ACTOR");
124 link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
128 s4u::Host* Actor::getHost()
130 return this->pimpl_->host;
133 void Actor::daemonize()
135 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
138 bool Actor::isDaemon()
140 return this->pimpl_->isDaemon();
143 const simgrid::xbt::string& Actor::get_name() const
145 return this->pimpl_->get_name();
148 const char* Actor::get_cname() const
150 return this->pimpl_->get_cname();
153 aid_t Actor::getPid()
155 return this->pimpl_->pid;
158 aid_t Actor::getPpid()
160 return this->pimpl_->ppid;
163 void Actor::suspend()
165 if (TRACE_actor_is_enabled())
166 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
168 simcall_process_suspend(pimpl_);
173 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
174 if (TRACE_actor_is_enabled())
175 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
178 int Actor::isSuspended()
180 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
183 void Actor::setKillTime(double time) {
184 simcall_process_set_kill_time(pimpl_,time);
187 /** \brief Get the kill time of an actor(or 0 if unset). */
188 double Actor::getKillTime()
190 return SIMIX_timer_get_date(pimpl_->kill_timer);
193 void Actor::kill(aid_t pid)
195 smx_actor_t killer = SIMIX_process_self();
196 smx_actor_t process = SIMIX_process_from_PID(pid);
197 if(process != nullptr) {
198 simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
200 std::ostringstream oss;
201 oss << "kill: (" << pid << ") - No such actor" << std::endl;
202 throw std::runtime_error(oss.str());
207 smx_actor_t process = SIMIX_process_self();
208 simgrid::simix::kernelImmediate(
209 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
212 smx_actor_t Actor::getImpl()
217 // ***** Static functions *****
219 ActorPtr Actor::byPid(aid_t pid)
221 smx_actor_t process = SIMIX_process_from_PID(pid);
222 if (process != nullptr)
223 return process->iface();
228 void Actor::killAll()
230 simcall_process_killall();
233 std::map<std::string, std::string>* Actor::getProperties()
235 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
238 /** Retrieve the property value (or nullptr if not set) */
239 const char* Actor::getProperty(const char* key)
241 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
244 void Actor::setProperty(const char* key, const char* value)
246 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
249 Actor* Actor::restart()
251 return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
254 // ***** this_actor *****
256 namespace this_actor {
258 /** Returns true if run from the kernel mode, and false if run from a real actor
260 * Everything that is run out of any actor (simulation setup before the engine is run,
261 * computing the model evolutions as a result to the actors' action, etc) is run in
262 * kernel mode, just as in any operating systems.
264 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
265 * because it is the one scheduling when the others should move or wait.
269 smx_actor_t process = SIMIX_process_self();
270 return process == nullptr || process == simix_global->maestro_process;
273 void sleep_for(double duration)
276 simcall_process_sleep(duration);
281 simgrid::simix::kernelImmediate([] { /* do nothing*/ });
284 XBT_PUBLIC void sleep_until(double timeout)
286 double now = SIMIX_get_clock();
288 simcall_process_sleep(timeout - now);
291 void execute(double flops)
293 smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
294 simcall_execution_wait(s);
297 void execute(double flops, double priority)
299 smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
300 simcall_execution_wait(s);
303 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
306 simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
307 simcall_execution_wait(s);
310 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
312 smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
313 simcall_execution_wait(s);
316 ExecPtr exec_init(double flops_amount)
318 ExecPtr res = ExecPtr(new Exec());
319 res->host_ = getHost();
320 res->flops_amount_ = flops_amount;
321 res->setRemains(flops_amount);
325 ExecPtr exec_async(double flops)
327 ExecPtr res = exec_init(flops);
334 return SIMIX_process_self()->pid;
339 return SIMIX_process_self()->ppid;
342 std::string get_name()
344 return SIMIX_process_self()->get_name();
347 const char* get_cname()
349 return SIMIX_process_self()->get_cname();
354 return SIMIX_process_self()->host;
359 if (TRACE_actor_is_enabled())
360 instr::Container::byName(get_name() + "-" + std::to_string(getPid()))
361 ->getState("ACTOR_STATE")
362 ->pushEvent("suspend");
363 simcall_process_suspend(SIMIX_process_self());
368 smx_actor_t process = SIMIX_process_self();
369 simgrid::simix::kernelImmediate([process] { process->resume(); });
371 if (TRACE_actor_is_enabled())
372 instr::Container::byName(get_name() + "-" + std::to_string(getPid()))->getState("ACTOR_STATE")->popEvent();
377 smx_actor_t process = SIMIX_process_self();
378 return simgrid::simix::kernelImmediate([process] { return process->suspended; });
383 smx_actor_t process = SIMIX_process_self();
384 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
387 void onExit(int_f_pvoid_pvoid_t fun, void* data)
389 simcall_process_on_exit(SIMIX_process_self(), fun, data);
392 /** @brief Moves the current actor to another host
394 * @see simgrid::s4u::Actor::migrate() for more information
396 void migrate(Host* new_host)
398 SIMIX_process_self()->iface()->migrate(new_host);
401 } // namespace this_actor
403 } // namespace simgrid
405 /* **************************** Public C interface *************************** */
407 /** \ingroup m_actor_management
408 * \brief Returns the process ID of \a actor.
410 * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
412 int sg_actor_get_PID(sg_actor_t actor)
414 /* Do not raise an exception here: this function is called by the logs
415 * and the exceptions, so it would be called back again and again */
416 if (actor == nullptr || actor->getImpl() == nullptr)
418 return actor->getPid();
421 /** \ingroup m_actor_management
422 * \brief Returns the process ID of the parent of \a actor.
424 * This function checks whether \a actor is a valid pointer and return its parent's PID.
425 * Returns -1 if the actor has not been created by any other actor.
427 int sg_actor_get_PPID(sg_actor_t actor)
429 return actor->getPpid();
432 /** \ingroup m_actor_management
433 * \brief Return the name of an actor.
435 const char* sg_actor_get_name(sg_actor_t actor)
437 return actor->get_cname();
440 sg_host_t sg_actor_get_host(sg_actor_t actor)
442 return actor->getHost();
445 /** \ingroup m_actor_management
446 * \brief Returns the value of a given actor property
448 * \param actor an actor
449 * \param name a property name
450 * \return value of a property (or nullptr if the property is not set)
452 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
454 return actor->getProperty(name);
457 /** \ingroup m_actor_management
458 * \brief Return the list of properties
460 * This function returns all the parameters associated with an actor
462 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
464 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
465 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
466 std::map<std::string, std::string>* props = actor->getProperties();
467 if (props == nullptr)
469 for (auto const& elm : *props) {
470 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
475 /** \ingroup m_actor_management
476 * \brief Suspend the actor.
478 * This function suspends the actor by suspending the task on which it was waiting for the completion.
480 void sg_actor_suspend(sg_actor_t actor)
482 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
486 /** \ingroup m_actor_management
487 * \brief Resume a suspended actor.
489 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
491 void sg_actor_resume(sg_actor_t actor)
493 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
497 /** \ingroup m_actor_management
498 * \brief Returns true if the actor is suspended .
500 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
502 int sg_actor_is_suspended(sg_actor_t actor)
504 return actor->isSuspended();
508 * \ingroup m_actor_management
509 * \brief Restarts an actor from the beginning.
511 sg_actor_t sg_actor_restart(sg_actor_t actor)
513 return actor->restart();
516 /** @ingroup m_actor_management
517 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
519 void sg_actor_daemonize(sg_actor_t actor)
524 /** \ingroup m_actor_management
525 * \brief Migrates an actor to another location.
527 * This function changes the value of the #sg_host_t on which \a actor is running.
529 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
531 process->migrate(host);
534 /** \ingroup m_actor_management
535 * \brief Wait for the completion of a #sg_actor_t.
537 * \param actor the actor to wait for
538 * \param timeout wait until the actor is over, or the timeout expires
540 void sg_actor_join(sg_actor_t actor, double timeout)
542 actor->join(timeout);
545 void sg_actor_kill(sg_actor_t actor)
550 /** \ingroup m_actor_management
551 * \brief Set the kill time of an actor.
553 * \param actor an actor
554 * \param kill_time the time when the actor is killed.
556 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
558 actor->setKillTime(kill_time);