Logo AND Algorithmique Numérique Distribuée

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