Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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,
13                                 "Logging specific to the SURF CPU IMPROVED module");
14
15 /*********
16  * Model *
17  *********/
18 void surf_cpu_model_init_Cas01()
19 {
20   xbt_assert(!surf_cpu_model_pm);
21   xbt_assert(!surf_cpu_model_vm);
22
23   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
24   if (!strcmp(optim, "TI")) {
25     surf_cpu_model_init_ti();
26     return;
27   }
28
29   surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
30   xbt_dynar_push(all_existing_models, &surf_cpu_model_pm);
31
32   surf_cpu_model_vm  = new simgrid::surf::CpuCas01Model();
33   xbt_dynar_push(all_existing_models, &surf_cpu_model_vm);
34 }
35
36 namespace simgrid {
37 namespace surf {
38
39 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
40 {
41   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
42   int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
43
44   if (!strcmp(optim, "Full")) {
45     p_updateMechanism = UM_FULL;
46     m_selectiveUpdate = select;
47   } else if (!strcmp(optim, "Lazy")) {
48     p_updateMechanism = UM_LAZY;
49     m_selectiveUpdate = 1;
50     xbt_assert((select == 1)
51                ||
52                (xbt_cfg_is_default_value
53                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
54                "Disabling selective update while using the lazy update mechanism is dumb!");
55   } else {
56     xbt_die("Unsupported optimization (%s) for this model", optim);
57   }
58
59   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
60
61   p_maxminSystem = lmm_system_new(m_selectiveUpdate);
62
63   if (getUpdateMechanism() == UM_LAZY) {
64     p_actionHeap = xbt_heap_new(8, NULL);
65     xbt_heap_set_update_callback(p_actionHeap,  surf_action_lmm_update_index_heap);
66     p_modifiedSet = new ActionLmmList();
67     p_maxminSystem->keep_track = p_modifiedSet;
68   }
69 }
70
71 CpuCas01Model::~CpuCas01Model()
72 {
73   lmm_system_free(p_maxminSystem);
74   p_maxminSystem = NULL;
75
76   if (p_actionHeap)
77     xbt_heap_free(p_actionHeap);
78   delete p_modifiedSet;
79
80   surf_cpu_model_pm = NULL;
81
82   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
83 }
84
85 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
86                       int pstate, double speedScale,
87                           tmgr_trace_t speedTrace, int core,
88                           int initiallyOn,
89                           tmgr_trace_t state_trace)
90 {
91   xbt_assert(xbt_dynar_getfirst_as(speedPeak, double) > 0.0,
92       "Speed has to be >0.0. Did you forget to specify the mandatory power attribute?");
93   xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
94   Cpu *cpu = new CpuCas01(this, host, speedPeak, pstate, speedScale, speedTrace, core, initiallyOn, state_trace);
95   return cpu;
96 }
97
98 double CpuCas01Model::next_occuring_event_full(double /*now*/)
99 {
100   return Model::shareResourcesMaxMin(getRunningActionSet(), p_maxminSystem, lmm_solve);
101 }
102
103 /************
104  * Resource *
105  ************/
106 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
107                          int pstate, double speedScale, tmgr_trace_t speedTrace, int core,
108                          int initiallyOn, tmgr_trace_t stateTrace)
109 : Cpu(model, host,
110   lmm_constraint_new(model->getMaxminSystem(), this, core * speedScale * xbt_dynar_get_as(speedPeak, pstate, double)),
111   speedPeak, pstate,
112   core, xbt_dynar_get_as(speedPeak, pstate, double), speedScale,
113     initiallyOn) {
114
115   XBT_DEBUG("CPU create: peak=%f, pstate=%d", p_speed.peak, m_pstate);
116
117   m_core = core;
118   if (speedTrace)
119     p_speed.event = future_evt_set->add_trace(speedTrace, 0.0, this);
120
121   if (stateTrace)
122     p_stateEvent = future_evt_set->add_trace(stateTrace, 0.0, this);
123 }
124
125 CpuCas01::~CpuCas01()
126 {
127   if (getModel() == surf_cpu_model_pm)
128     xbt_dynar_free(&p_speedPeakList);
129 }
130
131 xbt_dynar_t CpuCas01::getSpeedPeakList(){
132   return p_speedPeakList;
133 }
134
135 bool CpuCas01::isUsed()
136 {
137   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
138 }
139
140 /** @brief take into account changes of speed (either load or max) */
141 void CpuCas01::onSpeedChange() {
142   lmm_variable_t var = NULL;
143   lmm_element_t elem = NULL;
144
145     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
146                                 m_core * p_speed.scale * p_speed.peak);
147     while ((var = lmm_get_var_from_cnst
148             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
149       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
150
151       lmm_update_variable_bound(getModel()->getMaxminSystem(),
152                                 action->getVariable(),
153                                 p_speed.scale * p_speed.peak);
154     }
155
156   Cpu::onSpeedChange();
157 }
158
159 void CpuCas01::apply_event(tmgr_trace_iterator_t event, double value)
160 {
161   if (event == p_speed.event) {
162     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
163     xbt_assert(m_core == 1, "FIXME: add speed scaling code also for constraint_core[i]");
164
165     p_speed.scale = value;
166     onSpeedChange();
167
168     tmgr_trace_event_unref(&p_speed.event);
169   } else if (event == p_stateEvent) {
170     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
171     xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
172
173     if (value > 0) {
174       if(isOff())
175         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
176       turnOn();
177     } else {
178       lmm_constraint_t cnst = getConstraint();
179       lmm_variable_t var = NULL;
180       lmm_element_t elem = NULL;
181       double date = surf_get_clock();
182
183       turnOff();
184
185       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
186         Action *action = static_cast<Action*>(lmm_variable_id(var));
187
188         if (action->getState() == SURF_ACTION_RUNNING ||
189             action->getState() == SURF_ACTION_READY ||
190             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
191           action->setFinishTime(date);
192           action->setState(SURF_ACTION_FAILED);
193         }
194       }
195     }
196     tmgr_trace_event_unref(&p_stateEvent);
197
198   } else {
199     xbt_die("Unknown event!\n");
200   }
201 }
202
203 CpuAction *CpuCas01::execution_start(double size)
204 {
205
206   XBT_IN("(%s,%g)", getName(), size);
207   CpuCas01Action *action = new CpuCas01Action(getModel(), size, isOff(),
208       p_speed.scale * p_speed.peak, getConstraint());
209
210   XBT_OUT();
211   return action;
212 }
213
214 CpuAction *CpuCas01::sleep(double duration)
215 {
216   if (duration > 0)
217     duration = MAX(duration, sg_surf_precision);
218
219   XBT_IN("(%s,%g)", getName(), duration);
220   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, isOff(),
221       p_speed.scale * p_speed.peak, getConstraint());
222
223
224   // FIXME: sleep variables should not consume 1.0 in lmm_expand
225   action->m_maxDuration = duration;
226   action->m_suspended = 2;
227   if (duration == NO_MAX_DURATION) {
228     /* Move to the *end* of the corresponding action set. This convention
229        is used to speed up update_resource_state  */
230     action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
231     action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
232     action->getStateSet()->push_back(*action);
233   }
234
235   lmm_update_variable_weight(getModel()->getMaxminSystem(),
236       action->getVariable(), 0.0);
237   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
238     action->heapRemove(getModel()->getActionHeap());
239     // this is necessary for a variable with weight 0 since such
240     // variables are ignored in lmm and we need to set its max_duration
241     // correctly at the next call to share_resources
242     getModel()->getModifiedSet()->push_front(*action);
243   }
244
245   XBT_OUT();
246   return action;
247 }
248
249 /**********
250  * Action *
251  **********/
252
253 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double speed, lmm_constraint_t constraint)
254  : CpuAction(model, cost, failed,
255          lmm_variable_new(model->getMaxminSystem(), this,
256          1.0, speed, 1))
257 {
258   if (model->getUpdateMechanism() == UM_LAZY) {
259     m_indexHeap = -1;
260     m_lastUpdate = surf_get_clock();
261     m_lastValue = 0.0;
262   }
263   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
264 }
265
266 }
267 }