Logo AND Algorithmique Numérique Distribuée

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