1 /* Copyright (c) 2004-2022. 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_KERNEL_RESOURCE_ACTION_HPP
7 #define SIMGRID_KERNEL_RESOURCE_ACTION_HPP
9 #include <simgrid/forward.h>
10 #include <xbt/signal.hpp>
11 #include <xbt/utility.hpp>
13 #include <boost/heap/pairing_heap.hpp>
14 #include <boost/optional.hpp>
17 static constexpr double NO_MAX_DURATION = -1.0;
23 using heap_element_type = std::pair<double, Action*>;
25 boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
26 boost::heap::compare<simgrid::xbt::HeapComparator<heap_element_type>>>;
28 class XBT_PUBLIC ActionHeap : public heap_type {
33 latency = 100, /* this is a heap entry to warn us when the latency is paid */
34 max_duration, /* this is a heap entry to warn us when the max_duration limit (timeout) is reached */
35 normal, /* this is a normal heap entry stating the date to finish transmitting */
39 double top_date() const;
40 void insert(Action* action, double date, ActionHeap::Type type);
41 void update(Action* action, double date, ActionHeap::Type type);
42 void remove(Action* action);
46 /** @details An action is a consumption on a resource (e.g.: a communication for the network).
48 * It is related (but still different) from activities, that are the stuff on which an actor can be blocked.
50 * - A sequential execution activity encompasses 2 actions: one for the exec itself,
51 * and a time-limited sleep used as timeout detector.
52 * - A point-to-point communication activity encompasses 3 actions: one for the comm itself
53 * (which spans on all links of the path), and one infinite sleep used as failure detector
54 * on both sender and receiver hosts.
55 * - Synchronization activities may possibly be connected to no action.
58 class XBT_PUBLIC Action {
62 double sharing_penalty_ = 1.0; /**< priority (1.0 by default) */
63 double max_duration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
64 double remains_; /**< How much of that cost remains to be done in the currently running task */
65 double start_time_; /**< start time */
66 double finish_time_ = -1; /**< finish time (may fluctuate until the task is completed) */
67 std::string category_; /**< tracing category for categorized resource utilization monitoring */
71 void* data_ = nullptr; /**< for your convenience */
72 activity::ActivityImpl* activity_ = nullptr;
75 double factor_ = 1.0; /**< Factor for effective rate = var->get_value() * factor_ */
76 double last_update_ = 0;
77 double last_value_ = 0;
78 lmm::Variable* variable_ = nullptr;
79 double user_bound_ = -1;
81 ActionHeap::Type type_ = ActionHeap::Type::unset;
82 boost::optional<ActionHeap::handle_type> heap_hook_ = boost::none;
83 boost::intrusive::list_member_hook<> modified_set_hook_;
84 boost::intrusive::list_member_hook<> state_set_hook_;
87 /* Lazy update needs this Set hook to maintain a list of the tracked actions */
88 bool is_within_modified_set() const { return modified_set_hook_.is_linked(); }
89 using ModifiedSet = boost::intrusive::list<
90 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>;
92 using StateSet = boost::intrusive::list<
93 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>;
96 INITED, /**< Created, but not started yet */
97 STARTED, /**< Currently running */
98 FAILED, /**< either the resource failed, or the action was canceled */
99 FINISHED, /**< Successfully completed */
100 IGNORED /**< e.g. failure detectors: infinite sleep actions that are put on resources which failure should get
104 enum class SuspendStates {
105 RUNNING = 0, /**< Action currently not suspended **/
111 StateSet* state_set_;
112 Action::SuspendStates suspended_ = Action::SuspendStates::RUNNING;
116 * @brief Action constructor
118 * @param model The Model associated to this Action
119 * @param cost The cost of the Action
120 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
122 Action(Model* model, double cost, bool failed);
125 * @brief Action constructor
127 * @param model The Model associated to this Action
128 * @param cost The cost of the Action
129 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
130 * @param var The lmm variable associated to this Action if it is part of a LMM component
132 Action(Model* model, double cost, bool failed, lmm::Variable* var);
133 Action(const Action&) = delete;
134 Action& operator=(const Action&) = delete;
139 * @brief Mark that the action is now finished
141 * @param state the new [state](@ref simgrid::kernel::resource::Action::State) of the current Action
143 void finish(Action::State state);
145 /** @brief Get the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
146 Action::State get_state() const; /**< get the state*/
147 /** @brief Set the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
148 virtual void set_state(Action::State state);
150 /** @brief Get the bound of the current Action */
151 double get_bound() const;
152 /** @brief Set the bound of the current Action */
153 void set_bound(double bound);
155 /** @brief Get the start time of the current action */
156 double get_start_time() const { return start_time_; }
157 /** @brief Get the finish time of the current action */
158 double get_finish_time() const { return finish_time_; }
160 /** @brief Get the user data associated to the current action */
161 void* get_data() const { return data_; }
162 /** @brief Set the user data associated to the current action */
163 void set_data(void* data) { data_ = data; }
165 /** @brief Get the user data associated to the current action */
166 activity::ActivityImpl* get_activity() const { return activity_; }
167 /** @brief Set the user data associated to the current action */
168 void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
170 /** @brief Get the cost of the current action */
171 double get_cost() const { return cost_; }
172 /** @brief Set the cost of the current action */
173 void set_cost(double cost) { cost_ = cost; }
175 /** @brief Update the maximum duration of the current action
176 * @param delta Amount to remove from the MaxDuration */
177 void update_max_duration(double delta);
179 /** @brief Update the remaining time of the current action
180 * @param delta Amount to remove from the remaining time */
181 void update_remains(double delta);
183 virtual void update_remains_lazy(double now) = 0;
185 /** @brief Set the remaining time of the current action */
186 void set_remains(double value) { remains_ = value; }
188 /** @brief Get the remaining time of the current action after updating the resource */
189 virtual double get_remains();
190 /** @brief Get the remaining time of the current action without updating the resource */
191 double get_remains_no_update() const { return remains_; }
193 /** @brief Set the finish time of the current action */
194 void set_finish_time(double value) { finish_time_ = value; }
196 /**@brief Add a reference to the current action (refcounting) */
198 /** @brief Unref that action (and destroy it if refcount reaches 0)
199 * @return true if the action was destroyed and false if someone still has references on it */
202 /** @brief Cancel the current Action if running */
203 virtual void cancel();
205 /** @brief Suspend the current Action */
206 virtual void suspend();
208 /** @brief Resume the current Action */
209 virtual void resume();
211 /** @brief Returns true if the current action is suspended */
212 bool is_suspended() const { return suspended_ == SuspendStates::SUSPENDED; }
213 /** @brief Returns true if the current action is running */
214 bool is_running() const { return suspended_ == SuspendStates::RUNNING; }
216 /** @brief Get the maximum duration of the current action */
217 double get_max_duration() const { return max_duration_; }
218 /** @brief Set the maximum duration of the current Action */
219 virtual void set_max_duration(double duration);
221 /** @brief Get the tracing category associated to the current action */
222 const std::string& get_category() const { return category_; }
223 /** @brief Set the tracing category of the current Action */
224 void set_category(const std::string& category) { category_ = category; }
226 /** @brief Get the sharing_penalty (RTT or 1/thread_count) of the current Action */
227 double get_sharing_penalty() const { return sharing_penalty_; };
228 /** @brief Set the sharing_penalty (RTT or 1/thread_count) of the current Action */
229 virtual void set_sharing_penalty(double sharing_penalty);
230 void set_sharing_penalty_no_update(double sharing_penalty) { sharing_penalty_ = sharing_penalty; }
232 /** @brief Get the state set in which the action is */
233 StateSet* get_state_set() const { return state_set_; };
235 Model* get_model() const { return model_; }
237 ActionHeap::Type get_type() const { return type_; }
239 lmm::Variable* get_variable() const { return variable_; }
240 void set_variable(lmm::Variable* var) { variable_ = var; }
242 double get_user_bound() const { return user_bound_; }
243 void set_user_bound(double bound) { user_bound_ = bound; }
245 double get_last_update() const { return last_update_; }
246 void set_last_update();
249 * @brief Set a factor for this action
251 * Defines a multiplicative factor for the consumption of the underlying resource.
253 * @param factor Multiplicative factor for this action (e.g. 0.97)
255 void set_rate_factor(double factor) { factor_ = factor; }
257 * @brief Get the effective consumption rate of the resource
259 * The rate is based on the sharing given by the maxmin system underneath.
260 * However, it depends on the factor defined for this action.
262 * So, the effective rate is equal to var->get_value() * factor_
264 double get_rate() const;
265 double get_last_value() const { return last_value_; }
266 void set_last_value(double val) { last_value_ = val; }
267 void set_suspend_state(Action::SuspendStates state) { suspended_ = state; }
270 } // namespace resource
271 } // namespace kernel
272 } // namespace simgrid