Logo AND Algorithmique Numérique Distribuée

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