1 /* Copyright (c) 2004-2023. 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/EngineImpl.hpp"
9 #include "src/kernel/lmm/maxmin.hpp"
10 #include "src/simgrid/math_utils.h"
12 XBT_LOG_NEW_CATEGORY(kernel, "SimGrid internals");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_resource, kernel, "Resources, modeling the platform performance");
15 namespace simgrid::kernel::resource {
17 Action::Action(Model* model, double cost, bool failed) : Action(model, cost, failed, nullptr) {}
19 Action::Action(Model* model, double cost, bool failed, lmm::Variable* var)
20 : remains_(cost), start_time_(EngineImpl::get_clock()), cost_(cost), model_(model), variable_(var)
23 state_set_ = model_->get_failed_action_set();
25 state_set_ = model_->get_started_action_set();
27 state_set_->push_back(*this);
32 if (state_set_hook_.is_linked())
33 xbt::intrusive_erase(*state_set_, *this);
35 model_->get_maxmin_system()->variable_free(get_variable());
37 /* remove from heap on need (ie, if selective update) */
38 model_->get_action_heap().remove(this);
39 if (modified_set_hook_.is_linked())
40 xbt::intrusive_erase(*model_->get_modified_set(), *this);
43 void Action::finish(Action::State state)
45 finish_time_ = EngineImpl::get_clock();
50 Action::State Action::get_state() const
52 if (state_set_ == model_->get_inited_action_set())
53 return Action::State::INITED;
54 if (state_set_ == model_->get_started_action_set())
55 return Action::State::STARTED;
56 if (state_set_ == model_->get_failed_action_set())
57 return Action::State::FAILED;
58 if (state_set_ == model_->get_finished_action_set())
59 return Action::State::FINISHED;
60 if (state_set_ == model_->get_ignored_action_set())
61 return Action::State::IGNORED;
65 void Action::set_state(Action::State state)
67 xbt::intrusive_erase(*state_set_, *this);
69 case Action::State::INITED:
70 state_set_ = model_->get_inited_action_set();
72 case Action::State::STARTED:
73 state_set_ = model_->get_started_action_set();
75 case Action::State::FAILED:
76 state_set_ = model_->get_failed_action_set();
78 case Action::State::FINISHED:
79 state_set_ = model_->get_finished_action_set();
81 case Action::State::IGNORED:
82 state_set_ = model_->get_ignored_action_set();
89 state_set_->push_back(*this);
92 double Action::get_bound() const
94 return variable_ ? variable_->get_bound() : 0;
97 double Action::get_rate() const
99 return variable_ ? variable_->get_value() * factor_ : 0;
102 void Action::set_bound(double bound)
104 XBT_IN("(%p,%g)", this, bound);
106 model_->get_maxmin_system()->update_variable_bound(variable_, bound);
108 if (model_->is_update_lazy() && get_last_update() != EngineImpl::get_clock())
109 model_->get_action_heap().remove(this);
118 void Action::set_max_duration(double duration)
120 max_duration_ = duration;
121 if (model_->is_update_lazy()) // remove action from the heap
122 model_->get_action_heap().remove(this);
125 void Action::set_sharing_penalty(double sharing_penalty)
127 XBT_IN("(%p,%g)", this, sharing_penalty);
128 sharing_penalty_ = sharing_penalty;
129 model_->get_maxmin_system()->update_variable_penalty(get_variable(), sharing_penalty);
130 if (model_->is_update_lazy())
131 model_->get_action_heap().remove(this);
135 void Action::cancel()
137 set_state(Action::State::FAILED);
138 if (model_->is_update_lazy()) {
139 if (modified_set_hook_.is_linked())
140 xbt::intrusive_erase(*model_->get_modified_set(), *this);
141 model_->get_action_heap().remove(this);
155 void Action::suspend()
157 XBT_IN("(%p)", this);
158 if (suspended_ != SuspendStates::SLEEPING) {
159 model_->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
160 if (model_->is_update_lazy()) {
161 model_->get_action_heap().remove(this);
162 if (state_set_ == model_->get_started_action_set() && sharing_penalty_ > 0) {
163 // If we have a lazy model, we need to update the remaining value accordingly
164 update_remains_lazy(EngineImpl::get_clock());
167 suspended_ = SuspendStates::SUSPENDED;
172 void Action::resume()
174 XBT_IN("(%p)", this);
175 if (suspended_ != SuspendStates::SLEEPING) {
176 model_->get_maxmin_system()->update_variable_penalty(get_variable(), get_sharing_penalty());
177 suspended_ = SuspendStates::RUNNING;
178 if (model_->is_update_lazy())
179 model_->get_action_heap().remove(this);
184 double Action::get_remains()
186 XBT_IN("(%p)", this);
187 /* update remains before returning it */
188 if (model_->is_update_lazy()) /* update remains before return it */
189 update_remains_lazy(EngineImpl::get_clock());
194 void Action::update_max_duration(double delta)
196 if (max_duration_ != NO_MAX_DURATION)
197 double_update(&max_duration_, delta, sg_precision_timing);
200 void Action::update_remains(double delta)
202 double_update(&remains_, delta, sg_precision_workamount * sg_precision_timing);
205 void Action::set_last_update()
207 last_update_ = EngineImpl::get_clock();
210 double ActionHeap::top_date() const
215 void ActionHeap::insert(Action* action, double date, ActionHeap::Type type)
217 action->type_ = type;
218 action->heap_hook_ = emplace(std::make_pair(date, action));
221 void ActionHeap::remove(Action* action)
223 action->type_ = ActionHeap::Type::unset;
224 if (action->heap_hook_) {
225 erase(*action->heap_hook_);
226 action->heap_hook_ = boost::none;
230 void ActionHeap::update(Action* action, double date, ActionHeap::Type type)
232 action->type_ = type;
233 if (action->heap_hook_) {
234 heap_type::update(*action->heap_hook_, std::make_pair(date, action));
236 action->heap_hook_ = emplace(std::make_pair(date, action));
240 Action* ActionHeap::pop()
242 Action* action = top().second;
244 action->heap_hook_ = boost::none;
248 } // namespace simgrid::kernel::resource