1 /* Copyright (c) 2004-2018. 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);
45 bool empty() const { return heap_type::empty(); }
48 /** @details An action is a consumption on a resource (e.g.: a communication for the network).
50 * It is related (but still different) from activities, that are the stuff on which an actor can be blocked.
51 * See simgrid::s4u::Activity for more details.
53 class XBT_PUBLIC Action {
57 /* Lazy update needs this Set hook to maintain a list of the tracked actions */
58 boost::intrusive::list_member_hook<> modified_set_hook_;
59 bool is_within_modified_set() const { return modified_set_hook_.is_linked(); }
60 typedef boost::intrusive::list<
61 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>
64 boost::intrusive::list_member_hook<> state_set_hook_;
65 typedef boost::intrusive::list<
66 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>
70 INITED, /**< Created, but not started yet */
71 STARTED, /**< Currently running */
72 FAILED, /**< either the resource failed, or the action was canceled */
73 FINISHED, /**< Successfully completed */
74 IGNORED /**< e.g. failure detectors: infinite sleep actions that are put on resources which failure should get
78 enum class SuspendStates {
79 not_suspended = 0, /**< Action currently not suspended **/
85 * @brief Action constructor
87 * @param model The Model associated to this Action
88 * @param cost The cost of the Action
89 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
91 Action(Model* model, double cost, bool failed);
94 * @brief Action constructor
96 * @param model The Model associated to this Action
97 * @param cost The cost of the Action
98 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
99 * @param var The lmm variable associated to this Action if it is part of a LMM component
101 Action(Model* model, double cost, bool failed, lmm::Variable* var);
106 * @brief Mark that the action is now finished
108 * @param state the new [state](@ref simgrid::kernel::resource::Action::State) of the current Action
110 void finish(Action::State state);
112 /** @brief Get the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
113 Action::State get_state() const; /**< get the state*/
114 /** @brief Set the [state](@ref simgrid::kernel::resource::Action::State) of the current Action */
115 virtual void set_state(Action::State state);
117 /** @brief Get the bound of the current Action */
118 double get_bound() const;
119 /** @brief Set the bound of the current Action */
120 void set_bound(double bound);
122 /** @brief Get the start time of the current action */
123 double get_start_time() const { return start_time_; }
124 /** @brief Get the finish time of the current action */
125 double get_finish_time() const { return finish_time_; }
127 /** @brief Get the user data associated to the current action */
128 void* get_data() const { return data_; }
129 /** @brief Set the user data associated to the current action */
130 void set_data(void* data) { data_ = data; }
132 /** @brief Get the cost of the current action */
133 double get_cost() const { return cost_; }
134 /** @brief Set the cost of the current action */
135 void set_cost(double cost) { cost_ = cost; }
137 /** @brief Update the maximum duration of the current action
138 * @param delta Amount to remove from the MaxDuration */
139 void update_max_duration(double delta);
141 /** @brief Update the remaining time of the current action
142 * @param delta Amount to remove from the remaining time */
143 void update_remains(double delta);
145 virtual void update_remains_lazy(double now) = 0;
147 /** @brief Set the remaining time of the current action */
148 void set_remains(double value) { remains_ = value; }
150 /** @brief Get the remaining time of the current action after updating the resource */
151 virtual double get_remains();
152 /** @brief Get the remaining time of the current action without updating the resource */
153 double get_remains_no_update() const { return remains_; }
155 /** @brief Set the finish time of the current action */
156 void set_finish_time(double value) { finish_time_ = value; }
158 /**@brief Add a reference to the current action (refcounting) */
160 /** @brief Unref that action (and destroy it if refcount reaches 0)
161 * @return true if the action was destroyed and false if someone still has references on it */
164 /** @brief Cancel the current Action if running */
165 virtual void cancel();
167 /** @brief Suspend the current Action */
168 virtual void suspend();
170 /** @brief Resume the current Action */
171 virtual void resume();
173 /** @brief Returns true if the current action is running */
176 /** @brief Get the maximum duration of the current action */
177 double get_max_duration() const { return max_duration_; }
178 /** @brief Set the maximum duration of the current Action */
179 virtual void set_max_duration(double duration);
181 /** @brief Get the tracing category associated to the current action */
182 std::string get_category() const { return category_; }
183 /** @brief Set the tracing category of the current Action */
184 void set_category(std::string category) { category_ = category; }
186 /** @brief Get the priority of the current Action */
187 double get_priority() const { return sharing_priority_; };
188 /** @brief Set the priority of the current Action */
189 virtual void set_priority(double priority);
190 void set_priority_no_update(double priority) { sharing_priority_ = priority; }
192 /** @brief Get the state set in which the action is */
193 StateSet* get_state_set() const { return state_set_; };
195 simgrid::kernel::resource::Model* get_model() const { return model_; }
198 StateSet* state_set_;
202 double sharing_priority_ = 1.0; /**< priority (1.0 by default) */
203 double max_duration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
204 double remains_; /**< How much of that cost remains to be done in the currently running task */
205 double start_time_; /**< start time */
206 double finish_time_ = -1; /**< finish time (may fluctuate until the task is completed) */
207 std::string category_; /**< tracing category for categorized resource utilization monitoring */
210 simgrid::kernel::resource::Model* model_;
211 void* data_ = nullptr; /**< for your convenience */
214 double last_update_ = 0;
215 double last_value_ = 0;
216 kernel::lmm::Variable* variable_ = nullptr;
218 ActionHeap::Type type_ = ActionHeap::Type::unset;
219 boost::optional<ActionHeap::handle_type> heap_hook_ = boost::none;
222 ActionHeap::Type get_type() const { return type_; }
224 lmm::Variable* get_variable() const { return variable_; }
225 void set_variable(lmm::Variable* var) { variable_ = var; }
227 double get_last_update() const { return last_update_; }
228 void set_last_update();
230 double get_last_value() const { return last_value_; }
231 void set_last_value(double val) { last_value_ = val; }
234 Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
237 } // namespace resource
238 } // namespace kernel
239 } // namespace simgrid