Logo AND Algorithmique Numérique Distribuée

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