1 /* Copyright (c) 2009-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 "cpu_cas01.hpp"
8 #include "simgrid/sg_config.h"
9 #include "src/kernel/lmm/maxmin.hpp"
10 #include "xbt/utility.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU IMPROVED module");
18 void surf_cpu_model_init_Cas01()
20 xbt_assert(not surf_cpu_model_pm);
21 xbt_assert(not surf_cpu_model_vm);
23 if (xbt_cfg_get_string("cpu/optim") == "TI") {
24 surf_cpu_model_init_ti();
28 surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
29 all_existing_models->push_back(surf_cpu_model_pm);
31 surf_cpu_model_vm = new simgrid::surf::CpuCas01Model();
32 all_existing_models->push_back(surf_cpu_model_vm);
38 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
40 std::string optim = xbt_cfg_get_string("cpu/optim");
41 bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
43 if (optim == "Full") {
44 setUpdateMechanism(UM_FULL);
45 } else if (optim == "Lazy") {
46 setUpdateMechanism(UM_LAZY);
48 xbt_assert(select || (xbt_cfg_is_default_value("cpu/maxmin-selective-update")),
49 "Disabling selective update while using the lazy update mechanism is dumb!");
51 xbt_die("Unsupported optimization (%s) for this model", optim.c_str());
54 maxminSystem_ = new simgrid::kernel::lmm::System(select);
56 if (getUpdateMechanism() == UM_LAZY) {
57 modifiedSet_ = new kernel::resource::ActionLmmList();
58 maxminSystem_->keep_track = modifiedSet_;
62 CpuCas01Model::~CpuCas01Model()
64 surf_cpu_model_pm = nullptr;
67 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
69 return new CpuCas01(this, host, speedPerPstate, core);
75 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
76 : Cpu(model, host, model->getMaxminSystem()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
83 if (model() == surf_cpu_model_pm)
84 speedPerPstate_.clear();
87 std::vector<double> * CpuCas01::getSpeedPeakList(){
88 return &speedPerPstate_;
91 bool CpuCas01::isUsed()
93 return model()->getMaxminSystem()->constraint_used(constraint());
96 /** @brief take into account changes of speed (either load or max) */
97 void CpuCas01::onSpeedChange() {
98 kernel::lmm::Variable* var = nullptr;
99 const_lmm_element_t elem = nullptr;
101 model()->getMaxminSystem()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
102 while ((var = constraint()->get_variable(&elem))) {
103 CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
105 model()->getMaxminSystem()->update_variable_bound(action->getVariable(),
106 action->requestedCore() * speed_.scale * speed_.peak);
109 Cpu::onSpeedChange();
112 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
114 if (event == speed_.event) {
115 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
116 xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
118 speed_.scale = value;
121 tmgr_trace_event_unref(&speed_.event);
122 } else if (event == stateEvent_) {
123 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
124 xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
128 host_that_restart.push_back(getHost());
131 kernel::lmm::Constraint* cnst = constraint();
132 kernel::lmm::Variable* var = nullptr;
133 const_lmm_element_t elem = nullptr;
134 double date = surf_get_clock();
138 while ((var = cnst->get_variable(&elem))) {
139 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
141 if (action->getState() == kernel::resource::Action::State::running ||
142 action->getState() == kernel::resource::Action::State::ready ||
143 action->getState() == kernel::resource::Action::State::not_in_the_system) {
144 action->setFinishTime(date);
145 action->setState(kernel::resource::Action::State::failed);
149 tmgr_trace_event_unref(&stateEvent_);
152 xbt_die("Unknown event!\n");
156 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
157 CpuAction* CpuCas01::execution_start(double size)
159 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
162 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
164 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
167 CpuAction *CpuCas01::sleep(double duration)
170 duration = std::max(duration, sg_surf_precision);
172 XBT_IN("(%s,%g)", getCname(), duration);
173 CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
175 // FIXME: sleep variables should not consume 1.0 in System::expand()
176 action->setMaxDuration(duration);
177 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
178 if (duration < 0) { // NO_MAX_DURATION
179 /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
180 simgrid::xbt::intrusive_erase(*action->getStateSet(), *action);
181 action->stateSet_ = &static_cast<CpuCas01Model*>(model())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
182 action->getStateSet()->push_back(*action);
185 model()->getMaxminSystem()->update_variable_weight(action->getVariable(), 0.0);
186 if (model()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
187 action->heapRemove(model()->getActionHeap());
188 // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
189 // max_duration correctly at the next call to share_resources
190 model()->getModifiedSet()->push_front(*action);
200 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
201 kernel::lmm::Constraint* constraint, int requestedCore)
202 : CpuAction(model, cost, failed,
203 model->getMaxminSystem()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
204 , requestedCore_(requestedCore)
206 if (model->getUpdateMechanism() == UM_LAZY) {
210 model->getMaxminSystem()->expand(constraint, getVariable(), 1.0);
213 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
214 kernel::lmm::Constraint* constraint)
215 : CpuCas01Action(model, cost, failed, speed, constraint, 1)
219 int CpuCas01Action::requestedCore()
221 return requestedCore_;
224 CpuCas01Action::~CpuCas01Action()=default;