Logo AND Algorithmique Numérique Distribuée

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