Logo AND Algorithmique Numérique Distribuée

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