Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make HostL07 behave more like the regular Host
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011, 2013-2015. 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   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
21
22   xbt_assert(!surf_cpu_model_pm);
23   xbt_assert(!surf_cpu_model_vm);
24
25   if (!strcmp(optim, "TI")) {
26     surf_cpu_model_init_ti();
27     return;
28   }
29
30   surf_cpu_model_pm = new CpuCas01Model();
31   surf_cpu_model_vm  = new CpuCas01Model();
32
33   sg_platf_postparse_add_cb(cpu_add_traces);
34
35   Model *model_pm = surf_cpu_model_pm;
36   Model *model_vm = surf_cpu_model_vm;
37   xbt_dynar_push(all_existing_models, &model_pm);
38   xbt_dynar_push(all_existing_models, &model_vm);
39 }
40
41 CpuCas01Model::CpuCas01Model() : CpuModel()
42 {
43   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
44   int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
45
46   if (!strcmp(optim, "Full")) {
47     p_updateMechanism = UM_FULL;
48     m_selectiveUpdate = select;
49   } else if (!strcmp(optim, "Lazy")) {
50     p_updateMechanism = UM_LAZY;
51     m_selectiveUpdate = 1;
52     xbt_assert((select == 1)
53                ||
54                (xbt_cfg_is_default_value
55                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
56                "Disabling selective update while using the lazy update mechanism is dumb!");
57   } else {
58     xbt_die("Unsupported optimization (%s) for this model", optim);
59   }
60
61   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
62
63   if (getUpdateMechanism() == UM_LAZY) {
64         shareResources = &CpuCas01Model::shareResourcesLazy;
65         updateActionsState = &CpuCas01Model::updateActionsStateLazy;
66
67   } else if (getUpdateMechanism() == UM_FULL) {
68         shareResources = &CpuCas01Model::shareResourcesFull;
69         updateActionsState = &CpuCas01Model::updateActionsStateFull;
70   } else
71     xbt_die("Invalid cpu update mechanism!");
72
73   if (!p_maxminSystem) {
74     p_maxminSystem = lmm_system_new(m_selectiveUpdate);
75   }
76
77   if (getUpdateMechanism() == UM_LAZY) {
78     p_actionHeap = xbt_heap_new(8, NULL);
79     xbt_heap_set_update_callback(p_actionHeap,  surf_action_lmm_update_index_heap);
80     p_modifiedSet = new ActionLmmList();
81     p_maxminSystem->keep_track = p_modifiedSet;
82   }
83 }
84
85 CpuCas01Model::~CpuCas01Model()
86 {
87   lmm_system_free(p_maxminSystem);
88   p_maxminSystem = NULL;
89
90   if (p_actionHeap)
91     xbt_heap_free(p_actionHeap);
92   delete p_modifiedSet;
93
94   surf_cpu_model_pm = NULL;
95
96   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
97 }
98
99 Cpu *CpuCas01Model::createCpu(const char *name, xbt_dynar_t power_peak,
100                                   int pstate, double power_scale,
101                           tmgr_trace_t power_trace, int core,
102                           e_surf_resource_state_t state_initial,
103                           tmgr_trace_t state_trace,
104                           xbt_dict_t cpu_properties)
105 {
106   Cpu *cpu = NULL;
107   sg_host_t host = sg_host_by_name(name);
108   xbt_assert(!sg_host_surfcpu(host),
109              "Host '%s' declared several times in the platform file",
110              name);
111   xbt_assert(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
112       "Power has to be >0.0. Did you forget to specify the mandatory power attribute?");
113   xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
114
115   cpu = new CpuCas01(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
116   sg_host_surfcpu_register(host, cpu);
117   return cpu;
118 }
119
120 double CpuCas01Model::shareResourcesFull(double /*now*/)
121 {
122   return Model::shareResourcesMaxMin(getRunningActionSet(),
123                              p_maxminSystem, lmm_solve);
124 }
125
126 void CpuCas01Model::addTraces()
127 {
128   xbt_dict_cursor_t cursor = NULL;
129   char *trace_name, *elm;
130   static int called = 0;
131   if (called)
132     return;
133   called = 1;
134
135   /* connect all traces relative to hosts */
136   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
137     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
138     CpuCas01 *host = static_cast<CpuCas01*>(sg_host_surfcpu(sg_host_by_name(elm)));
139
140     xbt_assert(host, "Host %s undefined", elm);
141     xbt_assert(trace, "Trace %s undefined", trace_name);
142
143     host->setStateEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
144   }
145
146   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
147     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
148     CpuCas01 *host = static_cast<CpuCas01*>(sg_host_surfcpu(sg_host_by_name(elm)));
149
150     xbt_assert(host, "Host %s undefined", elm);
151     xbt_assert(trace, "Trace %s undefined", trace_name);
152
153     host->setPowerEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
154   }
155 }
156
157 /************
158  * Resource *
159  ************/
160 CpuCas01::CpuCas01(CpuCas01Model *model, const char *name, xbt_dynar_t powerPeak,
161                          int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
162                          e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
163                          xbt_dict_t properties)
164 : Cpu(model, name, properties,
165           lmm_constraint_new(model->getMaxminSystem(), this, core * powerScale * xbt_dynar_get_as(powerPeak, pstate, double)),
166           core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale,
167     stateInitial) {
168   p_powerEvent = NULL;
169
170   // Copy the power peak array:
171   p_powerPeakList = xbt_dynar_new(sizeof(double), nullptr);
172   unsigned long n = xbt_dynar_length(powerPeak);
173   for (unsigned long i = 0; i != n; ++i) {
174     double value = xbt_dynar_get_as(powerPeak, i, double);
175     xbt_dynar_push(p_powerPeakList, &value);
176   }
177
178   m_pstate = pstate;
179
180   XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
181
182   m_core = core;
183   if (powerTrace)
184     p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, this);
185
186   if (stateTrace)
187     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
188 }
189
190 CpuCas01::~CpuCas01(){
191   if (getModel() == surf_cpu_model_pm)
192     xbt_dynar_free(&p_powerPeakList);
193 }
194
195 void CpuCas01::setStateEvent(tmgr_trace_event_t stateEvent)
196 {
197   p_stateEvent = stateEvent;
198 }
199
200 void CpuCas01::setPowerEvent(tmgr_trace_event_t powerEvent)
201 {
202   p_powerEvent = powerEvent;
203 }
204
205 xbt_dynar_t CpuCas01::getPowerPeakList(){
206   return p_powerPeakList;
207 }
208
209 int CpuCas01::getPState()
210 {
211   return m_pstate;
212 }
213
214 bool CpuCas01::isUsed()
215 {
216   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
217 }
218
219 void CpuCas01::updateState(tmgr_trace_event_t event_type, double value, double date)
220 {
221   lmm_variable_t var = NULL;
222   lmm_element_t elem = NULL;
223
224   if (event_type == p_powerEvent) {
225         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
226         xbt_assert(m_core == 1, "FIXME: add power scaling code also for constraint_core[i]");
227
228     m_powerScale = value;
229     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
230                                 m_core * m_powerScale *
231                                 m_powerPeak);
232     TRACE_surf_host_set_power(date, getName(),
233                               m_core * m_powerScale *
234                               m_powerPeak);
235     while ((var = lmm_get_var_from_cnst
236             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
237       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
238
239       lmm_update_variable_bound(getModel()->getMaxminSystem(),
240                                 action->getVariable(),
241                                 m_powerScale * m_powerPeak);
242     }
243     if (tmgr_trace_event_free(event_type))
244       p_powerEvent = NULL;
245   } else if (event_type == p_stateEvent) {
246         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
247     xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
248
249     if (value > 0) {
250       if(getState() == SURF_RESOURCE_OFF)
251         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
252       setState(SURF_RESOURCE_ON);
253     } else {
254       lmm_constraint_t cnst = getConstraint();
255
256       setState(SURF_RESOURCE_OFF);
257
258       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
259         Action *action = static_cast<Action*>(lmm_variable_id(var));
260
261         if (action->getState() == SURF_ACTION_RUNNING ||
262             action->getState() == SURF_ACTION_READY ||
263             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
264           action->setFinishTime(date);
265           action->setState(SURF_ACTION_FAILED);
266         }
267       }
268     }
269     if (tmgr_trace_event_free(event_type))
270       p_stateEvent = NULL;
271   } else {
272     XBT_CRITICAL("Unknown event ! \n");
273     xbt_abort();
274   }
275
276   return;
277 }
278
279 CpuAction *CpuCas01::execute(double size)
280 {
281
282   XBT_IN("(%s,%g)", getName(), size);
283   CpuCas01Action *action = new CpuCas01Action(getModel(), size, getState() != SURF_RESOURCE_ON,
284                                                               m_powerScale * m_powerPeak, getConstraint());
285
286   XBT_OUT();
287   return action;
288 }
289
290 CpuAction *CpuCas01::sleep(double duration)
291 {
292   if (duration > 0)
293     duration = MAX(duration, sg_surf_precision);
294
295   XBT_IN("(%s,%g)", getName(), duration);
296   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, getState() != SURF_RESOURCE_ON,
297                                                       m_powerScale * m_powerPeak, getConstraint());
298
299
300   // FIXME: sleep variables should not consume 1.0 in lmm_expand
301   action->m_maxDuration = duration;
302   action->m_suspended = 2;
303   if (duration == NO_MAX_DURATION) {
304     /* Move to the *end* of the corresponding action set. This convention
305        is used to speed up update_resource_state  */
306         action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
307     action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
308     action->getStateSet()->push_back(*action);
309   }
310
311   lmm_update_variable_weight(getModel()->getMaxminSystem(),
312                              action->getVariable(), 0.0);
313   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
314     action->heapRemove(getModel()->getActionHeap());
315     // this is necessary for a variable with weight 0 since such
316     // variables are ignored in lmm and we need to set its max_duration
317     // correctly at the next call to share_resources
318     getModel()->getModifiedSet()->push_front(*action);
319   }
320
321   XBT_OUT();
322   return action;
323 }
324
325 double CpuCas01::getCurrentPowerPeak()
326 {
327   return m_powerPeak;
328 }
329
330 double CpuCas01::getPowerPeakAt(int pstate_index)
331 {
332   xbt_dynar_t plist = p_powerPeakList;
333   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
334
335   return xbt_dynar_get_as(plist, pstate_index, double);
336 }
337
338 int CpuCas01::getNbPstates()
339 {
340   return xbt_dynar_length(p_powerPeakList);
341 }
342
343 void CpuCas01::setPstate(int pstate_index)
344 {
345   xbt_dynar_t plist = p_powerPeakList;
346   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
347
348   double new_pstate = xbt_dynar_get_as(plist, pstate_index, double);
349   m_pstate = pstate_index;
350   m_powerPeak = new_pstate;
351 }
352
353 int CpuCas01::getPstate()
354 {
355   return m_pstate;
356 }
357
358 /**********
359  * Action *
360  **********/
361
362 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double power, lmm_constraint_t constraint)
363  : CpuAction(model, cost, failed,
364                      lmm_variable_new(model->getMaxminSystem(), this,
365                      1.0, power, 1))
366 {
367   m_suspended = 0;
368   if (model->getUpdateMechanism() == UM_LAZY) {
369     m_indexHeap = -1;
370     m_lastUpdate = surf_get_clock();
371     m_lastValue = 0.0;
372   }
373   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
374 }