Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
04d4f3ea74bc9ef94f69ab6e87b4dd8a5bdb21a5
[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
37 namespace simgrid {
38 namespace surf {
39
40 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
41 {
42   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
43   int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
44
45   if (!strcmp(optim, "Full")) {
46     p_updateMechanism = UM_FULL;
47     m_selectiveUpdate = select;
48   } else if (!strcmp(optim, "Lazy")) {
49     p_updateMechanism = UM_LAZY;
50     m_selectiveUpdate = 1;
51     xbt_assert((select == 1)
52                ||
53                (xbt_cfg_is_default_value
54                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
55                "Disabling selective update while using the lazy update mechanism is dumb!");
56   } else {
57     xbt_die("Unsupported optimization (%s) for this model", optim);
58   }
59
60   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
61
62   p_maxminSystem = lmm_system_new(m_selectiveUpdate);
63
64   if (getUpdateMechanism() == UM_LAZY) {
65     p_actionHeap = xbt_heap_new(8, NULL);
66     xbt_heap_set_update_callback(p_actionHeap,  surf_action_lmm_update_index_heap);
67     p_modifiedSet = new ActionLmmList();
68     p_maxminSystem->keep_track = p_modifiedSet;
69   }
70 }
71
72 CpuCas01Model::~CpuCas01Model()
73 {
74   lmm_system_free(p_maxminSystem);
75   p_maxminSystem = NULL;
76
77   if (p_actionHeap)
78     xbt_heap_free(p_actionHeap);
79   delete p_modifiedSet;
80
81   surf_cpu_model_pm = NULL;
82
83   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
84 }
85
86 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
87                       int pstate, double speedScale,
88                           tmgr_trace_t speedTrace, int core,
89                           int initiallyOn,
90                           tmgr_trace_t state_trace)
91 {
92   xbt_assert(xbt_dynar_getfirst_as(speedPeak, double) > 0.0,
93       "Speed has to be >0.0. Did you forget to specify the mandatory power attribute?");
94   xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
95   Cpu *cpu = new CpuCas01(this, host, speedPeak, pstate, speedScale, speedTrace, core, initiallyOn, state_trace);
96   return cpu;
97 }
98
99 double CpuCas01Model::next_occuring_event_full(double /*now*/)
100 {
101   return Model::shareResourcesMaxMin(getRunningActionSet(), p_maxminSystem, lmm_solve);
102 }
103
104 void CpuCas01Model::addTraces()
105 {
106   THROW_DEADCODE;
107 }
108
109 /************
110  * Resource *
111  ************/
112 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
113                          int pstate, double speedScale, tmgr_trace_t speedTrace, int core,
114                          int initiallyOn, tmgr_trace_t stateTrace)
115 : Cpu(model, host,
116   lmm_constraint_new(model->getMaxminSystem(), this, core * speedScale * xbt_dynar_get_as(speedPeak, pstate, double)),
117   speedPeak, pstate,
118   core, xbt_dynar_get_as(speedPeak, pstate, double), speedScale,
119     initiallyOn) {
120
121   XBT_DEBUG("CPU create: peak=%f, pstate=%d", p_speed.peak, m_pstate);
122
123   m_core = core;
124   if (speedTrace)
125     p_speed.event = future_evt_set->add_trace(speedTrace, 0.0, this);
126
127   if (stateTrace)
128     p_stateEvent = future_evt_set->add_trace(stateTrace, 0.0, this);
129 }
130
131 CpuCas01::~CpuCas01()
132 {
133   if (getModel() == surf_cpu_model_pm)
134     xbt_dynar_free(&p_speedPeakList);
135 }
136
137 xbt_dynar_t CpuCas01::getSpeedPeakList(){
138   return p_speedPeakList;
139 }
140
141 bool CpuCas01::isUsed()
142 {
143   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
144 }
145
146 /** @brief take into account changes of speed (either load or max) */
147 void CpuCas01::onSpeedChange() {
148   lmm_variable_t var = NULL;
149   lmm_element_t elem = NULL;
150
151     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
152                                 m_core * p_speed.scale * p_speed.peak);
153     while ((var = lmm_get_var_from_cnst
154             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
155       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
156
157       lmm_update_variable_bound(getModel()->getMaxminSystem(),
158                                 action->getVariable(),
159                                 p_speed.scale * p_speed.peak);
160     }
161
162   Cpu::onSpeedChange();
163 }
164
165 void CpuCas01::apply_event(tmgr_trace_iterator_t event, double value)
166 {
167   lmm_variable_t var = NULL;
168   lmm_element_t elem = NULL;
169
170   if (event == p_speed.event) {
171     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
172     xbt_assert(m_core == 1, "FIXME: add speed scaling code also for constraint_core[i]");
173
174     p_speed.scale = value;
175     onSpeedChange();
176
177     tmgr_trace_event_unref(&p_speed.event);
178   } else if (event == p_stateEvent) {
179     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
180     xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
181
182     if (value > 0) {
183       if(isOff())
184         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
185       turnOn();
186     } else {
187       lmm_constraint_t cnst = getConstraint();
188       double date = surf_get_clock();
189
190       turnOff();
191
192       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
193         Action *action = static_cast<Action*>(lmm_variable_id(var));
194
195         if (action->getState() == SURF_ACTION_RUNNING ||
196             action->getState() == SURF_ACTION_READY ||
197             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
198           action->setFinishTime(date);
199           action->setState(SURF_ACTION_FAILED);
200         }
201       }
202     }
203     tmgr_trace_event_unref(&p_stateEvent);
204   } else {
205     xbt_die("Unknown event!\n");
206   }
207
208   return;
209 }
210
211 CpuAction *CpuCas01::execution_start(double size)
212 {
213
214   XBT_IN("(%s,%g)", getName(), size);
215   CpuCas01Action *action = new CpuCas01Action(getModel(), size, isOff(),
216       p_speed.scale * p_speed.peak, getConstraint());
217
218   XBT_OUT();
219   return action;
220 }
221
222 CpuAction *CpuCas01::sleep(double duration)
223 {
224   if (duration > 0)
225     duration = MAX(duration, sg_surf_precision);
226
227   XBT_IN("(%s,%g)", getName(), duration);
228   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, isOff(),
229       p_speed.scale * p_speed.peak, getConstraint());
230
231
232   // FIXME: sleep variables should not consume 1.0 in lmm_expand
233   action->m_maxDuration = duration;
234   action->m_suspended = 2;
235   if (duration == NO_MAX_DURATION) {
236     /* Move to the *end* of the corresponding action set. This convention
237        is used to speed up update_resource_state  */
238     action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
239     action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
240     action->getStateSet()->push_back(*action);
241   }
242
243   lmm_update_variable_weight(getModel()->getMaxminSystem(),
244       action->getVariable(), 0.0);
245   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
246     action->heapRemove(getModel()->getActionHeap());
247     // this is necessary for a variable with weight 0 since such
248     // variables are ignored in lmm and we need to set its max_duration
249     // correctly at the next call to share_resources
250     getModel()->getModifiedSet()->push_front(*action);
251   }
252
253   XBT_OUT();
254   return action;
255 }
256
257 /**********
258  * Action *
259  **********/
260
261 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double speed, lmm_constraint_t constraint)
262  : CpuAction(model, cost, failed,
263          lmm_variable_new(model->getMaxminSystem(), this,
264          1.0, speed, 1))
265 {
266   if (model->getUpdateMechanism() == UM_LAZY) {
267     m_indexHeap = -1;
268     m_lastUpdate = surf_get_clock();
269     m_lastValue = 0.0;
270   }
271   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
272 }
273
274 }
275 }