Logo AND Algorithmique Numérique Distribuée

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