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() : 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;
28 delete readyActionSet_;
29 delete runningActionSet_;
30 delete failedActionSet_;
31 delete doneActionSet_;
36 Action* Model::actionHeapPop()
38 Action* action = actionHeap_.top().second;
40 action->clearHeapHandle();
44 double Model::nextOccuringEvent(double now)
46 // FIXME: set the good function once and for all
47 if (updateMechanism_ == UM_LAZY)
48 return nextOccuringEventLazy(now);
49 else if (updateMechanism_ == UM_FULL)
50 return nextOccuringEventFull(now);
52 xbt_die("Invalid cpu update mechanism!");
55 double Model::nextOccuringEventLazy(double now)
57 XBT_DEBUG("Before share resources, the size of modified actions set is %zu", modifiedSet_->size());
58 lmm_solve(maxminSystem_);
59 XBT_DEBUG("After share resources, The size of modified actions set is %zu", modifiedSet_->size());
61 while (not modifiedSet_->empty()) {
62 Action* action = &(modifiedSet_->front());
63 modifiedSet_->pop_front();
64 bool max_dur_flag = false;
66 if (action->getStateSet() != runningActionSet_)
69 /* bogus priority, skip it */
70 if (action->getPriority() <= 0 || action->getType() == Action::Type::LATENCY)
73 action->updateRemainingLazy(now);
76 double share = action->getVariable()->get_value();
79 double time_to_completion;
80 if (action->getRemains() > 0) {
81 time_to_completion = action->getRemainsNoUpdate() / share;
83 time_to_completion = 0.0;
85 min = now + time_to_completion; // when the task will complete if nothing changes
88 if ((action->getMaxDuration() > NO_MAX_DURATION) &&
89 (min <= -1 || action->getStartTime() + action->getMaxDuration() < min)) {
90 // when the task will complete anyway because of the deadline if any
91 min = action->getStartTime() + action->getMaxDuration();
95 XBT_DEBUG("Action(%p) corresponds to variable %d", action, action->getVariable()->id_int);
97 XBT_DEBUG("Action(%p) Start %f. May finish at %f (got a share of %f). Max_duration %f", action,
98 action->getStartTime(), min, share, action->getMaxDuration());
101 action->heapUpdate(actionHeap_, min, max_dur_flag ? Action::Type::MAX_DURATION : Action::Type::NORMAL);
102 XBT_DEBUG("Insert at heap action(%p) min %f now %f", action, min, now);
107 // hereafter must have already the min value for this resource model
108 if (not actionHeapIsEmpty()) {
109 double min = actionHeapTopDate() - now;
110 XBT_DEBUG("minimum with the HEAP %f", min);
113 XBT_DEBUG("The HEAP is empty, thus returning -1");
118 double Model::nextOccuringEventFull(double /*now*/)
120 maxminSystem_->solve_fun(maxminSystem_);
124 for (Action& action : *getRunningActionSet()) {
125 double value = action.getVariable()->get_value();
127 if (action.getRemains() > 0)
128 value = action.getRemainsNoUpdate() / value;
131 if (min < 0 || value < min) {
133 XBT_DEBUG("Updating min (value) with %p: %f", &action, min);
136 if ((action.getMaxDuration() >= 0) && (min < 0 || action.getMaxDuration() < min)) {
137 min = action.getMaxDuration();
138 XBT_DEBUG("Updating min (duration) with %p: %f", &action, min);
141 XBT_DEBUG("min value : %f", min);
146 void Model::updateActionsState(double now, double delta)
148 if (updateMechanism_ == UM_FULL)
149 updateActionsStateFull(now, delta);
150 else if (updateMechanism_ == UM_LAZY)
151 updateActionsStateLazy(now, delta);
153 xbt_die("Invalid cpu update mechanism!");
156 void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
161 void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
167 } // namespace simgrid
168 } // namespace simgrid