Logo AND Algorithmique Numérique Distribuée

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