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/Model.hpp"
7 #include "src/kernel/lmm/maxmin.hpp"
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(resource);
15 Model::Model() = default;
16 Model::Model(Model::UpdateAlgo algo) : update_mechanism_(algo) {}
20 delete ready_action_set_;
21 delete running_action_set_;
22 delete failed_action_set_;
23 delete done_action_set_;
24 delete maxmin_system_;
27 Action* Model::actionHeapPop()
29 Action* action = action_heap_.top().second;
31 action->clearHeapHandle();
35 Action::ModifiedSet* Model::get_modified_set() const
37 return maxmin_system_->modified_set_;
40 double Model::next_occuring_event(double now)
42 // FIXME: set the good function once and for all
43 if (update_mechanism_ == Model::UpdateAlgo::Lazy)
44 return next_occuring_event_lazy(now);
45 else if (update_mechanism_ == Model::UpdateAlgo::Full)
46 return next_occuring_event_full(now);
48 xbt_die("Invalid cpu update mechanism!");
51 double Model::next_occuring_event_lazy(double now)
53 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
54 maxmin_system_->lmm_solve();
55 XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
57 while (not maxmin_system_->modified_set_->empty()) {
58 Action* action = &(maxmin_system_->modified_set_->front());
59 maxmin_system_->modified_set_->pop_front();
60 bool max_duration_flag = false;
62 if (action->get_state_set() != running_action_set_)
65 /* bogus priority, skip it */
66 if (action->get_priority() <= 0 || action->get_type() == Action::Type::LATENCY)
69 action->update_remains_lazy(now);
72 double share = action->get_variable()->get_value();
75 double time_to_completion;
76 if (action->get_remains() > 0) {
77 time_to_completion = action->get_remains_no_update() / share;
79 time_to_completion = 0.0;
81 min = now + time_to_completion; // when the task will complete if nothing changes
84 if ((action->get_max_duration() > NO_MAX_DURATION) &&
85 (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
86 // when the task will complete anyway because of the deadline if any
87 min = action->get_start_time() + action->get_max_duration();
88 max_duration_flag = true;
91 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->id_int);
93 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
94 action->get_start_time(), min, share, action->get_max_duration());
97 action->heapUpdate(min, max_duration_flag ? Action::Type::MAX_DURATION : Action::Type::NORMAL);
98 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
103 // hereafter must have already the min value for this resource model
104 if (not actionHeapIsEmpty()) {
105 double min = actionHeapTopDate() - now;
106 XBT_DEBUG("minimum with the HEAP %f", min);
109 XBT_DEBUG("The HEAP is empty, thus returning -1");
114 double Model::next_occuring_event_full(double /*now*/)
116 maxmin_system_->solve();
120 for (Action& action : *get_running_action_set()) {
121 double value = action.get_variable()->get_value();
123 if (action.get_remains() > 0)
124 value = action.get_remains_no_update() / value;
127 if (min < 0 || value < min) {
129 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
132 if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
133 min = action.get_max_duration();
134 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
137 XBT_DEBUG("min value : %f", min);
142 void Model::update_actions_state(double now, double delta)
144 if (update_mechanism_ == Model::UpdateAlgo::Full)
145 update_actions_state_full(now, delta);
146 else if (update_mechanism_ == Model::UpdateAlgo::Lazy)
147 update_actions_state_lazy(now, delta);
149 xbt_die("Invalid cpu update mechanism!");
152 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
157 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
163 } // namespace simgrid
164 } // namespace simgrid