1 /* Copyright (c) 2004-2019. 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(Model::UpdateAlgo algo) : update_algorithm_(algo) {}
19 delete inited_action_set_;
20 delete started_action_set_;
21 delete failed_action_set_;
22 delete finished_action_set_;
23 delete ignored_action_set_;
24 delete maxmin_system_;
27 Action::ModifiedSet* Model::get_modified_set() const
29 return maxmin_system_->modified_set_;
32 double Model::next_occuring_event(double now)
34 // FIXME: set the good function once and for all
35 if (update_algorithm_ == Model::UpdateAlgo::LAZY)
36 return next_occuring_event_lazy(now);
37 else if (update_algorithm_ == Model::UpdateAlgo::FULL)
38 return next_occuring_event_full(now);
40 xbt_die("Invalid cpu update mechanism!");
43 double Model::next_occuring_event_lazy(double now)
45 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
46 maxmin_system_->lmm_solve();
47 XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
49 while (not maxmin_system_->modified_set_->empty()) {
50 Action* action = &(maxmin_system_->modified_set_->front());
51 maxmin_system_->modified_set_->pop_front();
52 bool max_duration_flag = false;
54 if (action->get_state_set() != started_action_set_)
57 /* bogus priority, skip it */
58 if (action->get_priority() <= 0 || action->get_type() == ActionHeap::Type::latency)
61 action->update_remains_lazy(now);
64 double share = action->get_variable()->get_value();
67 double time_to_completion;
68 if (action->get_remains() > 0) {
69 time_to_completion = action->get_remains_no_update() / share;
71 time_to_completion = 0.0;
73 min = now + time_to_completion; // when the task will complete if nothing changes
76 if ((action->get_max_duration() != NO_MAX_DURATION) &&
77 (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
78 // when the task will complete anyway because of the deadline if any
79 min = action->get_start_time() + action->get_max_duration();
80 max_duration_flag = true;
83 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->id_int);
85 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
86 action->get_start_time(), min, share, action->get_max_duration());
89 action_heap_.update(action, min, max_duration_flag ? ActionHeap::Type::max_duration : ActionHeap::Type::normal);
90 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
95 // hereafter must have already the min value for this resource model
96 if (not action_heap_.empty()) {
97 double min = action_heap_.top_date() - now;
98 XBT_DEBUG("minimum with the HEAP %f", min);
101 XBT_DEBUG("The HEAP is empty, thus returning -1");
106 double Model::next_occuring_event_full(double /*now*/)
108 maxmin_system_->solve();
112 for (Action& action : *get_started_action_set()) {
113 double value = action.get_variable()->get_value();
115 if (action.get_remains() > 0)
116 value = action.get_remains_no_update() / value;
119 if (min < 0 || value < min) {
121 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
124 if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
125 min = action.get_max_duration();
126 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
129 XBT_DEBUG("min value : %f", min);
134 void Model::update_actions_state(double now, double delta)
136 if (update_algorithm_ == Model::UpdateAlgo::FULL)
137 update_actions_state_full(now, delta);
138 else if (update_algorithm_ == Model::UpdateAlgo::LAZY)
139 update_actions_state_lazy(now, delta);
141 xbt_die("Invalid cpu update mechanism!");
144 /** Pops and returns the first action of that state set (or nullptr if none exist) */
145 Action* Model::extract_action(Action::StateSet* list)
149 simgrid::kernel::resource::Action* res = &list->front();
154 /** Pops and returns the first finished action (or nullptr if none exist) */
155 Action* Model::extract_done_action()
157 return extract_action(get_finished_action_set());
160 /** Pops and returns the failed finished action (or nullptr if none exist) */
161 Action* Model::extract_failed_action()
163 return extract_action(get_failed_action_set());
166 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
171 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
177 } // namespace kernel
178 } // namespace simgrid