Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f0d1a57bc090265553ab8c804f716a7a6b076b54
[simgrid.git] / src / s4u / s4u_actor.cpp
1 /* Copyright (c) 2006-2018. 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 "simgrid/s4u/Actor.hpp"
7 #include "simgrid/s4u/Exec.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/instr/instr_private.hpp"
10 #include "src/simix/smx_private.hpp"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor, "S4U actors");
13
14 namespace simgrid {
15 namespace s4u {
16
17 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_creation;
18 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Actor::on_destruction;
19
20 // ***** Actor creation *****
21 ActorPtr Actor::self()
22 {
23   smx_context_t self_context = SIMIX_context_self();
24   if (self_context == nullptr)
25     return simgrid::s4u::ActorPtr();
26
27   return self_context->process()->iface();
28 }
29
30 ActorPtr Actor::create(const char* name, s4u::Host* host, std::function<void()> code)
31 {
32   simgrid::kernel::actor::ActorImpl* actor = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
33   return actor->iface();
34 }
35
36 ActorPtr Actor::create(const char* name, s4u::Host* host, const char* function, std::vector<std::string> args)
37 {
38   simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function);
39   simgrid::simix::ActorCode code = factory(std::move(args));
40   simgrid::kernel::actor::ActorImpl* actor  = simcall_process_create(name, std::move(code), nullptr, host, nullptr);
41   return actor->iface();
42 }
43
44 void intrusive_ptr_add_ref(Actor* actor)
45 {
46   intrusive_ptr_add_ref(actor->pimpl_);
47 }
48 void intrusive_ptr_release(Actor* actor)
49 {
50   intrusive_ptr_release(actor->pimpl_);
51 }
52
53 // ***** Actor methods *****
54
55 void Actor::join() {
56   simcall_process_join(this->pimpl_, -1);
57 }
58
59 void Actor::join(double timeout)
60 {
61   simcall_process_join(this->pimpl_, timeout);
62 }
63
64 void Actor::set_auto_restart(bool autorestart)
65 {
66   simgrid::simix::kernelImmediate([this, autorestart]() { pimpl_->auto_restart = autorestart; });
67 }
68
69 void Actor::on_exit(int_f_pvoid_pvoid_t fun, void* data)
70 {
71   simcall_process_on_exit(pimpl_, fun, data);
72 }
73
74 /** @brief Moves the actor to another host
75  *
76  * If the actor is currently blocked on an execution activity, the activity is also
77  * migrated to the new host. If it's blocked on another kind of activity, an error is
78  * raised as the mandated code is not written yet. Please report that bug if you need it.
79  *
80  * Asynchronous activities started by the actor are not migrated automatically, so you have
81  * to take care of this yourself (only you knows which ones should be migrated).
82  */
83 void Actor::migrate(Host* new_host)
84 {
85   std::string key;
86   simgrid::instr::LinkType* link = nullptr;
87   if (TRACE_actor_is_enabled()) {
88     static long long int counter = 0;
89
90     key = std::to_string(counter);
91     counter++;
92
93     // start link
94     container_t actor_container = simgrid::instr::Container::byName(instr_pid(this));
95     link                        = simgrid::instr::Container::getRoot()->getLink("ACTOR_LINK");
96     link->startEvent(actor_container, "M", key);
97
98     // destroy existing container of this process
99     actor_container->removeFromParent();
100   }
101
102   simgrid::simix::kernelImmediate([this, new_host]() {
103     if (pimpl_->waiting_synchro != nullptr) {
104       // The actor is blocked on an activity. If it's an exec, migrate it too.
105       // FIXME: implement the migration of other kind of activities
106       simgrid::kernel::activity::ExecImplPtr exec =
107           boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_->waiting_synchro);
108       xbt_assert(exec.get() != nullptr, "We can only migrate blocked actors when they are blocked on executions.");
109       exec->migrate(new_host);
110     }
111     SIMIX_process_change_host(this->pimpl_, new_host);
112   });
113
114   if (TRACE_actor_is_enabled()) {
115     // create new container on the new_host location
116     simgrid::instr::Container::byName(new_host->get_name())->createChild(instr_pid(this), "ACTOR");
117     // end link
118     link->endEvent(simgrid::instr::Container::byName(instr_pid(this)), "M", key);
119   }
120 }
121
122 s4u::Host* Actor::get_host()
123 {
124   return this->pimpl_->host;
125 }
126
127 void Actor::daemonize()
128 {
129   simgrid::simix::kernelImmediate([this]() { pimpl_->daemonize(); });
130 }
131
132 bool Actor::is_daemon() const
133 {
134   return this->pimpl_->isDaemon();
135 }
136
137 const simgrid::xbt::string& Actor::get_name() const
138 {
139   return this->pimpl_->get_name();
140 }
141
142 const char* Actor::get_cname() const
143 {
144   return this->pimpl_->get_cname();
145 }
146
147 aid_t Actor::get_pid() const
148 {
149   return this->pimpl_->pid;
150 }
151
152 aid_t Actor::get_ppid() const
153 {
154   return this->pimpl_->ppid;
155 }
156
157 void Actor::suspend()
158 {
159   if (TRACE_actor_is_enabled())
160     simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->pushEvent("suspend");
161
162   simcall_process_suspend(pimpl_);
163 }
164
165 void Actor::resume()
166 {
167   simgrid::simix::kernelImmediate([this] { pimpl_->resume(); });
168   if (TRACE_actor_is_enabled())
169     simgrid::instr::Container::byName(instr_pid(this))->getState("ACTOR_STATE")->popEvent();
170 }
171
172 int Actor::is_suspended()
173 {
174   return simgrid::simix::kernelImmediate([this] { return pimpl_->suspended; });
175 }
176
177 void Actor::set_kill_time(double time)
178 {
179   simcall_process_set_kill_time(pimpl_,time);
180 }
181
182 /** \brief Get the kill time of an actor(or 0 if unset). */
183 double Actor::get_kill_time()
184 {
185   return SIMIX_timer_get_date(pimpl_->kill_timer);
186 }
187
188 void Actor::kill(aid_t pid)
189 {
190   smx_actor_t killer  = SIMIX_process_self();
191   smx_actor_t process = SIMIX_process_from_PID(pid);
192   if(process != nullptr) {
193     simgrid::simix::kernelImmediate([killer, process] { SIMIX_process_kill(process, killer); });
194   } else {
195     std::ostringstream oss;
196     oss << "kill: (" << pid << ") - No such actor" << std::endl;
197     throw std::runtime_error(oss.str());
198   }
199 }
200
201 void Actor::kill() {
202   smx_actor_t process = SIMIX_process_self();
203   simgrid::simix::kernelImmediate(
204       [this, process] { SIMIX_process_kill(pimpl_, (pimpl_ == simix_global->maestro_process) ? pimpl_ : process); });
205 }
206
207 smx_actor_t Actor::get_impl()
208 {
209   return pimpl_;
210 }
211
212 // ***** Static functions *****
213
214 ActorPtr Actor::by_pid(aid_t pid)
215 {
216   smx_actor_t process = SIMIX_process_from_PID(pid);
217   if (process != nullptr)
218     return process->iface();
219   else
220     return ActorPtr();
221 }
222
223 void Actor::kill_all()
224 {
225   simcall_process_killall();
226 }
227
228 std::map<std::string, std::string>* Actor::get_properties()
229 {
230   return simgrid::simix::kernelImmediate([this] { return this->pimpl_->getProperties(); });
231 }
232
233 /** Retrieve the property value (or nullptr if not set) */
234 const char* Actor::get_property(const char* key)
235 {
236   return simgrid::simix::kernelImmediate([this, key] { return pimpl_->getProperty(key); });
237 }
238
239 void Actor::set_property(const char* key, const char* value)
240 {
241   simgrid::simix::kernelImmediate([this, key, value] { pimpl_->setProperty(key, value); });
242 }
243
244 Actor* Actor::restart()
245 {
246   return simgrid::simix::kernelImmediate([this]() { return pimpl_->restart(); });
247 }
248
249 // ***** this_actor *****
250
251 namespace this_actor {
252
253 /** Returns true if run from the kernel mode, and false if run from a real actor
254  *
255  * Everything that is run out of any actor (simulation setup before the engine is run,
256  * computing the model evolutions as a result to the actors' action, etc) is run in
257  * kernel mode, just as in any operating systems.
258  *
259  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
260  * because it is the one scheduling when the others should move or wait.
261  */
262 bool isMaestro()
263 {
264   smx_actor_t process = SIMIX_process_self();
265   return process == nullptr || process == simix_global->maestro_process;
266 }
267
268 void sleep_for(double duration)
269 {
270   if (duration > 0)
271     simcall_process_sleep(duration);
272 }
273
274 void yield()
275 {
276   simgrid::simix::kernelImmediate([] { /* do nothing*/ });
277 }
278
279 XBT_PUBLIC void sleep_until(double timeout)
280 {
281   double now = SIMIX_get_clock();
282   if (timeout > now)
283     simcall_process_sleep(timeout - now);
284 }
285
286 void execute(double flops)
287 {
288   smx_activity_t s = simcall_execution_start(nullptr, flops, 1.0 /*priority*/, 0. /*bound*/, getHost());
289   simcall_execution_wait(s);
290 }
291
292 void execute(double flops, double priority)
293 {
294   smx_activity_t s = simcall_execution_start(nullptr, flops, 1 / priority /*priority*/, 0. /*bound*/, getHost());
295   simcall_execution_wait(s);
296 }
297
298 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount, double timeout)
299 {
300   smx_activity_t s =
301       simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, timeout);
302   simcall_execution_wait(s);
303 }
304
305 void parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
306 {
307   smx_activity_t s = simcall_execution_parallel_start(nullptr, host_nb, host_list, flops_amount, bytes_amount, -1, -1);
308   simcall_execution_wait(s);
309 }
310
311 ExecPtr exec_init(double flops_amount)
312 {
313   ExecPtr res        = ExecPtr(new Exec());
314   res->host_         = getHost();
315   res->flops_amount_ = flops_amount;
316   res->set_remaining(flops_amount);
317   return res;
318 }
319
320 ExecPtr exec_async(double flops)
321 {
322   ExecPtr res = exec_init(flops);
323   res->start();
324   return res;
325 }
326
327 aid_t getPid()
328 {
329   return SIMIX_process_self()->pid;
330 }
331
332 aid_t getPpid()
333 {
334   return SIMIX_process_self()->ppid;
335 }
336
337 std::string get_name()
338 {
339   return SIMIX_process_self()->get_name();
340 }
341
342 const char* get_cname()
343 {
344   return SIMIX_process_self()->get_cname();
345 }
346
347 Host* getHost()
348 {
349   return SIMIX_process_self()->host;
350 }
351
352 void suspend()
353 {
354   if (TRACE_actor_is_enabled())
355     instr::Container::byName(get_name() + "-" + std::to_string(getPid()))
356         ->getState("ACTOR_STATE")
357         ->pushEvent("suspend");
358   simcall_process_suspend(SIMIX_process_self());
359 }
360
361 void resume()
362 {
363   smx_actor_t process = SIMIX_process_self();
364   simgrid::simix::kernelImmediate([process] { process->resume(); });
365
366   if (TRACE_actor_is_enabled())
367     instr::Container::byName(get_name() + "-" + std::to_string(getPid()))->getState("ACTOR_STATE")->popEvent();
368 }
369
370 bool isSuspended()
371 {
372   smx_actor_t process = SIMIX_process_self();
373   return simgrid::simix::kernelImmediate([process] { return process->suspended; });
374 }
375
376 void kill()
377 {
378   smx_actor_t process = SIMIX_process_self();
379   simgrid::simix::kernelImmediate([process] { SIMIX_process_kill(process, process); });
380 }
381
382 void onExit(int_f_pvoid_pvoid_t fun, void* data)
383 {
384   simcall_process_on_exit(SIMIX_process_self(), fun, data);
385 }
386
387 /** @brief Moves the current actor to another host
388  *
389  * @see simgrid::s4u::Actor::migrate() for more information
390  */
391 void migrate(Host* new_host)
392 {
393   SIMIX_process_self()->iface()->migrate(new_host);
394 }
395
396 } // namespace this_actor
397 } // namespace s4u
398 } // namespace simgrid
399
400 /* **************************** Public C interface *************************** */
401
402 /** \ingroup m_actor_management
403  * \brief Returns the process ID of \a actor.
404  *
405  * This function checks whether \a actor is a valid pointer and return its PID (or 0 in case of problem).
406  */
407 int sg_actor_get_PID(sg_actor_t actor)
408 {
409   /* Do not raise an exception here: this function is called by the logs
410    * and the exceptions, so it would be called back again and again */
411   if (actor == nullptr || actor->get_impl() == nullptr)
412     return 0;
413   return actor->get_pid();
414 }
415
416 /** \ingroup m_actor_management
417  * \brief Returns the process ID of the parent of \a actor.
418  *
419  * This function checks whether \a actor is a valid pointer and return its parent's PID.
420  * Returns -1 if the actor has not been created by any other actor.
421  */
422 int sg_actor_get_PPID(sg_actor_t actor)
423 {
424   return actor->get_ppid();
425 }
426
427 /** \ingroup m_actor_management
428  * \brief Return the name of an actor.
429  */
430 const char* sg_actor_get_name(sg_actor_t actor)
431 {
432   return actor->get_cname();
433 }
434
435 sg_host_t sg_actor_get_host(sg_actor_t actor)
436 {
437   return actor->get_host();
438 }
439
440 /** \ingroup m_actor_management
441  * \brief Returns the value of a given actor property
442  *
443  * \param actor an actor
444  * \param name a property name
445  * \return value of a property (or nullptr if the property is not set)
446  */
447 const char* sg_actor_get_property_value(sg_actor_t actor, const char* name)
448 {
449   return actor->get_property(name);
450 }
451
452 /** \ingroup m_actor_management
453  * \brief Return the list of properties
454  *
455  * This function returns all the parameters associated with an actor
456  */
457 xbt_dict_t sg_actor_get_properties(sg_actor_t actor)
458 {
459   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
460   xbt_dict_t as_dict = xbt_dict_new_homogeneous(xbt_free_f);
461   std::map<std::string, std::string>* props = actor->get_properties();
462   if (props == nullptr)
463     return nullptr;
464   for (auto const& elm : *props) {
465     xbt_dict_set(as_dict, elm.first.c_str(), xbt_strdup(elm.second.c_str()), nullptr);
466   }
467   return as_dict;
468 }
469
470 /** \ingroup m_actor_management
471  * \brief Suspend the actor.
472  *
473  * This function suspends the actor by suspending the task on which it was waiting for the completion.
474  */
475 void sg_actor_suspend(sg_actor_t actor)
476 {
477   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
478   actor->suspend();
479 }
480
481 /** \ingroup m_actor_management
482  * \brief Resume a suspended actor.
483  *
484  * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
485  */
486 void sg_actor_resume(sg_actor_t actor)
487 {
488   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
489   actor->resume();
490 }
491
492 /** \ingroup m_actor_management
493  * \brief Returns true if the actor is suspended .
494  *
495  * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
496  */
497 int sg_actor_is_suspended(sg_actor_t actor)
498 {
499   return actor->is_suspended();
500 }
501
502 /**
503  * \ingroup m_actor_management
504  * \brief Restarts an actor from the beginning.
505  */
506 sg_actor_t sg_actor_restart(sg_actor_t actor)
507 {
508   return actor->restart();
509 }
510
511 /** @ingroup m_actor_management
512  * @brief This actor will be terminated automatically when the last non-daemon actor finishes
513  */
514 void sg_actor_daemonize(sg_actor_t actor)
515 {
516   actor->daemonize();
517 }
518
519 /** \ingroup m_actor_management
520  * \brief Migrates an actor to another location.
521  *
522  * This function changes the value of the #sg_host_t on  which \a actor is running.
523  */
524 void sg_actor_migrate(sg_actor_t process, sg_host_t host)
525 {
526   process->migrate(host);
527 }
528
529 /** \ingroup m_actor_management
530 * \brief Wait for the completion of a #sg_actor_t.
531 *
532 * \param actor the actor to wait for
533 * \param timeout wait until the actor is over, or the timeout expires
534 */
535 void sg_actor_join(sg_actor_t actor, double timeout)
536 {
537   actor->join(timeout);
538 }
539
540 void sg_actor_kill(sg_actor_t actor)
541 {
542   actor->kill();
543 }
544
545 /** \ingroup m_actor_management
546  * \brief Set the kill time of an actor.
547  *
548  * \param actor an actor
549  * \param kill_time the time when the actor is killed.
550  */
551 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
552 {
553   actor->set_kill_time(kill_time);
554 }