Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert "Revert "actually fix the memleak around smpi's process_data" and "try to...
[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 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onCreation;
25 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::onDestruction;
26
27 // ***** Actor creation *****
28 ActorPtr Actor::self()
29 {
30   smx_context_t self_context = SIMIX_context_self();
31   if (self_context == nullptr)
32     return simgrid::s4u::ActorPtr();
33
34   return self_context->process()->iface();
35 }
36
37 ActorPtr Actor::createActor(const char* name, s4u::Host* host, std::function<void()> code)
38 {
39   simgrid::simix::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
40   return actor->iface();
41 }
42
43 ActorPtr Actor::createActor(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
44 {
45   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
46   simgrid::simix::ActorCode code = factory(std::move(args));
47   simgrid::simix::ActorImpl* actor          = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
48   return actor->iface();
49 }
50
51 void intrusive_ptr_add_ref(Actor* actor)
52 {
53   intrusive_ptr_add_ref(actor->pimpl_);
54 }
55 void intrusive_ptr_release(Actor* actor)
56 {
57   intrusive_ptr_release(actor->pimpl_);
58 }
59
60 // ***** Actor methods *****
61
62 void Actor::join() {
63   simcall_process_join(this->pimpl_, -1);
64 }
65
66 void Actor::join(double timeout)
67 {
68   simcall_process_join(this->pimpl_, timeout);
69 }
70
71 void Actor::setAutoRestart(bool autorestart) {
72   simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
73 }
74
75 void Actor::onExit(int_f_pvoid_pvoid_t fun, void* data)
76 {
77   simcall_process_on_exit(pimpl_, fun, data);
78 }
79
80 /** @brief Moves the actor to another host
81  *
82  * If the actor is currently blocked on an execution activity, the activity is also
83  * migrated to the new host. If it's blocked on another kind of activity, an error is
84  * raised as the mandated code is not written yet. Please report that bug if you need it.
85  *
86  * Asynchronous activities started by the actor are not migrated automatically, so you have
87  * to take care of this yourself (only you knows which ones should be migrated).
88  */
89 void Actor::migrate(Host* new_host)
90 {
91   simgrid::simix::kernelImmediate([this, new_host]() {
92     if (pimpl_->waiting_synchro != nullptr) {
93       // The actor is blocked on an activity. If it's an exec, migrate it too.
94       // FIXME: implement the migration of other kind of activities
95       simgrid::kernel::activity::ExecImplPtr exec =
96           boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
97       xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
98       exec->migrate(new_host);
99     }
100     SIMIX_process_change_host(this->pimpl_, new_host);
101   });
102 }
103
104 s4u::Host* Actor::getHost()
105 {
106   return this->pimpl_->host;
107 }
108
109 void Actor::daemonize()
110 {
111   simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
112 }
113
114 const simgrid::xbt::string& Actor::getName() const
115 {
116   return this->pimpl_->getName();
117 }
118
119 const char* Actor::getCname() const
120 {
121   return this->pimpl_->getCname();
122 }
123
124 aid_t Actor::getPid()
125 {
126   return this->pimpl_->pid;
127 }
128
129 aid_t Actor::getPpid()
130 {
131   return this->pimpl_->ppid;
132 }
133
134 void Actor::suspend()
135 {
136   if (TRACE_actor_is_enabled())
137     simgrid::instr::Container::byName(instr_pid(this))->getState("MSG_PROCESS_STATE")->pushEvent("suspend");
138
139   simcall_process_suspend(pimpl_);
140 }
141
142 void Actor::resume()
143 {
144   simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
145   if (TRACE_actor_is_enabled())
146     simgrid::instr::Container::byName(instr_pid(this))->getState("MSG_PROCESS_STATE")->popEvent();
147 }
148
149 int Actor::isSuspended()
150 {
151   return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
152 }
153
154 void Actor::setKillTime(double time) {
155   simcall_process_set_kill_time(pimpl_,time);
156 }
157
158 /** \brief Get the kill time of an actor(or 0 if unset). */
159 double Actor::getKillTime()
160 {
161   return SIMIX_timer_get_date(pimpl_->kill_timer);
162 }
163
164 void Actor::kill(aid_t pid)
165 {
166   smx_actor_t process = SIMIX_process_from_PID(pid);
167   if(process != nullptr) {
168     simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
169   } else {
170     std::ostringstream oss;
171     oss << "kill: ("<< pid <<") - No such process" << std::endl;
172     throw std::runtime_error(oss.str());
173   }
174 }
175
176 smx_actor_t Actor::getImpl() {
177   return pimpl_;
178 }
179
180 void Actor::kill() {
181   smx_actor_t process = SIMIX_process_self();
182   simgrid::simix::kernelImmediate(
183       [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
184 }
185
186 // ***** Static functions *****
187
188 ActorPtr Actor::byPid(aid_t pid)
189 {
190   smx_actor_t process = SIMIX_process_from_PID(pid);
191   if (process != nullptr)
192     return process->iface();
193   else
194     return ActorPtr();
195 }
196
197 void Actor::killAll()
198 {
199   simcall_process_killall(1);
200 }
201
202 void Actor::killAll(int resetPid)
203 {
204   simcall_process_killall(resetPid);
205 }
206
207 std::map<std::string, std::string>* Actor::getProperties()
208 {
209   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
210 }
211
212 /** Retrieve the property value (or nullptr if not set) */
213 const char* Actor::getProperty(const char* key)
214 {
215   return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
216 }
217
218 void Actor::setProperty(const char* key, const char* value)
219 {
220   simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
221 }
222
223 Actor* Actor::restart()
224 {
225   return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
226 }
227
228 // ***** this_actor *****
229
230 namespace this_actor {
231
232 /** Returns true if run from the kernel mode, and false if run from a real actor
233  *
234  * Everything that is run out of any actor (simulation setup before the engine is run,
235  * computing the model evolutions as a result to the actors' action, etc) is run in
236  * kernel mode, just as in any operating systems.
237  *
238  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
239  * because it is the one scheduling when the others should move or wait.
240  */
241 bool isMaestro()
242 {
243   smx_actor_t process = SIMIX_process_self();
244   return process == nullptr || process == simix_global->maestro_process;
245 }
246
247 void sleep_for(double duration)
248 {
249   if (duration > 0)
250     simcall_process_sleep(duration);
251 }
252
253 void yield()
254 {
255   simgrid::simix::kernelImmediate([] { /* do nothing*/ });
256 }
257
258 XBT_PUBLIC(void) sleep_until(double timeout)
259 {
260   double now = SIMIX_get_clock();
261   if (timeout > now)
262     simcall_process_sleep(timeout - now);
263 }
264
265 void execute(double flops)
266 {
267   smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
268   simcall_execution_wait(s);
269 }
270
271 void execute(double flops, double priority)
272 {
273   smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
274   simcall_execution_wait(s);
275 }
276
277 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
278 {
279   smx_activity_t s =
280       simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
281   simcall_execution_wait(s);
282 }
283
284 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
285 {
286   smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
287   simcall_execution_wait(s);
288 }
289
290 ExecPtr exec_init(double flops_amount)
291 {
292   ExecPtr res        = ExecPtr(new Exec());
293   res->host_         = getHost();
294   res->flops_amount_ = flops_amount;
295   res->setRemains(flops_amount);
296   return res;
297 }
298
299 ExecPtr exec_async(double flops)
300 {
301   ExecPtr res = exec_init(flops);
302   res->start();
303   return res;
304 }
305
306 void* recv(MailboxPtr chan) // deprecated
307 {
308   return chan->get();
309 }
310
311 void* recv(MailboxPtr chan, double timeout) // deprecated
312 {
313   return chan->get(timeout);
314 }
315
316 void send(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
317 {
318   chan->put(payload, simulatedSize);
319 }
320
321 void send(MailboxPtr chan, void* payload, double simulatedSize, double timeout) // deprecated
322 {
323   chan->put(payload, simulatedSize, timeout);
324 }
325
326 CommPtr isend(MailboxPtr chan, void* payload, double simulatedSize) // deprecated
327 {
328   return chan->put_async(payload, simulatedSize);
329 }
330
331 CommPtr irecv(MailboxPtr chan, void** data) // deprecated
332 {
333   return chan->get_async(data);
334 }
335
336 aid_t getPid()
337 {
338   return SIMIX_process_self()->pid;
339 }
340
341 aid_t getPpid()
342 {
343   return SIMIX_process_self()->ppid;
344 }
345
346 std::string getName()
347 {
348   return SIMIX_process_self()->getName();
349 }
350
351 const char* getCname()
352 {
353   return SIMIX_process_self()->getCname();
354 }
355
356 Host* getHost()
357 {
358   return SIMIX_process_self()->host;
359 }
360
361 void suspend()
362 {
363   if (TRACE_actor_is_enabled())
364     instr::Container::byName(getName() + "-" + std::to_string(getPid()))
365         ->getState("MSG_PROCESS_STATE")
366         ->pushEvent("suspend");
367   simcall_process_suspend(SIMIX_process_self());
368 }
369
370 void resume()
371 {
372   smx_actor_t process = SIMIX_process_self();
373   simgrid::simix::kernelImmediate([process] { process->resume(); });
374
375   if (TRACE_actor_is_enabled())
376     instr::Container::byName(getName() + "-" + std::to_string(getPid()))->getState("MSG_PROCESS_STATE")->popEvent();
377 }
378
379 bool isSuspended()
380 {
381   smx_actor_t process = SIMIX_process_self();
382   return simgrid::simix::kernelImmediate([process] { return process->suspended; });
383 }
384
385 void kill()
386 {
387   smx_actor_t process = SIMIX_process_self();
388   simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
389 }
390
391 void onExit(int_f_pvoid_pvoid_t fun, void* data)
392 {
393   simcall_process_on_exit(SIMIX_process_self(), fun, data);
394 }
395
396 /** @brief Moves the current actor to another host
397  *
398  * @see simgrid::s4u::Actor::migrate() for more information
399  */
400 void migrate(Host* new_host)
401 {
402   SIMIX_process_self()->iface()->migrate(new_host);
403 }
404 }
405 }
406 }