Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further rename power to speed in surf internals
[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(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
109       "Power has to be >0.0. Did you forget to specify the mandatory power attribute?");
110   xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
111
112   cpu = new CpuCas01(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
113   sg_host_surfcpu_register(host, cpu);
114   return cpu;
115 }
116
117 double CpuCas01Model::shareResourcesFull(double /*now*/)
118 {
119   return Model::shareResourcesMaxMin(getRunningActionSet(),
120                              p_maxminSystem, lmm_solve);
121 }
122
123 void CpuCas01Model::addTraces()
124 {
125   xbt_dict_cursor_t cursor = NULL;
126   char *trace_name, *elm;
127   static int called = 0;
128   if (called)
129     return;
130   called = 1;
131
132   /* connect all traces relative to hosts */
133   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
134     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
135     CpuCas01 *host = static_cast<CpuCas01*>(sg_host_surfcpu(sg_host_by_name(elm)));
136
137     xbt_assert(host, "Host %s undefined", elm);
138     xbt_assert(trace, "Trace %s undefined", trace_name);
139
140     host->setStateEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
141   }
142
143   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
144     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
145     CpuCas01 *host = static_cast<CpuCas01*>(sg_host_surfcpu(sg_host_by_name(elm)));
146
147     xbt_assert(host, "Host %s undefined", elm);
148     xbt_assert(trace, "Trace %s undefined", trace_name);
149
150     host->setPowerEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
151   }
152 }
153
154 /************
155  * Resource *
156  ************/
157 CpuCas01::CpuCas01(CpuCas01Model *model, const char *name, xbt_dynar_t powerPeak,
158                          int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
159                          e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
160                          xbt_dict_t properties)
161 : Cpu(model, name, properties,
162           lmm_constraint_new(model->getMaxminSystem(), this, core * powerScale * xbt_dynar_get_as(powerPeak, pstate, double)),
163           core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale,
164     stateInitial) {
165   p_speedEvent = NULL;
166
167   // Copy the power peak array:
168   p_speedPeakList = xbt_dynar_new(sizeof(double), nullptr);
169   unsigned long n = xbt_dynar_length(powerPeak);
170   for (unsigned long i = 0; i != n; ++i) {
171     double value = xbt_dynar_get_as(powerPeak, i, double);
172     xbt_dynar_push(p_speedPeakList, &value);
173   }
174
175   m_pstate = pstate;
176
177   XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_speedPeak, m_pstate);
178
179   m_core = core;
180   if (powerTrace)
181     p_speedEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, this);
182
183   if (stateTrace)
184     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
185 }
186
187 CpuCas01::~CpuCas01(){
188   if (getModel() == surf_cpu_model_pm)
189     xbt_dynar_free(&p_speedPeakList);
190 }
191
192 void CpuCas01::setStateEvent(tmgr_trace_event_t stateEvent)
193 {
194   p_stateEvent = stateEvent;
195 }
196
197 void CpuCas01::setPowerEvent(tmgr_trace_event_t powerEvent)
198 {
199   p_speedEvent = powerEvent;
200 }
201
202 xbt_dynar_t CpuCas01::getPowerPeakList(){
203   return p_speedPeakList;
204 }
205
206 int CpuCas01::getPState()
207 {
208   return m_pstate;
209 }
210
211 bool CpuCas01::isUsed()
212 {
213   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
214 }
215
216 void CpuCas01::updateState(tmgr_trace_event_t event_type, double value, double date)
217 {
218   lmm_variable_t var = NULL;
219   lmm_element_t elem = NULL;
220
221   if (event_type == p_speedEvent) {
222         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
223         xbt_assert(m_core == 1, "FIXME: add power scaling code also for constraint_core[i]");
224
225     m_speedScale = value;
226     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
227                                 m_core * m_speedScale *
228                                 m_speedPeak);
229     TRACE_surf_host_set_power(date, getName(),
230                               m_core * m_speedScale *
231                               m_speedPeak);
232     while ((var = lmm_get_var_from_cnst
233             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
234       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
235
236       lmm_update_variable_bound(getModel()->getMaxminSystem(),
237                                 action->getVariable(),
238                                 m_speedScale * m_speedPeak);
239     }
240     if (tmgr_trace_event_free(event_type))
241       p_speedEvent = NULL;
242   } else if (event_type == p_stateEvent) {
243         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
244     xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
245
246     if (value > 0) {
247       if(getState() == SURF_RESOURCE_OFF)
248         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
249       setState(SURF_RESOURCE_ON);
250     } else {
251       lmm_constraint_t cnst = getConstraint();
252
253       setState(SURF_RESOURCE_OFF);
254
255       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
256         Action *action = static_cast<Action*>(lmm_variable_id(var));
257
258         if (action->getState() == SURF_ACTION_RUNNING ||
259             action->getState() == SURF_ACTION_READY ||
260             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
261           action->setFinishTime(date);
262           action->setState(SURF_ACTION_FAILED);
263         }
264       }
265     }
266     if (tmgr_trace_event_free(event_type))
267       p_stateEvent = NULL;
268   } else {
269     XBT_CRITICAL("Unknown event ! \n");
270     xbt_abort();
271   }
272
273   return;
274 }
275
276 CpuAction *CpuCas01::execute(double size)
277 {
278
279   XBT_IN("(%s,%g)", getName(), size);
280   CpuCas01Action *action = new CpuCas01Action(getModel(), size, getState() != SURF_RESOURCE_ON,
281                                                               m_speedScale * m_speedPeak, getConstraint());
282
283   XBT_OUT();
284   return action;
285 }
286
287 CpuAction *CpuCas01::sleep(double duration)
288 {
289   if (duration > 0)
290     duration = MAX(duration, sg_surf_precision);
291
292   XBT_IN("(%s,%g)", getName(), duration);
293   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, getState() != SURF_RESOURCE_ON,
294                                                       m_speedScale * m_speedPeak, getConstraint());
295
296
297   // FIXME: sleep variables should not consume 1.0 in lmm_expand
298   action->m_maxDuration = duration;
299   action->m_suspended = 2;
300   if (duration == NO_MAX_DURATION) {
301     /* Move to the *end* of the corresponding action set. This convention
302        is used to speed up update_resource_state  */
303         action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
304     action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
305     action->getStateSet()->push_back(*action);
306   }
307
308   lmm_update_variable_weight(getModel()->getMaxminSystem(),
309                              action->getVariable(), 0.0);
310   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
311     action->heapRemove(getModel()->getActionHeap());
312     // this is necessary for a variable with weight 0 since such
313     // variables are ignored in lmm and we need to set its max_duration
314     // correctly at the next call to share_resources
315     getModel()->getModifiedSet()->push_front(*action);
316   }
317
318   XBT_OUT();
319   return action;
320 }
321
322 double CpuCas01::getCurrentPowerPeak()
323 {
324   return m_speedPeak;
325 }
326
327 double CpuCas01::getPowerPeakAt(int pstate_index)
328 {
329   xbt_dynar_t plist = p_speedPeakList;
330   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
331
332   return xbt_dynar_get_as(plist, pstate_index, double);
333 }
334
335 int CpuCas01::getNbPstates()
336 {
337   return xbt_dynar_length(p_speedPeakList);
338 }
339
340 void CpuCas01::setPstate(int pstate_index)
341 {
342   xbt_dynar_t plist = p_speedPeakList;
343   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
344
345   double new_pstate = xbt_dynar_get_as(plist, pstate_index, double);
346   m_pstate = pstate_index;
347   m_speedPeak = new_pstate;
348 }
349
350 int CpuCas01::getPstate()
351 {
352   return m_pstate;
353 }
354
355 /**********
356  * Action *
357  **********/
358
359 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double power, lmm_constraint_t constraint)
360  : CpuAction(model, cost, failed,
361                      lmm_variable_new(model->getMaxminSystem(), this,
362                      1.0, power, 1))
363 {
364   m_suspended = 0;
365   if (model->getUpdateMechanism() == UM_LAZY) {
366     m_indexHeap = -1;
367     m_lastUpdate = surf_get_clock();
368     m_lastValue = 0.0;
369   }
370   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
371 }