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. */
6 #include "simgrid/s4u/Actor.hpp"
7 #include "simgrid/s4u/Exec.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/instr/instr_private.hpp"
10 #include "src/simix/smx_private.hpp"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
17 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_creation;
18 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_destruction;
20 // ***** Actor creation *****
21 ActorPtr Actor::self()
23 smx_context_t self_context = SIMIX_context_self();
24 if (self_context == nullptr)
25 return simgrid::s4u::ActorPtr();
27 return self_context->process()->iface();
30 ActorPtr Actor::create(const char* name, s4u::Host* host, std::function<void()> code)
32 simgrid::kernel::actor::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
33 return actor->iface();
36 ActorPtr Actor::create(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
38 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
39 simgrid::simix::ActorCode code = factory(std::move(args));
40 simgrid::kernel::actor::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
41 return actor->iface();
44 void intrusive_ptr_add_ref(Actor* actor)
46 intrusive_ptr_add_ref(actor->pimpl_);
48 void intrusive_ptr_release(Actor* actor)
50 intrusive_ptr_release(actor->pimpl_);
53 // ***** Actor methods *****
56 simcall_process_join(this->pimpl_, -1);
59 void Actor::join(double timeout)
61 simcall_process_join(this->pimpl_, timeout);
64 void Actor::setAutoRestart(bool autorestart) {
65 simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
68 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
70 simcall_process_on_exit(pimpl_, fun, data);
73 /** @brief Moves the actor to another host
75 * If the actor is currently blocked on an execution activity, the activity is also
76 * migrated to the new host. If it's blocked on another kind of activity, an error is
77 * raised as the mandated code is not written yet. Please report that bug if you need it.
79 * Asynchronous activities started by the actor are not migrated automatically, so you have
80 * to take care of this yourself (only you knows which ones should be migrated).
82 void Actor::migrate(Host* new_host)
85 simgrid::instr::LinkType* link = nullptr;
86 if (TRACE_actor_is_enabled()) {
87 static long long int counter = 0;
89 key = std::to_string(counter);
93 container_t actor_container = simgrid::instr::Container::byName(instr_pid(this));
94 link = simgrid::instr::Container::getRoot()->getLink("ACTOR_LINK");
95 link->startEvent(actor_container, "M", key);
97 // destroy existing container of this process
98 actor_container->removeFromParent();
101 simgrid::simix::kernelImmediate([this, new_host]() {
102 if (pimpl_->waiting_synchro != nullptr) {
103 // The actor is blocked on an activity. If it's an exec, migrate it too.
104 // FIXME: implement the migration of other kind of activities
105 simgrid::kernel::activity::ExecImplPtr exec =
106 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
107 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
108 exec->migrate(new_host);
110 SIMIX_process_change_host(this->pimpl_, new_host);
113 if (TRACE_actor_is_enabled()) {
114 // create new container on the new_host location
115 simgrid::instr::Container::byName(new_host->get_name())->createChild(instr_pid(this), "ACTOR");
117 link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
121 s4u::Host* Actor::get_host()
123 return this->pimpl_->host;
126 void Actor::daemonize()
128 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
131 bool Actor::is_daemon() const
133 return this->pimpl_->isDaemon();
136 const simgrid::xbt::string& Actor::get_name() const
138 return this->pimpl_->get_name();
141 const char* Actor::get_cname() const
143 return this->pimpl_->get_cname();
146 aid_t Actor::get_pid()
148 return this->pimpl_->pid;
151 aid_t Actor::get_ppid()
153 return this->pimpl_->ppid;
156 void Actor::suspend()
158 if (TRACE_actor_is_enabled())
159 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
161 simcall_process_suspend(pimpl_);
166 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
167 if (TRACE_actor_is_enabled())
168 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
171 int Actor::isSuspended()
173 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
176 void Actor::setKillTime(double time) {
177 simcall_process_set_kill_time(pimpl_,time);
180 /** \brief Get the kill time of an actor(or 0 if unset). */
181 double Actor::getKillTime()
183 return SIMIX_timer_get_date(pimpl_->kill_timer);
186 void Actor::kill(aid_t pid)
188 smx_actor_t killer = SIMIX_process_self();
189 smx_actor_t process = SIMIX_process_from_PID(pid);
190 if(process != nullptr) {
191 simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
193 std::ostringstream oss;
194 oss << "kill: (" << pid << ") - No such actor" << std::endl;
195 throw std::runtime_error(oss.str());
200 smx_actor_t process = SIMIX_process_self();
201 simgrid::simix::kernelImmediate(
202 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
205 smx_actor_t Actor::getImpl()
210 // ***** Static functions *****
212 ActorPtr Actor::byPid(aid_t pid)
214 smx_actor_t process = SIMIX_process_from_PID(pid);
215 if (process != nullptr)
216 return process->iface();
221 void Actor::killAll()
223 simcall_process_killall();
226 std::map<std::string, std::string>* Actor::getProperties()
228 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
231 /** Retrieve the property value (or nullptr if not set) */
232 const char* Actor::getProperty(const char* key)
234 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
237 void Actor::setProperty(const char* key, const char* value)
239 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
242 Actor* Actor::restart()
244 return simgrid::simix::kernelImmediate([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 simcall_process_sleep(duration);
274 simgrid::simix::kernelImmediate([] { /* do nothing*/ });
277 XBT_PUBLIC void sleep_until(double timeout)
279 double now = SIMIX_get_clock();
281 simcall_process_sleep(timeout - now);
284 void execute(double flops)
286 smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
287 simcall_execution_wait(s);
290 void execute(double flops, double priority)
292 smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
293 simcall_execution_wait(s);
296 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
299 simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
300 simcall_execution_wait(s);
303 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
305 smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
306 simcall_execution_wait(s);
309 ExecPtr exec_init(double flops_amount)
311 ExecPtr res = ExecPtr(new Exec());
312 res->host_ = getHost();
313 res->flops_amount_ = flops_amount;
314 res->set_remaining(flops_amount);
318 ExecPtr exec_async(double flops)
320 ExecPtr res = exec_init(flops);
327 return SIMIX_process_self()->pid;
332 return SIMIX_process_self()->ppid;
335 std::string get_name()
337 return SIMIX_process_self()->get_name();
340 const char* get_cname()
342 return SIMIX_process_self()->get_cname();
347 return SIMIX_process_self()->host;
352 if (TRACE_actor_is_enabled())
353 instr::Container::byName(get_name() + "-" + std::to_string(getPid()))
354 ->getState("ACTOR_STATE")
355 ->pushEvent("suspend");
356 simcall_process_suspend(SIMIX_process_self());
361 smx_actor_t process = SIMIX_process_self();
362 simgrid::simix::kernelImmediate([process] { process->resume(); });
364 if (TRACE_actor_is_enabled())
365 instr::Container::byName(get_name() + "-" + std::to_string(getPid()))->getState("ACTOR_STATE")->popEvent();
370 smx_actor_t process = SIMIX_process_self();
371 return simgrid::simix::kernelImmediate([process] { return process->suspended; });
376 smx_actor_t process = SIMIX_process_self();
377 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
380 void onExit(int_f_pvoid_pvoid_t fun, void* data)
382 simcall_process_on_exit(SIMIX_process_self(), fun, data);
385 /** @brief Moves the current actor to another host
387 * @see simgrid::s4u::Actor::migrate() for more information
389 void migrate(Host* new_host)
391 SIMIX_process_self()->iface()->migrate(new_host);
394 } // namespace this_actor
396 } // namespace simgrid
398 /* **************************** Public C interface *************************** */
400 /** \ingroup m_actor_management
401 * \brief Returns the process ID of \a actor.
403 * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
405 int sg_actor_get_PID(sg_actor_t actor)
407 /* Do not raise an exception here: this function is called by the logs
408 * and the exceptions, so it would be called back again and again */
409 if (actor == nullptr || actor->getImpl() == nullptr)
411 return actor->get_pid();
414 /** \ingroup m_actor_management
415 * \brief Returns the process ID of the parent of \a actor.
417 * This function checks whether \a actor is a valid pointer and return its parent's PID.
418 * Returns -1 if the actor has not been created by any other actor.
420 int sg_actor_get_PPID(sg_actor_t actor)
422 return actor->get_ppid();
425 /** \ingroup m_actor_management
426 * \brief Return the name of an actor.
428 const char* sg_actor_get_name(sg_actor_t actor)
430 return actor->get_cname();
433 sg_host_t sg_actor_get_host(sg_actor_t actor)
435 return actor->get_host();
438 /** \ingroup m_actor_management
439 * \brief Returns the value of a given actor property
441 * \param actor an actor
442 * \param name a property name
443 * \return value of a property (or nullptr if the property is not set)
445 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
447 return actor->getProperty(name);
450 /** \ingroup m_actor_management
451 * \brief Return the list of properties
453 * This function returns all the parameters associated with an actor
455 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
457 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
458 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
459 std::map<std::string, std::string>* props = actor->getProperties();
460 if (props == nullptr)
462 for (auto const& elm : *props) {
463 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
468 /** \ingroup m_actor_management
469 * \brief Suspend the actor.
471 * This function suspends the actor by suspending the task on which it was waiting for the completion.
473 void sg_actor_suspend(sg_actor_t actor)
475 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
479 /** \ingroup m_actor_management
480 * \brief Resume a suspended actor.
482 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
484 void sg_actor_resume(sg_actor_t actor)
486 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
490 /** \ingroup m_actor_management
491 * \brief Returns true if the actor is suspended .
493 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
495 int sg_actor_is_suspended(sg_actor_t actor)
497 return actor->isSuspended();
501 * \ingroup m_actor_management
502 * \brief Restarts an actor from the beginning.
504 sg_actor_t sg_actor_restart(sg_actor_t actor)
506 return actor->restart();
509 /** @ingroup m_actor_management
510 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
512 void sg_actor_daemonize(sg_actor_t actor)
517 /** \ingroup m_actor_management
518 * \brief Migrates an actor to another location.
520 * This function changes the value of the #sg_host_t on which \a actor is running.
522 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
524 process->migrate(host);
527 /** \ingroup m_actor_management
528 * \brief Wait for the completion of a #sg_actor_t.
530 * \param actor the actor to wait for
531 * \param timeout wait until the actor is over, or the timeout expires
533 void sg_actor_join(sg_actor_t actor, double timeout)
535 actor->join(timeout);
538 void sg_actor_kill(sg_actor_t actor)
543 /** \ingroup m_actor_management
544 * \brief Set the kill time of an actor.
546 * \param actor an actor
547 * \param kill_time the time when the actor is killed.
549 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
551 actor->setKillTime(kill_time);