Logo AND Algorithmique Numérique Distribuée

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