Logo AND Algorithmique Numérique Distribuée

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