Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / s4u / s4u_actor.cpp
1 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "xbt/log.h"
7
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"
13
14 #include "src/kernel/context/Context.hpp"
15 #include "src/simix/smx_private.hpp"
16
17 #include <sstream>
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
20
21 namespace simgrid {
22 namespace s4u {
23
24 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onCreation;
25
26 // ***** Actor creation *****
27 ActorPtr Actor::self()
28 {
29   smx_context_t self_context = SIMIX_context_self();
30   if (self_context == nullptr)
31     return simgrid::s4u::ActorPtr();
32
33   return self_context->process()->iface();
34 }
35
36 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
37 {
38   simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
39   return actor->iface();
40 }
41
42 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
43 {
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();
48 }
49
50 void intrusive_ptr_add_ref(Actor* actor)
51 {
52   intrusive_ptr_add_ref(actor->pimpl_);
53 }
54 void intrusive_ptr_release(Actor* actor)
55 {
56   intrusive_ptr_release(actor->pimpl_);
57 }
58
59 // ***** Actor methods *****
60
61 void Actor::join() {
62   simcall_process_join(this->pimpl_, -1);
63 }
64
65 void Actor::join(double timeout)
66 {
67   simcall_process_join(this->pimpl_, timeout);
68 }
69
70 void Actor::setAutoRestart(bool autorestart) {
71   simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
72 }
73
74 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
75 {
76   simcall_process_on_exit(pimpl_, fun, data);
77 }
78
79 /** @brief Moves the actor to another host
80  *
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.
84  *
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).
87  */
88 void Actor::migrate(Host* new_host)
89 {
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);
98     }
99     SIMIX_process_change_host(this->pimpl_, new_host);
100   });
101 }
102
103 s4u::Host* Actor::getHost()
104 {
105   return this->pimpl_->host;
106 }
107
108 void Actor::daemonize()
109 {
110   simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
111 }
112
113 const simgrid::xbt::string& Actor::getName() const
114 {
115   return this->pimpl_->getName();
116 }
117
118 const char* Actor::getCname() const
119 {
120   return this->pimpl_->getCname();
121 }
122
123 aid_t Actor::getPid()
124 {
125   return this->pimpl_->pid;
126 }
127
128 aid_t Actor::getPpid()
129 {
130   return this->pimpl_->ppid;
131 }
132
133 void Actor::suspend()
134 {
135   if (TRACE_actor_is_enabled())
136     simgrid::instr::Container::byName(instr_pid(this))->getState("MSG_PROCESS_STATE")->pushEvent("suspend");
137
138   simcall_process_suspend(pimpl_);
139 }
140
141 void Actor::resume()
142 {
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();
146 }
147
148 int Actor::isSuspended()
149 {
150   return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
151 }
152
153 void Actor::setKillTime(double time) {
154   simcall_process_set_kill_time(pimpl_,time);
155 }
156
157 /** \brief Get the kill time of an actor(or 0 if unset). */
158 double Actor::getKillTime()
159 {
160   return SIMIX_timer_get_date(pimpl_->kill_timer);
161 }
162
163 void Actor::kill(aid_t pid)
164 {
165   smx_actor_t process = SIMIX_process_from_PID(pid);
166   if(process != nullptr) {
167     simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
168   } else {
169     std::ostringstream oss;
170     oss << "kill: ("<< pid <<") - No such process" << std::endl;
171     throw std::runtime_error(oss.str());
172   }
173 }
174
175 smx_actor_t Actor::getImpl() {
176   return pimpl_;
177 }
178
179 void Actor::kill() {
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); });
183 }
184
185 // ***** Static functions *****
186
187 ActorPtr Actor::byPid(aid_t pid)
188 {
189   smx_actor_t process = SIMIX_process_from_PID(pid);
190   if (process != nullptr)
191     return process->iface();
192   else
193     return ActorPtr();
194 }
195
196 void Actor::killAll()
197 {
198   simcall_process_killall(1);
199 }
200
201 void Actor::killAll(int resetPid)
202 {
203   simcall_process_killall(resetPid);
204 }
205
206 std::map<std::string, std::string>* Actor::getProperties()
207 {
208   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
209 }
210
211 /** Retrieve the property value (or nullptr if not set) */
212 const char* Actor::getProperty(const char* key)
213 {
214   return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
215 }
216
217 void Actor::setProperty(const char* key, const char* value)
218 {
219   simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
220 }
221
222 Actor* Actor::restart()
223 {
224   return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
225 }
226
227 // ***** this_actor *****
228
229 namespace this_actor {
230
231 /** Returns true if run from the kernel mode, and false if run from a real actor
232  *
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.
236  *
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.
239  */
240 bool isMaestro()
241 {
242   smx_actor_t process = SIMIX_process_self();
243   return process == nullptr || process == simix_global->maestro_process;
244 }
245
246 void sleep_for(double duration)
247 {
248   if (duration > 0)
249     simcall_process_sleep(duration);
250 }
251
252 void yield()
253 {
254   simgrid::simix::kernelImmediate([] { /* do nothing*/ });
255 }
256
257 XBT_PUBLIC(void) sleep_until(double timeout)
258 {
259   double now = SIMIX_get_clock();
260   if (timeout > now)
261     simcall_process_sleep(timeout - now);
262 }
263
264 void execute(double flops)
265 {
266   smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
267   simcall_execution_wait(s);
268 }
269
270 void execute(double flops, double priority)
271 {
272   smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
273   simcall_execution_wait(s);
274 }
275
276 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
277 {
278   smx_activity_t s =
279       simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
280   simcall_execution_wait(s);
281 }
282
283 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
284 {
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);
287 }
288
289 ExecPtr exec_init(double flops_amount)
290 {
291   ExecPtr res        = ExecPtr(new Exec());
292   res->host_         = getHost();
293   res->flops_amount_ = flops_amount;
294   res->setRemains(flops_amount);
295   return res;
296 }
297
298 ExecPtr exec_async(double flops)
299 {
300   ExecPtr res = exec_init(flops);
301   res->start();
302   return res;
303 }
304
305 void* recv(MailboxPtr chan) // deprecated
306 {
307   return chan->get();
308 }
309
310 void* recv(MailboxPtr chan, double timeout) // deprecated
311 {
312   return chan->get(timeout);
313 }
314
315 void send(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
316 {
317   chan->put(payload, simulatedSize);
318 }
319
320 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout) // deprecated
321 {
322   chan->put(payload, simulatedSize, timeout);
323 }
324
325 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
326 {
327   return chan->put_async(payload, simulatedSize);
328 }
329
330 CommPtr irecv(MailboxPtr chan, void** data) // deprecated
331 {
332   return chan->get_async(data);
333 }
334
335 aid_t getPid()
336 {
337   return SIMIX_process_self()->pid;
338 }
339
340 aid_t getPpid()
341 {
342   return SIMIX_process_self()->ppid;
343 }
344
345 std::string getName()
346 {
347   return SIMIX_process_self()->getName();
348 }
349
350 const char* getCname()
351 {
352   return SIMIX_process_self()->getCname();
353 }
354
355 Host* getHost()
356 {
357   return SIMIX_process_self()->host;
358 }
359
360 void suspend()
361 {
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());
367 }
368
369 void resume()
370 {
371   smx_actor_t process = SIMIX_process_self();
372   simgrid::simix::kernelImmediate([process] { process->resume(); });
373
374   if (TRACE_actor_is_enabled())
375     instr::Container::byName(getName() + "-" + std::to_string(getPid()))->getState("MSG_PROCESS_STATE")->popEvent();
376 }
377
378 bool isSuspended()
379 {
380   smx_actor_t process = SIMIX_process_self();
381   return simgrid::simix::kernelImmediate([process] { return process->suspended; });
382 }
383
384 void kill()
385 {
386   smx_actor_t process = SIMIX_process_self();
387   simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
388 }
389
390 void onExit(int_f_pvoid_pvoid_t fun, void* data)
391 {
392   simcall_process_on_exit(SIMIX_process_self(), fun, data);
393 }
394
395 /** @brief Moves the current actor to another host
396  *
397  * @see simgrid::s4u::Actor::migrate() for more information
398  */
399 void migrate(Host* new_host)
400 {
401   SIMIX_process_self()->iface()->migrate(new_host);
402 }
403 }
404 }
405 }