Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
finish snake_casing resource::Action
[simgrid.git] / src / kernel / resource / Action.cpp
1 /* Copyright (c) 2004-2018. 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
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");
13
14 namespace simgrid {
15 namespace kernel {
16 namespace resource {
17
18 Action::Action(simgrid::kernel::resource::Model* model, double cost, bool failed) : Action(model, cost, failed, nullptr)
19 {
20 }
21
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)
24 {
25   if (failed)
26     state_set_ = get_model()->getFailedActionSet();
27   else
28     state_set_ = get_model()->getRunningActionSet();
29
30   state_set_->push_back(*this);
31 }
32
33 Action::~Action()
34 {
35   xbt_free(category_);
36 }
37
38 void Action::finish(Action::State state)
39 {
40   finish_time_ = surf_get_clock();
41   set_state(state);
42   set_remains(0);
43 }
44
45 Action::State Action::get_state() const
46 {
47   if (state_set_ == model_->getReadyActionSet())
48     return Action::State::ready;
49   if (state_set_ == model_->getRunningActionSet())
50     return Action::State::running;
51   if (state_set_ == model_->getFailedActionSet())
52     return Action::State::failed;
53   if (state_set_ == model_->getDoneActionSet())
54     return Action::State::done;
55   return Action::State::not_in_the_system;
56 }
57
58 void Action::set_state(Action::State state)
59 {
60   simgrid::xbt::intrusive_erase(*state_set_, *this);
61   switch (state) {
62     case Action::State::ready:
63       state_set_ = model_->getReadyActionSet();
64       break;
65     case Action::State::running:
66       state_set_ = model_->getRunningActionSet();
67       break;
68     case Action::State::failed:
69       state_set_ = model_->getFailedActionSet();
70       break;
71     case Action::State::done:
72       state_set_ = model_->getDoneActionSet();
73       break;
74     default:
75       state_set_ = nullptr;
76       break;
77   }
78   if (state_set_)
79     state_set_->push_back(*this);
80 }
81
82 double Action::get_bound() const
83 {
84   return variable_ ? variable_->get_bound() : 0;
85 }
86
87 void Action::set_bound(double bound)
88 {
89   XBT_IN("(%p,%g)", this, bound);
90   if (variable_)
91     get_model()->getMaxminSystem()->update_variable_bound(variable_, bound);
92
93   if (get_model()->getUpdateMechanism() == UM_LAZY && getLastUpdate() != surf_get_clock())
94     heapRemove(get_model()->getActionHeap());
95   XBT_OUT();
96 }
97
98 void Action::set_category(const char* category)
99 {
100   category_ = xbt_strdup(category);
101 }
102
103 void Action::ref()
104 {
105   refcount_++;
106 }
107
108 void Action::set_max_duration(double duration)
109 {
110   max_duration_ = duration;
111   if (get_model()->getUpdateMechanism() == UM_LAZY) // remove action from the heap
112     heapRemove(get_model()->getActionHeap());
113 }
114
115 void Action::set_priority(double weight)
116 {
117   XBT_IN("(%p,%g)", this, weight);
118   sharing_priority_ = weight;
119   get_model()->getMaxminSystem()->update_variable_weight(getVariable(), weight);
120
121   if (get_model()->getUpdateMechanism() == UM_LAZY)
122     heapRemove(get_model()->getActionHeap());
123   XBT_OUT();
124 }
125
126 void Action::cancel()
127 {
128   set_state(Action::State::failed);
129   if (get_model()->getUpdateMechanism() == UM_LAZY) {
130     if (modified_set_hook_.is_linked())
131       simgrid::xbt::intrusive_erase(*get_model()->getModifiedSet(), *this);
132     heapRemove(get_model()->getActionHeap());
133   }
134 }
135
136 int Action::unref()
137 {
138   refcount_--;
139   if (not refcount_) {
140     if (state_set_hook_.is_linked())
141       simgrid::xbt::intrusive_erase(*state_set_, *this);
142     if (getVariable())
143       get_model()->getMaxminSystem()->variable_free(getVariable());
144     if (get_model()->getUpdateMechanism() == UM_LAZY) {
145       /* remove from heap */
146       heapRemove(get_model()->getActionHeap());
147       if (modified_set_hook_.is_linked())
148         simgrid::xbt::intrusive_erase(*get_model()->getModifiedSet(), *this);
149     }
150     delete this;
151     return 1;
152   }
153   return 0;
154 }
155
156 void Action::suspend()
157 {
158   XBT_IN("(%p)", this);
159   if (suspended_ != SuspendStates::sleeping) {
160     get_model()->getMaxminSystem()->update_variable_weight(getVariable(), 0.0);
161     if (get_model()->getUpdateMechanism() == UM_LAZY) {
162       heapRemove(get_model()->getActionHeap());
163       if (get_model()->getUpdateMechanism() == UM_LAZY && state_set_ == get_model()->getRunningActionSet() &&
164           sharing_priority_ > 0) {
165         // If we have a lazy model, we need to update the remaining value accordingly
166         updateRemainingLazy(surf_get_clock());
167       }
168     }
169     suspended_ = SuspendStates::suspended;
170   }
171   XBT_OUT();
172 }
173
174 void Action::resume()
175 {
176   XBT_IN("(%p)", this);
177   if (suspended_ != SuspendStates::sleeping) {
178     get_model()->getMaxminSystem()->update_variable_weight(getVariable(), get_priority());
179     suspended_ = SuspendStates::not_suspended;
180     if (get_model()->getUpdateMechanism() == UM_LAZY)
181       heapRemove(get_model()->getActionHeap());
182   }
183   XBT_OUT();
184 }
185
186 bool Action::isSuspended()
187 {
188   return suspended_ == SuspendStates::suspended;
189 }
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:
192  *
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
196  */
197 void Action::heapInsert(heap_type& heap, double key, Action::Type hat)
198 {
199   type_       = hat;
200   heap_handle_ = heap.emplace(std::make_pair(key, this));
201 }
202
203 void Action::heapRemove(heap_type& heap)
204 {
205   type_ = Action::Type::NOTSET;
206   if (heap_handle_) {
207     heap.erase(*heap_handle_);
208     clearHeapHandle();
209   }
210 }
211
212 void Action::heapUpdate(heap_type& heap, double key, Action::Type hat)
213 {
214   type_ = hat;
215   if (heap_handle_) {
216     heap.update(*heap_handle_, std::make_pair(key, this));
217   } else {
218     heap_handle_ = heap.emplace(std::make_pair(key, this));
219   }
220 }
221
222 double Action::get_remains()
223 {
224   XBT_IN("(%p)", this);
225   /* update remains before return it */
226   if (get_model()->getUpdateMechanism() == UM_LAZY) /* update remains before return it */
227     updateRemainingLazy(surf_get_clock());
228   XBT_OUT();
229   return remains_;
230 }
231
232 void Action::update_max_duration(double delta)
233 {
234   double_update(&max_duration_, delta, sg_surf_precision);
235 }
236 void Action::update_remains(double delta)
237 {
238   double_update(&remains_, delta, sg_maxmin_precision * sg_surf_precision);
239 }
240
241 void Action::refreshLastUpdate()
242 {
243   last_update_ = surf_get_clock();
244 }
245
246 } // namespace surf
247 } // namespace simgrid
248 } // namespace simgrid