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::set_auto_restart(bool autorestart)
66 simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
69 void Actor::on_exit(int_f_pvoid_pvoid_t fun, void* data)
71 simcall_process_on_exit(pimpl_, fun, data);
74 /** @brief Moves the actor to another host
76 * If the actor is currently blocked on an execution activity, the activity is also
77 * migrated to the new host. If it's blocked on another kind of activity, an error is
78 * raised as the mandated code is not written yet. Please report that bug if you need it.
80 * Asynchronous activities started by the actor are not migrated automatically, so you have
81 * to take care of this yourself (only you knows which ones should be migrated).
83 void Actor::migrate(Host* new_host)
86 simgrid::instr::LinkType* link = nullptr;
87 bool tracing = TRACE_actor_is_enabled();
89 static long long int counter = 0;
91 key = std::to_string(counter);
95 container_t actor_container = simgrid::instr::Container::byName(instr_pid(this));
96 link = simgrid::instr::Container::getRoot()->getLink("ACTOR_LINK");
97 link->startEvent(actor_container, "M", key);
99 // destroy existing container of this process
100 actor_container->removeFromParent();
103 simgrid::simix::kernelImmediate([this, new_host]() {
104 if (pimpl_->waiting_synchro != nullptr) {
105 // The actor is blocked on an activity. If it's an exec, migrate it too.
106 // FIXME: implement the migration of other kind of activities
107 simgrid::kernel::activity::ExecImplPtr exec =
108 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
109 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
110 exec->migrate(new_host);
112 SIMIX_process_change_host(this->pimpl_, new_host);
116 // create new container on the new_host location
117 simgrid::instr::Container::byName(new_host->get_name())->createChild(instr_pid(this), "ACTOR");
119 link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
123 s4u::Host* Actor::get_host()
125 return this->pimpl_->host;
128 void Actor::daemonize()
130 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
133 bool Actor::is_daemon() const
135 return this->pimpl_->isDaemon();
138 const simgrid::xbt::string& Actor::get_name() const
140 return this->pimpl_->get_name();
143 const char* Actor::get_cname() const
145 return this->pimpl_->get_cname();
148 aid_t Actor::get_pid() const
150 return this->pimpl_->pid;
153 aid_t Actor::get_ppid() const
155 return this->pimpl_->ppid;
158 void Actor::suspend()
160 if (TRACE_actor_is_enabled())
161 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
163 simcall_process_suspend(pimpl_);
168 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
169 if (TRACE_actor_is_enabled())
170 simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
173 int Actor::is_suspended()
175 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
178 void Actor::set_kill_time(double time)
180 simcall_process_set_kill_time(pimpl_,time);
183 /** \brief Get the kill time of an actor(or 0 if unset). */
184 double Actor::get_kill_time()
186 return SIMIX_timer_get_date(pimpl_->kill_timer);
189 void Actor::kill(aid_t pid)
191 smx_actor_t killer = SIMIX_process_self();
192 smx_actor_t process = SIMIX_process_from_PID(pid);
193 if(process != nullptr) {
194 simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
196 std::ostringstream oss;
197 oss << "kill: (" << pid << ") - No such actor" << std::endl;
198 throw std::runtime_error(oss.str());
203 smx_actor_t process = SIMIX_process_self();
204 simgrid::simix::kernelImmediate(
205 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
208 smx_actor_t Actor::get_impl()
213 // ***** Static functions *****
215 ActorPtr Actor::by_pid(aid_t pid)
217 smx_actor_t process = SIMIX_process_from_PID(pid);
218 if (process != nullptr)
219 return process->iface();
224 void Actor::kill_all()
226 simcall_process_killall();
229 std::map<std::string, std::string>* Actor::get_properties()
231 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
234 /** Retrieve the property value (or nullptr if not set) */
235 const char* Actor::get_property(const char* key)
237 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
240 void Actor::set_property(const char* key, const char* value)
242 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
245 Actor* Actor::restart()
247 return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
250 // ***** this_actor *****
252 namespace this_actor {
254 /** Returns true if run from the kernel mode, and false if run from a real actor
256 * Everything that is run out of any actor (simulation setup before the engine is run,
257 * computing the model evolutions as a result to the actors' action, etc) is run in
258 * kernel mode, just as in any operating systems.
260 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
261 * because it is the one scheduling when the others should move or wait.
265 smx_actor_t process = SIMIX_process_self();
266 return process == nullptr || process == simix_global->maestro_process;
269 void sleep_for(double duration)
272 simcall_process_sleep(duration);
277 simgrid::simix::kernelImmediate([] { /* do nothing*/ });
280 XBT_PUBLIC void sleep_until(double timeout)
282 double now = SIMIX_get_clock();
284 simcall_process_sleep(timeout - now);
287 void execute(double flops)
289 smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, get_host());
290 simcall_execution_wait(s);
293 void execute(double flops, double priority)
295 smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, get_host());
296 simcall_execution_wait(s);
299 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
302 simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
303 simcall_execution_wait(s);
306 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
308 smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
309 simcall_execution_wait(s);
312 ExecPtr exec_init(double flops_amount)
314 ExecPtr res = ExecPtr(new Exec());
315 res->host_ = get_host();
316 res->flops_amount_ = flops_amount;
317 res->set_remaining(flops_amount);
321 ExecPtr exec_async(double flops)
323 ExecPtr res = exec_init(flops);
330 return SIMIX_process_self()->pid;
335 return SIMIX_process_self()->ppid;
338 std::string get_name()
340 return SIMIX_process_self()->get_name();
343 const char* get_cname()
345 return SIMIX_process_self()->get_cname();
350 return SIMIX_process_self()->host;
355 if (TRACE_actor_is_enabled())
356 instr::Container::byName(get_name() + "-" + std::to_string(get_pid()))
357 ->getState("ACTOR_STATE")
358 ->pushEvent("suspend");
359 simcall_process_suspend(SIMIX_process_self());
364 smx_actor_t process = SIMIX_process_self();
365 simgrid::simix::kernelImmediate([process] { process->resume(); });
367 if (TRACE_actor_is_enabled())
368 instr::Container::byName(get_name() + "-" + std::to_string(get_pid()))->getState("ACTOR_STATE")->popEvent();
373 smx_actor_t process = SIMIX_process_self();
374 return simgrid::simix::kernelImmediate([process] { return process->suspended; });
379 smx_actor_t process = SIMIX_process_self();
380 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
383 void on_exit(int_f_pvoid_pvoid_t fun, void* data)
385 simcall_process_on_exit(SIMIX_process_self(), fun, data);
388 /** @brief Moves the current actor to another host
390 * @see simgrid::s4u::Actor::migrate() for more information
392 void migrate(Host* new_host)
394 SIMIX_process_self()->iface()->migrate(new_host);
397 std::string getName() /* deprecated */
401 const char* getCname() /* deprecated */
405 bool isMaestro() /* deprecated */
409 aid_t getPid() /* deprecated */
413 aid_t getPpid() /* deprecated */
417 Host* getHost() /* deprecated */
421 bool isSuspended() /* deprecated */
423 return is_suspended();
425 void onExit /* deprecated */ (int_f_pvoid_pvoid_t fun, void* data)
430 } // namespace this_actor
432 } // namespace simgrid
434 /* **************************** Public C interface *************************** */
436 /** \ingroup m_actor_management
437 * \brief Returns the process ID of \a actor.
439 * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
441 int sg_actor_get_PID(sg_actor_t actor)
443 /* Do not raise an exception here: this function is called by the logs
444 * and the exceptions, so it would be called back again and again */
445 if (actor == nullptr || actor->get_impl() == nullptr)
447 return actor->get_pid();
450 /** \ingroup m_actor_management
451 * \brief Returns the process ID of the parent of \a actor.
453 * This function checks whether \a actor is a valid pointer and return its parent's PID.
454 * Returns -1 if the actor has not been created by any other actor.
456 int sg_actor_get_PPID(sg_actor_t actor)
458 return actor->get_ppid();
461 /** \ingroup m_actor_management
462 * \brief Return the name of an actor.
464 const char* sg_actor_get_name(sg_actor_t actor)
466 return actor->get_cname();
469 sg_host_t sg_actor_get_host(sg_actor_t actor)
471 return actor->get_host();
474 /** \ingroup m_actor_management
475 * \brief Returns the value of a given actor property
477 * \param actor an actor
478 * \param name a property name
479 * \return value of a property (or nullptr if the property is not set)
481 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
483 return actor->get_property(name);
486 /** \ingroup m_actor_management
487 * \brief Return the list of properties
489 * This function returns all the parameters associated with an actor
491 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
493 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
494 xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
495 std::map<std::string, std::string>* props = actor->get_properties();
496 if (props == nullptr)
498 for (auto const& elm : *props) {
499 xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
504 /** \ingroup m_actor_management
505 * \brief Suspend the actor.
507 * This function suspends the actor by suspending the task on which it was waiting for the completion.
509 void sg_actor_suspend(sg_actor_t actor)
511 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
515 /** \ingroup m_actor_management
516 * \brief Resume a suspended actor.
518 * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
520 void sg_actor_resume(sg_actor_t actor)
522 xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
526 /** \ingroup m_actor_management
527 * \brief Returns true if the actor is suspended .
529 * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
531 int sg_actor_is_suspended(sg_actor_t actor)
533 return actor->is_suspended();
537 * \ingroup m_actor_management
538 * \brief Restarts an actor from the beginning.
540 sg_actor_t sg_actor_restart(sg_actor_t actor)
542 return actor->restart();
545 /** @ingroup m_actor_management
546 * @brief This actor will be terminated automatically when the last non-daemon actor finishes
548 void sg_actor_daemonize(sg_actor_t actor)
553 /** \ingroup m_actor_management
554 * \brief Migrates an actor to another location.
556 * This function changes the value of the #sg_host_t on which \a actor is running.
558 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
560 process->migrate(host);
563 /** \ingroup m_actor_management
564 * \brief Wait for the completion of a #sg_actor_t.
566 * \param actor the actor to wait for
567 * \param timeout wait until the actor is over, or the timeout expires
569 void sg_actor_join(sg_actor_t actor, double timeout)
571 actor->join(timeout);
574 void sg_actor_kill(sg_actor_t actor)
579 /** \ingroup m_actor_management
580 * \brief Set the kill time of an actor.
582 * \param actor an actor
583 * \param kill_time the time when the actor is killed.
585 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
587 actor->set_kill_time(kill_time);