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>
16 static constexpr int NO_MAX_DURATION = -1.0;
22 typedef std::pair<double, simgrid::kernel::resource::Action*> heap_element_type;
23 typedef boost::heap::pairing_heap<heap_element_type, boost::heap::constant_time_size<false>, boost::heap::stable<true>,
24 boost::heap::compare<simgrid::xbt::HeapComparator<heap_element_type>>>
27 typedef std::pair<double, simgrid::kernel::resource::Action*> 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 payed */
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);
44 bool empty() const { return heap_type::empty(); }
47 /** @details An action is a consumption on a resource (e.g.: a communication for the network) */
48 class XBT_PUBLIC Action {
52 /* Lazy update needs this Set hook to maintain a list of the tracked actions */
53 boost::intrusive::list_member_hook<> modified_set_hook_;
54 bool is_within_modified_set() const { return modified_set_hook_.is_linked(); }
55 typedef boost::intrusive::list<
56 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>
59 boost::intrusive::list_member_hook<> state_set_hook_;
60 typedef boost::intrusive::list<
61 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>
65 ready = 0, /**< Ready */
66 running, /**< Running */
67 failed, /**< Task Failure */
68 done, /**< Completed */
69 to_free, /**< Action to free in next cleanup */
70 not_in_the_system /**< Not in the system anymore. Why did you ask ? */
73 enum class SuspendStates {
74 not_suspended = 0, /**< Action currently not suspended **/
80 * @brief Action constructor
82 * @param model The Model associated to this Action
83 * @param cost The cost of the Action
84 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
86 Action(Model* model, double cost, bool failed);
89 * @brief Action constructor
91 * @param model The Model associated to this Action
92 * @param cost The cost of the Action
93 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
94 * @param var The lmm variable associated to this Action if it is part of a LMM component
96 Action(Model* model, double cost, bool failed, lmm::Variable* var);
101 * @brief Mark that the action is now finished
103 * @param state the new [state](\ref simgrid::kernel::resource::Action::State) of the current Action
105 void finish(Action::State state);
107 /** @brief Get the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
108 Action::State get_state() const; /**< get the state*/
109 /** @brief Set the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
110 virtual void set_state(Action::State state);
112 /** @brief Get the bound of the current Action */
113 double get_bound() const;
114 /** @brief Set the bound of the current Action */
115 void set_bound(double bound);
117 /** @brief Get the start time of the current action */
118 double get_start_time() const { return start_time_; }
119 /** @brief Get the finish time of the current action */
120 double get_finish_time() const { return finish_time_; }
122 /** @brief Get the user data associated to the current action */
123 void* get_data() const { return data_; }
124 /** @brief Set the user data associated to the current action */
125 void set_data(void* data) { data_ = data; }
127 /** @brief Get the cost of the current action */
128 double get_cost() const { return cost_; }
129 /** @brief Set the cost of the current action */
130 void set_cost(double cost) { cost_ = cost; }
132 /** @brief Update the maximum duration of the current action
133 * @param delta Amount to remove from the MaxDuration */
134 void update_max_duration(double delta);
136 /** @brief Update the remaining time of the current action
137 * @param delta Amount to remove from the remaining time */
138 void update_remains(double delta);
140 virtual void update_remains_lazy(double now) = 0;
142 /** @brief Set the remaining time of the current action */
143 void set_remains(double value) { remains_ = value; }
145 /** @brief Get the remaining time of the current action after updating the resource */
146 virtual double get_remains();
147 /** @brief Get the remaining time of the current action without updating the resource */
148 double get_remains_no_update() const { return remains_; }
150 /** @brief Set the finish time of the current action */
151 void set_finish_time(double value) { finish_time_ = value; }
153 /**@brief Add a reference to the current action (refcounting) */
155 /** @brief Unref that action (and destroy it if refcount reaches 0)
156 * @return true if the action was destroyed and false if someone still has references on it */
159 /** @brief Cancel the current Action if running */
160 virtual void cancel();
162 /** @brief Suspend the current Action */
163 virtual void suspend();
165 /** @brief Resume the current Action */
166 virtual void resume();
168 /** @brief Returns true if the current action is running */
171 /** @brief Get the maximum duration of the current action */
172 double get_max_duration() const { return max_duration_; }
173 /** @brief Set the maximum duration of the current Action */
174 virtual void set_max_duration(double duration);
176 /** @brief Get the tracing category associated to the current action */
177 char* get_category() const { return category_; }
178 /** @brief Set the tracing category of the current Action */
179 void set_category(const char* category);
181 /** @brief Get the priority of the current Action */
182 double get_priority() const { return sharing_priority_; };
183 /** @brief Set the priority of the current Action */
184 virtual void set_priority(double priority);
185 void set_priority_no_update(double priority) { sharing_priority_ = priority; }
187 /** @brief Get the state set in which the action is */
188 StateSet* get_state_set() const { return state_set_; };
190 simgrid::kernel::resource::Model* get_model() const { return model_; }
193 StateSet* state_set_;
197 double sharing_priority_ = 1.0; /**< priority (1.0 by default) */
198 double max_duration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
199 double remains_; /**< How much of that cost remains to be done in the currently running task */
200 double start_time_; /**< start time */
201 double finish_time_ = -1; /**< finish time (may fluctuate until the task is completed) */
202 char* category_ = nullptr; /**< tracing category for categorized resource utilization monitoring */
205 simgrid::kernel::resource::Model* model_;
206 void* data_ = nullptr; /**< for your convenience */
209 double last_update_ = 0;
210 double last_value_ = 0;
211 kernel::lmm::Variable* variable_ = nullptr;
213 ActionHeap::Type type_ = ActionHeap::Type::unset;
214 boost::optional<ActionHeap::handle_type> heap_hook_ = boost::none;
217 ActionHeap::Type get_type() const { return type_; }
219 lmm::Variable* get_variable() const { return variable_; }
220 void set_variable(lmm::Variable* var) { variable_ = var; }
222 double get_last_update() const { return last_update_; }
223 void set_last_update();
225 double get_last_value() const { return last_value_; }
226 void set_last_value(double val) { last_value_ = val; }
229 Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
232 } // namespace resource
233 } // namespace kernel
234 } // namespace simgrid