Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cf86b5c73a573e215fffead600876f802e68b678
[simgrid.git] / src / kernel / resource / Action.cpp
1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
11
12 XBT_LOG_NEW_CATEGORY(kernel, "Logging specific to the internals of SimGrid");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(resource, kernel, "Logging specific to the resources");
14
15 namespace simgrid {
16 namespace kernel {
17 namespace resource {
18
19 Action::Action(simgrid::kernel::resource::Model* model, double cost, bool failed) : Action(model, cost, failed, nullptr)
20 {
21 }
22
23 Action::Action(simgrid::kernel::resource::Model* model, double cost, bool failed, kernel::lmm::Variable* var)
24     : remains_(cost), start_time_(surf_get_clock()), cost_(cost), model_(model), variable_(var)
25 {
26   if (failed)
27     state_set_ = get_model()->get_failed_action_set();
28   else
29     state_set_ = get_model()->get_started_action_set();
30
31   state_set_->push_back(*this);
32 }
33
34 Action::~Action()
35 {
36   if (state_set_hook_.is_linked())
37     simgrid::xbt::intrusive_erase(*state_set_, *this);
38   if (get_variable())
39     get_model()->get_maxmin_system()->variable_free(get_variable());
40
41   /* remove from heap on need (ie, if selective update) */
42   get_model()->get_action_heap().remove(this);
43   if (modified_set_hook_.is_linked())
44     simgrid::xbt::intrusive_erase(*get_model()->get_modified_set(), *this);
45 }
46
47 void Action::finish(Action::State state)
48 {
49   finish_time_ = surf_get_clock();
50   set_remains(0);
51   set_state(state);
52 }
53
54 Action::State Action::get_state() const
55 {
56   if (state_set_ == model_->get_inited_action_set())
57     return Action::State::INITED;
58   if (state_set_ == model_->get_started_action_set())
59     return Action::State::STARTED;
60   if (state_set_ == model_->get_failed_action_set())
61     return Action::State::FAILED;
62   if (state_set_ == model_->get_finished_action_set())
63     return Action::State::FINISHED;
64   if (state_set_ == model_->get_ignored_action_set())
65     return Action::State::IGNORED;
66   THROW_IMPOSSIBLE;
67 }
68
69 void Action::set_state(Action::State state)
70 {
71   simgrid::xbt::intrusive_erase(*state_set_, *this);
72   switch (state) {
73     case Action::State::INITED:
74       state_set_ = model_->get_inited_action_set();
75       break;
76     case Action::State::STARTED:
77       state_set_ = model_->get_started_action_set();
78       break;
79     case Action::State::FAILED:
80       state_set_ = model_->get_failed_action_set();
81       break;
82     case Action::State::FINISHED:
83       state_set_ = model_->get_finished_action_set();
84       break;
85     case Action::State::IGNORED:
86       state_set_ = model_->get_ignored_action_set();
87       break;
88     default:
89       state_set_ = nullptr;
90       break;
91   }
92   if (state_set_)
93     state_set_->push_back(*this);
94 }
95
96 double Action::get_bound() const
97 {
98   return variable_ ? variable_->get_bound() : 0;
99 }
100
101 void Action::set_bound(double bound)
102 {
103   XBT_IN("(%p,%g)", this, bound);
104   if (variable_)
105     get_model()->get_maxmin_system()->update_variable_bound(variable_, bound);
106
107   if (get_model()->get_update_algorithm() == Model::UpdateAlgo::LAZY && get_last_update() != surf_get_clock())
108     get_model()->get_action_heap().remove(this);
109   XBT_OUT();
110 }
111
112 void Action::ref()
113 {
114   refcount_++;
115 }
116
117 void Action::set_max_duration(double duration)
118 {
119   max_duration_ = duration;
120   if (get_model()->get_update_algorithm() == Model::UpdateAlgo::LAZY) // remove action from the heap
121     get_model()->get_action_heap().remove(this);
122 }
123
124 void Action::set_priority(double weight)
125 {
126   XBT_IN("(%p,%g)", this, weight);
127   sharing_priority_ = weight;
128   get_model()->get_maxmin_system()->update_variable_weight(get_variable(), weight);
129
130   if (get_model()->get_update_algorithm() == Model::UpdateAlgo::LAZY)
131     get_model()->get_action_heap().remove(this);
132   XBT_OUT();
133 }
134
135 void Action::cancel()
136 {
137   set_state(Action::State::FAILED);
138   if (get_model()->get_update_algorithm() == Model::UpdateAlgo::LAZY) {
139     if (modified_set_hook_.is_linked())
140       simgrid::xbt::intrusive_erase(*get_model()->get_modified_set(), *this);
141     get_model()->get_action_heap().remove(this);
142   }
143 }
144
145 bool Action::unref()
146 {
147   refcount_--;
148   if (not refcount_) {
149     delete this;
150     return true;
151   }
152   return false;
153 }
154
155 void Action::suspend()
156 {
157   XBT_IN("(%p)", this);
158   if (suspended_ != SuspendStates::sleeping) {
159     get_model()->get_maxmin_system()->update_variable_weight(get_variable(), 0.0);
160     if (get_model()->get_update_algorithm() == Model::UpdateAlgo::LAZY) {
161       get_model()->get_action_heap().remove(this);
162       if (state_set_ == get_model()->get_started_action_set() && sharing_priority_ > 0) {
163         // If we have a lazy model, we need to update the remaining value accordingly
164         update_remains_lazy(surf_get_clock());
165       }
166     }
167     suspended_ = SuspendStates::suspended;
168   }
169   XBT_OUT();
170 }
171
172 void Action::resume()
173 {
174   XBT_IN("(%p)", this);
175   if (suspended_ != SuspendStates::sleeping) {
176     get_model()->get_maxmin_system()->update_variable_weight(get_variable(), get_priority());
177     suspended_ = SuspendStates::not_suspended;
178     if (get_model()->get_update_algorithm() == Model::UpdateAlgo::LAZY)
179       get_model()->get_action_heap().remove(this);
180   }
181   XBT_OUT();
182 }
183
184 bool Action::is_suspended()
185 {
186   return suspended_ == SuspendStates::suspended;
187 }
188
189 double Action::get_remains()
190 {
191   XBT_IN("(%p)", this);
192   /* update remains before returning it */
193   if (get_model()->get_update_algorithm() == Model::UpdateAlgo::LAZY) /* update remains before return it */
194     update_remains_lazy(surf_get_clock());
195   XBT_OUT();
196   return remains_;
197 }
198
199 void Action::update_max_duration(double delta)
200 {
201   if (max_duration_ != NO_MAX_DURATION)
202     double_update(&max_duration_, delta, sg_surf_precision);
203 }
204 void Action::update_remains(double delta)
205 {
206   double_update(&remains_, delta, sg_maxmin_precision * sg_surf_precision);
207 }
208
209 void Action::set_last_update()
210 {
211   last_update_ = surf_get_clock();
212 }
213
214 double ActionHeap::top_date() const
215 {
216   return top().first;
217 }
218 void ActionHeap::insert(Action* action, double date, ActionHeap::Type type)
219 {
220   action->type_      = type;
221   action->heap_hook_ = emplace(std::make_pair(date, action));
222 }
223 void ActionHeap::remove(Action* action)
224 {
225   action->type_ = ActionHeap::Type::unset;
226   if (action->heap_hook_) {
227     erase(*action->heap_hook_);
228     action->heap_hook_ = boost::none;
229   }
230 }
231 void ActionHeap::update(Action* action, double date, ActionHeap::Type type)
232 {
233   action->type_ = type;
234   if (action->heap_hook_) {
235     heap_type::update(*action->heap_hook_, std::make_pair(date, action));
236   } else {
237     action->heap_hook_ = emplace(std::make_pair(date, action));
238   }
239 }
240 Action* ActionHeap::pop()
241 {
242   Action* action = top().second;
243   heap_type::pop();
244   action->heap_hook_ = boost::none;
245   return action;
246 }
247
248 } // namespace surf
249 } // namespace kernel
250 } // namespace simgrid