Logo AND Algorithmique Numérique Distribuée

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