Logo AND Algorithmique Numérique Distribuée

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