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)
91 simgrid::simix::kernelImmediate([this, new_host]() {
92 if (pimpl_->waiting_synchro != nullptr) {
93 // The actor is blocked on an activity. If it's an exec, migrate it too.
94 // FIXME: implement the migration of other kind of activities
95 simgrid::kernel::activity::ExecImplPtr exec =
96 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
97 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
98 exec->migrate(new_host);
100 SIMIX_process_change_host(this->pimpl_, new_host);
104 s4u::Host* Actor::getHost()
106 return this->pimpl_->host;
109 void Actor::daemonize()
111 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
114 bool Actor::isDaemon()
116 return this->pimpl_->isDaemon();
119 const simgrid::xbt::string& Actor::getName() const
121 return this->pimpl_->getName();
124 const char* Actor::getCname() const
126 return this->pimpl_->getCname();
129 aid_t Actor::getPid()
131 return this->pimpl_->pid;
134 aid_t Actor::getPpid()
136 return this->pimpl_->ppid;
139 void Actor::suspend()
141 if (TRACE_actor_is_enabled())
142 simgrid::instr::Container::byName(instr_pid(this))->getState("MSG_PROCESS_STATE")->pushEvent("suspend");
144 simcall_process_suspend(pimpl_);
149 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
150 if (TRACE_actor_is_enabled())
151 simgrid::instr::Container::byName(instr_pid(this))->getState("MSG_PROCESS_STATE")->popEvent();
154 int Actor::isSuspended()
156 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
159 void Actor::setKillTime(double time) {
160 simcall_process_set_kill_time(pimpl_,time);
163 /** \brief Get the kill time of an actor(or 0 if unset). */
164 double Actor::getKillTime()
166 return SIMIX_timer_get_date(pimpl_->kill_timer);
169 void Actor::kill(aid_t pid)
171 smx_actor_t killer = SIMIX_process_self();
172 smx_actor_t process = SIMIX_process_from_PID(pid);
173 if(process != nullptr) {
174 simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
176 std::ostringstream oss;
177 oss << "kill: (" << pid << ") - No such actor" << std::endl;
178 throw std::runtime_error(oss.str());
183 smx_actor_t process = SIMIX_process_self();
184 simgrid::simix::kernelImmediate(
185 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
188 smx_actor_t Actor::getImpl()
193 // ***** Static functions *****
195 ActorPtr Actor::byPid(aid_t pid)
197 smx_actor_t process = SIMIX_process_from_PID(pid);
198 if (process != nullptr)
199 return process->iface();
204 void Actor::killAll()
206 simcall_process_killall();
209 std::map<std::string, std::string>* Actor::getProperties()
211 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
214 /** Retrieve the property value (or nullptr if not set) */
215 const char* Actor::getProperty(const char* key)
217 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
220 void Actor::setProperty(const char* key, const char* value)
222 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
225 Actor* Actor::restart()
227 return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
230 // ***** this_actor *****
232 namespace this_actor {
234 /** Returns true if run from the kernel mode, and false if run from a real actor
236 * Everything that is run out of any actor (simulation setup before the engine is run,
237 * computing the model evolutions as a result to the actors' action, etc) is run in
238 * kernel mode, just as in any operating systems.
240 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
241 * because it is the one scheduling when the others should move or wait.
245 smx_actor_t process = SIMIX_process_self();
246 return process == nullptr || process == simix_global->maestro_process;
249 void sleep_for(double duration)
252 simcall_process_sleep(duration);
257 simgrid::simix::kernelImmediate([] { /* do nothing*/ });
260 XBT_PUBLIC void sleep_until(double timeout)
262 double now = SIMIX_get_clock();
264 simcall_process_sleep(timeout - now);
267 void execute(double flops)
269 smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
270 simcall_execution_wait(s);
273 void execute(double flops, double priority)
275 smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
276 simcall_execution_wait(s);
279 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
282 simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
283 simcall_execution_wait(s);
286 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
288 smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
289 simcall_execution_wait(s);
292 ExecPtr exec_init(double flops_amount)
294 ExecPtr res = ExecPtr(new Exec());
295 res->host_ = getHost();
296 res->flops_amount_ = flops_amount;
297 res->setRemains(flops_amount);
301 ExecPtr exec_async(double flops)
303 ExecPtr res = exec_init(flops);
308 void* recv(MailboxPtr chan) // deprecated
313 void* recv(MailboxPtr chan, double timeout) // deprecated
315 return chan->get(timeout);
318 void send(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
320 chan->put(payload, simulatedSize);
323 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout) // deprecated
325 chan->put(payload, simulatedSize, timeout);
328 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
330 return chan->put_async(payload, simulatedSize);
333 CommPtr irecv(MailboxPtr chan, void** data) // deprecated
335 return chan->get_async(data);
340 return SIMIX_process_self()->pid;
345 return SIMIX_process_self()->ppid;
348 std::string getName()
350 return SIMIX_process_self()->getName();
353 const char* getCname()
355 return SIMIX_process_self()->getCname();
360 return SIMIX_process_self()->host;
365 if (TRACE_actor_is_enabled())
366 instr::Container::byName(getName() + "-" + std::to_string(getPid()))
367 ->getState("MSG_PROCESS_STATE")
368 ->pushEvent("suspend");
369 simcall_process_suspend(SIMIX_process_self());
374 smx_actor_t process = SIMIX_process_self();
375 simgrid::simix::kernelImmediate([process] { process->resume(); });
377 if (TRACE_actor_is_enabled())
378 instr::Container::byName(getName() + "-" + std::to_string(getPid()))->getState("MSG_PROCESS_STATE")->popEvent();
383 smx_actor_t process = SIMIX_process_self();
384 return simgrid::simix::kernelImmediate([process] { return process->suspended; });
389 smx_actor_t process = SIMIX_process_self();
390 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
393 void onExit(int_f_pvoid_pvoid_t fun, void* data)
395 simcall_process_on_exit(SIMIX_process_self(), fun, data);
398 /** @brief Moves the current actor to another host
400 * @see simgrid::s4u::Actor::migrate() for more information
402 void migrate(Host* new_host)
404 SIMIX_process_self()->iface()->migrate(new_host);
406 } // namespace this_actor
408 } // namespace simgrid
410 /* **************************** Public C interface *************************** */
412 /** \ingroup m_actor_management
413 * \brief Returns the process ID of \a actor.
415 * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
417 int sg_actor_get_PID(sg_actor_t actor)
419 /* Do not raise an exception here: this function is called by the logs
420 * and the exceptions, so it would be called back again and again */
421 if (actor == nullptr || actor->getImpl() == nullptr)
423 return actor->getPid();
426 /** \ingroup m_actor_management
427 * \brief Returns the process ID of the parent of \a actor.
429 * This function checks whether \a actor is a valid pointer and return its parent's PID.
430 * Returns -1 if the actor has not been created by any other actor.
432 int sg_actor_get_PPID(sg_actor_t actor)
434 return actor->getPpid();
437 /** \ingroup m_actor_management
438 * \brief Return the name of an actor.
440 const char* sg_actor_get_name(sg_actor_t actor)
442 return actor->getCname();
445 sg_host_t sg_actor_get_host(sg_actor_t actor)
447 return actor->getHost();
450 /** \ingroup m_actor_management
451 * \brief Returns the value of a given actor property
453 * \param actor an actor
454 * \param name a property name
455 * \return value of a property (or nullptr if the property is not set)
457 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
459 return actor->getProperty(name);
462 /** \ingroup m_actor_management
463 * \brief Return the list of properties
465 * This function returns all the parameters associated with an actor
467 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
469 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
470 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
471 std::map<std::string, std::string>* props = actor->getProperties();
472 if (props == nullptr)
474 for (auto const& elm : *props) {
475 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
480 /** \ingroup m_actor_management
481 * \brief Suspend the actor.
483 * This function suspends the actor by suspending the task on which it was waiting for the completion.
485 void sg_actor_suspend(sg_actor_t actor)
487 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
491 /** \ingroup m_actor_management
492 * \brief Resume a suspended actor.
494 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
496 void sg_actor_resume(sg_actor_t actor)
498 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
502 /** \ingroup m_actor_management
503 * \brief Returns true if the actor is suspended .
505 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
507 int sg_actor_is_suspended(sg_actor_t actor)
509 return actor->isSuspended();
513 * \ingroup m_actor_management
514 * \brief Restarts an actor from the beginning.
516 sg_actor_t sg_actor_restart(sg_actor_t actor)
518 return actor->restart();
521 /** @ingroup m_actor_management
522 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
524 void sg_actor_daemonize(sg_actor_t actor)