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.hpp"
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(Model::UpdateAlgo::Full);
45 } else if (optim == "Lazy") {
46 xbt_assert(select || xbt_cfg_is_default_value("cpu/maxmin-selective-update"),
47 "You cannot disable cpu selective update when using the lazy update mechanism");
48 setUpdateMechanism(Model::UpdateAlgo::Lazy);
51 xbt_die("Unsupported optimization (%s) for this model", optim.c_str());
54 set_maxmin_system(new simgrid::kernel::lmm::System(select));
56 if (getUpdateMechanism() == Model::UpdateAlgo::Lazy)
57 get_maxmin_system()->modified_set_ = new kernel::resource::Action::ModifiedSet();
60 CpuCas01Model::~CpuCas01Model()
62 surf_cpu_model_pm = nullptr;
65 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
67 return new CpuCas01(this, host, speedPerPstate, core);
73 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
74 : Cpu(model, host, model->get_maxmin_system()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
81 if (model() == surf_cpu_model_pm)
82 speedPerPstate_.clear();
85 std::vector<double> * CpuCas01::getSpeedPeakList(){
86 return &speedPerPstate_;
89 bool CpuCas01::isUsed()
91 return model()->get_maxmin_system()->constraint_used(constraint());
94 /** @brief take into account changes of speed (either load or max) */
95 void CpuCas01::onSpeedChange() {
96 kernel::lmm::Variable* var = nullptr;
97 const_lmm_element_t elem = nullptr;
99 model()->get_maxmin_system()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
100 while ((var = constraint()->get_variable(&elem))) {
101 CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
103 model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
104 action->requestedCore() * speed_.scale * speed_.peak);
107 Cpu::onSpeedChange();
110 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
112 if (event == speed_.event) {
113 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
114 xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
116 speed_.scale = value;
119 tmgr_trace_event_unref(&speed_.event);
120 } else if (event == stateEvent_) {
121 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
122 xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
126 host_that_restart.push_back(getHost());
129 kernel::lmm::Constraint* cnst = constraint();
130 kernel::lmm::Variable* var = nullptr;
131 const_lmm_element_t elem = nullptr;
132 double date = surf_get_clock();
136 while ((var = cnst->get_variable(&elem))) {
137 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
139 if (action->get_state() == kernel::resource::Action::State::running ||
140 action->get_state() == kernel::resource::Action::State::ready ||
141 action->get_state() == kernel::resource::Action::State::not_in_the_system) {
142 action->set_finish_time(date);
143 action->set_state(kernel::resource::Action::State::failed);
147 tmgr_trace_event_unref(&stateEvent_);
150 xbt_die("Unknown event!\n");
154 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
155 CpuAction* CpuCas01::execution_start(double size)
157 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
160 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
162 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
165 CpuAction *CpuCas01::sleep(double duration)
168 duration = std::max(duration, sg_surf_precision);
170 XBT_IN("(%s,%g)", getCname(), duration);
171 CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
173 // FIXME: sleep variables should not consume 1.0 in System::expand()
174 action->set_max_duration(duration);
175 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
176 if (duration < 0) { // NO_MAX_DURATION
177 /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
178 simgrid::xbt::intrusive_erase(*action->get_state_set(), *action);
179 action->state_set_ = &static_cast<CpuCas01Model*>(model())->cpuRunningActionSetThatDoesNotNeedBeingChecked_;
180 action->get_state_set()->push_back(*action);
183 model()->get_maxmin_system()->update_variable_weight(action->get_variable(), 0.0);
184 if (model()->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) { // remove action from the heap
185 action->heapRemove();
186 // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
187 // max_duration correctly at the next call to share_resources
188 model()->get_modified_set()->push_front(*action);
198 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
199 kernel::lmm::Constraint* constraint, int requestedCore)
200 : CpuAction(model, cost, failed,
201 model->get_maxmin_system()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
202 , requestedCore_(requestedCore)
204 if (model->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
208 model->get_maxmin_system()->expand(constraint, get_variable(), 1.0);
211 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
212 kernel::lmm::Constraint* constraint)
213 : CpuCas01Action(model, cost, failed, speed, constraint, 1)
217 int CpuCas01Action::requestedCore()
219 return requestedCore_;
222 CpuCas01Action::~CpuCas01Action()=default;