1 /* Copyright (c) 2004-2021. 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"
10 #include "surf/surf.hpp"
12 XBT_LOG_NEW_CATEGORY(kernel, "SimGrid internals");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_resource, kernel, "Resources, modeling the platform performance");
19 Action::Action(Model* model, double cost, bool failed) : Action(model, cost, failed, nullptr) {}
21 Action::Action(Model* model, double cost, bool failed, lmm::Variable* var)
22 : remains_(cost), start_time_(surf_get_clock()), cost_(cost), model_(model), variable_(var)
25 state_set_ = model_->get_failed_action_set();
27 state_set_ = model_->get_started_action_set();
29 state_set_->push_back(*this);
34 if (state_set_hook_.is_linked())
35 xbt::intrusive_erase(*state_set_, *this);
37 model_->get_maxmin_system()->variable_free(get_variable());
39 /* remove from heap on need (ie, if selective update) */
40 model_->get_action_heap().remove(this);
41 if (modified_set_hook_.is_linked())
42 xbt::intrusive_erase(*model_->get_modified_set(), *this);
45 void Action::finish(Action::State state)
47 finish_time_ = surf_get_clock();
52 Action::State Action::get_state() const
54 if (state_set_ == model_->get_inited_action_set())
55 return Action::State::INITED;
56 if (state_set_ == model_->get_started_action_set())
57 return Action::State::STARTED;
58 if (state_set_ == model_->get_failed_action_set())
59 return Action::State::FAILED;
60 if (state_set_ == model_->get_finished_action_set())
61 return Action::State::FINISHED;
62 if (state_set_ == model_->get_ignored_action_set())
63 return Action::State::IGNORED;
67 void Action::set_state(Action::State state)
69 xbt::intrusive_erase(*state_set_, *this);
71 case Action::State::INITED:
72 state_set_ = model_->get_inited_action_set();
74 case Action::State::STARTED:
75 state_set_ = model_->get_started_action_set();
77 case Action::State::FAILED:
78 state_set_ = model_->get_failed_action_set();
80 case Action::State::FINISHED:
81 state_set_ = model_->get_finished_action_set();
83 case Action::State::IGNORED:
84 state_set_ = model_->get_ignored_action_set();
91 state_set_->push_back(*this);
94 double Action::get_bound() const
96 return variable_ ? variable_->get_bound() : 0;
99 double Action::get_rate() const
101 return variable_ ? variable_->get_value() * factor_ : 0;
104 void Action::set_bound(double bound)
106 XBT_IN("(%p,%g)", this, bound);
108 model_->get_maxmin_system()->update_variable_bound(variable_, bound);
110 if (model_->is_update_lazy() && get_last_update() != surf_get_clock())
111 model_->get_action_heap().remove(this);
120 void Action::set_max_duration(double duration)
122 max_duration_ = duration;
123 if (model_->is_update_lazy()) // remove action from the heap
124 model_->get_action_heap().remove(this);
127 void Action::set_sharing_penalty(double sharing_penalty)
129 XBT_IN("(%p,%g)", this, sharing_penalty);
130 sharing_penalty_ = sharing_penalty;
131 model_->get_maxmin_system()->update_variable_penalty(get_variable(), sharing_penalty);
133 if (model_->is_update_lazy())
134 model_->get_action_heap().remove(this);
138 void Action::cancel()
140 set_state(Action::State::FAILED);
141 if (model_->is_update_lazy()) {
142 if (modified_set_hook_.is_linked())
143 xbt::intrusive_erase(*model_->get_modified_set(), *this);
144 model_->get_action_heap().remove(this);
158 void Action::suspend()
160 XBT_IN("(%p)", this);
161 if (suspended_ != SuspendStates::SLEEPING) {
162 model_->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
163 if (model_->is_update_lazy()) {
164 model_->get_action_heap().remove(this);
165 if (state_set_ == model_->get_started_action_set() && sharing_penalty_ > 0) {
166 // If we have a lazy model, we need to update the remaining value accordingly
167 update_remains_lazy(surf_get_clock());
170 suspended_ = SuspendStates::SUSPENDED;
175 void Action::resume()
177 XBT_IN("(%p)", this);
178 if (suspended_ != SuspendStates::SLEEPING) {
179 model_->get_maxmin_system()->update_variable_penalty(get_variable(), get_sharing_penalty());
180 suspended_ = SuspendStates::RUNNING;
181 if (model_->is_update_lazy())
182 model_->get_action_heap().remove(this);
187 double Action::get_remains()
189 XBT_IN("(%p)", this);
190 /* update remains before returning it */
191 if (model_->is_update_lazy()) /* update remains before return it */
192 update_remains_lazy(surf_get_clock());
197 void Action::update_max_duration(double delta)
199 if (max_duration_ != NO_MAX_DURATION)
200 double_update(&max_duration_, delta, sg_surf_precision);
203 void Action::update_remains(double delta)
205 double_update(&remains_, delta, sg_maxmin_precision * sg_surf_precision);
208 void Action::set_last_update()
210 last_update_ = surf_get_clock();
213 double ActionHeap::top_date() const
218 void ActionHeap::insert(Action* action, double date, ActionHeap::Type type)
220 action->type_ = type;
221 action->heap_hook_ = emplace(std::make_pair(date, action));
224 void ActionHeap::remove(Action* action)
226 action->type_ = ActionHeap::Type::unset;
227 if (action->heap_hook_) {
228 erase(*action->heap_hook_);
229 action->heap_hook_ = boost::none;
233 void ActionHeap::update(Action* action, double date, ActionHeap::Type type)
235 action->type_ = type;
236 if (action->heap_hook_) {
237 heap_type::update(*action->heap_hook_, std::make_pair(date, action));
239 action->heap_hook_ = emplace(std::make_pair(date, action));
243 Action* ActionHeap::pop()
245 Action* action = top().second;
247 action->heap_hook_ = boost::none;
251 } // namespace resource
252 } // namespace kernel
253 } // namespace simgrid