1 /* Copyright (c) 2006-2017. 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;
26 // ***** Actor creation *****
27 ActorPtr Actor::self()
29 smx_context_t self_context = SIMIX_context_self();
30 if (self_context == nullptr)
31 return simgrid::s4u::ActorPtr();
33 return self_context->process()->iface();
36 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
38 simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
39 return actor->iface();
42 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
44 simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
45 simgrid::simix::ActorCode code = factory(std::move(args));
46 simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
47 return actor->iface();
50 void intrusive_ptr_add_ref(Actor* actor)
52 intrusive_ptr_add_ref(actor->pimpl_);
54 void intrusive_ptr_release(Actor* actor)
56 intrusive_ptr_release(actor->pimpl_);
59 // ***** Actor methods *****
62 simcall_process_join(this->pimpl_, -1);
65 void Actor::join(double timeout)
67 simcall_process_join(this->pimpl_, timeout);
70 void Actor::setAutoRestart(bool autorestart) {
71 simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
74 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
76 simcall_process_on_exit(pimpl_, fun, data);
79 /** @brief Moves the actor to another host
81 * If the actor is currently blocked on an execution activity, the activity is also
82 * migrated to the new host. If it's blocked on another kind of activity, an error is
83 * raised as the mandated code is not written yet. Please report that bug if you need it.
85 * Asynchronous activities started by the actor are not migrated automatically, so you have
86 * to take care of this yourself (only you knows which ones should be migrated).
88 void Actor::migrate(Host* new_host)
90 simgrid::simix::kernelImmediate([this, new_host]() {
91 if (pimpl_->waiting_synchro != nullptr) {
92 // The actor is blocked on an activity. If it's an exec, migrate it too.
93 // FIXME: implement the migration of other kind of activities
94 simgrid::kernel::activity::ExecImplPtr exec =
95 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
96 xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
97 exec->migrate(new_host);
99 SIMIX_process_change_host(this->pimpl_, new_host);
103 s4u::Host* Actor::getHost()
105 return this->pimpl_->host;
108 void Actor::daemonize()
110 simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
113 const simgrid::xbt::string& Actor::getName() const
115 return this->pimpl_->getName();
118 const char* Actor::getCname() const
120 return this->pimpl_->getCname();
123 aid_t Actor::getPid()
125 return this->pimpl_->pid;
128 aid_t Actor::getPpid()
130 return this->pimpl_->ppid;
133 void Actor::suspend()
135 if (TRACE_actor_is_enabled())
136 simgrid::instr::Container::byName(instr_pid(this))->getState("MSG_PROCESS_STATE")->pushEvent("suspend");
138 simcall_process_suspend(pimpl_);
143 simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
144 if (TRACE_actor_is_enabled())
145 simgrid::instr::Container::byName(instr_pid(this))->getState("MSG_PROCESS_STATE")->popEvent();
148 int Actor::isSuspended()
150 return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
153 void Actor::setKillTime(double time) {
154 simcall_process_set_kill_time(pimpl_,time);
157 /** \brief Get the kill time of an actor(or 0 if unset). */
158 double Actor::getKillTime()
160 return SIMIX_timer_get_date(pimpl_->kill_timer);
163 void Actor::kill(aid_t pid)
165 smx_actor_t process = SIMIX_process_from_PID(pid);
166 if(process != nullptr) {
167 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
169 std::ostringstream oss;
170 oss << "kill: ("<< pid <<") - No such process" << std::endl;
171 throw std::runtime_error(oss.str());
175 smx_actor_t Actor::getImpl() {
180 smx_actor_t process = SIMIX_process_self();
181 simgrid::simix::kernelImmediate(
182 [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
185 // ***** Static functions *****
187 ActorPtr Actor::byPid(aid_t pid)
189 smx_actor_t process = SIMIX_process_from_PID(pid);
190 if (process != nullptr)
191 return process->iface();
196 void Actor::killAll()
198 simcall_process_killall(1);
201 void Actor::killAll(int resetPid)
203 simcall_process_killall(resetPid);
206 std::map<std::string, std::string>* Actor::getProperties()
208 return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
211 /** Retrieve the property value (or nullptr if not set) */
212 const char* Actor::getProperty(const char* key)
214 return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
217 void Actor::setProperty(const char* key, const char* value)
219 simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
222 Actor* Actor::restart()
224 return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
227 // ***** this_actor *****
229 namespace this_actor {
231 /** Returns true if run from the kernel mode, and false if run from a real actor
233 * Everything that is run out of any actor (simulation setup before the engine is run,
234 * computing the model evolutions as a result to the actors' action, etc) is run in
235 * kernel mode, just as in any operating systems.
237 * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
238 * because it is the one scheduling when the others should move or wait.
242 smx_actor_t process = SIMIX_process_self();
243 return process == nullptr || process == simix_global->maestro_process;
246 void sleep_for(double duration)
249 simcall_process_sleep(duration);
254 simgrid::simix::kernelImmediate([] { /* do nothing*/ });
257 XBT_PUBLIC(void) sleep_until(double timeout)
259 double now = SIMIX_get_clock();
261 simcall_process_sleep(timeout - now);
264 void execute(double flops)
266 smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
267 simcall_execution_wait(s);
270 void execute(double flops, double priority)
272 smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
273 simcall_execution_wait(s);
276 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
279 simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
280 simcall_execution_wait(s);
283 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
285 smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
286 simcall_execution_wait(s);
289 ExecPtr exec_init(double flops_amount)
291 ExecPtr res = ExecPtr(new Exec());
292 res->host_ = getHost();
293 res->flops_amount_ = flops_amount;
294 res->setRemains(flops_amount);
298 ExecPtr exec_async(double flops)
300 ExecPtr res = exec_init(flops);
305 void* recv(MailboxPtr chan) // deprecated
310 void* recv(MailboxPtr chan, double timeout) // deprecated
312 return chan->get(timeout);
315 void send(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
317 chan->put(payload, simulatedSize);
320 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout) // deprecated
322 chan->put(payload, simulatedSize, timeout);
325 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
327 return chan->put_async(payload, simulatedSize);
330 CommPtr irecv(MailboxPtr chan, void** data) // deprecated
332 return chan->get_async(data);
337 return SIMIX_process_self()->pid;
342 return SIMIX_process_self()->ppid;
345 std::string getName()
347 return SIMIX_process_self()->getName();
350 const char* getCname()
352 return SIMIX_process_self()->getCname();
357 return SIMIX_process_self()->host;
362 if (TRACE_actor_is_enabled())
363 instr::Container::byName(getName() + "-" + std::to_string(getPid()))
364 ->getState("MSG_PROCESS_STATE")
365 ->pushEvent("suspend");
366 simcall_process_suspend(SIMIX_process_self());
371 smx_actor_t process = SIMIX_process_self();
372 simgrid::simix::kernelImmediate([process] { process->resume(); });
374 if (TRACE_actor_is_enabled())
375 instr::Container::byName(getName() + "-" + std::to_string(getPid()))->getState("MSG_PROCESS_STATE")->popEvent();
380 smx_actor_t process = SIMIX_process_self();
381 return simgrid::simix::kernelImmediate([process] { return process->suspended; });
386 smx_actor_t process = SIMIX_process_self();
387 simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
390 void onExit(int_f_pvoid_pvoid_t fun, void* data)
392 simcall_process_on_exit(SIMIX_process_self(), fun, data);
395 /** @brief Moves the current actor to another host
397 * @see simgrid::s4u::Actor::migrate() for more information
399 void migrate(Host* new_host)
401 SIMIX_process_self()->iface()->migrate(new_host);