Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Factorize s_type and value instr class
[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.h"
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::setAutoRestart(bool autorestart) {
65   simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
66 }
67
68 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
69 {
70   simcall_process_on_exit(pimpl_, fun, data);
71 }
72
73 void Actor::migrate(Host* new_host)
74 {
75   simgrid::simix::kernelImmediate([this, new_host]() { pimpl_->new_host = new_host; });
76 }
77
78 s4u::Host* Actor::getHost()
79 {
80   return this->pimpl_->host;
81 }
82
83 void Actor::daemonize()
84 {
85   simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
86 }
87
88 const char* Actor::getCname()
89 {
90   return this->pimpl_->name.c_str();
91 }
92
93 simgrid::xbt::string Actor::getName()
94 {
95   return this->pimpl_->name;
96 }
97
98 aid_t Actor::getPid()
99 {
100   return this->pimpl_->pid;
101 }
102
103 aid_t Actor::getPpid()
104 {
105   return this->pimpl_->ppid;
106 }
107
108 void Actor::suspend()
109 {
110   simcall_process_suspend(pimpl_);
111 }
112
113 void Actor::resume()
114 {
115   simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
116 }
117
118 int Actor::isSuspended()
119 {
120   return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
121 }
122
123 void Actor::setKillTime(double time) {
124   simcall_process_set_kill_time(pimpl_,time);
125 }
126
127 /** \brief Get the kill time of an actor(or 0 if unset). */
128 double Actor::getKillTime()
129 {
130   return SIMIX_timer_get_date(pimpl_->kill_timer);
131 }
132
133 void Actor::kill(aid_t pid)
134 {
135   smx_actor_t process = SIMIX_process_from_PID(pid);
136   if(process != nullptr) {
137     simcall_process_kill(process);
138   } else {
139     std::ostringstream oss;
140     oss << "kill: ("<< pid <<") - No such process" << std::endl;
141     throw std::runtime_error(oss.str());
142   }
143 }
144
145 smx_actor_t Actor::getImpl() {
146   return pimpl_;
147 }
148
149 void Actor::kill() {
150   simcall_process_kill(pimpl_);
151 }
152
153 // ***** Static functions *****
154
155 ActorPtr Actor::byPid(aid_t pid)
156 {
157   smx_actor_t process = SIMIX_process_from_PID(pid);
158   if (process != nullptr)
159     return process->iface();
160   else
161     return ActorPtr();
162 }
163
164 void Actor::killAll()
165 {
166   simcall_process_killall(1);
167 }
168
169 void Actor::killAll(int resetPid)
170 {
171   simcall_process_killall(resetPid);
172 }
173
174 /** Retrieve the property value (or nullptr if not set) */
175 const char* Actor::getProperty(const char* key)
176 {
177   return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
178 }
179
180 void Actor::setProperty(const char* key, const char* value)
181 {
182   simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
183 }
184
185 Actor* Actor::restart()
186 {
187   return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
188 }
189
190 // ***** this_actor *****
191
192 namespace this_actor {
193
194 /** Returns true if run from the kernel mode, and false if run from a real actor
195  *
196  * Everything that is run out of any actor (simulation setup before the engine is run,
197  * computing the model evolutions as a result to the actors' action, etc) is run in
198  * kernel mode, just as in any operating systems.
199  *
200  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
201  * because it is the one scheduling when the others should move or wait.
202  */
203 bool isMaestro()
204 {
205   smx_actor_t process = SIMIX_process_self();
206   return process == nullptr || process == simix_global->maestro_process;
207 }
208
209 void sleep_for(double duration)
210 {
211   if (duration > 0)
212     simcall_process_sleep(duration);
213 }
214
215 XBT_PUBLIC(void) sleep_until(double timeout)
216 {
217   double now = SIMIX_get_clock();
218   if (timeout > now)
219     simcall_process_sleep(timeout - now);
220 }
221
222 void execute(double flops)
223 {
224   smx_activity_t s = simcall_execution_start(nullptr,flops,1.0/*priority*/,0./*bound*/);
225   simcall_execution_wait(s);
226 }
227
228 void* recv(MailboxPtr chan) // deprecated
229 {
230   return chan->get();
231 }
232
233 void* recv(MailboxPtr chan, double timeout) // deprecated
234 {
235   return chan->get(timeout);
236 }
237
238 void send(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
239 {
240   chan->put(payload, simulatedSize);
241 }
242
243 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout) // deprecated
244 {
245   chan->put(payload, simulatedSize, timeout);
246 }
247
248 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
249 {
250   return chan->put_async(payload, simulatedSize);
251 }
252
253 CommPtr irecv(MailboxPtr chan, void** data) // deprecated
254 {
255   return chan->get_async(data);
256 }
257
258 aid_t getPid()
259 {
260   return SIMIX_process_self()->pid;
261 }
262
263 aid_t getPpid()
264 {
265   return SIMIX_process_self()->ppid;
266 }
267
268 std::string getName()
269 {
270   return SIMIX_process_self()->name;
271 }
272
273 Host* getHost()
274 {
275   return SIMIX_process_self()->host;
276 }
277
278 void suspend()
279 {
280   simcall_process_suspend(SIMIX_process_self());
281 }
282
283 void resume()
284 {
285   smx_actor_t process = SIMIX_process_self();
286   simgrid::simix::kernelImmediate([process] { process->resume(); });
287 }
288
289 bool isSuspended()
290 {
291   smx_actor_t process = SIMIX_process_self();
292   return simgrid::simix::kernelImmediate([process] { return process->suspended; });
293 }
294
295 void kill()
296 {
297   simcall_process_kill(SIMIX_process_self());
298 }
299
300 void onExit(int_f_pvoid_pvoid_t fun, void* data)
301 {
302   simcall_process_on_exit(SIMIX_process_self(), fun, data);
303 }
304
305 void migrate(Host* new_host)
306 {
307   smx_actor_t process = SIMIX_process_self();
308   simgrid::simix::kernelImmediate([process, new_host] { process->new_host = new_host; });
309 }
310 }
311 }
312 }