Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert process-join to actor-join
[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/Host.hpp"
11 #include "simgrid/s4u/Mailbox.hpp"
12
13 #include "src/kernel/context/Context.hpp"
14 #include "src/simix/smx_private.hpp"
15
16 #include <sstream>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
19
20 namespace simgrid {
21 namespace s4u {
22
23 // ***** Actor creation *****
24 ActorPtr Actor::self()
25 {
26   smx_context_t self_context = SIMIX_context_self();
27   if (self_context == nullptr)
28     return simgrid::s4u::ActorPtr();
29
30   return self_context->process()->iface();
31 }
32
33 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
34 {
35   simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
36   return actor->iface();
37 }
38
39 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
40 {
41   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
42   simgrid::simix::ActorCode code = factory(std::move(args));
43   simgrid::simix::ActorImpl* actor          = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
44   return actor->iface();
45 }
46
47 void intrusive_ptr_add_ref(Actor* actor)
48 {
49   xbt_assert(actor != nullptr);
50   intrusive_ptr_add_ref(actor->pimpl_);
51 }
52 void intrusive_ptr_release(Actor* actor)
53 {
54   xbt_assert(actor != nullptr);
55   intrusive_ptr_release(actor->pimpl_);
56 }
57
58 // ***** Actor methods *****
59
60 void Actor::join() {
61   simcall_process_join(this->pimpl_, -1);
62 }
63
64 void Actor::join(double timeout)
65 {
66   simcall_process_join(this->pimpl_, timeout);
67 }
68
69 void Actor::setAutoRestart(bool autorestart) {
70   simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
71 }
72
73 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
74 {
75   simcall_process_on_exit(pimpl_, fun, data);
76 }
77
78 void Actor::migrate(Host* new_host)
79 {
80   simgrid::simix::kernelImmediate([this, new_host]() { pimpl_->new_host = new_host; });
81 }
82
83 s4u::Host* Actor::getHost()
84 {
85   return this->pimpl_->host;
86 }
87
88 void Actor::daemonize()
89 {
90   simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
91 }
92
93 const simgrid::xbt::string& Actor::getName() const
94 {
95   return this->pimpl_->getName();
96 }
97
98 const char* Actor::getCname() const
99 {
100   return this->pimpl_->getCname();
101 }
102
103 aid_t Actor::getPid()
104 {
105   return this->pimpl_->pid;
106 }
107
108 aid_t Actor::getPpid()
109 {
110   return this->pimpl_->ppid;
111 }
112
113 void Actor::suspend()
114 {
115   simcall_process_suspend(pimpl_);
116 }
117
118 void Actor::resume()
119 {
120   simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
121 }
122
123 int Actor::isSuspended()
124 {
125   return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
126 }
127
128 void Actor::setKillTime(double time) {
129   simcall_process_set_kill_time(pimpl_,time);
130 }
131
132 /** \brief Get the kill time of an actor(or 0 if unset). */
133 double Actor::getKillTime()
134 {
135   return SIMIX_timer_get_date(pimpl_->kill_timer);
136 }
137
138 void Actor::kill(aid_t pid)
139 {
140   smx_actor_t process = SIMIX_process_from_PID(pid);
141   if(process != nullptr) {
142     simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
143   } else {
144     std::ostringstream oss;
145     oss << "kill: ("<< pid <<") - No such process" << std::endl;
146     throw std::runtime_error(oss.str());
147   }
148 }
149
150 smx_actor_t Actor::getImpl() {
151   return pimpl_;
152 }
153
154 void Actor::kill() {
155   smx_actor_t process = SIMIX_process_self();
156   simgrid::simix::kernelImmediate(
157       [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
158 }
159
160 // ***** Static functions *****
161
162 ActorPtr Actor::byPid(aid_t pid)
163 {
164   smx_actor_t process = SIMIX_process_from_PID(pid);
165   if (process != nullptr)
166     return process->iface();
167   else
168     return ActorPtr();
169 }
170
171 void Actor::killAll()
172 {
173   simcall_process_killall(1);
174 }
175
176 void Actor::killAll(int resetPid)
177 {
178   simcall_process_killall(resetPid);
179 }
180
181 std::map<std::string, std::string>* Actor::getProperties()
182 {
183   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
184 }
185
186 /** Retrieve the property value (or nullptr if not set) */
187 const char* Actor::getProperty(const char* key)
188 {
189   return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
190 }
191
192 void Actor::setProperty(const char* key, const char* value)
193 {
194   simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
195 }
196
197 Actor* Actor::restart()
198 {
199   return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
200 }
201
202 // ***** this_actor *****
203
204 namespace this_actor {
205
206 /** Returns true if run from the kernel mode, and false if run from a real actor
207  *
208  * Everything that is run out of any actor (simulation setup before the engine is run,
209  * computing the model evolutions as a result to the actors' action, etc) is run in
210  * kernel mode, just as in any operating systems.
211  *
212  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
213  * because it is the one scheduling when the others should move or wait.
214  */
215 bool isMaestro()
216 {
217   smx_actor_t process = SIMIX_process_self();
218   return process == nullptr || process == simix_global->maestro_process;
219 }
220
221 void sleep_for(double duration)
222 {
223   if (duration > 0)
224     simcall_process_sleep(duration);
225 }
226
227 void yield()
228 {
229   simgrid::simix::kernelImmediate([] { /* do nothing*/ });
230 }
231
232 XBT_PUBLIC(void) sleep_until(double timeout)
233 {
234   double now = SIMIX_get_clock();
235   if (timeout > now)
236     simcall_process_sleep(timeout - now);
237 }
238
239 void execute(double flops)
240 {
241   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
242   simcall_execution_wait(s);
243 }
244
245 void execute(double flops, double priority)
246 {
247   smx_activity_t s = simcall_execution_start(nullptr,flops,1 / priority/*priority*/,0./*bound*/);
248   simcall_execution_wait(s);
249 }
250
251 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
252 {
253   smx_activity_t s =
254       simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
255   simcall_execution_wait(s);
256 }
257
258 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
259 {
260   smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
261   simcall_execution_wait(s);
262 }
263
264 void* recv(MailboxPtr chan) // deprecated
265 {
266   return chan->get();
267 }
268
269 void* recv(MailboxPtr chan, double timeout) // deprecated
270 {
271   return chan->get(timeout);
272 }
273
274 void send(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
275 {
276   chan->put(payload, simulatedSize);
277 }
278
279 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout) // deprecated
280 {
281   chan->put(payload, simulatedSize, timeout);
282 }
283
284 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
285 {
286   return chan->put_async(payload, simulatedSize);
287 }
288
289 CommPtr irecv(MailboxPtr chan, void** data) // deprecated
290 {
291   return chan->get_async(data);
292 }
293
294 aid_t getPid()
295 {
296   return SIMIX_process_self()->pid;
297 }
298
299 aid_t getPpid()
300 {
301   return SIMIX_process_self()->ppid;
302 }
303
304 std::string getName()
305 {
306   return SIMIX_process_self()->getName();
307 }
308
309 const char* getCname()
310 {
311   return SIMIX_process_self()->getCname();
312 }
313
314 Host* getHost()
315 {
316   return SIMIX_process_self()->host;
317 }
318
319 void suspend()
320 {
321   simcall_process_suspend(SIMIX_process_self());
322 }
323
324 void resume()
325 {
326   smx_actor_t process = SIMIX_process_self();
327   simgrid::simix::kernelImmediate([process] { process->resume(); });
328 }
329
330 bool isSuspended()
331 {
332   smx_actor_t process = SIMIX_process_self();
333   return simgrid::simix::kernelImmediate([process] { return process->suspended; });
334 }
335
336 void kill()
337 {
338   smx_actor_t process = SIMIX_process_self();
339   simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
340 }
341
342 void onExit(int_f_pvoid_pvoid_t fun, void* data)
343 {
344   simcall_process_on_exit(SIMIX_process_self(), fun, data);
345 }
346
347 void migrate(Host* new_host)
348 {
349   smx_actor_t process = SIMIX_process_self();
350   simgrid::simix::kernelImmediate([process, new_host] { process->new_host = new_host; });
351 }
352 }
353 }
354 }