Logo AND Algorithmique Numérique Distribuée

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