Logo AND Algorithmique Numérique Distribuée

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