Logo AND Algorithmique Numérique Distribuée

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