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;
19 delete readyActionSet_;
20 delete runningActionSet_;
21 delete failedActionSet_;
22 delete doneActionSet_;
27 Action* Model::actionHeapPop()
29 Action* action = actionHeap_.top().second;
31 action->clearHeapHandle();
35 double Model::nextOccuringEvent(double now)
37 // FIXME: set the good function once and for all
38 if (updateMechanism_ == UM_LAZY)
39 return nextOccuringEventLazy(now);
40 else if (updateMechanism_ == UM_FULL)
41 return nextOccuringEventFull(now);
43 xbt_die("Invalid cpu update mechanism!");
46 double Model::nextOccuringEventLazy(double now)
48 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", modifiedSet_->size());
49 lmm_solve(maxminSystem_);
50 XBT_DEBUG("After share resources, The size of modified actions set is %zu", modifiedSet_->size());
52 while (not modifiedSet_->empty()) {
53 Action* action = &(modifiedSet_->front());
54 modifiedSet_->pop_front();
55 bool max_dur_flag = false;
57 if (action->getStateSet() != runningActionSet_)
60 /* bogus priority, skip it */
61 if (action->getPriority() <= 0 || action->getType() == Action::Type::LATENCY)
64 action->updateRemainingLazy(now);
67 double share = action->getVariable()->get_value();
70 double time_to_completion;
71 if (action->getRemains() > 0) {
72 time_to_completion = action->getRemainsNoUpdate() / share;
74 time_to_completion = 0.0;
76 min = now + time_to_completion; // when the task will complete if nothing changes
79 if ((action->getMaxDuration() > NO_MAX_DURATION) &&
80 (min <= -1 || action->getStartTime() + action->getMaxDuration() < min)) {
81 // when the task will complete anyway because of the deadline if any
82 min = action->getStartTime() + action->getMaxDuration();
86 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->getVariable()->id_int);
88 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
89 action->getStartTime(), min, share, action->getMaxDuration());
92 action->heapUpdate(actionHeap_, min, max_dur_flag ? Action::Type::MAX_DURATION : Action::Type::NORMAL);
93 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
98 // hereafter must have already the min value for this resource model
99 if (not actionHeapIsEmpty()) {
100 double min = actionHeapTopDate() - now;
101 XBT_DEBUG("minimum with the HEAP %f", min);
104 XBT_DEBUG("The HEAP is empty, thus returning -1");
109 double Model::nextOccuringEventFull(double /*now*/)
111 maxminSystem_->solve_fun(maxminSystem_);
115 for (Action& action : *getRunningActionSet()) {
116 double value = action.getVariable()->get_value();
118 if (action.getRemains() > 0)
119 value = action.getRemainsNoUpdate() / value;
122 if (min < 0 || value < min) {
124 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
127 if ((action.getMaxDuration() >= 0) && (min < 0 || action.getMaxDuration() < min)) {
128 min = action.getMaxDuration();
129 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
132 XBT_DEBUG("min value : %f", min);
137 void Model::updateActionsState(double now, double delta)
139 if (updateMechanism_ == UM_FULL)
140 updateActionsStateFull(now, delta);
141 else if (updateMechanism_ == UM_LAZY)
142 updateActionsStateLazy(now, delta);
144 xbt_die("Invalid cpu update mechanism!");
147 void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
152 void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
158 } // namespace simgrid
159 } // namespace simgrid