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 #include "simgrid/kernel/resource/Action.hpp"
7 #include "simgrid/kernel/resource/Model.hpp"
8 #include "src/kernel/lmm/maxmin.hpp"
9 #include "src/surf/surf_interface.hpp"
11 XBT_LOG_NEW_CATEGORY(kernel, "Logging specific to the internals of SimGrid");
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(resource, kernel, "Logging specific to the resources");
18 Action::Action(simgrid::kernel::resource::Model* model, double cost, bool failed) : Action(model, cost, failed, nullptr)
22 Action::Action(simgrid::kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var)
23 : remains_(cost), start_time_(surf_get_clock()), cost_(cost), model_(model), variable_(var)
26 state_set_ = get_model()->getFailedActionSet();
28 state_set_ = get_model()->getRunningActionSet();
30 state_set_->push_back(*this);
35 if (state_set_hook_.is_linked())
36 simgrid::xbt::intrusive_erase(*state_set_, *this);
38 get_model()->getMaxminSystem()->variable_free(get_variable());
39 if (get_model()->getUpdateMechanism() == UM_LAZY) {
40 /* remove from heap */
41 heapRemove(get_model()->getActionHeap());
42 if (modified_set_hook_.is_linked())
43 simgrid::xbt::intrusive_erase(*get_model()->getModifiedSet(), *this);
49 void Action::finish(Action::State state)
51 finish_time_ = surf_get_clock();
56 Action::State Action::get_state() const
58 if (state_set_ == model_->getReadyActionSet())
59 return Action::State::ready;
60 if (state_set_ == model_->getRunningActionSet())
61 return Action::State::running;
62 if (state_set_ == model_->getFailedActionSet())
63 return Action::State::failed;
64 if (state_set_ == model_->getDoneActionSet())
65 return Action::State::done;
66 return Action::State::not_in_the_system;
69 void Action::set_state(Action::State state)
71 simgrid::xbt::intrusive_erase(*state_set_, *this);
73 case Action::State::ready:
74 state_set_ = model_->getReadyActionSet();
76 case Action::State::running:
77 state_set_ = model_->getRunningActionSet();
79 case Action::State::failed:
80 state_set_ = model_->getFailedActionSet();
82 case Action::State::done:
83 state_set_ = model_->getDoneActionSet();
90 state_set_->push_back(*this);
93 double Action::get_bound() const
95 return variable_ ? variable_->get_bound() : 0;
98 void Action::set_bound(double bound)
100 XBT_IN("(%p,%g)", this, bound);
102 get_model()->getMaxminSystem()->update_variable_bound(variable_, bound);
104 if (get_model()->getUpdateMechanism() == UM_LAZY && get_last_update() != surf_get_clock())
105 heapRemove(get_model()->getActionHeap());
109 void Action::set_category(const char* category)
111 category_ = xbt_strdup(category);
119 void Action::set_max_duration(double duration)
121 max_duration_ = duration;
122 if (get_model()->getUpdateMechanism() == UM_LAZY) // remove action from the heap
123 heapRemove(get_model()->getActionHeap());
126 void Action::set_priority(double weight)
128 XBT_IN("(%p,%g)", this, weight);
129 sharing_priority_ = weight;
130 get_model()->getMaxminSystem()->update_variable_weight(get_variable(), weight);
132 if (get_model()->getUpdateMechanism() == UM_LAZY)
133 heapRemove(get_model()->getActionHeap());
137 void Action::cancel()
139 set_state(Action::State::failed);
140 if (get_model()->getUpdateMechanism() == UM_LAZY) {
141 if (modified_set_hook_.is_linked())
142 simgrid::xbt::intrusive_erase(*get_model()->getModifiedSet(), *this);
143 heapRemove(get_model()->getActionHeap());
157 void Action::suspend()
159 XBT_IN("(%p)", this);
160 if (suspended_ != SuspendStates::sleeping) {
161 get_model()->getMaxminSystem()->update_variable_weight(get_variable(), 0.0);
162 if (get_model()->getUpdateMechanism() == UM_LAZY) {
163 heapRemove(get_model()->getActionHeap());
164 if (state_set_ == get_model()->getRunningActionSet() && sharing_priority_ > 0) {
165 // If we have a lazy model, we need to update the remaining value accordingly
166 update_remains_lazy(surf_get_clock());
169 suspended_ = SuspendStates::suspended;
174 void Action::resume()
176 XBT_IN("(%p)", this);
177 if (suspended_ != SuspendStates::sleeping) {
178 get_model()->getMaxminSystem()->update_variable_weight(get_variable(), get_priority());
179 suspended_ = SuspendStates::not_suspended;
180 if (get_model()->getUpdateMechanism() == UM_LAZY)
181 heapRemove(get_model()->getActionHeap());
186 bool Action::is_suspended()
188 return suspended_ == SuspendStates::suspended;
190 /* insert action on heap using a given key and a hat (heap_action_type)
191 * a hat can be of three types for communications:
193 * NORMAL = this is a normal heap entry stating the date to finish transmitting
194 * LATENCY = this is a heap entry to warn us when the latency is payed
195 * MAX_DURATION =this is a heap entry to warn us when the max_duration limit is reached
197 void Action::heapInsert(heap_type& heap, double key, Action::Type hat)
200 heap_hook_ = heap.emplace(std::make_pair(key, this));
203 void Action::heapRemove(heap_type& heap)
205 type_ = Action::Type::NOTSET;
207 heap.erase(*heap_hook_);
212 void Action::heapUpdate(heap_type& heap, double key, Action::Type hat)
216 heap.update(*heap_hook_, std::make_pair(key, this));
218 heap_hook_ = heap.emplace(std::make_pair(key, this));
222 double Action::get_remains()
224 XBT_IN("(%p)", this);
225 /* update remains before return it */
226 if (get_model()->getUpdateMechanism() == UM_LAZY) /* update remains before return it */
227 update_remains_lazy(surf_get_clock());
232 void Action::update_max_duration(double delta)
234 double_update(&max_duration_, delta, sg_surf_precision);
236 void Action::update_remains(double delta)
238 double_update(&remains_, delta, sg_maxmin_precision * sg_surf_precision);
241 void Action::set_last_update()
243 last_update_ = surf_get_clock();
247 } // namespace simgrid
248 } // namespace simgrid