Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SURF Documentation
[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 surf_callback(void, CpuPtr) createCpuCallbacks;
20 surf_callback(void, CpuPtr) deleteCpuCallbacks;
21 surf_callback(void, CpuActionPtr) updateCpuActionCallbacks;
22
23 /*********
24  * Model *
25  *********/
26
27 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
28 {
29   CpuActionPtr action;
30   while ((xbt_heap_size(getActionHeap()) > 0)
31          && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
32     action = static_cast<CpuActionPtr>(static_cast<ActionPtr>(xbt_heap_pop(getActionHeap())));
33     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
34 #ifdef HAVE_TRACING
35     if (TRACE_is_enabled()) {
36       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
37       TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
38                                       lmm_variable_getvalue(action->getVariable()),
39                                       action->getLastUpdate(),
40                                       now - action->getLastUpdate());
41     }
42 #endif
43
44     action->finish();
45     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
46
47     surf_callback_emit(updateCpuActionCallbacks, 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->getRemains() <= 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     surf_callback_emit(updateCpuActionCallbacks, action);
119     //action->updateEnergy();
120   }
121
122   return;
123 }
124
125 /************
126  * Resource *
127  ************/
128
129 Cpu::Cpu(){
130   surf_callback_emit(createCpuCallbacks, this);
131 }
132
133 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
134                  int core, double powerPeak, double powerScale)
135  : Resource(model, name, props)
136  , m_core(core)
137  , m_powerPeak(powerPeak)
138  , m_powerScale(powerScale)
139  , p_constraintCore(NULL)
140  , p_constraintCoreId(NULL)
141 {
142   surf_callback_emit(createCpuCallbacks, this);
143 }
144
145 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
146                  lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
147  : Resource(model, name, props, constraint)
148  , m_core(core)
149  , m_powerPeak(powerPeak)
150  , m_powerScale(powerScale)
151 {
152   surf_callback_emit(createCpuCallbacks, this);
153   /* At now, we assume that a VM does not have a multicore CPU. */
154   if (core > 1)
155     xbt_assert(model == surf_cpu_model_pm);
156
157   p_constraintCore = NULL;
158   p_constraintCoreId = NULL;
159   if (model->getUpdateMechanism() != UM_UNDEFINED) {
160         p_constraintCore = xbt_new(lmm_constraint_t, core);
161         p_constraintCoreId = xbt_new(void*, core);
162
163     int i;
164     for (i = 0; i < core; i++) {
165       /* just for a unique id, never used as a string. */
166       p_constraintCoreId[i] = bprintf("%s:%i", name, i);
167       p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
168     }
169   }
170 }
171
172 Cpu::~Cpu(){
173   surf_callback_emit(deleteCpuCallbacks, this);
174   if (p_constraintCoreId){
175     for (int i = 0; i < m_core; i++) {
176           xbt_free(p_constraintCoreId[i]);
177     }
178     xbt_free(p_constraintCore);
179   }
180   if (p_constraintCoreId)
181     xbt_free(p_constraintCoreId);
182 }
183
184 double Cpu::getSpeed(double load)
185 {
186   return load * m_powerPeak;
187 }
188
189 double Cpu::getAvailableSpeed()
190 {
191 /* number between 0 and 1 */
192   return m_powerScale;
193 }
194
195 int Cpu::getCore()
196 {
197   return m_core;
198 }
199
200 /**********
201  * Action *
202  **********/
203
204 void CpuAction::updateRemainingLazy(double now)
205 {
206   double delta = 0.0;
207
208   xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
209       "You're updating an action that is not running.");
210
211   /* bogus priority, skip it */
212   xbt_assert(getPriority() > 0,
213       "You're updating an action that seems suspended.");
214
215   delta = now - m_lastUpdate;
216
217   if (m_remains > 0) {
218     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
219     double_update(&(m_remains), m_lastValue * delta);
220
221 #ifdef HAVE_TRACING
222     if (TRACE_is_enabled()) {
223       CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
224       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
225     }
226 #endif
227     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %lf", this, m_remains);
228   }
229
230   m_lastUpdate = now;
231   m_lastValue = lmm_variable_getvalue(getVariable());
232 }
233
234 void CpuAction::setBound(double bound)
235 {
236   XBT_IN("(%p,%g)", this, bound);
237   m_bound = bound;
238   lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), bound);
239
240   if (getModel()->getUpdateMechanism() == UM_LAZY)
241         heapRemove(getModel()->getActionHeap());
242   XBT_OUT();
243 }
244
245 /*
246  *
247  * This function formulates a constraint problem that pins a given task to
248  * particular cores. Currently, it is possible to pin a task to an exactly one
249  * specific core. The system links the variable object of the task to the
250  * per-core constraint object.
251  *
252  * But, the taskset command on Linux takes a mask value specifying a CPU
253  * affinity setting of a given task. If the mask value is 0x03, the given task
254  * will be executed on the first core (CPU0) or the second core (CPU1) on the
255  * given PM. The schedular will determine appropriate placements of tasks,
256  * considering given CPU affinities and task activities.
257  *
258  * How should the system formulate constraint problems for an affinity to
259  * multiple cores?
260  *
261  * The cpu argument must be the host where the task is being executed. The
262  * action object does not have the information about the location where the
263  * action is being executed.
264  */
265 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
266 {
267   lmm_variable_t var_obj = getVariable();
268   XBT_IN("(%p,%lx)", this, mask);
269
270   {
271     unsigned long nbits = 0;
272
273     /* FIXME: There is much faster algorithms doing this. */
274     for (int i = 0; i < cpu->m_core; i++) {
275       unsigned long has_affinity = (1UL << i) & mask;
276       if (has_affinity)
277         nbits += 1;
278     }
279
280     if (nbits > 1) {
281       XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
282       XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
283       DIE_IMPOSSIBLE;
284     }
285   }
286
287   for (int i = 0; i < cpu->m_core; i++) {
288     XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i,  cpu->getName());
289     lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
290
291     unsigned long has_affinity = (1UL << i) & mask;
292     if (has_affinity) {
293       /* This function only accepts an affinity setting on the host where the
294        * task is now running. In future, a task might move to another host.
295        * But, at this moment, this function cannot take an affinity setting on
296        * that future host.
297        *
298        * It might be possible to extend the code to allow this function to
299        * accept affinity settings on a future host. We might be able to assign
300        * zero to elem->value to maintain such inactive affinity settings in the
301        * system. But, this will make the system complex. */
302       XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
303       lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
304     }
305   }
306
307   if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
308     /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
309   }
310
311   XBT_OUT();
312 }