Logo AND Algorithmique Numérique Distribuée

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