Logo AND Algorithmique Numérique Distribuée

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