1 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
12 #include <simgrid/forward.h>
15 #include <string_view>
17 #include <xbt/Extendable.hpp>
18 #include <xbt/asserts.h>
19 #include <xbt/signal.hpp>
20 #include <xbt/utility.hpp>
22 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
26 extern template class XBT_PUBLIC xbt::Extendable<s4u::Activity>;
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.
35 class XBT_PUBLIC Activity : public xbt::Extendable<Activity> {
40 friend kernel::activity::ActivityImpl;
41 friend std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
42 friend std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename);
43 friend std::vector<ActivityPtr> create_DAG_from_json(const std::string& filename);
47 // enum class State { ... }
48 XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, FAILED, CANCELED, FINISHED);
50 virtual bool is_assigned() const = 0;
51 bool dependencies_solved() const { return dependencies_.empty(); }
52 bool has_no_successor() const { return successors_.empty(); }
53 const std::set<ActivityPtr>& get_dependencies() const { return dependencies_; }
54 const std::vector<ActivityPtr>& get_successors() const { return successors_; }
58 virtual ~Activity() = default;
61 void release_dependencies()
63 while (not successors_.empty()) {
64 ActivityPtr b = successors_.back();
65 XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
66 b->dependencies_.erase(this);
67 if (b->dependencies_solved()) {
70 successors_.pop_back();
74 void add_successor(ActivityPtr a)
77 throw std::invalid_argument("Cannot be its own successor");
79 if (std::any_of(begin(successors_), end(successors_), [a](ActivityPtr const& i) { return i.get() == a.get(); }))
80 throw std::invalid_argument("Dependency already exists");
82 successors_.push_back(a);
83 a->dependencies_.insert({this});
86 void remove_successor(ActivityPtr a)
89 throw std::invalid_argument("Cannot ask to remove itself from successors list");
92 std::find_if(successors_.begin(), successors_.end(), [a](ActivityPtr const& i) { return i.get() == a.get(); });
93 if (p == successors_.end())
94 throw std::invalid_argument("Dependency does not exist. Can not be removed.");
97 a->dependencies_.erase({this});
100 static std::set<Activity*>* vetoed_activities_;
102 /** Set the [remaining] amount of work that this Activity will entail
104 * It is forbidden to change the amount of work once the Activity is started */
105 Activity* set_remaining(double remains);
107 virtual void fire_on_completion() const = 0;
108 virtual void fire_on_this_completion() const = 0;
109 virtual void fire_on_suspend() const = 0;
110 virtual void fire_on_this_suspend() const = 0;
111 virtual void fire_on_resume() const = 0;
112 virtual void fire_on_this_resume() const = 0;
113 virtual void fire_on_veto() const = 0;
114 virtual void fire_on_this_veto() const = 0;
117 XBT_ATTRIB_DEPRECATED_v334("All start() are vetoable now. Please use start() ") void vetoable_start()
123 state_ = State::STARTING;
124 if (dependencies_solved() && is_assigned()) {
125 XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
128 if (vetoed_activities_ != nullptr)
129 vetoed_activities_->insert(this);
135 void complete(Activity::State state)
137 // Ensure that the current activity remains alive until the end of the function, even if its last reference is
138 // released by the on_completion() callbacks.
139 ActivityPtr keepalive(this);
141 fire_on_completion();
142 fire_on_this_completion();
143 if (state == State::FINISHED)
144 release_dependencies();
147 static std::set<Activity*>* get_vetoed_activities() { return vetoed_activities_; }
148 static void set_vetoed_activities(std::set<Activity*>* whereto) { vetoed_activities_ = whereto; }
151 Activity(Activity const&) = delete;
152 Activity& operator=(Activity const&) = delete;
155 /** Starts a previously created activity.
157 * This function is optional: you can call wait() even if you didn't call start()
159 virtual Activity* do_start() = 0;
160 /** Tests whether the given activity is terminated yet. */
162 /*! take a vector s4u::ActivityPtr and return the rank of the first finished one (or -1 if none is done). */
163 static ssize_t test_any(const std::vector<ActivityPtr>& activities);
165 /** Blocks the current actor until the activity is terminated */
166 Activity* wait() { return wait_for(-1.0); }
167 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
168 * Raises: timeout exception.*/
169 Activity* wait_for(double timeout);
170 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
171 * Raises: timeout exception. */
172 void wait_until(double time_limit);
173 /*! take a vector of s4u::ActivityPtr and return when one of them is finished.
174 * The return value is the rank of the first finished ActivityPtr. */
175 static ssize_t wait_any(const std::vector<ActivityPtr>& activities) { return wait_any_for(activities, -1); }
176 /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
177 static ssize_t wait_any_for(const std::vector<ActivityPtr>& activities, double timeout);
179 /** Cancel that activity */
181 /** Retrieve the current state of the activity */
182 Activity::State get_state() const { return state_; }
183 /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
184 const char* get_state_str() const;
185 void set_state(Activity::State state) { state_ = state; }
187 /** Blocks the progression of this activity until it gets resumed */
188 virtual Activity* suspend();
189 /** Unblock the progression of this activity if it was suspended previously */
190 virtual Activity* resume();
191 /** Whether or not the progression of this activity is blocked */
192 bool is_suspended() const { return suspended_; }
194 virtual const char* get_cname() const = 0;
195 virtual const std::string& get_name() const = 0;
197 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
198 virtual double get_remaining() const;
200 double get_start_time() const;
201 double get_finish_time() const;
202 void mark() { marked_ = true; }
203 bool is_marked() const { return marked_; }
205 /** Returns the internal implementation of this Activity */
206 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
209 friend void intrusive_ptr_release(Activity* a)
211 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
212 std::atomic_thread_fence(std::memory_order_acquire);
216 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
220 intrusive_ptr_add_ref(this);
223 void unref() { intrusive_ptr_release(this); }
226 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
227 Activity::State state_ = Activity::State::INITED;
229 bool suspended_ = false;
230 bool marked_ = false;
231 std::vector<ActivityPtr> successors_;
232 std::set<ActivityPtr> dependencies_;
233 std::atomic_int_fast32_t refcount_{0};
236 template <class AnyActivity> class Activity_T : public Activity {
237 std::string name_ = "unnamed";
238 std::string tracing_category_ = "";
241 inline static xbt::signal<void(AnyActivity const&)> on_completion;
242 xbt::signal<void(AnyActivity const&)> on_this_completion;
243 inline static xbt::signal<void(AnyActivity const&)> on_suspend;
244 xbt::signal<void(AnyActivity const&)> on_this_suspend;
245 inline static xbt::signal<void(AnyActivity const&)> on_resume;
246 xbt::signal<void(AnyActivity const&)> on_this_resume;
247 inline static xbt::signal<void(AnyActivity&)> on_veto;
248 xbt::signal<void(AnyActivity&)> on_this_veto;
251 /*! \static Add a callback fired when any activity completes (either normally, cancelled or failed) */
252 static void on_completion_cb(const std::function<void(AnyActivity const&)>& cb) { on_completion.connect(cb); }
253 /*! Add a callback fired when this specific activity completes (either normally, cancelled or failed) */
254 void on_this_completion_cb(const std::function<void(AnyActivity const&)>& cb) { on_this_completion.connect(cb); }
255 /*! \static Add a callback fired when any activity is suspended */
256 static void on_suspend_cb(const std::function<void(AnyActivity const&)>& cb) { on_suspend.connect(cb); }
257 /*! Add a callback fired when this specific activity is suspended */
258 void on_this_suspend_cb(const std::function<void(AnyActivity const&)>& cb) { on_this_suspend.connect(cb); }
259 /*! \static Add a callback fired when any activity is resumed after being suspended */
260 static void on_resume_cb(const std::function<void(AnyActivity const&)>& cb) { on_resume.connect(cb); }
261 /*! Add a callback fired when this specific activity is resumed after being suspended */
262 void on_this_resume_cb(const std::function<void(AnyActivity const&)>& cb) { on_this_resume.connect(cb); }
263 /*! \static Add a callback fired each time that any activity fails to start because of a veto (e.g., unsolved
264 * dependency or no resource assigned) */
265 static void on_veto_cb(const std::function<void(AnyActivity&)>& cb) { on_veto.connect(cb); }
266 /*! Add a callback fired each time that this specific activity fails to start because of a veto (e.g., unsolved
267 * dependency or no resource assigned) */
268 void on_this_veto_cb(const std::function<void(AnyActivity&)>& cb) { on_this_veto.connect(cb); }
270 XBT_ATTRIB_DEPRECATED_v337("Please use on_suspend_cb() instead") static void on_suspended_cb(
271 const std::function<void(Activity const&)>& cb) { on_suspend.connect(cb); }
272 XBT_ATTRIB_DEPRECATED_v337("Please use on_resume_cb() instead") static void on_resumed_cb(
273 const std::function<void(Activity const&)>& cb) { on_resume.connect(cb); }
276 AnyActivity* add_successor(ActivityPtr a)
278 Activity::add_successor(a);
279 return static_cast<AnyActivity*>(this);
281 AnyActivity* remove_successor(ActivityPtr a)
283 Activity::remove_successor(a);
284 return static_cast<AnyActivity*>(this);
286 AnyActivity* set_name(std::string_view name)
289 return static_cast<AnyActivity*>(this);
291 const std::string& get_name() const override { return name_; }
292 const char* get_cname() const override { return name_.c_str(); }
294 AnyActivity* set_tracing_category(std::string_view category)
296 xbt_assert(get_state() == State::INITED || get_state() == State::STARTING,
297 "Cannot change the tracing category of an activity after its start");
298 tracing_category_ = category;
299 return static_cast<AnyActivity*>(this);
301 const std::string& get_tracing_category() const { return tracing_category_; }
303 XBT_ATTRIB_DEPRECATED_v334("Please use Activity::set_data()") AnyActivity* set_user_data(void* data)
306 return static_cast<AnyActivity*>(this);
309 XBT_ATTRIB_DEPRECATED_v334("Please use Activity::get_data<>()") void* get_user_data() const
311 return get_data<void>();
313 XBT_ATTRIB_DEPRECATED_v334("All start() are vetoable now. Please use start() ") AnyActivity* vetoable_start()
320 return static_cast<AnyActivity*>(this);
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));
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.
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)); }
340 } // namespace simgrid
342 #endif /* SIMGRID_S4U_ACTIVITY_HPP */