Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc++' into mc-merge
[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) cpuStateChangedCallbacks;
29 surf_callback(void, CpuActionPtr) 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   Resource::setState(state);
207   surf_callback_emit(cpuStateChangedCallbacks, this);
208 }
209 /**********
210  * Action *
211  **********/
212
213 void CpuAction::updateRemainingLazy(double now)
214 {
215   double delta = 0.0;
216
217   xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
218       "You're updating an action that is not running.");
219
220   /* bogus priority, skip it */
221   xbt_assert(getPriority() > 0,
222       "You're updating an action that seems suspended.");
223
224   delta = now - m_lastUpdate;
225
226   if (m_remains > 0) {
227     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
228     double_update(&(m_remains), m_lastValue * delta);
229
230 #ifdef HAVE_TRACING
231     if (TRACE_is_enabled()) {
232       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
233       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
234     }
235 #endif
236     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, m_remains);
237   }
238
239   m_lastUpdate = now;
240   m_lastValue = lmm_variable_getvalue(getVariable());
241 }
242
243 /*
244  *
245  * This function formulates a constraint problem that pins a given task to
246  * particular cores. Currently, it is possible to pin a task to an exactly one
247  * specific core. The system links the variable object of the task to the
248  * per-core constraint object.
249  *
250  * But, the taskset command on Linux takes a mask value specifying a CPU
251  * affinity setting of a given task. If the mask value is 0x03, the given task
252  * will be executed on the first core (CPU0) or the second core (CPU1) on the
253  * given PM. The schedular will determine appropriate placements of tasks,
254  * considering given CPU affinities and task activities.
255  *
256  * How should the system formulate constraint problems for an affinity to
257  * multiple cores?
258  *
259  * The cpu argument must be the host where the task is being executed. The
260  * action object does not have the information about the location where the
261  * action is being executed.
262  */
263 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
264 {
265   lmm_variable_t var_obj = getVariable();
266   XBT_IN("(%p,%lx)", this, mask);
267
268   {
269     unsigned long nbits = 0;
270
271     /* FIXME: There is much faster algorithms doing this. */
272     for (int i = 0; i < cpu->m_core; i++) {
273       unsigned long has_affinity = (1UL << i) & mask;
274       if (has_affinity)
275         nbits += 1;
276     }
277
278     if (nbits > 1) {
279       XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
280       XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
281       DIE_IMPOSSIBLE;
282     }
283   }
284
285   for (int i = 0; i < cpu->m_core; i++) {
286     XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i,  cpu->getName());
287     lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
288
289     unsigned long has_affinity = (1UL << i) & mask;
290     if (has_affinity) {
291       /* This function only accepts an affinity setting on the host where the
292        * task is now running. In future, a task might move to another host.
293        * But, at this moment, this function cannot take an affinity setting on
294        * that future host.
295        *
296        * It might be possible to extend the code to allow this function to
297        * accept affinity settings on a future host. We might be able to assign
298        * zero to elem->value to maintain such inactive affinity settings in the
299        * system. But, this will make the system complex. */
300       XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
301       lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
302     }
303   }
304
305   if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
306     /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
307   }
308   XBT_OUT();
309 }
310
311 void CpuAction::setState(e_surf_action_state_t state){
312   Action::setState(state);
313   surf_callback_emit(cpuActionStateChangedCallbacks, this);
314 }