Logo AND Algorithmique Numérique Distribuée

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