Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix bug #17132 (surf.c:366: The Impossible Did Happen (yet again)).
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 2013-2014. 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_interface.hpp"
8
9 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
11                                 "Logging specific to the SURF cpu module");
12
13 CpuModelPtr surf_cpu_model_pm;
14 CpuModelPtr surf_cpu_model_vm;
15
16 /*************
17  * Callbacks *
18  *************/
19
20 CpuPtr getActionCpu(CpuActionPtr action) {
21   return static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var
22                                          (action->getModel()->getMaxminSystem(),
23                                          action->getVariable(), 0)));
24 }
25
26 surf_callback(void, CpuPtr) cpuCreatedCallbacks;
27 surf_callback(void, CpuPtr) cpuDestructedCallbacks;
28 surf_callback(void, CpuPtr, e_surf_resource_state_t, e_surf_resource_state_t) cpuStateChangedCallbacks;
29 surf_callback(void, CpuActionPtr, e_surf_action_state_t, e_surf_action_state_t) cpuActionStateChangedCallbacks;
30
31 /*********
32  * Model *
33  *********/
34
35 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
36 {
37   CpuActionPtr action;
38   while ((xbt_heap_size(getActionHeap()) > 0)
39          && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
40     action = static_cast<CpuActionPtr>(static_cast<ActionPtr>(xbt_heap_pop(getActionHeap())));
41     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
42 #ifdef HAVE_TRACING
43     if (TRACE_is_enabled()) {
44       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
45       TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
46                                       lmm_variable_getvalue(action->getVariable()),
47                                       action->getLastUpdate(),
48                                       now - action->getLastUpdate());
49     }
50 #endif
51
52     action->finish();
53     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
54
55     /* set the remains to 0 due to precision problems when updating the remaining amount */
56     action->setRemains(0);
57     action->setState(SURF_ACTION_DONE);
58     action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
59   }
60 #ifdef HAVE_TRACING
61   if (TRACE_is_enabled()) {
62     //defining the last timestamp that we can safely dump to trace file
63     //without losing the event ascending order (considering all CPU's)
64     double smaller = -1;
65     ActionListPtr actionSet = getRunningActionSet();
66     for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
67        ; it != itend ; ++it) {
68       action = static_cast<CpuActionPtr>(&*it);
69         if (smaller < 0) {
70           smaller = action->getLastUpdate();
71           continue;
72         }
73         if (action->getLastUpdate() < smaller) {
74           smaller = action->getLastUpdate();
75         }
76     }
77     if (smaller > 0) {
78       TRACE_last_timestamp_to_dump = smaller;
79     }
80   }
81 #endif
82   return;
83 }
84
85 void CpuModel::updateActionsStateFull(double now, double delta)
86 {
87   CpuActionPtr action = NULL;
88   ActionListPtr running_actions = getRunningActionSet();
89
90   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
91      ; it != itend ; it=itNext) {
92         ++itNext;
93     action = static_cast<CpuActionPtr>(&*it);
94 #ifdef HAVE_TRACING
95     if (TRACE_is_enabled()) {
96       CpuPtr x = (CpuPtr) lmm_constraint_id(lmm_get_cnst_from_var
97                               (getMaxminSystem(), action->getVariable(), 0));
98
99       TRACE_surf_host_set_utilization(x->getName(),
100                                       action->getCategory(),
101                                       lmm_variable_getvalue(action->getVariable()),
102                                       now - delta,
103                                       delta);
104       TRACE_last_timestamp_to_dump = now - delta;
105     }
106 #endif
107
108     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
109
110
111     if (action->getMaxDuration() != NO_MAX_DURATION)
112       action->updateMaxDuration(delta);
113
114
115     if ((action->getRemainsNoUpdate() <= 0) &&
116         (lmm_get_variable_weight(action->getVariable()) > 0)) {
117       action->finish();
118       action->setState(SURF_ACTION_DONE);
119     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
120                (action->getMaxDuration() <= 0)) {
121       action->finish();
122       action->setState(SURF_ACTION_DONE);
123     }
124   }
125
126   return;
127 }
128
129 /************
130  * Resource *
131  ************/
132
133 Cpu::Cpu(){
134   surf_callback_emit(cpuCreatedCallbacks, this);
135 }
136
137 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
138                  int core, double powerPeak, double powerScale)
139  : Resource(model, name, props)
140  , m_core(core)
141  , m_powerPeak(powerPeak)
142  , m_powerScale(powerScale)
143  , p_constraintCore(NULL)
144  , p_constraintCoreId(NULL)
145 {
146   surf_callback_emit(cpuCreatedCallbacks, this);
147 }
148
149 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
150                  lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
151  : Resource(model, name, props, constraint)
152  , m_core(core)
153  , m_powerPeak(powerPeak)
154  , m_powerScale(powerScale)
155 {
156   surf_callback_emit(cpuCreatedCallbacks, this);
157   /* At now, we assume that a VM does not have a multicore CPU. */
158   if (core > 1)
159     xbt_assert(model == surf_cpu_model_pm);
160
161   p_constraintCore = NULL;
162   p_constraintCoreId = NULL;
163   if (model->getUpdateMechanism() != UM_UNDEFINED) {
164         p_constraintCore = xbt_new(lmm_constraint_t, core);
165         p_constraintCoreId = xbt_new(void*, core);
166
167     int i;
168     for (i = 0; i < core; i++) {
169       /* just for a unique id, never used as a string. */
170       p_constraintCoreId[i] = bprintf("%s:%i", name, i);
171       p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
172     }
173   }
174 }
175
176 Cpu::~Cpu(){
177   surf_callback_emit(cpuDestructedCallbacks, this);
178   if (p_constraintCoreId){
179     for (int i = 0; i < m_core; i++) {
180           xbt_free(p_constraintCoreId[i]);
181     }
182     xbt_free(p_constraintCore);
183   }
184   if (p_constraintCoreId)
185     xbt_free(p_constraintCoreId);
186 }
187
188 double Cpu::getSpeed(double load)
189 {
190   return load * m_powerPeak;
191 }
192
193 double Cpu::getAvailableSpeed()
194 {
195 /* number between 0 and 1 */
196   return m_powerScale;
197 }
198
199 int Cpu::getCore()
200 {
201   return m_core;
202 }
203
204 void Cpu::setState(e_surf_resource_state_t state)
205 {
206   e_surf_resource_state_t old = Resource::getState();
207   Resource::setState(state);
208   surf_callback_emit(cpuStateChangedCallbacks, this, old, state);
209 }
210 /**********
211  * Action *
212  **********/
213
214 void CpuAction::updateRemainingLazy(double now)
215 {
216   double delta = 0.0;
217
218   xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
219       "You're updating an action that is not running.");
220
221   /* bogus priority, skip it */
222   xbt_assert(getPriority() > 0,
223       "You're updating an action that seems suspended.");
224
225   delta = now - m_lastUpdate;
226
227   if (m_remains > 0) {
228     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
229     double_update(&(m_remains), m_lastValue * delta);
230
231 #ifdef HAVE_TRACING
232     if (TRACE_is_enabled()) {
233       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
234       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
235     }
236 #endif
237     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, m_remains);
238   }
239
240   m_lastUpdate = now;
241   m_lastValue = lmm_variable_getvalue(getVariable());
242 }
243
244 /*
245  *
246  * This function formulates a constraint problem that pins a given task to
247  * particular cores. Currently, it is possible to pin a task to an exactly one
248  * specific core. The system links the variable object of the task to the
249  * per-core constraint object.
250  *
251  * But, the taskset command on Linux takes a mask value specifying a CPU
252  * affinity setting of a given task. If the mask value is 0x03, the given task
253  * will be executed on the first core (CPU0) or the second core (CPU1) on the
254  * given PM. The schedular will determine appropriate placements of tasks,
255  * considering given CPU affinities and task activities.
256  *
257  * How should the system formulate constraint problems for an affinity to
258  * multiple cores?
259  *
260  * The cpu argument must be the host where the task is being executed. The
261  * action object does not have the information about the location where the
262  * action is being executed.
263  */
264 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
265 {
266   lmm_variable_t var_obj = getVariable();
267   XBT_IN("(%p,%lx)", this, mask);
268
269   {
270     unsigned long nbits = 0;
271
272     /* FIXME: There is much faster algorithms doing this. */
273     for (int i = 0; i < cpu->m_core; i++) {
274       unsigned long has_affinity = (1UL << i) & mask;
275       if (has_affinity)
276         nbits += 1;
277     }
278
279     if (nbits > 1) {
280       XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
281       XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
282       DIE_IMPOSSIBLE;
283     }
284   }
285
286   for (int i = 0; i < cpu->m_core; i++) {
287     XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i,  cpu->getName());
288     lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
289
290     unsigned long has_affinity = (1UL << i) & mask;
291     if (has_affinity) {
292       /* This function only accepts an affinity setting on the host where the
293        * task is now running. In future, a task might move to another host.
294        * But, at this moment, this function cannot take an affinity setting on
295        * that future host.
296        *
297        * It might be possible to extend the code to allow this function to
298        * accept affinity settings on a future host. We might be able to assign
299        * zero to elem->value to maintain such inactive affinity settings in the
300        * system. But, this will make the system complex. */
301       XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
302       lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
303     }
304   }
305
306   if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
307     /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
308   }
309   XBT_OUT();
310 }
311
312 void CpuAction::setState(e_surf_action_state_t state){
313   e_surf_action_state_t old = getState();
314   Action::setState(state);
315   surf_callback_emit(cpuActionStateChangedCallbacks, this, old, state);
316 }