Logo AND Algorithmique Numérique Distribuée

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