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 "src/kernel/resource/Model.hpp"
7 #include "src/kernel/lmm/maxmin.hpp"
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(resource);
15 Model::Model() : maxminSystem_(nullptr)
17 readyActionSet_ = new ActionList();
18 runningActionSet_ = new ActionList();
19 failedActionSet_ = new ActionList();
20 doneActionSet_ = new ActionList();
22 modifiedSet_ = nullptr;
23 updateMechanism_ = UM_UNDEFINED;
29 delete readyActionSet_;
30 delete runningActionSet_;
31 delete failedActionSet_;
32 delete doneActionSet_;
37 Action* Model::actionHeapPop()
39 Action* action = actionHeap_.top().second;
41 action->clearHeapHandle();
45 double Model::nextOccuringEvent(double now)
47 // FIXME: set the good function once and for all
48 if (updateMechanism_ == UM_LAZY)
49 return nextOccuringEventLazy(now);
50 else if (updateMechanism_ == UM_FULL)
51 return nextOccuringEventFull(now);
53 xbt_die("Invalid cpu update mechanism!");
56 double Model::nextOccuringEventLazy(double now)
58 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", modifiedSet_->size());
59 lmm_solve(maxminSystem_);
60 XBT_DEBUG("After share resources, The size of modified actions set is %zu", modifiedSet_->size());
62 while (not modifiedSet_->empty()) {
63 Action* action = &(modifiedSet_->front());
64 modifiedSet_->pop_front();
65 bool max_dur_flag = false;
67 if (action->getStateSet() != runningActionSet_)
70 /* bogus priority, skip it */
71 if (action->getPriority() <= 0 || action->getType() == Action::Type::LATENCY)
74 action->updateRemainingLazy(now);
77 double share = action->getVariable()->get_value();
80 double time_to_completion;
81 if (action->getRemains() > 0) {
82 time_to_completion = action->getRemainsNoUpdate() / share;
84 time_to_completion = 0.0;
86 min = now + time_to_completion; // when the task will complete if nothing changes
89 if ((action->getMaxDuration() > NO_MAX_DURATION) &&
90 (min <= -1 || action->getStartTime() + action->getMaxDuration() < min)) {
91 // when the task will complete anyway because of the deadline if any
92 min = action->getStartTime() + action->getMaxDuration();
96 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->getVariable()->id_int);
98 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
99 action->getStartTime(), min, share, action->getMaxDuration());
102 action->heapUpdate(actionHeap_, min, max_dur_flag ? Action::Type::MAX_DURATION : Action::Type::NORMAL);
103 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
108 // hereafter must have already the min value for this resource model
109 if (not actionHeapIsEmpty()) {
110 double min = actionHeapTopDate() - now;
111 XBT_DEBUG("minimum with the HEAP %f", min);
114 XBT_DEBUG("The HEAP is empty, thus returning -1");
119 double Model::nextOccuringEventFull(double /*now*/)
121 maxminSystem_->solve_fun(maxminSystem_);
125 for (Action& action : *getRunningActionSet()) {
126 double value = action.getVariable()->get_value();
128 if (action.getRemains() > 0)
129 value = action.getRemainsNoUpdate() / value;
132 if (min < 0 || value < min) {
134 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
137 if ((action.getMaxDuration() >= 0) && (min < 0 || action.getMaxDuration() < min)) {
138 min = action.getMaxDuration();
139 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
142 XBT_DEBUG("min value : %f", min);
147 void Model::updateActionsState(double now, double delta)
149 if (updateMechanism_ == UM_FULL)
150 updateActionsStateFull(now, delta);
151 else if (updateMechanism_ == UM_LAZY)
152 updateActionsStateLazy(now, delta);
154 xbt_die("Invalid cpu update mechanism!");
157 void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
162 void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
168 } // namespace simgrid
169 } // namespace simgrid