Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Also deprecate Activity::waitany/waitall/testany
[simgrid.git] / include / simgrid / s4u / Activity.hpp
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 #ifndef SIMGRID_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
8
9 #include <algorithm>
10 #include <atomic>
11 #include <set>
12 #include <simgrid/forward.h>
13 #include <stdexcept>
14 #include <string>
15 #include <string_view>
16 #include <vector>
17 #include <xbt/Extendable.hpp>
18 #include <xbt/asserts.h>
19 #include <xbt/signal.hpp>
20 #include <xbt/utility.hpp>
21
22 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
23
24 namespace simgrid {
25
26 extern template class XBT_PUBLIC xbt::Extendable<s4u::Activity>;
27
28 namespace s4u {
29
30 /** @brief Activities
31  *
32  * This class is the ancestor of every activities that an actor can undertake.
33  * That is, activities are all the things that do take time to the actor in the simulated world.
34  */
35 class XBT_PUBLIC Activity : public xbt::Extendable<Activity> {
36 #ifndef DOXYGEN
37   friend ActivitySet;
38   friend Comm;
39   friend Exec;
40   friend Io;
41   friend kernel::activity::ActivityImpl;
42   friend std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
43   friend std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename);
44   friend std::vector<ActivityPtr> create_DAG_from_json(const std::string& filename);
45 #endif
46
47 public:
48   // enum class State { ... }
49   XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, FAILED, CANCELED, FINISHED);
50
51   virtual bool is_assigned() const = 0;
52   bool dependencies_solved() const { return dependencies_.empty(); }
53   bool has_no_successor() const { return successors_.empty(); }
54   const std::set<ActivityPtr>& get_dependencies() const { return dependencies_; }
55   const std::vector<ActivityPtr>& get_successors() const { return successors_; }
56
57 protected:
58   Activity()  = default;
59   virtual ~Activity() = default;
60   void destroy();
61
62   void release_dependencies()
63   {
64     while (not successors_.empty()) {
65       ActivityPtr b = successors_.back();
66       XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
67       b->dependencies_.erase(this);
68       if (b->dependencies_solved()) {
69         b->start();
70       }
71       successors_.pop_back();
72     }
73   }
74
75   void add_successor(ActivityPtr a)
76   {
77     if(this == a)
78       throw std::invalid_argument("Cannot be its own successor");
79
80     if (std::any_of(begin(successors_), end(successors_), [a](ActivityPtr const& i) { return i.get() == a.get(); }))
81       throw std::invalid_argument("Dependency already exists");
82
83     successors_.push_back(a);
84     a->dependencies_.insert({this});
85   }
86
87   void remove_successor(ActivityPtr a)
88   {
89     if(this == a)
90       throw std::invalid_argument("Cannot ask to remove itself from successors list");
91
92     auto p =
93         std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i) { return i.get() == a.get(); });
94     if (p == successors_.end())
95       throw std::invalid_argument("Dependency does not exist. Can not be removed.");
96
97     successors_.erase(p);
98     a->dependencies_.erase({this});
99   }
100
101   static std::set<Activity*>* vetoed_activities_;
102
103   /** Set the [remaining] amount of work that this Activity will entail
104    *
105    * It is forbidden to change the amount of work once the Activity is started */
106   Activity* set_remaining(double remains);
107
108   virtual void fire_on_start() const           = 0;
109   virtual void fire_on_this_start() const      = 0;
110   virtual void fire_on_completion() const      = 0;
111   virtual void fire_on_this_completion() const = 0;
112   virtual void fire_on_suspend() const         = 0;
113   virtual void fire_on_this_suspend() const    = 0;
114   virtual void fire_on_resume() const          = 0;
115   virtual void fire_on_this_resume() const     = 0;
116   virtual void fire_on_veto()                  = 0;
117   virtual void fire_on_this_veto()             = 0;
118
119 public:
120   void start()
121   {
122     state_ = State::STARTING;
123     if (dependencies_solved() && is_assigned()) {
124       XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
125       do_start();
126     } else {
127       if (vetoed_activities_ != nullptr)
128         vetoed_activities_->insert(this);
129       fire_on_veto();
130       fire_on_this_veto();
131     }
132   }
133
134   void complete(Activity::State state)
135   {
136     // Ensure that the current activity remains alive until the end of the function, even if its last reference is
137     // released by the on_completion() callbacks.
138     ActivityPtr keepalive(this);
139     state_ = state;
140     fire_on_completion();
141     fire_on_this_completion();
142     if (state == State::FINISHED)
143       release_dependencies();
144   }
145
146   static std::set<Activity*>* get_vetoed_activities() { return vetoed_activities_; }
147   static void set_vetoed_activities(std::set<Activity*>* whereto) { vetoed_activities_ = whereto; }
148
149 #ifndef DOXYGEN
150   Activity(Activity const&) = delete;
151   Activity& operator=(Activity const&) = delete;
152 #endif
153
154   /** Starts a previously created activity.
155    *
156    * This function is optional: you can call wait() even if you didn't call start()
157    */
158   virtual Activity* do_start() = 0;
159   /** Tests whether the given activity is terminated yet. */
160   virtual bool test();
161
162   /** Blocks the current actor until the activity is terminated */
163   Activity* wait() { return wait_for(-1.0); }
164   /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
165    *  Raises: timeout exception.*/
166   Activity* wait_for(double timeout);
167   /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
168    * Raises: timeout exception. */
169   void wait_until(double time_limit);
170
171   /** Cancel that activity */
172   Activity* cancel();
173   /** Retrieve the current state of the activity */
174   Activity::State get_state() const { return state_; }
175   /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
176   const char* get_state_str() const;
177   void set_state(Activity::State state) { state_ = state; }
178
179   /** Blocks the progression of this activity until it gets resumed */
180   virtual Activity* suspend();
181   /** Unblock the progression of this activity if it was suspended previously */
182   virtual Activity* resume();
183   /** Whether or not the progression of this activity is blocked */
184   bool is_suspended() const { return suspended_; }
185
186   virtual const char* get_cname() const       = 0;
187   virtual const std::string& get_name() const = 0;
188
189   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
190   virtual double get_remaining() const;
191
192   double get_start_time() const;
193   double get_finish_time() const;
194   void mark() { marked_ = true; }
195   bool is_marked() const { return marked_; }
196
197   /** Returns the internal implementation of this Activity */
198   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
199
200 #ifndef DOXYGEN
201   static ssize_t deprecated_wait_any_for(const std::vector<ActivityPtr>& activities, double timeout); // XBT_ATTRIB_DEPRECATED_v339
202   XBT_ATTRIB_DEPRECATED_v339("Please use ActivitySet instead") static ssize_t test_any(const std::vector<ActivityPtr>& activities);
203
204   friend void intrusive_ptr_release(Activity* a)
205   {
206     if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
207       std::atomic_thread_fence(std::memory_order_acquire);
208       delete a;
209     }
210   }
211   friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
212 #endif
213   Activity* add_ref()
214   {
215     intrusive_ptr_add_ref(this);
216     return this;
217   }
218   void unref() { intrusive_ptr_release(this); }
219
220 private:
221   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
222   Activity::State state_                   = Activity::State::INITED;
223   double remains_                          = 0;
224   bool suspended_                          = false;
225   bool marked_                             = false;
226   std::vector<ActivityPtr> successors_;
227   std::set<ActivityPtr> dependencies_;
228   std::atomic_int_fast32_t refcount_{0};
229 };
230
231 template <class AnyActivity> class Activity_T : public Activity {
232   std::string name_             = "unnamed";
233   std::string tracing_category_ = "";
234
235   inline static xbt::signal<void(AnyActivity const&)> on_start;
236   xbt::signal<void(AnyActivity const&)> on_this_start;
237   inline static xbt::signal<void(AnyActivity const&)> on_completion;
238   xbt::signal<void(AnyActivity const&)> on_this_completion;
239   inline static xbt::signal<void(AnyActivity const&)> on_suspend;
240   xbt::signal<void(AnyActivity const&)> on_this_suspend;
241   inline static xbt::signal<void(AnyActivity const&)> on_resume;
242   xbt::signal<void(AnyActivity const&)> on_this_resume;
243   inline static xbt::signal<void(AnyActivity&)> on_veto;
244   xbt::signal<void(AnyActivity&)> on_this_veto;
245
246 protected:
247   void fire_on_start() const override { on_start(static_cast<const AnyActivity&>(*this)); }
248   void fire_on_this_start() const override { on_this_start(static_cast<const AnyActivity&>(*this)); }
249   void fire_on_completion() const override { on_completion(static_cast<const AnyActivity&>(*this)); }
250   void fire_on_this_completion() const override { on_this_completion(static_cast<const AnyActivity&>(*this)); }
251   void fire_on_suspend() const override { on_suspend(static_cast<const AnyActivity&>(*this)); }
252   void fire_on_this_suspend() const override { on_this_suspend(static_cast<const AnyActivity&>(*this)); }
253   void fire_on_resume() const override { on_resume(static_cast<const AnyActivity&>(*this)); }
254   void fire_on_this_resume() const override { on_this_resume(static_cast<const AnyActivity&>(*this)); }
255   void fire_on_veto() override { on_veto(static_cast<AnyActivity&>(*this)); }
256   void fire_on_this_veto() override { on_this_veto(static_cast<AnyActivity&>(*this)); }
257
258 public:
259   /*! \static Add a callback fired when any activity starts (no veto) */
260   static void on_start_cb(const std::function<void(AnyActivity const&)>& cb) { on_start.connect(cb); }
261   /*!  Add a callback fired when this specific activity starts (no veto) */
262   void on_this_start_cb(const std::function<void(AnyActivity const&)>& cb) { on_this_start.connect(cb); }
263   /*! \static Add a callback fired when any activity completes (either normally, cancelled or failed) */
264   static void on_completion_cb(const std::function<void(AnyActivity const&)>& cb) { on_completion.connect(cb); }
265   /*! Add a callback fired when this specific activity completes (either normally, cancelled or failed) */
266   void on_this_completion_cb(const std::function<void(AnyActivity const&)>& cb) { on_this_completion.connect(cb); }
267   /*! \static Add a callback fired when any activity is suspended */
268   static void on_suspend_cb(const std::function<void(AnyActivity const&)>& cb) { on_suspend.connect(cb); }
269   /*! Add a callback fired when this specific activity is suspended */
270   void on_this_suspend_cb(const std::function<void(AnyActivity const&)>& cb) { on_this_suspend.connect(cb); }
271   /*! \static Add a callback fired when any activity is resumed after being suspended */
272   static void on_resume_cb(const std::function<void(AnyActivity const&)>& cb) { on_resume.connect(cb); }
273   /*! Add a callback fired when this specific activity is resumed after being suspended */
274   void on_this_resume_cb(const std::function<void(AnyActivity const&)>& cb) { on_this_resume.connect(cb); }
275   /*! \static Add a callback fired each time that any activity fails to start because of a veto (e.g., unsolved
276    *  dependency or no resource assigned) */
277   static void on_veto_cb(const std::function<void(AnyActivity&)>& cb) { on_veto.connect(cb); }
278   /*! Add a callback fired each time that this specific activity fails to start because of a veto (e.g., unsolved
279    *  dependency or no resource assigned) */
280   void on_this_veto_cb(const std::function<void(AnyActivity&)>& cb) { on_this_veto.connect(cb); }
281
282 #ifndef DOXYGEN
283   XBT_ATTRIB_DEPRECATED_v338("Please use on_suspend_cb() instead") static void on_suspended_cb(const std::function<void(Activity const&)>& cb) { on_suspend.connect(cb); }
284   XBT_ATTRIB_DEPRECATED_v338("Please use on_resume_cb() instead") static void on_resumed_cb(const std::function<void(Activity const&)>& cb) { on_resume.connect(cb); }
285
286   XBT_ATTRIB_DEPRECATED_v339("Please use ActivitySet instead") static ssize_t wait_any(const std::vector<ActivityPtr>& activities) { return deprecated_wait_any_for(activities, -1); }
287   XBT_ATTRIB_DEPRECATED_v339("Please use ActivitySet instead") static ssize_t wait_any_for(const std::vector<ActivityPtr>& activities, double timeout) { return deprecated_wait_any_for(activities, timeout); }
288 #endif
289
290   AnyActivity* add_successor(ActivityPtr a)
291   {
292     Activity::add_successor(a);
293     return static_cast<AnyActivity*>(this);
294   }
295   AnyActivity* remove_successor(ActivityPtr a)
296   {
297     Activity::remove_successor(a);
298     return static_cast<AnyActivity*>(this);
299   }
300   AnyActivity* set_name(std::string_view name)
301   {
302     name_ = name;
303     return static_cast<AnyActivity*>(this);
304   }
305   const std::string& get_name() const override { return name_; }
306   const char* get_cname() const override { return name_.c_str(); }
307
308   AnyActivity* set_tracing_category(std::string_view category)
309   {
310     xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
311                "Cannot change the tracing category of an activity after its start");
312     tracing_category_ = category;
313     return static_cast<AnyActivity*>(this);
314   }
315   const std::string& get_tracing_category() const { return tracing_category_; }
316
317   AnyActivity* start()
318   {
319     Activity::start();
320     return static_cast<AnyActivity*>(this);
321   }
322
323   AnyActivity* cancel() { return static_cast<AnyActivity*>(Activity::cancel()); }
324   AnyActivity* wait() { return wait_for(-1.0); }
325   virtual AnyActivity* wait_for(double timeout) {
326     return static_cast<AnyActivity*>(Activity::wait_for(timeout));
327   }
328
329 #ifndef DOXYGEN
330   /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
331    * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
332    * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
333    */
334   friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
335   friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
336 #endif
337 };
338
339 } // namespace s4u
340 } // namespace simgrid
341
342 #endif /* SIMGRID_S4U_ACTIVITY_HPP */