Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
give a default value at field declaration, not everywhere in subclasses
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 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 <xbt/dynar.h>
8 #include "cpu_interface.hpp"
9 #include "plugins/energy.hpp"
10
11 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
13                                 "Logging specific to the SURF cpu module");
14
15 int autoload_surf_cpu_model = 1;
16 void_f_void_t surf_cpu_model_init_preparse = NULL;
17
18 simgrid::surf::CpuModel *surf_cpu_model_pm;
19 simgrid::surf::CpuModel *surf_cpu_model_vm;
20
21 namespace simgrid {
22 namespace surf {
23
24 simgrid::xbt::Extension<simgrid::Host, Cpu> Cpu::EXTENSION_ID;
25
26 void Cpu::classInit()
27 {
28   if (!EXTENSION_ID.valid())
29     EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Cpu>();
30 }
31
32 /*************
33  * Callbacks *
34  *************/
35
36 Cpu *getActionCpu(CpuAction *action) {
37   return static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var
38                                          (action->getModel()->getMaxminSystem(),
39                                          action->getVariable(), 0)));
40 }
41
42 simgrid::surf::signal<void(CpuAction*, e_surf_action_state_t, e_surf_action_state_t)> cpuActionStateChangedCallbacks;
43
44 void cpu_add_traces(){
45   surf_cpu_model_pm->addTraces();
46 }
47
48 /*********
49  * Model *
50  *********/
51
52 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
53 {
54   CpuAction *action;
55   while ((xbt_heap_size(getActionHeap()) > 0)
56          && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
57     action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
58     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
59     if (TRACE_is_enabled()) {
60       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
61       TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
62                                       lmm_variable_getvalue(action->getVariable()),
63                                       action->getLastUpdate(),
64                                       now - action->getLastUpdate());
65     }
66
67     action->finish();
68     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
69
70     /* set the remains to 0 due to precision problems when updating the remaining amount */
71     action->setRemains(0);
72     action->setState(SURF_ACTION_DONE);
73     action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
74   }
75   if (TRACE_is_enabled()) {
76     //defining the last timestamp that we can safely dump to trace file
77     //without losing the event ascending order (considering all CPU's)
78     double smaller = -1;
79     ActionList *actionSet = getRunningActionSet();
80     for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
81        ; it != itend ; ++it) {
82       action = static_cast<CpuAction*>(&*it);
83         if (smaller < 0) {
84           smaller = action->getLastUpdate();
85           continue;
86         }
87         if (action->getLastUpdate() < smaller) {
88           smaller = action->getLastUpdate();
89         }
90     }
91     if (smaller > 0) {
92       TRACE_last_timestamp_to_dump = smaller;
93     }
94   }
95   return;
96 }
97
98 void CpuModel::updateActionsStateFull(double now, double delta)
99 {
100   CpuAction *action = NULL;
101   ActionList *running_actions = getRunningActionSet();
102
103   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
104      ; it != itend ; it=itNext) {
105         ++itNext;
106     action = static_cast<CpuAction*>(&*it);
107     if (TRACE_is_enabled()) {
108       Cpu *x = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
109
110       TRACE_surf_host_set_utilization(x->getName(),
111                                       action->getCategory(),
112                                       lmm_variable_getvalue(action->getVariable()),
113                                       now - delta,
114                                       delta);
115       TRACE_last_timestamp_to_dump = now - delta;
116     }
117
118     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
119
120
121     if (action->getMaxDuration() != NO_MAX_DURATION)
122       action->updateMaxDuration(delta);
123
124
125     if ((action->getRemainsNoUpdate() <= 0) &&
126         (lmm_get_variable_weight(action->getVariable()) > 0)) {
127       action->finish();
128       action->setState(SURF_ACTION_DONE);
129     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
130                (action->getMaxDuration() <= 0)) {
131       action->finish();
132       action->setState(SURF_ACTION_DONE);
133     }
134   }
135
136   return;
137 }
138
139 /************
140  * Resource *
141  ************/
142 Cpu::Cpu()
143 {
144 }
145
146
147 Cpu::Cpu(Model *model, simgrid::Host *host,
148              xbt_dynar_t speedPeakList, int pstate,
149                  int core, double speedPeak, double speedScale,
150          e_surf_resource_state_t stateInitial)
151  : Resource(model, host->getName().c_str(), stateInitial)
152  , m_core(core)
153  , m_speedPeak(speedPeak)
154  , m_speedScale(speedScale)
155  , m_host(host)
156 {
157   host->extension_set(Cpu::EXTENSION_ID, this);
158
159   // Copy the power peak array:
160   p_speedPeakList = xbt_dynar_new(sizeof(double), nullptr);
161   unsigned long n = xbt_dynar_length(speedPeakList);
162   for (unsigned long i = 0; i != n; ++i) {
163     double value = xbt_dynar_get_as(speedPeakList, i, double);
164     xbt_dynar_push(p_speedPeakList, &value);
165   }
166
167   m_pstate = pstate;
168 }
169
170 Cpu::Cpu(Model *model, simgrid::Host *host, lmm_constraint_t constraint,
171               xbt_dynar_t speedPeakList, int pstate,
172                   int core, double speedPeak,
173         double speedScale, e_surf_resource_state_t stateInitial)
174  : Resource(model, host->getName().c_str(), constraint, stateInitial)
175  , m_core(core)
176  , m_speedPeak(speedPeak)
177  , m_speedScale(speedScale)
178  , m_host(host)
179 {
180   host->extension_set(Cpu::EXTENSION_ID, this);
181
182   // Copy the power peak array:
183   p_speedPeakList = xbt_dynar_new(sizeof(double), nullptr);
184   unsigned long n = xbt_dynar_length(speedPeakList);
185   for (unsigned long i = 0; i != n; ++i) {
186     double value = xbt_dynar_get_as(speedPeakList, i, double);
187     xbt_dynar_push(p_speedPeakList, &value);
188   }
189
190   m_pstate = pstate;
191
192   /* Currently, we assume that a VM does not have a multicore CPU. */
193   if (core > 1)
194     xbt_assert(model == surf_cpu_model_pm);
195
196   if (model->getUpdateMechanism() != UM_UNDEFINED) {
197         p_constraintCore = xbt_new(lmm_constraint_t, core);
198         p_constraintCoreId = xbt_new(void*, core);
199
200     int i;
201     for (i = 0; i < core; i++) {
202       /* just for a unique id, never used as a string. */
203       p_constraintCoreId[i] = bprintf("%s:%i", host->getName().c_str(), i);
204       p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_speedScale * m_speedPeak);
205     }
206   }
207 }
208
209 Cpu::Cpu(Model *model, simgrid::Host *host, lmm_constraint_t constraint,
210   xbt_dynar_t speedPeakList, int pstate,
211   int core, double speedPeak, double speedScale)
212 : Cpu(model, host, constraint, speedPeakList, pstate, core, speedPeak, speedScale, SURF_RESOURCE_ON)
213 {
214         xbt_assert(0,"FIXME: this constructor could be removed");
215 }
216
217 Cpu::Cpu(Model *model, simgrid::Host *host,
218   xbt_dynar_t speedPeakList, int pstate,
219   int core, double speedPeak, double speedScale)
220 : Cpu(model, host, speedPeakList, pstate, core, speedPeak, speedScale, SURF_RESOURCE_ON)
221 {}
222
223 Cpu::~Cpu()
224 {
225   if (p_constraintCoreId){
226     for (int i = 0; i < m_core; i++) {
227           xbt_free(p_constraintCoreId[i]);
228     }
229     xbt_free(p_constraintCore);
230   }
231   if (p_constraintCoreId)
232     xbt_free(p_constraintCoreId);
233 }
234
235 double Cpu::getCurrentPowerPeak()
236 {
237   return m_speedPeak;
238 }
239
240 int Cpu::getNbPStates()
241 {
242   return xbt_dynar_length(p_speedPeakList);
243 }
244
245 void Cpu::setPState(int pstate_index)
246 {
247   xbt_dynar_t plist = p_speedPeakList;
248   xbt_assert(pstate_index <= (int)xbt_dynar_length(plist),
249                   "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index, (int)xbt_dynar_length(plist));
250
251   double new_peak_speed = xbt_dynar_get_as(plist, pstate_index, double);
252   m_pstate = pstate_index;
253   m_speedPeak = new_peak_speed;
254 }
255
256 int Cpu::getPState()
257 {
258   return m_pstate;
259 }
260
261 double Cpu::getPowerPeakAt(int pstate_index)
262 {
263   xbt_dynar_t plist = p_speedPeakList;
264   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
265
266   return xbt_dynar_get_as(plist, pstate_index, double);
267 }
268
269 double Cpu::getSpeed(double load)
270 {
271   return load * m_speedPeak;
272 }
273
274 double Cpu::getAvailableSpeed()
275 {
276 /* number between 0 and 1 */
277   return m_speedScale;
278 }
279
280 int Cpu::getCore()
281 {
282   return m_core;
283 }
284
285 /**********
286  * Action *
287  **********/
288
289 void CpuAction::updateRemainingLazy(double now)
290 {
291   double delta = 0.0;
292
293   xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
294       "You're updating an action that is not running.");
295
296   /* bogus priority, skip it */
297   xbt_assert(getPriority() > 0,
298       "You're updating an action that seems suspended.");
299
300   delta = now - m_lastUpdate;
301
302   if (m_remains > 0) {
303     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
304     double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
305
306     if (TRACE_is_enabled()) {
307       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
308       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
309     }
310     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, m_remains);
311   }
312
313   m_lastUpdate = now;
314   m_lastValue = lmm_variable_getvalue(getVariable());
315 }
316
317 /*
318  *
319  * This function formulates a constraint problem that pins a given task to
320  * particular cores. Currently, it is possible to pin a task to an exactly one
321  * specific core. The system links the variable object of the task to the
322  * per-core constraint object.
323  *
324  * But, the taskset command on Linux takes a mask value specifying a CPU
325  * affinity setting of a given task. If the mask value is 0x03, the given task
326  * will be executed on the first core (CPU0) or the second core (CPU1) on the
327  * given PM. The schedular will determine appropriate placements of tasks,
328  * considering given CPU affinities and task activities.
329  *
330  * How should the system formulate constraint problems for an affinity to
331  * multiple cores?
332  *
333  * The cpu argument must be the host where the task is being executed. The
334  * action object does not have the information about the location where the
335  * action is being executed.
336  */
337 void CpuAction::setAffinity(Cpu *cpu, unsigned long mask)
338 {
339   lmm_variable_t var_obj = getVariable();
340   XBT_IN("(%p,%lx)", this, mask);
341
342   {
343     unsigned long nbits = 0;
344
345     /* FIXME: There is much faster algorithms doing this. */
346     for (int i = 0; i < cpu->m_core; i++) {
347       unsigned long has_affinity = (1UL << i) & mask;
348       if (has_affinity)
349         nbits += 1;
350     }
351
352     if (nbits > 1) {
353       XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
354       XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
355       DIE_IMPOSSIBLE;
356     }
357   }
358
359   for (int i = 0; i < cpu->m_core; i++) {
360     XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i,  cpu->getName());
361     lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
362
363     unsigned long has_affinity = (1UL << i) & mask;
364     if (has_affinity) {
365       /* This function only accepts an affinity setting on the host where the
366        * task is now running. In future, a task might move to another host.
367        * But, at this moment, this function cannot take an affinity setting on
368        * that future host.
369        *
370        * It might be possible to extend the code to allow this function to
371        * accept affinity settings on a future host. We might be able to assign
372        * zero to elem->value to maintain such inactive affinity settings in the
373        * system. But, this will make the system complex. */
374       XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
375       lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
376     }
377   }
378
379   if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
380     /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
381   }
382   XBT_OUT();
383 }
384
385 simgrid::surf::signal<void(simgrid::surf::CpuAction*, e_surf_action_state_t, e_surf_action_state_t)> CpuAction::onStateChange;
386
387 void CpuAction::setState(e_surf_action_state_t state){
388   e_surf_action_state_t old = getState();
389   Action::setState(state);
390   onStateChange(this, old, state);
391 }
392
393 }
394 }