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 /** @ingroup SURF_interface
28 * @brief SURF action interface class
29 * @details An action is an event generated by a resource (e.g.: a communication for the network)
31 class XBT_PUBLIC Action {
33 boost::intrusive::list_member_hook<> modifiedSetHook_; /* Used by the lazy update to list the actions to track */
34 bool isLinkedModifiedSet() const { return modifiedSetHook_.is_linked(); }
36 typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::modifiedSetHook_>
38 typedef boost::intrusive::list<Action, ActionLmmOptions> ActionLmmList;
40 boost::intrusive::list_member_hook<> stateSetHook_;
41 typedef boost::intrusive::member_hook<Action, boost::intrusive::list_member_hook<>, &Action::stateSetHook_>
43 typedef boost::intrusive::list<Action, ActionOptions> ActionList;
46 ready = 0, /**< Ready */
47 running, /**< Running */
48 failed, /**< Task Failure */
49 done, /**< Completed */
50 to_free, /**< Action to free in next cleanup */
51 not_in_the_system /**< Not in the system anymore. Why did you ask ? */
54 enum class SuspendStates {
55 not_suspended = 0, /**< Action currently not suspended **/
60 enum class Type { LATENCY = 100, MAX_DURATION, NORMAL, NOTSET };
63 * @brief Action constructor
65 * @param model The Model associated to this Action
66 * @param cost The cost of the Action
67 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
69 Action(simgrid::kernel::resource::Model* model, double cost, bool failed);
72 * @brief Action constructor
74 * @param model The Model associated to this Action
75 * @param cost The cost of the Action
76 * @param failed If the action is impossible (e.g.: execute something on a switched off host)
77 * @param var The lmm variable associated to this Action if it is part of a LMM component
79 Action(simgrid::kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var);
84 * @brief Mark that the action is now finished
86 * @param state the new [state](\ref simgrid::kernel::resource::Action::State) of the current Action
88 void finish(Action::State state);
90 /** @brief Get the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
91 Action::State getState() const; /**< get the state*/
92 /** @brief Set the [state](\ref simgrid::kernel::resource::Action::State) of the current Action */
93 virtual void setState(Action::State state);
95 /** @brief Get the bound of the current Action */
96 double getBound() const;
97 /** @brief Set the bound of the current Action */
98 void setBound(double bound);
100 /** @brief Get the start time of the current action */
101 double getStartTime() const { return start_; }
102 /** @brief Get the finish time of the current action */
103 double getFinishTime() const { return finish_time_; }
105 /** @brief Get the user data associated to the current action */
106 void* getData() const { return data_; }
107 /** @brief Set the user data associated to the current action */
108 void setData(void* data) { data_ = data; }
110 /** @brief Get the cost of the current action */
111 double getCost() const { return cost_; }
112 /** @brief Set the cost of the current action */
113 void setCost(double cost) { cost_ = cost; }
115 /** @brief Update the maximum duration of the current action
116 * @param delta Amount to remove from the MaxDuration */
117 void updateMaxDuration(double delta);
119 /** @brief Update the remaining time of the current action
120 * @param delta Amount to remove from the remaining time */
121 void updateRemains(double delta);
123 /** @brief Set the remaining time of the current action */
124 void setRemains(double value) { remains_ = value; }
125 /** @brief Get the remaining time of the current action after updating the resource */
126 virtual double getRemains();
127 /** @brief Get the remaining time of the current action without updating the resource */
128 double getRemainsNoUpdate() const { return remains_; }
130 /** @brief Set the finish time of the current action */
131 void setFinishTime(double value) { finish_time_ = value; }
133 /**@brief Add a reference to the current action (refcounting) */
135 /** @brief Unref that action (and destroy it if refcount reaches 0)
136 * @return true if the action was destroyed and false if someone still has references on it
140 /** @brief Cancel the current Action if running */
141 virtual void cancel();
143 /** @brief Suspend the current Action */
144 virtual void suspend();
146 /** @brief Resume the current Action */
147 virtual void resume();
149 /** @brief Returns true if the current action is running */
150 virtual bool isSuspended();
152 /** @brief Get the maximum duration of the current action */
153 double getMaxDuration() const { return max_duration_; }
154 /** @brief Set the maximum duration of the current Action */
155 virtual void setMaxDuration(double duration);
157 /** @brief Get the tracing category associated to the current action */
158 char* getCategory() const { return category_; }
159 /** @brief Set the tracing category of the current Action */
160 void setCategory(const char* category);
162 /** @brief Get the priority of the current Action */
163 double getPriority() const { return sharing_weight_; };
164 /** @brief Set the priority of the current Action */
165 virtual void setSharingWeight(double priority);
166 void setSharingWeightNoUpdate(double weight) { sharing_weight_ = weight; }
168 /** @brief Get the state set in which the action is */
169 ActionList* getStateSet() const { return state_set_; };
171 simgrid::kernel::resource::Model* getModel() const { return model_; }
174 ActionList* state_set_;
178 double sharing_weight_ = 1.0; /**< priority (1.0 by default) */
179 double max_duration_ = NO_MAX_DURATION; /*< max_duration (may fluctuate until the task is completed) */
180 double remains_; /**< How much of that cost remains to be done in the currently running task */
181 double start_; /**< start time */
182 char* category_ = nullptr; /**< tracing category for categorized resource utilization monitoring */
183 double finish_time_ =
184 -1; /**< finish time : this is modified during the run and fluctuates until the task is completed */
187 simgrid::kernel::resource::Model* model_;
188 void* data_ = nullptr; /**< for your convenience */
191 double last_update_ = 0;
192 double last_value_ = 0;
193 kernel::lmm::Variable* variable_ = nullptr;
194 Action::Type type_ = Action::Type::NOTSET;
195 boost::optional<heap_type::handle_type> heap_handle_ = boost::none;
198 virtual void updateRemainingLazy(double now) = 0;
199 void heapInsert(heap_type& heap, double key, Action::Type hat);
200 void heapRemove(heap_type& heap);
201 void heapUpdate(heap_type& heap, double key, Action::Type hat);
202 void clearHeapHandle() { heap_handle_ = boost::none; }
203 kernel::lmm::Variable* getVariable() const { return variable_; }
204 void setVariable(kernel::lmm::Variable* var) { variable_ = var; }
205 double getLastUpdate() const { return last_update_; }
206 void refreshLastUpdate();
207 double getLastValue() const { return last_value_; }
208 void setLastValue(double val) { last_value_ = val; }
209 Action::Type getType() const { return type_; }
212 Action::SuspendStates suspended_ = Action::SuspendStates::not_suspended;
215 typedef Action::ActionList ActionList;
216 typedef Action::ActionLmmList ActionLmmList;
217 typedef Action::ActionLmmList* ActionLmmListPtr;
218 } // namespace resource
219 } // namespace kernel
220 } // namespace simgrid