Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce a Trait to deal with autorestart matter in ActorImpl, and fix bugs
[simgrid.git] / src / kernel / actor / ActorImpl.hpp
1 /* Copyright (c) 2007-2022. 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 #ifndef SIMGRID_KERNEL_ACTOR_ACTORIMPL_HPP
7 #define SIMGRID_KERNEL_ACTOR_ACTORIMPL_HPP
8
9 #include "Simcall.hpp"
10 #include "simgrid/kernel/Timer.hpp"
11 #include "simgrid/s4u/Actor.hpp"
12 #include "xbt/PropertyHolder.hpp"
13 #include <boost/intrusive/list.hpp>
14 #include <functional>
15 #include <list>
16 #include <map>
17 #include <memory>
18
19 namespace simgrid {
20 namespace kernel {
21 namespace actor {
22 class ProcessArg;
23
24 class XBT_PUBLIC ActorRestartingTrait {
25   bool auto_restart_ = false;
26   int restart_count_ = 0;
27
28   friend ActorImpl;
29
30 public:
31   bool has_to_auto_restart() const { return auto_restart_; }
32   void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; }
33   int get_restart_count() const { return restart_count_; }
34 };
35
36 class XBT_PUBLIC ActorImpl : public xbt::PropertyHolder, public ActorRestartingTrait {
37   s4u::Host* host_   = nullptr; /* the host on which the actor is running */
38   aid_t pid_         = 0;
39   aid_t ppid_        = -1;
40   bool daemon_       = false; /* Daemon actors are automatically killed when the last non-daemon leaves */
41   unsigned stacksize_; // set to default value in constructor
42
43   std::vector<activity::MailboxImpl*> mailboxes;
44   friend activity::MailboxImpl;
45
46 public:
47   xbt::string name_;
48   ActorImpl(xbt::string name, s4u::Host* host);
49   ActorImpl(const ActorImpl&) = delete;
50   ActorImpl& operator=(const ActorImpl&) = delete;
51   ~ActorImpl();
52
53   /** Retrieve the actor implementation from its PID (or nullptr if non-existent) */
54   static ActorImpl* by_pid(aid_t pid);
55
56   static ActorImpl* self();
57   double get_kill_time() const;
58   void set_kill_time(double kill_time);
59   boost::intrusive::list_member_hook<> host_actor_list_hook;     /* resource::HostImpl::actor_list_ */
60   boost::intrusive::list_member_hook<> kernel_destroy_list_hook; /* EngineImpl actors_to_destroy */
61   boost::intrusive::list_member_hook<> smx_synchro_hook;       /* {mutex,cond,sem}->sleeping */
62
63   const xbt::string& get_name() const { return name_; }
64   const char* get_cname() const { return name_.c_str(); }
65
66   // Accessors to private fields
67   s4u::Host* get_host() const { return host_; }
68   void set_host(s4u::Host* dest);
69   aid_t get_pid() const { return pid_; }
70   aid_t get_ppid() const { return ppid_; }
71   void set_ppid(aid_t ppid) { ppid_ = ppid; }
72   bool is_daemon() const { return daemon_; } /** Whether this actor has been daemonized */
73   bool is_maestro() const; /** Whether this actor is actually maestro (cheap call but may segfault before actor creation
74                               / after terminaison) */
75   void set_stacksize(unsigned stacksize) { stacksize_ = stacksize; }
76   unsigned get_stacksize() const { return stacksize_; }
77
78   std::unique_ptr<context::Context> context_; /* the context (uctx/raw/thread) that executes the user function */
79
80   std::exception_ptr exception_;
81   bool finished_  = false;
82   bool suspended_ = false;
83
84   activity::ActivityImplPtr waiting_synchro_ = nullptr; /* the current blocking synchro if any */
85   std::list<activity::ActivityImplPtr> activities_;     /* the current non-blocking synchros */
86   Simcall simcall_;
87   /* list of functions executed when the actor dies */
88   std::shared_ptr<std::vector<std::function<void(bool)>>> on_exit =
89       std::make_shared<std::vector<std::function<void(bool)>>>();
90
91   std::function<void()> code_;
92   timer::Timer* kill_timer_ = nullptr;
93
94 private:
95   /* Refcounting */
96   std::atomic_int_fast32_t refcount_{0};
97
98 public:
99   int get_refcount() const { return refcount_; }
100   friend void intrusive_ptr_add_ref(ActorImpl* actor)
101   {
102     // This whole memory consistency semantic drives me nuts.
103     // std::memory_order_relaxed proves to not be enough: There is a threading issue when actors commit suicide.
104     //   My guess is that the maestro context wants to propagate changes to the actor's fields after the
105     //   actor context frees that memory area or something. But I'm not 100% certain of what's going on.
106     // std::memory_order_seq_cst works but that's rather demanding.
107     // AFAIK, std::memory_order_acq_rel works on all tested platforms, so let's stick to it.
108     // Reducing the requirements to _relaxed would require to fix our suicide procedure, which is a messy piece of code.
109     actor->refcount_.fetch_add(1, std::memory_order_acq_rel);
110   }
111   friend void intrusive_ptr_release(ActorImpl* actor)
112   {
113     // inspired from http://www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html
114     if (actor->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
115       // Make sure that any changes done on other threads before their acquire are committed before our delete
116       // http://stackoverflow.com/questions/27751025/why-is-an-acquire-barrier-needed-before-deleting-the-data-in-an-atomically-refer
117       std::atomic_thread_fence(std::memory_order_acquire);
118       delete actor;
119     }
120   }
121
122   /* S4U/implem interfaces */
123 private:
124   s4u::Actor piface_; // Our interface is part of ourselves
125
126   void cleanup_from_simix();
127   void undaemonize();
128
129 public:
130   s4u::ActorPtr get_iface() { return s4u::ActorPtr(&piface_); }
131   s4u::Actor* get_ciface() { return &piface_; }
132
133   ActorImplPtr init(const std::string& name, s4u::Host* host) const;
134   ActorImpl* start(const ActorCode& code);
135
136   static ActorImplPtr create(const std::string& name, const ActorCode& code, void* data, s4u::Host* host,
137                              const ActorImpl* parent_actor);
138   static ActorImplPtr create(ProcessArg* args);
139   static ActorImplPtr attach(const std::string& name, void* data, s4u::Host* host);
140   static void detach();
141   void cleanup();
142   void exit();
143   void kill(ActorImpl* actor) const;
144   void kill_all() const;
145
146   void yield();
147   void daemonize();
148   bool is_suspended() const { return suspended_; }
149   s4u::Actor* restart();
150   void suspend();
151   void resume();
152   activity::ActivityImplPtr join(const ActorImpl* actor, double timeout);
153   activity::ActivityImplPtr sleep(double duration);
154   /** Ask the actor to throw an exception right away */
155   void throw_exception(std::exception_ptr e);
156
157   /** execute the pending simcall -- must be called from the maestro context */
158   void simcall_handle(int value);
159   /** Terminates a simcall currently executed in maestro context. The actor will be restarted in the next scheduling
160    * round */
161   void simcall_answer();
162 };
163
164 class ProcessArg {
165 public:
166   std::string name;
167   std::function<void()> code;
168   void* data                                                               = nullptr;
169   s4u::Host* host                                                          = nullptr;
170   double kill_time                                                         = 0.0;
171   const std::unordered_map<std::string, std::string> properties{};
172   bool auto_restart                                                        = false;
173   bool daemon_;
174   /* list of functions executed when the actor dies */
175   const std::shared_ptr<std::vector<std::function<void(bool)>>> on_exit;
176   int restart_count_ = 0;
177
178   ProcessArg()                  = delete;
179   ProcessArg(const ProcessArg&) = delete;
180   ProcessArg& operator=(const ProcessArg&) = delete;
181
182   explicit ProcessArg(const std::string& name, const std::function<void()>& code, void* data, s4u::Host* host,
183                       double kill_time, const std::unordered_map<std::string, std::string>& properties,
184                       bool auto_restart, bool daemon, int restart_count)
185       : name(name)
186       , code(code)
187       , data(data)
188       , host(host)
189       , kill_time(kill_time)
190       , properties(properties)
191       , auto_restart(auto_restart)
192       , daemon_(daemon)
193       , restart_count_(restart_count)
194   {
195   }
196
197   explicit ProcessArg(s4u::Host* host, ActorImpl* actor)
198       : name(actor->get_name())
199       , code(actor->code_)
200       , data(actor->get_ciface()->get_data<void>())
201       , host(host)
202       , kill_time(actor->get_kill_time())
203       , auto_restart(actor->has_to_auto_restart())
204       , daemon_(actor->is_daemon())
205       , on_exit(actor->on_exit)
206       , restart_count_(actor->get_restart_count() + 1)
207   {
208   }
209 };
210
211 /* Used to keep the list of actors blocked on a synchro  */
212 using SynchroList =
213     boost::intrusive::list<ActorImpl, boost::intrusive::member_hook<ActorImpl, boost::intrusive::list_member_hook<>,
214                                                                     &ActorImpl::smx_synchro_hook>>;
215
216 XBT_PUBLIC void create_maestro(const std::function<void()>& code);
217 XBT_PUBLIC unsigned long get_maxpid();
218 XBT_PUBLIC unsigned long* get_maxpid_addr(); // In MC mode, the application sends this pointers to the MC
219
220 } // namespace actor
221 } // namespace kernel
222 } // namespace simgrid
223
224 #endif