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 const 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 /** @details An action is a consumption on a resource (e.g.: a communication for the network) */
28 class XBT_PUBLIC Action {
30 /* Lazy update needs this Set hook to maintain a list of the tracked actions */
31 boost::intrusive::list_member_hook<> modified_set_hook_;
32 bool isLinkedModifiedSet() const { return modified_set_hook_.is_linked(); }
33 typedef boost::intrusive::list<
34 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modified_set_hook_>>
37 boost::intrusive::list_member_hook<> state_set_hook_;
38 typedef boost::intrusive::list<
39 Action, boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::state_set_hook_>>
43 ready = 0, /**< Ready */
44 running, /**< Running */
45 failed, /**< Task Failure */
46 done, /**< Completed */
47 to_free, /**< Action to free in next cleanup */
48 not_in_the_system /**< Not in the system anymore. Why did you ask ? */
51 enum class SuspendStates {
52 not_suspended = 0, /**< Action currently not suspended **/
57 enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
60 * @brief Action constructor
62 * @param model The Model associated to this Action
63 * @param cost The cost of the Action
64 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
66 Action(Model* model, double cost, bool failed);
69 * @brief Action constructor
71 * @param model The Model associated to this Action
72 * @param cost The cost of the Action
73 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
74 * @param var The lmm variable associated to this Action if it is part of a LMM component
76 Action(Model* model, double cost, bool failed, lmm::Variable* var);
81 * @brief Mark that the action is now finished
83 * @param state the new [state](\ref simgrid::kernel::resource::Action::State) of the current Action
85 void finish(Action::State state);
87 /** @brief Get the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
88 Action::State get_state() const; /**< get the state*/
89 /** @brief Set the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
90 virtual void set_state(Action::State state);
92 /** @brief Get the bound of the current Action */
93 double get_bound() const;
94 /** @brief Set the bound of the current Action */
95 void set_bound(double bound);
97 /** @brief Get the start time of the current action */
98 double get_start_time() const { return start_time_; }
99 /** @brief Get the finish time of the current action */
100 double getFinishTime() const { return finish_time_; }
102 /** @brief Get the user data associated to the current action */
103 void* get_data() const { return data_; }
104 /** @brief Set the user data associated to the current action */
105 void set_data(void* data) { data_ = data; }
107 /** @brief Get the cost of the current action */
108 double getCost() const { return cost_; }
109 /** @brief Set the cost of the current action */
110 void setCost(double cost) { cost_ = cost; }
112 /** @brief Update the maximum duration of the current action
113 * @param delta Amount to remove from the MaxDuration */
114 void updateMaxDuration(double delta);
116 /** @brief Update the remaining time of the current action
117 * @param delta Amount to remove from the remaining time */
118 void updateRemains(double delta);
120 /** @brief Set the remaining time of the current action */
121 void setRemains(double value) { remains_ = value; }
122 /** @brief Get the remaining time of the current action after updating the resource */
123 virtual double getRemains();
124 /** @brief Get the remaining time of the current action without updating the resource */
125 double getRemainsNoUpdate() const { return remains_; }
127 /** @brief Set the finish time of the current action */
128 void setFinishTime(double value) { finish_time_ = value; }
130 /**@brief Add a reference to the current action (refcounting) */
132 /** @brief Unref that action (and destroy it if refcount reaches 0)
133 * @return true if the action was destroyed and false if someone still has references on it
137 /** @brief Cancel the current Action if running */
138 virtual void cancel();
140 /** @brief Suspend the current Action */
141 virtual void suspend();
143 /** @brief Resume the current Action */
144 virtual void resume();
146 /** @brief Returns true if the current action is running */
147 virtual bool isSuspended();
149 /** @brief Get the maximum duration of the current action */
150 double getMaxDuration() const { return max_duration_; }
151 /** @brief Set the maximum duration of the current Action */
152 virtual void setMaxDuration(double duration);
154 /** @brief Get the tracing category associated to the current action */
155 char* getCategory() const { return category_; }
156 /** @brief Set the tracing category of the current Action */
157 void setCategory(const char* category);
159 /** @brief Get the priority of the current Action */
160 double getPriority() const { return sharing_weight_; };
161 /** @brief Set the priority of the current Action */
162 virtual void setSharingWeight(double priority);
163 void setSharingWeightNoUpdate(double weight) { sharing_weight_ = weight; }
165 /** @brief Get the state set in which the action is */
166 StateSet* getStateSet() const { return state_set_; };
168 simgrid::kernel::resource::Model* getModel() const { return model_; }
171 StateSet* state_set_;
175 double sharing_weight_ = 1.0; /**< priority (1.0 by default) */
176 double max_duration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
177 double remains_; /**< How much of that cost remains to be done in the currently running task */
178 double start_time_; /**< start time */
179 char* category_ = nullptr; /**< tracing category for categorized resource utilization monitoring */
180 double finish_time_ =
181 -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
184 simgrid::kernel::resource::Model* model_;
185 void* data_ = nullptr; /**< for your convenience */
188 double last_update_ = 0;
189 double last_value_ = 0;
190 kernel::lmm::Variable* variable_ = nullptr;
191 Action::Type type_ = Action::Type::NOTSET;
192 boost::optional<heap_type::handle_type> heap_handle_ = boost::none;
195 virtual void updateRemainingLazy(double now) = 0;
196 void heapInsert(heap_type& heap, double key, Action::Type hat);
197 void heapRemove(heap_type& heap);
198 void heapUpdate(heap_type& heap, double key, Action::Type hat);
199 void clearHeapHandle() { heap_handle_ = boost::none; }
200 kernel::lmm::Variable* getVariable() const { return variable_; }
201 void setVariable(kernel::lmm::Variable* var) { variable_ = var; }
202 double getLastUpdate() const { return last_update_; }
203 void refreshLastUpdate();
204 double getLastValue() const { return last_value_; }
205 void setLastValue(double val) { last_value_ = val; }
206 Action::Type getType() const { return type_; }
209 Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
212 } // namespace resource
213 } // namespace kernel
214 } // namespace simgrid