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::simix::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::simix::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 new simgrid::instr::Container(instr_pid(this), "ACTOR", simgrid::instr::Container::byName(new_host->getName()));
125 link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
129 s4u::Host* Actor::getHost()
131 return this->pimpl_->host;
134 void Actor::daemonize()
136 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
139 bool Actor::isDaemon()
141 return this->pimpl_->isDaemon();
144 const simgrid::xbt::string& Actor::getName() const
146 return this->pimpl_->getName();
149 const char* Actor::getCname() const
151 return this->pimpl_->getCname();
154 aid_t Actor::getPid()
156 return this->pimpl_->pid;
159 aid_t Actor::getPpid()
161 return this->pimpl_->ppid;
164 void Actor::suspend()
166 if (TRACE_actor_is_enabled())
167 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
169 simcall_process_suspend(pimpl_);
174 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
175 if (TRACE_actor_is_enabled())
176 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
179 int Actor::isSuspended()
181 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
184 void Actor::setKillTime(double time) {
185 simcall_process_set_kill_time(pimpl_,time);
188 /** \brief Get the kill time of an actor(or 0 if unset). */
189 double Actor::getKillTime()
191 return SIMIX_timer_get_date(pimpl_->kill_timer);
194 void Actor::kill(aid_t pid)
196 smx_actor_t killer = SIMIX_process_self();
197 smx_actor_t process = SIMIX_process_from_PID(pid);
198 if(process != nullptr) {
199 simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
201 std::ostringstream oss;
202 oss << "kill: (" << pid << ") - No such actor" << std::endl;
203 throw std::runtime_error(oss.str());
208 smx_actor_t process = SIMIX_process_self();
209 simgrid::simix::kernelImmediate(
210 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
213 smx_actor_t Actor::getImpl()
218 // ***** Static functions *****
220 ActorPtr Actor::byPid(aid_t pid)
222 smx_actor_t process = SIMIX_process_from_PID(pid);
223 if (process != nullptr)
224 return process->iface();
229 void Actor::killAll()
231 simcall_process_killall();
234 std::map<std::string, std::string>* Actor::getProperties()
236 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
239 /** Retrieve the property value (or nullptr if not set) */
240 const char* Actor::getProperty(const char* key)
242 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
245 void Actor::setProperty(const char* key, const char* value)
247 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
250 Actor* Actor::restart()
252 return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
255 // ***** this_actor *****
257 namespace this_actor {
259 /** Returns true if run from the kernel mode, and false if run from a real actor
261 * Everything that is run out of any actor (simulation setup before the engine is run,
262 * computing the model evolutions as a result to the actors' action, etc) is run in
263 * kernel mode, just as in any operating systems.
265 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
266 * because it is the one scheduling when the others should move or wait.
270 smx_actor_t process = SIMIX_process_self();
271 return process == nullptr || process == simix_global->maestro_process;
274 void sleep_for(double duration)
277 simcall_process_sleep(duration);
282 simgrid::simix::kernelImmediate([] { /* do nothing*/ });
285 XBT_PUBLIC void sleep_until(double timeout)
287 double now = SIMIX_get_clock();
289 simcall_process_sleep(timeout - now);
292 void execute(double flops)
294 smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
295 simcall_execution_wait(s);
298 void execute(double flops, double priority)
300 smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
301 simcall_execution_wait(s);
304 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
307 simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
308 simcall_execution_wait(s);
311 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
313 smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
314 simcall_execution_wait(s);
317 ExecPtr exec_init(double flops_amount)
319 ExecPtr res = ExecPtr(new Exec());
320 res->host_ = getHost();
321 res->flops_amount_ = flops_amount;
322 res->setRemains(flops_amount);
326 ExecPtr exec_async(double flops)
328 ExecPtr res = exec_init(flops);
335 return SIMIX_process_self()->pid;
340 return SIMIX_process_self()->ppid;
343 std::string getName()
345 return SIMIX_process_self()->getName();
348 const char* getCname()
350 return SIMIX_process_self()->getCname();
355 return SIMIX_process_self()->host;
360 if (TRACE_actor_is_enabled())
361 instr::Container::byName(getName() + "-" + std::to_string(getPid()))->getState("ACTOR_STATE")->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(getName() + "-" + std::to_string(getPid()))->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 onExit(int_f_pvoid_pvoid_t fun, void* data)
388 simcall_process_on_exit(SIMIX_process_self(), 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);
399 } // namespace this_actor
401 } // namespace simgrid
403 /* **************************** Public C interface *************************** */
405 /** \ingroup m_actor_management
406 * \brief Returns the process ID of \a actor.
408 * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
410 int sg_actor_get_PID(sg_actor_t actor)
412 /* Do not raise an exception here: this function is called by the logs
413 * and the exceptions, so it would be called back again and again */
414 if (actor == nullptr || actor->getImpl() == nullptr)
416 return actor->getPid();
419 /** \ingroup m_actor_management
420 * \brief Returns the process ID of the parent of \a actor.
422 * This function checks whether \a actor is a valid pointer and return its parent's PID.
423 * Returns -1 if the actor has not been created by any other actor.
425 int sg_actor_get_PPID(sg_actor_t actor)
427 return actor->getPpid();
430 /** \ingroup m_actor_management
431 * \brief Return the name of an actor.
433 const char* sg_actor_get_name(sg_actor_t actor)
435 return actor->getCname();
438 sg_host_t sg_actor_get_host(sg_actor_t actor)
440 return actor->getHost();
443 /** \ingroup m_actor_management
444 * \brief Returns the value of a given actor property
446 * \param actor an actor
447 * \param name a property name
448 * \return value of a property (or nullptr if the property is not set)
450 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
452 return actor->getProperty(name);
455 /** \ingroup m_actor_management
456 * \brief Return the list of properties
458 * This function returns all the parameters associated with an actor
460 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
462 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
463 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
464 std::map<std::string, std::string>* props = actor->getProperties();
465 if (props == nullptr)
467 for (auto const& elm : *props) {
468 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
473 /** \ingroup m_actor_management
474 * \brief Suspend the actor.
476 * This function suspends the actor by suspending the task on which it was waiting for the completion.
478 void sg_actor_suspend(sg_actor_t actor)
480 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
484 /** \ingroup m_actor_management
485 * \brief Resume a suspended actor.
487 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
489 void sg_actor_resume(sg_actor_t actor)
491 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
495 /** \ingroup m_actor_management
496 * \brief Returns true if the actor is suspended .
498 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
500 int sg_actor_is_suspended(sg_actor_t actor)
502 return actor->isSuspended();
506 * \ingroup m_actor_management
507 * \brief Restarts an actor from the beginning.
509 sg_actor_t sg_actor_restart(sg_actor_t actor)
511 return actor->restart();
514 /** @ingroup m_actor_management
515 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
517 void sg_actor_daemonize(sg_actor_t actor)
522 /** \ingroup m_process_management
523 * \brief Migrates an actor to another location.
525 * This function changes the value of the #sg_host_t on which \a actor is running.
527 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
529 process->migrate(host);