Logo AND Algorithmique Numérique Distribuée

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