Logo AND Algorithmique Numérique Distribuée

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