Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try again to find this lib.. and don't crash if not found
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011, 2013-2016. 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 #include "src/surf/platform.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu,
14                                 "Logging specific to the SURF CPU IMPROVED module");
15
16 /*********
17  * Model *
18  *********/
19 void surf_cpu_model_init_Cas01()
20 {
21   xbt_assert(!surf_cpu_model_pm);
22   xbt_assert(!surf_cpu_model_vm);
23
24   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
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   xbt_dynar_push(all_existing_models, &surf_cpu_model_pm);
32
33   surf_cpu_model_vm  = new simgrid::surf::CpuCas01Model();
34   xbt_dynar_push(all_existing_models, &surf_cpu_model_vm);
35
36   simgrid::surf::on_postparse.connect(simgrid::surf::cpu_add_traces);
37 }
38
39 namespace simgrid {
40 namespace surf {
41
42 CpuCas01Model::CpuCas01Model() : simgrid::surf::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   p_maxminSystem = lmm_system_new(m_selectiveUpdate);
65
66   if (getUpdateMechanism() == UM_LAZY) {
67     p_actionHeap = xbt_heap_new(8, NULL);
68     xbt_heap_set_update_callback(p_actionHeap,  surf_action_lmm_update_index_heap);
69     p_modifiedSet = new ActionLmmList();
70     p_maxminSystem->keep_track = p_modifiedSet;
71   }
72 }
73
74 CpuCas01Model::~CpuCas01Model()
75 {
76   lmm_system_free(p_maxminSystem);
77   p_maxminSystem = NULL;
78
79   if (p_actionHeap)
80     xbt_heap_free(p_actionHeap);
81   delete p_modifiedSet;
82
83   surf_cpu_model_pm = NULL;
84
85   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
86 }
87
88 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
89                                   int pstate, double speedScale,
90                           tmgr_trace_t speedTrace, int core,
91                           int initiallyOn,
92                           tmgr_trace_t state_trace)
93 {
94   xbt_assert(xbt_dynar_getfirst_as(speedPeak, double) > 0.0,
95       "Speed has to be >0.0. Did you forget to specify the mandatory power attribute?");
96   xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
97   Cpu *cpu = new CpuCas01(this, host, speedPeak, pstate, speedScale, speedTrace, core, initiallyOn, state_trace);
98   return cpu;
99 }
100
101 double CpuCas01Model::shareResourcesFull(double /*now*/)
102 {
103   return Model::shareResourcesMaxMin(getRunningActionSet(),
104                              p_maxminSystem, lmm_solve);
105 }
106
107 void CpuCas01Model::addTraces()
108 {
109   xbt_dict_cursor_t cursor = NULL;
110   char *trace_name, *elm;
111   static int called = 0;
112   if (called)
113     return;
114   called = 1;
115
116   /* connect all traces relative to hosts */
117   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
118     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
119     CpuCas01 *host = static_cast<CpuCas01*>(sg_host_by_name(elm)->pimpl_cpu);
120
121     xbt_assert(host, "Host %s undefined", elm);
122     xbt_assert(trace, "Trace %s undefined", trace_name);
123
124     host->setStateEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
125   }
126
127   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
128     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
129     CpuCas01 *host = static_cast<CpuCas01*>(sg_host_by_name(elm)->pimpl_cpu);
130
131     xbt_assert(host, "Host %s undefined", elm);
132     xbt_assert(trace, "Trace %s undefined", trace_name);
133
134     host->setPowerEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
135   }
136 }
137
138 /************
139  * Resource *
140  ************/
141 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
142                          int pstate, double speedScale, tmgr_trace_t speedTrace, int core,
143                          int initiallyOn, tmgr_trace_t stateTrace)
144 : Cpu(model, host,
145         lmm_constraint_new(model->getMaxminSystem(), this, core * speedScale * xbt_dynar_get_as(speedPeak, pstate, double)),
146         speedPeak, pstate,
147         core, xbt_dynar_get_as(speedPeak, pstate, double), speedScale,
148     initiallyOn) {
149   p_speedEvent = NULL;
150
151   XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_speedPeak, m_pstate);
152
153   m_core = core;
154   if (speedTrace)
155     p_speedEvent = tmgr_history_add_trace(history, speedTrace, 0.0, 0, this);
156
157   if (stateTrace)
158     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
159 }
160
161 CpuCas01::~CpuCas01()
162 {
163   if (getModel() == surf_cpu_model_pm)
164     xbt_dynar_free(&p_speedPeakList);
165 }
166
167 void CpuCas01::setStateEvent(tmgr_trace_event_t stateEvent)
168 {
169   p_stateEvent = stateEvent;
170 }
171
172 void CpuCas01::setPowerEvent(tmgr_trace_event_t powerEvent)
173 {
174   p_speedEvent = powerEvent;
175 }
176
177 xbt_dynar_t CpuCas01::getSpeedPeakList(){
178   return p_speedPeakList;
179 }
180
181 bool CpuCas01::isUsed()
182 {
183   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
184 }
185
186 /** @brief take into account changes of speed (either load or max) */
187 void CpuCas01::onSpeedChange() {
188         lmm_variable_t var = NULL;
189         lmm_element_t elem = NULL;
190
191     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
192                                 m_core * m_speedScale * m_speedPeak);
193     while ((var = lmm_get_var_from_cnst
194             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
195       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
196
197       lmm_update_variable_bound(getModel()->getMaxminSystem(),
198                                 action->getVariable(),
199                                 m_speedScale * m_speedPeak);
200     }
201
202         Cpu::onSpeedChange();
203 }
204
205 void CpuCas01::updateState(tmgr_trace_event_t event_type, double value, double date)
206 {
207   lmm_variable_t var = NULL;
208   lmm_element_t elem = NULL;
209
210   if (event_type == p_speedEvent) {
211         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
212         xbt_assert(m_core == 1, "FIXME: add speed scaling code also for constraint_core[i]");
213
214     m_speedScale = value;
215     onSpeedChange();
216
217     if (tmgr_trace_event_free(event_type))
218       p_speedEvent = NULL;
219   } else if (event_type == p_stateEvent) {
220         /* TODO (Hypervisor): do the same thing for constraint_core[i] */
221     xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
222
223     if (value > 0) {
224       if(isOff())
225         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
226       turnOn();
227     } else {
228       lmm_constraint_t cnst = getConstraint();
229
230       turnOff();
231
232       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
233         Action *action = static_cast<Action*>(lmm_variable_id(var));
234
235         if (action->getState() == SURF_ACTION_RUNNING ||
236             action->getState() == SURF_ACTION_READY ||
237             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
238           action->setFinishTime(date);
239           action->setState(SURF_ACTION_FAILED);
240         }
241       }
242     }
243     if (tmgr_trace_event_free(event_type))
244       p_stateEvent = NULL;
245   } else {
246     XBT_CRITICAL("Unknown event ! \n");
247     xbt_abort();
248   }
249
250   return;
251 }
252
253 CpuAction *CpuCas01::execute(double size)
254 {
255
256   XBT_IN("(%s,%g)", getName(), size);
257   CpuCas01Action *action = new CpuCas01Action(getModel(), size, isOff(),
258                                                               m_speedScale * m_speedPeak, getConstraint());
259
260   XBT_OUT();
261   return action;
262 }
263
264 CpuAction *CpuCas01::sleep(double duration)
265 {
266   if (duration > 0)
267     duration = MAX(duration, sg_surf_precision);
268
269   XBT_IN("(%s,%g)", getName(), duration);
270   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, isOff(),
271                                                       m_speedScale * m_speedPeak, getConstraint());
272
273
274   // FIXME: sleep variables should not consume 1.0 in lmm_expand
275   action->m_maxDuration = duration;
276   action->m_suspended = 2;
277   if (duration == NO_MAX_DURATION) {
278     /* Move to the *end* of the corresponding action set. This convention
279        is used to speed up update_resource_state  */
280         action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
281     action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
282     action->getStateSet()->push_back(*action);
283   }
284
285   lmm_update_variable_weight(getModel()->getMaxminSystem(),
286                              action->getVariable(), 0.0);
287   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
288     action->heapRemove(getModel()->getActionHeap());
289     // this is necessary for a variable with weight 0 since such
290     // variables are ignored in lmm and we need to set its max_duration
291     // correctly at the next call to share_resources
292     getModel()->getModifiedSet()->push_front(*action);
293   }
294
295   XBT_OUT();
296   return action;
297 }
298
299 /**********
300  * Action *
301  **********/
302
303 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double speed, lmm_constraint_t constraint)
304  : CpuAction(model, cost, failed,
305                      lmm_variable_new(model->getMaxminSystem(), this,
306                      1.0, speed, 1))
307 {
308   if (model->getUpdateMechanism() == UM_LAZY) {
309     m_indexHeap = -1;
310     m_lastUpdate = surf_get_clock();
311     m_lastValue = 0.0;
312   }
313   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
314 }
315
316 }
317 }