1 /* Copyright (c) 2004-2019. 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 int NO_MAX_DURATION = -1.0;
23 typedef std::pair<double, simgrid::kernel::resource::Action*> heap_element_type;
24 typedef boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
25 boost::heap::compare<simgrid::xbt::HeapComparator<heap_element_type>>>
28 typedef std::pair<double, simgrid::kernel::resource::Action*> heap_element_type;
29 class XBT_PUBLIC ActionHeap : public heap_type {
34 latency = 100, /* this is a heap entry to warn us when the latency is payed */
35 max_duration, /* this is a heap entry to warn us when the max_duration limit (timeout) is reached */
36 normal, /* this is a normal heap entry stating the date to finish transmitting */
40 double top_date() const;
41 void insert(Action* action, double date, ActionHeap::Type type);
42 void update(Action* action, double date, ActionHeap::Type type);
43 void remove(Action* action);
47 /** @details An action is a consumption on a resource (e.g.: a communication for the network).
49 * It is related (but still different) from activities, that are the stuff on which an actor can be blocked.
51 * - A sequential execution activity encompasses 2 actions: one for the exec itself,
52 * and a time-limited sleep used as timeout detector.
53 * - A point-to-point communication activity encompasses 3 actions: one for the comm itself
54 * (which spans on all links of the path), and one infinite sleep used as failure detector
55 * on both sender and receiver hosts.
56 * - Synchronization activities may possibly be connected to no action.
59 class XBT_PUBLIC Action {
63 /* Lazy update needs this Set hook to maintain a list of the tracked actions */
64 boost::intrusive::list_member_hook<> modified_set_hook_;
65 bool is_within_modified_set() const { return modified_set_hook_.is_linked(); }
66 typedef boost::intrusive::list<
67 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>
70 boost::intrusive::list_member_hook<> state_set_hook_;
71 typedef boost::intrusive::list<
72 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>
76 INITED, /**< Created, but not started yet */
77 STARTED, /**< Currently running */
78 FAILED, /**< either the resource failed, or the action was canceled */
79 FINISHED, /**< Successfully completed */
80 IGNORED /**< e.g. failure detectors: infinite sleep actions that are put on resources which failure should get
84 enum class SuspendStates {
85 RUNNING = 0, /**< Action currently not suspended **/
91 * @brief Action constructor
93 * @param model The Model associated to this Action
94 * @param cost The cost of the Action
95 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
97 Action(Model* model, double cost, bool failed);
100 * @brief Action constructor
102 * @param model The Model associated to this Action
103 * @param cost The cost of the Action
104 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
105 * @param var The lmm variable associated to this Action if it is part of a LMM component
107 Action(Model* model, double cost, bool failed, lmm::Variable* var);
108 Action(const Action&) = delete;
109 Action& operator=(const Action&) = delete;
114 * @brief Mark that the action is now finished
116 * @param state the new [state](@ref simgrid::kernel::resource::Action::State) of the current Action
118 void finish(Action::State state);
120 /** @brief Get the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
121 Action::State get_state() const; /**< get the state*/
122 /** @brief Set the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
123 virtual void set_state(Action::State state);
125 /** @brief Get the bound of the current Action */
126 double get_bound() const;
127 /** @brief Set the bound of the current Action */
128 void set_bound(double bound);
130 /** @brief Get the start time of the current action */
131 double get_start_time() const { return start_time_; }
132 /** @brief Get the finish time of the current action */
133 double get_finish_time() const { return finish_time_; }
135 /** @brief Get the user data associated to the current action */
136 void* get_data() const { return data_; }
137 /** @brief Set the user data associated to the current action */
138 void set_data(void* data) { data_ = data; }
140 /** @brief Get the user data associated to the current action */
141 activity::ActivityImpl* get_activity() const { return activity_; }
142 /** @brief Set the user data associated to the current action */
143 void set_activity(activity::ActivityImpl* activity) { activity_ = activity; }
145 /** @brief Get the cost of the current action */
146 double get_cost() const { return cost_; }
147 /** @brief Set the cost of the current action */
148 void set_cost(double cost) { cost_ = cost; }
150 /** @brief Update the maximum duration of the current action
151 * @param delta Amount to remove from the MaxDuration */
152 void update_max_duration(double delta);
154 /** @brief Update the remaining time of the current action
155 * @param delta Amount to remove from the remaining time */
156 void update_remains(double delta);
158 virtual void update_remains_lazy(double now) = 0;
160 /** @brief Set the remaining time of the current action */
161 void set_remains(double value) { remains_ = value; }
163 /** @brief Get the remaining time of the current action after updating the resource */
164 virtual double get_remains();
165 /** @brief Get the remaining time of the current action without updating the resource */
166 double get_remains_no_update() const { return remains_; }
168 /** @brief Set the finish time of the current action */
169 void set_finish_time(double value) { finish_time_ = value; }
171 /**@brief Add a reference to the current action (refcounting) */
173 /** @brief Unref that action (and destroy it if refcount reaches 0)
174 * @return true if the action was destroyed and false if someone still has references on it */
177 /** @brief Cancel the current Action if running */
178 virtual void cancel();
180 /** @brief Suspend the current Action */
181 virtual void suspend();
183 /** @brief Resume the current Action */
184 virtual void resume();
186 /** @brief Returns true if the current action is suspended */
187 bool is_suspended() const { return suspended_ == SuspendStates::SUSPENDED; }
188 /** @brief Returns true if the current action is running */
189 bool is_running() const { return suspended_ == SuspendStates::RUNNING; }
191 /** @brief Get the maximum duration of the current action */
192 double get_max_duration() const { return max_duration_; }
193 /** @brief Set the maximum duration of the current Action */
194 virtual void set_max_duration(double duration);
196 /** @brief Get the tracing category associated to the current action */
197 const std::string& get_category() const { return category_; }
198 /** @brief Set the tracing category of the current Action */
199 void set_category(const std::string& category) { category_ = category; }
201 /** @brief Get the sharing_penalty (RTT or 1/thread_count) of the current Action */
202 double get_sharing_penalty() const { return sharing_penalty_; };
203 /** @brief Set the sharing_penalty (RTT or 1/thread_count) of the current Action */
204 virtual void set_sharing_penalty(double sharing_penalty);
205 void set_sharing_penalty_no_update(double sharing_penalty) { sharing_penalty_ = sharing_penalty; }
207 /** @brief Get the state set in which the action is */
208 StateSet* get_state_set() const { return state_set_; };
210 simgrid::kernel::resource::Model* get_model() const { return model_; }
213 StateSet* state_set_;
214 Action::SuspendStates suspended_ = Action::SuspendStates::RUNNING;
216 double sharing_penalty_ = 1.0; /**< priority (1.0 by default) */
217 double max_duration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
218 double remains_; /**< How much of that cost remains to be done in the currently running task */
219 double start_time_; /**< start time */
220 double finish_time_ = -1; /**< finish time (may fluctuate until the task is completed) */
221 std::string category_; /**< tracing category for categorized resource utilization monitoring */
224 simgrid::kernel::resource::Model* model_;
225 void* data_ = nullptr; /**< for your convenience */
226 activity::ActivityImpl* activity_ = nullptr;
229 double last_update_ = 0;
230 double last_value_ = 0;
231 kernel::lmm::Variable* variable_ = nullptr;
233 ActionHeap::Type type_ = ActionHeap::Type::unset;
234 boost::optional<ActionHeap::handle_type> heap_hook_ = boost::none;
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_last_update() const { return last_update_; }
243 void set_last_update();
245 double get_last_value() const { return last_value_; }
246 void set_last_value(double val) { last_value_ = val; }
247 void set_suspend_state(Action::SuspendStates state) { suspended_ = state; }
250 } // namespace resource
251 } // namespace kernel
252 } // namespace simgrid