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/config.hpp"
11 #include "xbt/utility.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU module");
21 static simgrid::config::Flag<std::string>
22 cpu_optim_opt("cpu/optim", "Optimization algorithm to use for CPU resources. ", "Lazy",
24 std::map<std::string, std::string>({
25 {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining)."},
26 {"TI", "Trace integration. Highly optimized mode when using availability traces (only available "
27 "for the Cas01 CPU model for now)."},
28 {"Full", "Full update of remaining and variables. Slow but may be useful when debugging."},
31 [](std::string const& val) {
32 xbt_assert(_sg_cfg_init_status < 2,
33 "Cannot change the optimization algorithm after the initialization");
39 void surf_cpu_model_init_Cas01()
41 xbt_assert(not surf_cpu_model_pm);
42 xbt_assert(not surf_cpu_model_vm);
44 if (cpu_optim_opt == "TI") {
45 surf_cpu_model_init_ti();
49 simgrid::kernel::resource::Model::UpdateAlgo algo;
50 if (cpu_optim_opt == "Lazy")
51 algo = simgrid::kernel::resource::Model::UpdateAlgo::Lazy;
53 algo = simgrid::kernel::resource::Model::UpdateAlgo::Full;
55 surf_cpu_model_pm = new simgrid::surf::CpuCas01Model(algo);
56 all_existing_models->push_back(surf_cpu_model_pm);
58 surf_cpu_model_vm = new simgrid::surf::CpuCas01Model(algo);
59 all_existing_models->push_back(surf_cpu_model_vm);
65 CpuCas01Model::CpuCas01Model(kernel::resource::Model::UpdateAlgo algo) : simgrid::surf::CpuModel(algo)
67 bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
69 if (algo == Model::UpdateAlgo::Lazy) {
70 xbt_assert(select || xbt_cfg_is_default_value("cpu/maxmin-selective-update"),
71 "You cannot disable cpu selective update when using the lazy update mechanism");
75 set_maxmin_system(new simgrid::kernel::lmm::System(select));
78 CpuCas01Model::~CpuCas01Model()
80 surf_cpu_model_pm = nullptr;
83 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
85 return new CpuCas01(this, host, speedPerPstate, core);
91 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
92 : Cpu(model, host, model->get_maxmin_system()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
99 if (model() == surf_cpu_model_pm)
100 speedPerPstate_.clear();
103 std::vector<double> * CpuCas01::getSpeedPeakList(){
104 return &speedPerPstate_;
107 bool CpuCas01::is_used()
109 return model()->get_maxmin_system()->constraint_used(constraint());
112 /** @brief take into account changes of speed (either load or max) */
113 void CpuCas01::onSpeedChange() {
114 kernel::lmm::Variable* var = nullptr;
115 const_lmm_element_t elem = nullptr;
117 model()->get_maxmin_system()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
118 while ((var = constraint()->get_variable(&elem))) {
119 CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
121 model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
122 action->requestedCore() * speed_.scale * speed_.peak);
125 Cpu::onSpeedChange();
128 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
130 if (event == speed_.event) {
131 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
132 xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
134 speed_.scale = value;
137 tmgr_trace_event_unref(&speed_.event);
138 } else if (event == stateEvent_) {
139 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
140 xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
144 host_that_restart.push_back(getHost());
147 kernel::lmm::Constraint* cnst = constraint();
148 kernel::lmm::Variable* var = nullptr;
149 const_lmm_element_t elem = nullptr;
150 double date = surf_get_clock();
154 while ((var = cnst->get_variable(&elem))) {
155 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
157 if (action->get_state() == kernel::resource::Action::State::running ||
158 action->get_state() == kernel::resource::Action::State::ready ||
159 action->get_state() == kernel::resource::Action::State::not_in_the_system) {
160 action->set_finish_time(date);
161 action->set_state(kernel::resource::Action::State::failed);
165 tmgr_trace_event_unref(&stateEvent_);
168 xbt_die("Unknown event!\n");
172 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
173 CpuAction* CpuCas01::execution_start(double size)
175 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
178 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
180 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
183 CpuAction *CpuCas01::sleep(double duration)
186 duration = std::max(duration, sg_surf_precision);
188 XBT_IN("(%s,%g)", getCname(), duration);
189 CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
191 // FIXME: sleep variables should not consume 1.0 in System::expand()
192 action->set_max_duration(duration);
193 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
194 if (duration < 0) { // NO_MAX_DURATION
195 /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
196 simgrid::xbt::intrusive_erase(*action->get_state_set(), *action);
197 action->state_set_ = &static_cast<CpuCas01Model*>(model())->cpuRunningActionSetThatDoesNotNeedBeingChecked_;
198 action->get_state_set()->push_back(*action);
201 model()->get_maxmin_system()->update_variable_weight(action->get_variable(), 0.0);
202 if (model()->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) { // remove action from the heap
203 action->heapRemove();
204 // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
205 // max_duration correctly at the next call to share_resources
206 model()->get_modified_set()->push_front(*action);
216 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
217 kernel::lmm::Constraint* constraint, int requestedCore)
218 : CpuAction(model, cost, failed,
219 model->get_maxmin_system()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
220 , requestedCore_(requestedCore)
222 if (model->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
226 model->get_maxmin_system()->expand(constraint, get_variable(), 1.0);
229 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
230 kernel::lmm::Constraint* constraint)
231 : CpuCas01Action(model, cost, failed, speed, constraint, 1)
235 int CpuCas01Action::requestedCore()
237 return requestedCore_;
240 CpuCas01Action::~CpuCas01Action()=default;