Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / s4u / s4u_Actor.cpp
1 /* Copyright (c) 2006-2023. 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/Exception.hpp>
7 #include <simgrid/actor.h>
8 #include <simgrid/modelchecker.h>
9 #include <simgrid/s4u/Actor.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/Exec.hpp>
12 #include <simgrid/s4u/VirtualMachine.hpp>
13
14 #include "src/kernel/EngineImpl.hpp"
15 #include "src/kernel/actor/ActorImpl.hpp"
16 #include "src/kernel/resource/HostImpl.hpp"
17 #include "src/mc/mc.h"
18 #include "src/mc/mc_replay.hpp"
19
20 #include <algorithm>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_actor, s4u, "S4U actors");
23
24 namespace simgrid {
25
26 template class xbt::Extendable<s4u::Actor>;
27
28 namespace s4u {
29
30 xbt::signal<void(Actor&)> s4u::Actor::on_creation;
31 xbt::signal<void(Actor const&)> s4u::Actor::on_suspend;
32 xbt::signal<void(Actor const&)> s4u::Actor::on_resume;
33 xbt::signal<void(Actor const&)> s4u::Actor::on_sleep;
34 xbt::signal<void(Actor const&)> s4u::Actor::on_wake_up;
35 xbt::signal<void(Actor const&, Host const& previous_location)> s4u::Actor::on_host_change;
36 xbt::signal<void(Actor const&)> s4u::Actor::on_termination;
37 xbt::signal<void(Actor const&)> s4u::Actor::on_destruction;
38
39 // ***** Actor creation *****
40 Actor* Actor::self()
41 {
42   const kernel::context::Context* self_context = kernel::context::Context::self();
43   if (self_context == nullptr)
44     return nullptr;
45
46   return self_context->get_actor()->get_ciface();
47 }
48
49 ActorPtr Actor::init(const std::string& name, s4u::Host* host)
50 {
51   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
52   kernel::actor::ActorImpl* actor =
53       kernel::actor::simcall_answered([self, &name, host] { return self->init(name, host).get(); });
54   return actor->get_iface();
55 }
56
57 /** Set a non-default stack size for this context (in Kb)
58  *
59  * This must be done before starting the actor, and it won't work with the thread factory. */
60 ActorPtr Actor::set_stacksize(unsigned stacksize)
61 {
62   pimpl_->set_stacksize(stacksize * 1024);
63   return this;
64 }
65
66 ActorPtr Actor::start(const std::function<void()>& code)
67 {
68   simgrid::kernel::actor::simcall_answered([this, &code] { pimpl_->start(code); });
69   return this;
70 }
71
72 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::function<void()>& code)
73 {
74   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
75   kernel::actor::ActorImpl* actor =
76       kernel::actor::simcall_answered([self, &name, host, &code] { return self->init(name, host)->start(code); });
77
78   return actor->get_iface();
79 }
80
81 ActorPtr Actor::create(const std::string& name, s4u::Host* host, const std::string& function,
82                        std::vector<std::string> args)
83 {
84   const simgrid::kernel::actor::ActorCodeFactory& factory =
85       simgrid::kernel::EngineImpl::get_instance()->get_function(function);
86   return create(name, host, factory(std::move(args)));
87 }
88
89 void intrusive_ptr_add_ref(const Actor* actor)
90 {
91   intrusive_ptr_add_ref(actor->pimpl_);
92 }
93 void intrusive_ptr_release(const Actor* actor)
94 {
95   intrusive_ptr_release(actor->pimpl_);
96 }
97 int Actor::get_refcount() const
98 {
99   return pimpl_->get_refcount();
100 }
101
102 // ***** Actor methods *****
103
104 void Actor::join() const
105 {
106   join(-1);
107 }
108
109 void Actor::join(double timeout) const
110 {
111   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
112   const kernel::actor::ActorImpl* target = pimpl_;
113   kernel::actor::ActorJoinSimcall observer{issuer, get_impl(), timeout};
114
115   kernel::actor::simcall_blocking(
116       [issuer, target, timeout] {
117         if (target->wannadie()) {
118           // The joined actor is already finished, just wake up the issuer right away
119           issuer->simcall_answer();
120         } else {
121           kernel::activity::ActivityImplPtr sync = issuer->join(target, timeout);
122           sync->register_simcall(&issuer->simcall_);
123         }
124       },
125       &observer);
126 }
127
128 Actor* Actor::set_auto_restart(bool autorestart)
129 {
130   if (autorestart == pimpl_->has_to_auto_restart()) // not changed
131     return this;
132
133   kernel::actor::simcall_answered([this, autorestart]() {
134     xbt_assert(autorestart, "Asking an actor to stop being autorestart is not implemented yet. Ask us if you need it.");
135     pimpl_->set_auto_restart(autorestart);
136
137     auto* arg = new kernel::actor::ProcessArg(pimpl_->get_host(), pimpl_);
138     XBT_DEBUG("Adding %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
139     pimpl_->get_host()->get_impl()->add_actor_at_boot(arg);
140   });
141   return this;
142 }
143 int Actor::get_restart_count() const
144 {
145   return pimpl_->get_restart_count();
146 }
147
148 void Actor::on_exit(const std::function<void(bool /*failed*/)>& fun) const
149 {
150   kernel::actor::simcall_answered([this, &fun] { pimpl_->on_exit->emplace_back(fun); });
151 }
152
153 void Actor::set_host(Host* new_host)
154 {
155   const s4u::Host* previous_location = get_host();
156
157   kernel::actor::simcall_answered([this, new_host]() {
158     for (auto const& activity : pimpl_->activities_) {
159       // FIXME: implement the migration of other kinds of activities
160       if (auto exec = boost::dynamic_pointer_cast<kernel::activity::ExecImpl>(activity))
161         exec->migrate(new_host);
162     }
163     this->pimpl_->set_host(new_host);
164   });
165
166   s4u::Actor::on_host_change(*this, *previous_location);
167   s4u::Actor::on_this_host_change(*this, *previous_location);
168 }
169
170 s4u::Host* Actor::get_host() const
171 {
172   return this->pimpl_->get_host();
173 }
174
175 Actor* Actor::daemonize()
176 {
177   kernel::actor::simcall_answered([this]() { pimpl_->daemonize(); });
178   return this;
179 }
180
181 bool Actor::is_daemon() const
182 {
183   return this->pimpl_->is_daemon();
184 }
185
186 bool Actor::is_maestro()
187 {
188   const auto* self = kernel::actor::ActorImpl::self();
189   return self == nullptr || kernel::EngineImpl::get_instance()->is_maestro(self);
190 }
191
192 const std::string& Actor::get_name() const
193 {
194   return this->pimpl_->get_name();
195 }
196
197 const char* Actor::get_cname() const
198 {
199   return this->pimpl_->get_cname();
200 }
201
202 aid_t Actor::get_pid() const
203 {
204   return this->pimpl_->get_pid();
205 }
206
207 aid_t Actor::get_ppid() const
208 {
209   return this->pimpl_->get_ppid();
210 }
211
212 void Actor::suspend()
213 {
214   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
215   kernel::actor::ActorImpl* target = pimpl_;
216   s4u::Actor::on_suspend(*this);
217   s4u::Actor::on_this_suspend(*this);
218   kernel::actor::simcall_blocking([issuer, target]() {
219     target->suspend();
220     if (target != issuer) {
221       /* If we are suspending ourselves, then just do not finish the simcall now */
222       issuer->simcall_answer();
223     }
224   });
225 }
226
227 void Actor::resume()
228 {
229   kernel::actor::simcall_answered([this] { pimpl_->resume(); });
230   s4u::Actor::on_resume(*this);
231   s4u::Actor::on_this_resume(*this);
232 }
233
234 bool Actor::is_suspended() const
235 {
236   return pimpl_->is_suspended();
237 }
238
239 void Actor::set_kill_time(double kill_time)
240 {
241   kernel::actor::simcall_answered([this, kill_time] { pimpl_->set_kill_time(kill_time); });
242 }
243
244 /** @brief Get the kill time of an actor(or 0 if unset). */
245 double Actor::get_kill_time() const
246 {
247   return pimpl_->get_kill_time();
248 }
249
250 void Actor::kill()
251 {
252   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
253   kernel::actor::simcall_answered([this, self] { self->kill(pimpl_); });
254 }
255
256 // ***** Static functions *****
257
258 ActorPtr Actor::by_pid(aid_t pid)
259 {
260   kernel::actor::ActorImpl* actor = kernel::EngineImpl::get_instance()->get_actor_by_pid(pid);
261   if (actor != nullptr)
262     return actor->get_iface();
263   else
264     return ActorPtr();
265 }
266
267 void Actor::kill_all()
268 {
269   const kernel::actor::ActorImpl* self = kernel::actor::ActorImpl::self();
270   kernel::actor::simcall_answered([self] { self->kill_all(); });
271 }
272
273 const std::unordered_map<std::string, std::string>* Actor::get_properties() const
274 {
275   return pimpl_->get_properties();
276 }
277
278 /** Retrieve the property value (or nullptr if not set) */
279 const char* Actor::get_property(const std::string& key) const
280 {
281   return pimpl_->get_property(key);
282 }
283
284 void Actor::set_property(const std::string& key, const std::string& value)
285 {
286   kernel::actor::simcall_answered([this, &key, &value] { pimpl_->set_property(key, value); });
287 }
288
289 Actor* Actor::restart()
290 {
291   return kernel::actor::simcall_answered([this]() { return pimpl_->restart(); });
292 }
293
294 // ***** this_actor *****
295
296 namespace this_actor {
297
298 /** Returns true if run from the kernel mode, and false if run from a real actor
299  *
300  * Everything that is run out of any actor (simulation setup before the engine is run,
301  * computing the model evolutions as a result to the actors' action, etc) is run in
302  * kernel mode, just as in any operating systems.
303  *
304  * In SimGrid, the actor in charge of doing the stuff in kernel mode is called Maestro,
305  * because it is the one scheduling when the others should move or wait.
306  */
307 bool is_maestro()
308 {
309   return Actor::is_maestro();
310 }
311
312 void sleep_for(double duration)
313 {
314   xbt_assert(std::isfinite(duration), "duration is not finite!");
315
316   if (duration <= 0) /* that's a no-op */
317     return;
318
319   if (duration < sg_precision_timing) {
320     static unsigned int warned = 0; // At most 20 such warnings
321     warned++;
322     if (warned <= 20)
323       XBT_INFO("The parameter to sleep_for() is smaller than the SimGrid numerical accuracy (%g < %g). "
324                "Please refer to https://simgrid.org/doc/latest/Configuring_SimGrid.html#numerical-precision",
325                duration, sg_precision_timing);
326     if (warned == 20)
327       XBT_VERB("(further warnings about the numerical accuracy of sleep_for() will be omitted).");
328   }
329
330   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
331   kernel::actor::ActorSleepSimcall observer(issuer);
332
333   Actor::on_sleep(*issuer->get_ciface());
334   issuer->get_ciface()->on_this_sleep(*issuer->get_ciface());
335
336   kernel::actor::simcall_blocking(
337       [issuer, duration]() {
338         if (MC_is_active() || MC_record_replay_is_active()) {
339           // MC_process_clock_add(issuer, duration); // BUG: Makes the exploration loop
340           issuer->simcall_answer();
341         } else {
342           kernel::activity::ActivityImplPtr sync = issuer->sleep(duration);
343           sync->register_simcall(&issuer->simcall_);
344         }
345       },
346       &observer);
347
348   Actor::on_wake_up(*issuer->get_ciface());
349   issuer->get_ciface()->on_this_wake_up(*issuer->get_ciface());
350 }
351
352 void yield()
353 {
354   kernel::actor::simcall_answered([] { /* do nothing*/ });
355 }
356
357 XBT_PUBLIC void sleep_until(double wakeup_time)
358 {
359   double now = s4u::Engine::get_clock();
360   if (wakeup_time > now)
361     sleep_for(wakeup_time - now);
362 }
363
364 void execute(double flops)
365 {
366   execute(flops, 1.0 /* priority */);
367 }
368
369 void execute(double flops, double priority)
370 {
371   exec_init(flops)->set_priority(priority)->start()->wait();
372 }
373
374 void parallel_execute(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
375                       const std::vector<double>& bytes_amounts)
376 {
377   exec_init(hosts, flops_amounts, bytes_amounts)->wait();
378 }
379
380 void thread_execute(s4u::Host* host, double flops_amount, int thread_count)
381 {
382   Exec::init()->set_flops_amount(flops_amount)->set_host(host)->set_thread_count(thread_count)->wait();
383 }
384
385 ExecPtr exec_init(double flops_amount)
386 {
387   return Exec::init()->set_flops_amount(flops_amount)->set_host(get_host());
388 }
389
390 ExecPtr exec_init(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
391                   const std::vector<double>& bytes_amounts)
392 {
393   xbt_assert(not hosts.empty(), "Your parallel executions must span over at least one host.");
394   xbt_assert(hosts.size() == flops_amounts.size() || flops_amounts.empty(),
395              "Host count (%zu) does not match flops_amount count (%zu).", hosts.size(), flops_amounts.size());
396   xbt_assert(hosts.size() * hosts.size() == bytes_amounts.size() || bytes_amounts.empty(),
397              "bytes_amounts must be a matrix of size host_count * host_count (%zu*%zu), but it's of size %zu.",
398              hosts.size(), hosts.size(), bytes_amounts.size());
399   /* Check that we are not mixing VMs and PMs in the parallel task */
400   bool is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(hosts.front()));
401   xbt_assert(std::all_of(hosts.begin(), hosts.end(),
402                          [is_a_vm](s4u::Host* elm) {
403                            bool tmp_is_a_vm = (nullptr != dynamic_cast<VirtualMachine*>(elm));
404                            return is_a_vm == tmp_is_a_vm;
405                          }),
406              "parallel_execute: mixing VMs and PMs is not supported (yet).");
407   /* checking for infinite values */
408   xbt_assert(std::all_of(flops_amounts.begin(), flops_amounts.end(), [](double elm) { return std::isfinite(elm); }),
409              "flops_amounts comprises infinite values!");
410   xbt_assert(std::all_of(bytes_amounts.begin(), bytes_amounts.end(), [](double elm) { return std::isfinite(elm); }),
411              "flops_amounts comprises infinite values!");
412
413   return Exec::init()->set_flops_amounts(flops_amounts)->set_bytes_amounts(bytes_amounts)->set_hosts(hosts);
414 }
415
416 ExecPtr exec_async(double flops)
417 {
418   ExecPtr res = exec_init(flops);
419   res->start();
420   return res;
421 }
422
423 aid_t get_pid()
424 {
425   const auto* self = simgrid::kernel::actor::ActorImpl::self();
426   return self ? self->get_pid() : 0;
427 }
428
429 aid_t get_ppid()
430 {
431   return simgrid::kernel::actor::ActorImpl::self()->get_ppid();
432 }
433
434 std::string get_name()
435 {
436   return simgrid::kernel::actor::ActorImpl::self()->get_name();
437 }
438
439 const char* get_cname()
440 {
441   const auto* self = simgrid::kernel::actor::ActorImpl::self();
442   return self ? self->get_cname() : nullptr;
443 }
444
445 Host* get_host()
446 {
447   return simgrid::kernel::actor::ActorImpl::self()->get_host();
448 }
449
450 void suspend()
451 {
452   kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
453   s4u::Actor::on_suspend(*self->get_ciface());
454   self->get_ciface()->on_this_suspend(*self->get_ciface());
455   kernel::actor::simcall_blocking([self] { self->suspend(); });
456 }
457
458 void exit()
459 {
460   kernel::actor::ActorImpl* self = simgrid::kernel::actor::ActorImpl::self();
461   simgrid::kernel::actor::simcall_answered([self] { self->exit(); });
462   THROW_IMPOSSIBLE;
463 }
464
465 void on_exit(const std::function<void(bool)>& fun)
466 {
467   simgrid::kernel::actor::ActorImpl::self()->get_iface()->on_exit(fun);
468 }
469
470 /** @brief Moves the current actor to another host
471  *
472  * @see simgrid::s4u::Actor::migrate() for more information
473  */
474 void set_host(Host* new_host)
475 {
476   simgrid::kernel::actor::ActorImpl::self()->get_iface()->set_host(new_host);
477 }
478
479 } // namespace this_actor
480 } // namespace s4u
481 } // namespace simgrid
482
483 /* **************************** Public C interface *************************** */
484 size_t sg_actor_count()
485 {
486   return simgrid::s4u::Engine::get_instance()->get_actor_count();
487 }
488
489 sg_actor_t* sg_actor_list()
490 {
491   const simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
492   size_t actor_count      = e->get_actor_count();
493   xbt_assert(actor_count > 0, "There is no actor!");
494   std::vector<simgrid::s4u::ActorPtr> actors = e->get_all_actors();
495
496   auto* res = xbt_new(sg_actor_t, actors.size());
497   for (size_t i = 0; i < actor_count; i++)
498     res[i] = actors[i].get();
499   return res;
500 }
501
502 sg_actor_t sg_actor_init(const char* name, sg_host_t host)
503 {
504   return simgrid::s4u::Actor::init(name, host).get();
505 }
506
507 void sg_actor_start_(sg_actor_t actor, xbt_main_func_t code, int argc, const char* const* argv)
508 {
509   simgrid::kernel::actor::ActorCode function;
510   if (code)
511     function = simgrid::xbt::wrap_main(code, argc, argv);
512   actor->start(function);
513 }
514
515 sg_actor_t sg_actor_create_(const char* name, sg_host_t host, xbt_main_func_t code, int argc, const char* const* argv)
516 {
517   simgrid::kernel::actor::ActorCode function = simgrid::xbt::wrap_main(code, argc, argv);
518   return simgrid::s4u::Actor::init(name, host)->start(function).get();
519 }
520
521 void sg_actor_set_stacksize(sg_actor_t actor, unsigned size)
522 {
523   actor->set_stacksize(size);
524 }
525
526 void sg_actor_exit()
527 {
528   simgrid::s4u::this_actor::exit();
529 }
530
531 /**
532  * @brief Returns the process ID of @a actor.
533  *
534  * This function checks whether @a actor is a valid pointer and return its PID (or 0 in case of problem).
535  */
536
537 aid_t sg_actor_get_pid(const_sg_actor_t actor)
538 {
539   /* Do not raise an exception here: this function is called by the logs
540    * and the exceptions, so it would be called back again and again */
541   if (actor == nullptr || actor->get_impl() == nullptr)
542     return 0;
543   return actor->get_pid();
544 }
545
546 /**
547  * @brief Returns the process ID of the parent of @a actor.
548  *
549  * This function checks whether @a actor is a valid pointer and return its parent's PID.
550  * Returns -1 if the actor has not been created by any other actor.
551  */
552 aid_t sg_actor_get_ppid(const_sg_actor_t actor)
553 {
554   return actor->get_ppid();
555 }
556
557 /**
558  * @brief Return a #sg_actor_t given its PID.
559  *
560  * This function search in the list of all the created sg_actor_t for a sg_actor_t  whose PID is equal to @a PID.
561  * If none is found, @c nullptr is returned.
562    Note that the PID are unique in the whole simulation, not only on a given host.
563  */
564 sg_actor_t sg_actor_by_pid(aid_t pid)
565 {
566   return simgrid::s4u::Actor::by_pid(pid).get();
567 }
568
569 /** @brief Return the name of an actor. */
570 const char* sg_actor_get_name(const_sg_actor_t actor)
571 {
572   return actor->get_cname();
573 }
574
575 sg_host_t sg_actor_get_host(const_sg_actor_t actor)
576 {
577   return actor->get_host();
578 }
579
580 /**
581  * @brief Returns the value of a given actor property
582  *
583  * @param actor an actor
584  * @param name a property name
585  * @return value of a property (or nullptr if the property is not set)
586  */
587 const char* sg_actor_get_property_value(const_sg_actor_t actor, const char* name)
588 {
589   return actor->get_property(name);
590 }
591
592 /**
593  * @brief Return the list of properties
594  *
595  * This function returns all the parameters associated with an actor
596  */
597 xbt_dict_t sg_actor_get_properties(const_sg_actor_t actor)
598 {
599   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
600   xbt_dict_t as_dict                        = xbt_dict_new_homogeneous(xbt_free_f);
601   const std::unordered_map<std::string, std::string>* props = actor->get_properties();
602   if (props == nullptr)
603     return nullptr;
604   for (auto const& [key, value] : *props) {
605     xbt_dict_set(as_dict, key.c_str(), xbt_strdup(value.c_str()));
606   }
607   return as_dict;
608 }
609
610 /**
611  * @brief Suspend the actor.
612  *
613  * This function suspends the actor by suspending the task on which it was waiting for the completion.
614  */
615 void sg_actor_suspend(sg_actor_t actor)
616 {
617   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
618   actor->suspend();
619 }
620
621 /**
622  * @brief Resume a suspended actor.
623  *
624  * This function resumes a suspended actor by resuming the task on which it was waiting for the completion.
625  */
626 void sg_actor_resume(sg_actor_t actor)
627 {
628   xbt_assert(actor != nullptr, "Invalid parameter: First argument must not be nullptr");
629   actor->resume();
630 }
631
632 /**
633  * @brief Returns true if the actor is suspended .
634  *
635  * This checks whether an actor is suspended or not by inspecting the task on which it was waiting for the completion.
636  */
637 int sg_actor_is_suspended(const_sg_actor_t actor)
638 {
639   return actor->is_suspended();
640 }
641
642 /** @brief Restarts an actor from the beginning. */
643 sg_actor_t sg_actor_restart(sg_actor_t actor)
644 {
645   return actor->restart();
646 }
647
648 /**
649  * @brief Sets the "auto-restart" flag of the actor.
650  * If the flag is set to 1, the actor will be automatically restarted when its host comes back up.
651  */
652 void sg_actor_set_auto_restart(sg_actor_t actor, int auto_restart)
653 {
654   actor->set_auto_restart(auto_restart);
655 }
656
657 /** @brief This actor will be terminated automatically when the last non-daemon actor finishes */
658 void sg_actor_daemonize(sg_actor_t actor)
659 {
660   actor->daemonize();
661 }
662
663 /** Returns whether or not this actor has been daemonized or not */
664 int sg_actor_is_daemon(const_sg_actor_t actor)
665 {
666   return actor->is_daemon();
667 }
668
669 /**
670  * @brief Migrates an actor to another location.
671  *
672  * This function changes the value of the #sg_host_t on  which @a actor is running.
673  */
674 void sg_actor_set_host(sg_actor_t actor, sg_host_t host)
675 {
676   actor->set_host(host);
677 }
678
679 /**
680  * @brief Wait for the completion of a #sg_actor_t.
681  *
682  * @param actor the actor to wait for
683  * @param timeout wait until the actor is over, or the timeout expires
684  */
685 void sg_actor_join(const_sg_actor_t actor, double timeout)
686 {
687   actor->join(timeout);
688 }
689
690 void sg_actor_kill(sg_actor_t actor)
691 {
692   actor->kill();
693 }
694
695 void sg_actor_kill_all()
696 {
697   simgrid::s4u::Actor::kill_all();
698 }
699
700 /**
701  * @brief Set the kill time of an actor.
702  *
703  * @param actor an actor
704  * @param kill_time the time when the actor is killed.
705  */
706 void sg_actor_set_kill_time(sg_actor_t actor, double kill_time)
707 {
708   actor->set_kill_time(kill_time);
709 }
710
711 /** Yield the current actor; let the other actors execute first */
712 void sg_actor_yield()
713 {
714   simgrid::s4u::this_actor::yield();
715 }
716
717 void sg_actor_sleep_for(double duration)
718 {
719   simgrid::s4u::this_actor::sleep_for(duration);
720 }
721
722 void sg_actor_sleep_until(double wakeup_time)
723 {
724   simgrid::s4u::this_actor::sleep_until(wakeup_time);
725 }
726
727 sg_actor_t sg_actor_attach(const char* name, void* data, sg_host_t host, xbt_dict_t properties)
728 {
729   xbt_assert(host != nullptr, "Invalid parameters: host and code params must not be nullptr");
730   std::unordered_map<std::string, std::string> props;
731   xbt_dict_cursor_t cursor = nullptr;
732   char* key;
733   char* value;
734   xbt_dict_foreach (properties, cursor, key, value)
735     props[key] = value;
736   xbt_dict_free(&properties);
737
738   /* Let's create the actor: SIMIX may decide to start it right now, even before returning the flow control to us */
739   simgrid::kernel::actor::ActorImpl* actor = nullptr;
740   try {
741     actor = simgrid::kernel::actor::ActorImpl::attach(name, data, host).get();
742     actor->set_properties(props);
743   } catch (simgrid::HostFailureException const&) {
744     xbt_die("Could not attach");
745   }
746
747   simgrid::s4u::this_actor::yield();
748   return actor->get_ciface();
749 }
750
751 void sg_actor_detach()
752 {
753   simgrid::kernel::actor::ActorImpl::detach();
754 }
755
756 aid_t sg_actor_self_get_pid()
757 {
758   return simgrid::s4u::this_actor::get_pid();
759 }
760
761 aid_t sg_actor_self_get_ppid()
762 {
763   return simgrid::s4u::this_actor::get_ppid();
764 }
765
766 const char* sg_actor_self_get_name()
767 {
768   if (simgrid::s4u::Actor::is_maestro())
769     return "maestro";
770   return simgrid::s4u::this_actor::get_cname();
771 }
772
773 void* sg_actor_self_get_data()
774 {
775   return simgrid::s4u::Actor::self()->get_data<void>();
776 }
777
778 void sg_actor_self_set_data(void* userdata)
779 {
780   simgrid::s4u::Actor::self()->set_data(userdata);
781 }
782
783 sg_actor_t sg_actor_self()
784 {
785   return simgrid::s4u::Actor::self();
786 }
787
788 void sg_actor_execute(double flops)
789 {
790   simgrid::s4u::this_actor::execute(flops);
791 }
792 void sg_actor_execute_with_priority(double flops, double priority)
793 {
794   simgrid::s4u::this_actor::exec_init(flops)->set_priority(priority)->wait();
795 }
796
797 void sg_actor_parallel_execute(int host_nb, sg_host_t* host_list, double* flops_amount, double* bytes_amount)
798 {
799   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
800   std::vector<double> flops;
801   std::vector<double> bytes;
802   if (flops_amount != nullptr)
803     flops = std::vector<double>(flops_amount, flops_amount + host_nb);
804   if (bytes_amount != nullptr)
805     bytes = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
806
807   simgrid::s4u::this_actor::parallel_execute(hosts, flops, bytes);
808 }
809
810 /** @brief Take an extra reference on that actor to prevent it to be garbage-collected */
811 void sg_actor_ref(const_sg_actor_t actor)
812 {
813   intrusive_ptr_add_ref(actor);
814 }
815 /** @brief Release a reference on that actor so that it can get be garbage-collected */
816 void sg_actor_unref(const_sg_actor_t actor)
817 {
818   intrusive_ptr_release(actor);
819 }
820
821 /** @brief Return the user data of a #sg_actor_t */
822 void* sg_actor_get_data(const_sg_actor_t actor)
823 {
824   return actor->get_data<void>();
825 }
826
827 /** @brief Set the user data of a #sg_actor_t */
828 void sg_actor_set_data(sg_actor_t actor, void* userdata)
829 {
830   actor->set_data(userdata);
831 }
832
833 /** @brief Add a function to the list of "on_exit" functions for the current actor.
834  *  The on_exit functions are the functions executed when your actor is killed.
835  *  You should use them to free the data used by your actor.
836  */
837 void sg_actor_on_exit(void_f_int_pvoid_t fun, void* data)
838 {
839   simgrid::s4u::this_actor::on_exit([fun, data](bool failed) { fun(failed ? 1 /*FAILURE*/ : 0 /*SUCCESS*/, data); });
840 }
841
842 sg_exec_t sg_actor_exec_init(double computation_amount)
843 {
844   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(computation_amount);
845   exec->add_ref();
846   return exec.get();
847 }
848
849 sg_exec_t sg_actor_parallel_exec_init(int host_nb, const sg_host_t* host_list, double* flops_amount,
850                                       double* bytes_amount)
851 {
852   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
853   std::vector<double> flops;
854   std::vector<double> bytes;
855   if (flops_amount != nullptr)
856     flops = std::vector<double>(flops_amount, flops_amount + host_nb);
857   if (bytes_amount != nullptr)
858     bytes = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
859
860   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(hosts, flops, bytes);
861   exec->add_ref();
862   return exec.get();
863 }
864
865 sg_exec_t sg_actor_exec_async(double computation_amount)
866 {
867   simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_async(computation_amount);
868   exec->add_ref();
869   return exec.get();
870 }