Logo AND Algorithmique Numérique Distribuée

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