Logo AND Algorithmique Numérique Distribuée

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