Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
put back a leak (for now) to fix tests
[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(not surf_cpu_model_pm);
20   xbt_assert(not surf_cpu_model_vm);
21
22   char *optim = xbt_cfg_get_string("cpu/optim");
23   if (not 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 (not strcmp(optim, "Full")) {
44     updateMechanism_ = UM_FULL;
45     selectiveUpdate_ = select;
46   } else if (not 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   maxminSystem_ = lmm_system_new(selectiveUpdate_);
57
58   if (getUpdateMechanism() == UM_LAZY) {
59     actionHeap_ = xbt_heap_new(8, nullptr);
60     xbt_heap_set_update_callback(actionHeap_,  surf_action_lmm_update_index_heap);
61     modifiedSet_ = new ActionLmmList();
62     maxminSystem_->keep_track = modifiedSet_;
63   }
64 }
65
66 CpuCas01Model::~CpuCas01Model()
67 {
68   lmm_system_free(maxminSystem_);
69   maxminSystem_ = nullptr;
70   xbt_heap_free(actionHeap_);
71   delete modifiedSet_;
72
73   surf_cpu_model_pm = nullptr;
74
75   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
76 }
77
78 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
79 {
80   return new CpuCas01(this, host, speedPerPstate, core);
81 }
82
83 /************
84  * Resource *
85  ************/
86 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
87 : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, core * speedPerPstate->front()),
88     speedPerPstate, core)
89 {
90 }
91
92 CpuCas01::~CpuCas01()
93 {
94   if (model() == surf_cpu_model_pm)
95     speedPerPstate_.clear();
96 }
97
98 std::vector<double> * CpuCas01::getSpeedPeakList(){
99   return &speedPerPstate_;
100 }
101
102 bool CpuCas01::isUsed()
103 {
104   return lmm_constraint_used(model()->getMaxminSystem(), constraint());
105 }
106
107 /** @brief take into account changes of speed (either load or max) */
108 void CpuCas01::onSpeedChange() {
109   lmm_variable_t var = nullptr;
110   lmm_element_t elem = nullptr;
111
112   lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), coresAmount_ * speed_.scale * speed_.peak);
113   while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
114     CpuCas01Action* action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
115
116     lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(),
117                               action->requestedCore() * speed_.scale * speed_.peak);
118   }
119
120   Cpu::onSpeedChange();
121 }
122
123 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
124 {
125   if (event == speed_.event) {
126     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
127     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
128
129     speed_.scale = value;
130     onSpeedChange();
131
132     tmgr_trace_event_unref(&speed_.event);
133   } else if (event == stateEvent_) {
134     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
135     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
136
137     if (value > 0) {
138       if(isOff())
139         host_that_restart.push_back(getHost());
140       turnOn();
141     } else {
142       lmm_constraint_t cnst = constraint();
143       lmm_variable_t var = nullptr;
144       lmm_element_t elem = nullptr;
145       double date = surf_get_clock();
146
147       turnOff();
148
149       while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), cnst, &elem))) {
150         Action *action = static_cast<Action*>(lmm_variable_id(var));
151
152         if (action->getState() == Action::State::running ||
153             action->getState() == Action::State::ready ||
154             action->getState() == Action::State::not_in_the_system) {
155           action->setFinishTime(date);
156           action->setState(Action::State::failed);
157         }
158       }
159     }
160     tmgr_trace_event_unref(&stateEvent_);
161
162   } else {
163     xbt_die("Unknown event!\n");
164   }
165 }
166
167 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
168 CpuAction *CpuCas01::execution_start(double size)
169 {
170   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
171 }
172 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
173 {
174   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
175 }
176
177 CpuAction *CpuCas01::sleep(double duration)
178 {
179   if (duration > 0)
180     duration = MAX(duration, sg_surf_precision);
181
182   XBT_IN("(%s,%g)", cname(), duration);
183   CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
184
185   // FIXME: sleep variables should not consume 1.0 in lmm_expand
186   action->maxDuration_ = duration;
187   action->suspended_ = 2;
188   if (duration < 0) { // NO_MAX_DURATION
189     /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
190     action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
191     action->stateSet_ = static_cast<CpuCas01Model*>(model())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
192     action->getStateSet()->push_back(*action);
193   }
194
195   lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), 0.0);
196   if (model()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
197     action->heapRemove(model()->getActionHeap());
198     // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
199     // max_duration correctly at the next call to share_resources
200     model()->getModifiedSet()->push_front(*action);
201   }
202
203   XBT_OUT();
204   return action;
205 }
206
207 /**********
208  * Action *
209  **********/
210 CpuCas01Action::CpuCas01Action(Model* model, double cost, bool failed, double speed, lmm_constraint_t constraint,
211                                int requestedCore)
212     : CpuAction(model, cost, failed,
213                 lmm_variable_new(model->getMaxminSystem(), this, 1.0 / requestedCore, requestedCore * speed, 1))
214     , requestedCore_(requestedCore)
215 {
216   if (model->getUpdateMechanism() == UM_LAZY) {
217     indexHeap_ = -1;
218     lastUpdate_ = surf_get_clock();
219     lastValue_ = 0.0;
220   }
221   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
222 }
223
224 CpuCas01Action::CpuCas01Action(Model* model, double cost, bool failed, double speed, lmm_constraint_t constraint)
225     : CpuCas01Action(model, cost, failed, speed, constraint, 1)
226 {
227 }
228
229 int CpuCas01Action::requestedCore()
230 {
231   return requestedCore_;
232 }
233
234 CpuCas01Action::~CpuCas01Action()=default;
235
236 }
237 }