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 *****
57 simcall_process_join(this->pimpl_, -1);
60 void Actor::join(double timeout)
62 simcall_process_join(this->pimpl_, timeout);
65 void Actor::set_auto_restart(bool autorestart)
67 simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
70 void Actor::on_exit(int_f_pvoid_pvoid_t fun, void* data)
72 simgrid::simix::kernelImmediate([this, fun, data] { SIMIX_process_on_exit(pimpl_, fun, data); });
75 /** @brief Moves the actor to another host
77 * If the actor is currently blocked on an execution activity, the activity is also
78 * migrated to the new host. If it's blocked on another kind of activity, an error is
79 * raised as the mandated code is not written yet. Please report that bug if you need it.
81 * Asynchronous activities started by the actor are not migrated automatically, so you have
82 * to take care of this yourself (only you knows which ones should be migrated).
84 void Actor::migrate(Host* new_host)
87 simgrid::instr::LinkType* link = nullptr;
88 bool tracing = TRACE_actor_is_enabled();
90 static long long int counter = 0;
92 key = std::to_string(counter);
96 container_t actor_container = simgrid::instr::Container::byName(instr_pid(this));
97 link = simgrid::instr::Container::getRoot()->getLink("ACTOR_LINK");
98 link->startEvent(actor_container, "M", key);
100 // destroy existing container of this process
101 actor_container->removeFromParent();
104 simgrid::simix::kernelImmediate([this, new_host]() {
105 if (pimpl_->waiting_synchro != nullptr) {
106 // The actor is blocked on an activity. If it's an exec, migrate it too.
107 // FIXME: implement the migration of other kind of activities
108 simgrid::kernel::activity::ExecImplPtr exec =
109 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
110 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
111 exec->migrate(new_host);
113 SIMIX_process_change_host(this->pimpl_, new_host);
117 // create new container on the new_host location
118 simgrid::instr::Container::byName(new_host->get_name())->createChild(instr_pid(this), "ACTOR");
120 link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
124 s4u::Host* Actor::get_host()
126 return this->pimpl_->host;
129 void Actor::daemonize()
131 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
134 bool Actor::is_daemon() const
136 return this->pimpl_->isDaemon();
139 const simgrid::xbt::string& Actor::get_name() const
141 return this->pimpl_->get_name();
144 const char* Actor::get_cname() const
146 return this->pimpl_->get_cname();
149 aid_t Actor::get_pid() const
151 return this->pimpl_->pid;
154 aid_t Actor::get_ppid() const
156 return this->pimpl_->ppid;
159 void Actor::suspend()
161 if (TRACE_actor_is_enabled())
162 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
164 simcall_process_suspend(pimpl_);
169 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
170 if (TRACE_actor_is_enabled())
171 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
174 int Actor::is_suspended()
176 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
179 void Actor::set_kill_time(double time)
181 simcall_process_set_kill_time(pimpl_, time);
184 /** \brief Get the kill time of an actor(or 0 if unset). */
185 double Actor::get_kill_time()
187 return SIMIX_timer_get_date(pimpl_->kill_timer);
190 void Actor::kill(aid_t pid)
192 smx_actor_t killer = SIMIX_process_self();
193 smx_actor_t process = SIMIX_process_from_PID(pid);
194 if (process != nullptr) {
195 simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
197 std::ostringstream oss;
198 oss << "kill: (" << pid << ") - No such actor" << std::endl;
199 throw std::runtime_error(oss.str());
205 smx_actor_t process = SIMIX_process_self();
206 simgrid::simix::kernelImmediate(
207 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
210 smx_actor_t Actor::get_impl()
215 // ***** Static functions *****
217 ActorPtr Actor::by_pid(aid_t pid)
219 smx_actor_t process = SIMIX_process_from_PID(pid);
220 if (process != nullptr)
221 return process->iface();
226 void Actor::kill_all()
228 smx_actor_t self = SIMIX_process_self();
229 simgrid::simix::kernelImmediate([&self] { SIMIX_process_killall(self); });
232 std::map<std::string, std::string>* Actor::get_properties()
234 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
237 /** Retrieve the property value (or nullptr if not set) */
238 const char* Actor::get_property(const char* key)
240 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
243 void Actor::set_property(const char* key, const char* value)
245 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
248 Actor* Actor::restart()
250 return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
253 // ***** this_actor *****
255 namespace this_actor {
257 /** Returns true if run from the kernel mode, and false if run from a real actor
259 * Everything that is run out of any actor (simulation setup before the engine is run,
260 * computing the model evolutions as a result to the actors' action, etc) is run in
261 * kernel mode, just as in any operating systems.
263 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
264 * because it is the one scheduling when the others should move or wait.
268 smx_actor_t process = SIMIX_process_self();
269 return process == nullptr || process == simix_global->maestro_process;
272 void sleep_for(double duration)
275 simcall_process_sleep(duration);
280 simgrid::simix::kernelImmediate([] { /* do nothing*/ });
283 XBT_PUBLIC void sleep_until(double timeout)
285 double now = SIMIX_get_clock();
287 simcall_process_sleep(timeout - now);
290 void execute(double flops)
292 smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, get_host());
293 simcall_execution_wait(s);
296 void execute(double flops, double priority)
298 smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, get_host());
299 simcall_execution_wait(s);
302 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
305 simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
306 simcall_execution_wait(s);
309 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
311 smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
312 simcall_execution_wait(s);
315 ExecPtr exec_init(double flops_amount)
317 ExecPtr res = ExecPtr(new Exec());
318 res->host_ = get_host();
319 res->flops_amount_ = flops_amount;
320 res->set_remaining(flops_amount);
324 ExecPtr exec_async(double flops)
326 ExecPtr res = exec_init(flops);
333 return SIMIX_process_self()->pid;
338 return SIMIX_process_self()->ppid;
341 std::string get_name()
343 return SIMIX_process_self()->get_name();
346 const char* get_cname()
348 return SIMIX_process_self()->get_cname();
353 return SIMIX_process_self()->host;
358 if (TRACE_actor_is_enabled())
359 instr::Container::byName(get_name() + "-" + std::to_string(get_pid()))
360 ->getState("ACTOR_STATE")
361 ->pushEvent("suspend");
362 simcall_process_suspend(SIMIX_process_self());
367 smx_actor_t process = SIMIX_process_self();
368 simgrid::simix::kernelImmediate([process] { process->resume(); });
370 if (TRACE_actor_is_enabled())
371 instr::Container::byName(get_name() + "-" + std::to_string(get_pid()))->getState("ACTOR_STATE")->popEvent();
376 smx_actor_t process = SIMIX_process_self();
377 return simgrid::simix::kernelImmediate([process] { return process->suspended; });
382 smx_actor_t process = SIMIX_process_self();
383 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
386 void on_exit(int_f_pvoid_pvoid_t fun, void* data)
388 SIMIX_process_self()->iface()->on_exit(fun, data);
391 /** @brief Moves the current actor to another host
393 * @see simgrid::s4u::Actor::migrate() for more information
395 void migrate(Host* new_host)
397 SIMIX_process_self()->iface()->migrate(new_host);
400 std::string getName() /* deprecated */
404 const char* getCname() /* deprecated */
408 bool isMaestro() /* deprecated */
412 aid_t getPid() /* deprecated */
416 aid_t getPpid() /* deprecated */
420 Host* getHost() /* deprecated */
424 bool isSuspended() /* deprecated */
426 return is_suspended();
428 void onExit /* deprecated */ (int_f_pvoid_pvoid_t fun, void* data)
433 } // namespace this_actor
435 } // namespace simgrid
437 /* **************************** Public C interface *************************** */
439 /** \ingroup m_actor_management
440 * \brief Returns the process ID of \a actor.
442 * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
444 int sg_actor_get_PID(sg_actor_t actor)
446 /* Do not raise an exception here: this function is called by the logs
447 * and the exceptions, so it would be called back again and again */
448 if (actor == nullptr || actor->get_impl() == nullptr)
450 return actor->get_pid();
453 /** \ingroup m_actor_management
454 * \brief Returns the process ID of the parent of \a actor.
456 * This function checks whether \a actor is a valid pointer and return its parent's PID.
457 * Returns -1 if the actor has not been created by any other actor.
459 int sg_actor_get_PPID(sg_actor_t actor)
461 return actor->get_ppid();
464 /** \ingroup m_actor_management
466 * \brief Return a #sg_actor_t given its PID.
468 * 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.
469 * If none is found, \c nullptr is returned.
470 Note that the PID are unique in the whole simulation, not only on a given host.
472 sg_actor_t sg_actor_by_PID(aid_t pid)
474 return simgrid::s4u::Actor::by_pid(pid).get();
477 /** \ingroup m_actor_management
478 * \brief Return the name of an actor.
480 const char* sg_actor_get_name(sg_actor_t actor)
482 return actor->get_cname();
485 sg_host_t sg_actor_get_host(sg_actor_t actor)
487 return actor->get_host();
490 /** \ingroup m_actor_management
491 * \brief Returns the value of a given actor property
493 * \param actor an actor
494 * \param name a property name
495 * \return value of a property (or nullptr if the property is not set)
497 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
499 return actor->get_property(name);
502 /** \ingroup m_actor_management
503 * \brief Return the list of properties
505 * This function returns all the parameters associated with an actor
507 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
509 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
510 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
511 std::map<std::string, std::string>* props = actor->get_properties();
512 if (props == nullptr)
514 for (auto const& elm : *props) {
515 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
520 /** \ingroup m_actor_management
521 * \brief Suspend the actor.
523 * This function suspends the actor by suspending the task on which it was waiting for the completion.
525 void sg_actor_suspend(sg_actor_t actor)
527 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
531 /** \ingroup m_actor_management
532 * \brief Resume a suspended actor.
534 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
536 void sg_actor_resume(sg_actor_t actor)
538 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
542 /** \ingroup m_actor_management
543 * \brief Returns true if the actor is suspended .
545 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
547 int sg_actor_is_suspended(sg_actor_t actor)
549 return actor->is_suspended();
553 * \ingroup m_actor_management
554 * \brief Restarts an actor from the beginning.
556 sg_actor_t sg_actor_restart(sg_actor_t actor)
558 return actor->restart();
561 /** @ingroup m_actor_management
562 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
564 void sg_actor_daemonize(sg_actor_t actor)
569 /** \ingroup m_actor_management
570 * \brief Migrates an actor to another location.
572 * This function changes the value of the #sg_host_t on which \a actor is running.
574 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
576 process->migrate(host);
579 /** \ingroup m_actor_management
580 * \brief Wait for the completion of a #sg_actor_t.
582 * \param actor the actor to wait for
583 * \param timeout wait until the actor is over, or the timeout expires
585 void sg_actor_join(sg_actor_t actor, double timeout)
587 actor->join(timeout);
590 void sg_actor_kill(sg_actor_t actor)
595 /** \ingroup m_actor_management
596 * \brief Set the kill time of an actor.
598 * \param actor an actor
599 * \param kill_time the time when the actor is killed.
601 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
603 actor->set_kill_time(kill_time);