Logo AND Algorithmique Numérique Distribuée

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