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) {}
17 Action::ModifiedSet* Model::get_modified_set() const
19 return maxmin_system_->modified_set_;
22 void Model::set_maxmin_system(lmm::System* system)
24 maxmin_system_.release(); // ugly...
25 maxmin_system_.reset(system);
28 double Model::next_occuring_event(double now)
30 // FIXME: set the good function once and for all
31 if (update_algorithm_ == Model::UpdateAlgo::LAZY)
32 return next_occuring_event_lazy(now);
33 else if (update_algorithm_ == Model::UpdateAlgo::FULL)
34 return next_occuring_event_full(now);
36 xbt_die("Invalid cpu update mechanism!");
39 double Model::next_occuring_event_lazy(double now)
41 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", maxmin_system_->modified_set_->size());
42 maxmin_system_->lmm_solve();
43 XBT_DEBUG("After share resources, The size of modified actions set is %zu", maxmin_system_->modified_set_->size());
45 while (not maxmin_system_->modified_set_->empty()) {
46 Action* action = &(maxmin_system_->modified_set_->front());
47 maxmin_system_->modified_set_->pop_front();
48 bool max_duration_flag = false;
50 if (action->get_state_set() != &started_action_set_)
53 /* bogus priority, skip it */
54 if (action->get_priority() <= 0 || action->get_type() == ActionHeap::Type::latency)
57 action->update_remains_lazy(now);
60 double share = action->get_variable()->get_value();
63 double time_to_completion;
64 if (action->get_remains() > 0) {
65 time_to_completion = action->get_remains_no_update() / share;
67 time_to_completion = 0.0;
69 min = now + time_to_completion; // when the task will complete if nothing changes
72 if ((action->get_max_duration() != NO_MAX_DURATION) &&
73 (min <= -1 || action->get_start_time() + action->get_max_duration() < min)) {
74 // when the task will complete anyway because of the deadline if any
75 min = action->get_start_time() + action->get_max_duration();
76 max_duration_flag = true;
79 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->get_variable()->id_int);
81 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
82 action->get_start_time(), min, share, action->get_max_duration());
85 action_heap_.update(action, min, max_duration_flag ? ActionHeap::Type::max_duration : ActionHeap::Type::normal);
86 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
91 // hereafter must have already the min value for this resource model
92 if (not action_heap_.empty()) {
93 double min = action_heap_.top_date() - now;
94 XBT_DEBUG("minimum with the HEAP %f", min);
97 XBT_DEBUG("The HEAP is empty, thus returning -1");
102 double Model::next_occuring_event_full(double /*now*/)
104 maxmin_system_->solve();
108 for (Action& action : *get_started_action_set()) {
109 double value = action.get_variable()->get_value();
111 if (action.get_remains() > 0)
112 value = action.get_remains_no_update() / value;
115 if (min < 0 || value < min) {
117 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
120 if ((action.get_max_duration() >= 0) && (min < 0 || action.get_max_duration() < min)) {
121 min = action.get_max_duration();
122 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
125 XBT_DEBUG("min value : %f", min);
130 void Model::update_actions_state(double now, double delta)
132 if (update_algorithm_ == Model::UpdateAlgo::FULL)
133 update_actions_state_full(now, delta);
134 else if (update_algorithm_ == Model::UpdateAlgo::LAZY)
135 update_actions_state_lazy(now, delta);
137 xbt_die("Invalid cpu update mechanism!");
140 /** Pops and returns the first action of that state set (or nullptr if none exist) */
141 Action* Model::extract_action(Action::StateSet* list)
145 simgrid::kernel::resource::Action* res = &list->front();
150 /** Pops and returns the first finished action (or nullptr if none exist) */
151 Action* Model::extract_done_action()
153 return extract_action(get_finished_action_set());
156 /** Pops and returns the failed finished action (or nullptr if none exist) */
157 Action* Model::extract_failed_action()
159 return extract_action(get_failed_action_set());
162 void Model::update_actions_state_lazy(double /*now*/, double /*delta*/)
167 void Model::update_actions_state_full(double /*now*/, double /*delta*/)
173 } // namespace kernel
174 } // namespace simgrid