Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[ENERGY] Introduce idle/1core/maxCore energy consumption
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011, 2013-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "cpu_cas01.hpp"
8 #include "cpu_ti.hpp"
9 #include "maxmin_private.hpp"
10 #include "simgrid/sg_config.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU IMPROVED module");
13
14 /*********
15  * Model *
16  *********/
17 void surf_cpu_model_init_Cas01()
18 {
19   xbt_assert(!surf_cpu_model_pm);
20   xbt_assert(!surf_cpu_model_vm);
21
22   char *optim = xbt_cfg_get_string("cpu/optim");
23   if (!strcmp(optim, "TI")) {
24     surf_cpu_model_init_ti();
25     return;
26   }
27
28   surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
29   all_existing_models->push_back(surf_cpu_model_pm);
30
31   surf_cpu_model_vm  = new simgrid::surf::CpuCas01Model();
32   all_existing_models->push_back(surf_cpu_model_vm);
33 }
34
35 namespace simgrid {
36 namespace surf {
37
38 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
39 {
40   char *optim = xbt_cfg_get_string("cpu/optim");
41   bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
42
43   if (!strcmp(optim, "Full")) {
44     updateMechanism_ = UM_FULL;
45     selectiveUpdate_ = select;
46   } else if (!strcmp(optim, "Lazy")) {
47     updateMechanism_ = UM_LAZY;
48     selectiveUpdate_ = true;
49     xbt_assert(select || (xbt_cfg_is_default_value("cpu/maxmin-selective-update")),
50                "Disabling selective update while using the lazy update mechanism is dumb!");
51   } else {
52     xbt_die("Unsupported optimization (%s) for this model", optim);
53   }
54
55   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
56
57   maxminSystem_ = lmm_system_new(selectiveUpdate_);
58
59   if (getUpdateMechanism() == UM_LAZY) {
60     actionHeap_ = xbt_heap_new(8, nullptr);
61     xbt_heap_set_update_callback(actionHeap_,  surf_action_lmm_update_index_heap);
62     modifiedSet_ = new ActionLmmList();
63     maxminSystem_->keep_track = modifiedSet_;
64   }
65 }
66
67 CpuCas01Model::~CpuCas01Model()
68 {
69   lmm_system_free(maxminSystem_);
70   maxminSystem_ = nullptr;
71   xbt_heap_free(actionHeap_);
72   delete modifiedSet_;
73
74   surf_cpu_model_pm = nullptr;
75
76   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
77 }
78
79 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
80 {
81   return new CpuCas01(this, host, speedPerPstate, core);
82 }
83
84 /************
85  * Resource *
86  ************/
87 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
88 : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, core * speedPerPstate->front()),
89     speedPerPstate, core)
90 {
91 }
92
93 CpuCas01::~CpuCas01()
94 {
95   if (getModel() == surf_cpu_model_pm)
96     speedPerPstate_.clear();
97 }
98
99 std::vector<double> * CpuCas01::getSpeedPeakList(){
100   return &speedPerPstate_;
101 }
102
103 bool CpuCas01::isUsed()
104 {
105   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
106 }
107
108 /** @brief take into account changes of speed (either load or max) */
109 void CpuCas01::onSpeedChange() {
110   lmm_variable_t var = nullptr;
111   lmm_element_t elem = nullptr;
112
113     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
114                                 coresAmount_ * speed_.scale * speed_.peak);
115     while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
116       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
117
118       lmm_update_variable_bound(getModel()->getMaxminSystem(), action->getVariable(), speed_.scale * speed_.peak);
119     }
120
121   Cpu::onSpeedChange();
122 }
123
124 void CpuCas01::apply_event(tmgr_trace_iterator_t event, double value)
125 {
126   if (event == speed_.event) {
127     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
128     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
129
130     speed_.scale = value;
131     onSpeedChange();
132
133     tmgr_trace_event_unref(&speed_.event);
134   } else if (event == stateEvent_) {
135     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
136     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
137
138     if (value > 0) {
139       if(isOff())
140         host_that_restart.push_back(getHost());
141       turnOn();
142     } else {
143       lmm_constraint_t cnst = getConstraint();
144       lmm_variable_t var = nullptr;
145       lmm_element_t elem = nullptr;
146       double date = surf_get_clock();
147
148       turnOff();
149
150       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
151         Action *action = static_cast<Action*>(lmm_variable_id(var));
152
153         if (action->getState() == Action::State::running ||
154             action->getState() == Action::State::ready ||
155             action->getState() == Action::State::not_in_the_system) {
156           action->setFinishTime(date);
157           action->setState(Action::State::failed);
158         }
159       }
160     }
161     tmgr_trace_event_unref(&stateEvent_);
162
163   } else {
164     xbt_die("Unknown event!\n");
165   }
166 }
167
168 CpuAction *CpuCas01::execution_start(double size)
169 {
170   return new CpuCas01Action(getModel(), size, isOff(), speed_.scale * speed_.peak, getConstraint());
171 }
172
173 CpuAction *CpuCas01::sleep(double duration)
174 {
175   if (duration > 0)
176     duration = MAX(duration, sg_surf_precision);
177
178   XBT_IN("(%s,%g)", getName(), duration);
179   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, isOff(), speed_.scale * speed_.peak, getConstraint());
180
181   // FIXME: sleep variables should not consume 1.0 in lmm_expand
182   action->maxDuration_ = duration;
183   action->suspended_ = 2;
184   if (duration == NO_MAX_DURATION) {
185     /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
186     action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
187     action->stateSet_ = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
188     action->getStateSet()->push_back(*action);
189   }
190
191   lmm_update_variable_weight(getModel()->getMaxminSystem(), action->getVariable(), 0.0);
192   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
193     action->heapRemove(getModel()->getActionHeap());
194     // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
195     // max_duration correctly at the next call to share_resources
196     getModel()->getModifiedSet()->push_front(*action);
197   }
198
199   XBT_OUT();
200   return action;
201 }
202
203 /**********
204  * Action *
205  **********/
206
207 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double speed, lmm_constraint_t constraint)
208  : CpuAction(model, cost, failed, lmm_variable_new(model->getMaxminSystem(), this, 1.0, speed, 1))
209 {
210   if (model->getUpdateMechanism() == UM_LAZY) {
211     indexHeap_ = -1;
212     lastUpdate_ = surf_get_clock();
213     lastValue_ = 0.0;
214   }
215   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
216 }
217
218 CpuCas01Action::~CpuCas01Action()=default;
219
220 }
221 }