Logo AND Algorithmique Numérique Distribuée

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