Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Remove SIMIX_process_create_from_wrapper()
[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 #include "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
11
12 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
14
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 /*************
24  * Callbacks *
25  *************/
26
27 simgrid::xbt::signal<void(CpuAction*, Action::State, Action::State)> cpuActionStateChangedCallbacks;
28
29 /*********
30  * Model *
31  *********/
32
33 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
34 {
35   CpuAction *action;
36   while ((xbt_heap_size(getActionHeap()) > 0)
37          && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
38     action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
39     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
40     if (TRACE_is_enabled()) {
41       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
42       TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
43                                       lmm_variable_getvalue(action->getVariable()),
44                                       action->getLastUpdate(),
45                                       now - action->getLastUpdate());
46     }
47
48     action->finish();
49     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
50
51     /* set the remains to 0 due to precision problems when updating the remaining amount */
52     action->setRemains(0);
53     action->setState(Action::State::done);
54     action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
55   }
56   if (TRACE_is_enabled()) {
57     //defining the last timestamp that we can safely dump to trace file
58     //without losing the event ascending order (considering all CPU's)
59     double smaller = -1;
60     ActionList *actionSet = getRunningActionSet();
61     for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
62        ; it != itend ; ++it) {
63       action = static_cast<CpuAction*>(&*it);
64         if (smaller < 0) {
65           smaller = action->getLastUpdate();
66           continue;
67         }
68         if (action->getLastUpdate() < smaller) {
69           smaller = action->getLastUpdate();
70         }
71     }
72     if (smaller > 0) {
73       TRACE_last_timestamp_to_dump = smaller;
74     }
75   }
76   return;
77 }
78
79 void CpuModel::updateActionsStateFull(double now, double delta)
80 {
81   CpuAction *action = NULL;
82   ActionList *running_actions = getRunningActionSet();
83
84   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
85      ; it != itend ; it=itNext) {
86     ++itNext;
87     action = static_cast<CpuAction*>(&*it);
88     if (TRACE_is_enabled()) {
89       Cpu *cpu = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
90
91       TRACE_surf_host_set_utilization(cpu->getName(),
92                                       action->getCategory(),
93                                       lmm_variable_getvalue(action->getVariable()),
94                                       now - delta,
95                                       delta);
96       TRACE_last_timestamp_to_dump = now - delta;
97     }
98
99     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
100
101
102     if (action->getMaxDuration() != NO_MAX_DURATION)
103       action->updateMaxDuration(delta);
104
105
106     if ((action->getRemainsNoUpdate() <= 0) &&
107         (lmm_get_variable_weight(action->getVariable()) > 0)) {
108       action->finish();
109       action->setState(Action::State::done);
110     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
111                (action->getMaxDuration() <= 0)) {
112       action->finish();
113       action->setState(Action::State::done);
114     }
115   }
116 }
117
118 /************
119  * Resource *
120  ************/
121 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core)
122  : Cpu(model, host, NULL/*constraint*/, speedPerPstate, core)
123 {
124 }
125
126 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint,
127     xbt_dynar_t speedPerPstate, int core)
128  : Resource(model, host->name().c_str(), constraint)
129  , coresAmount_(core)
130  , host_(host)
131 {
132   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->name().c_str());
133
134   speed_.peak = xbt_dynar_get_as(speedPerPstate, 0/*pstate*/, double);
135   speed_.scale = 1;
136   host->pimpl_cpu = this;
137   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->name().c_str());
138
139   // Copy the power peak array:
140   speedPerPstate_ = xbt_dynar_new(sizeof(double), nullptr);
141   unsigned long n = xbt_dynar_length(speedPerPstate);
142   for (unsigned long i = 0; i != n; ++i) {
143     double value = xbt_dynar_get_as(speedPerPstate, i, double);
144     xbt_dynar_push(speedPerPstate_, &value);
145   }
146
147   xbt_assert(model == surf_cpu_model_pm || core==1, "Currently, VM cannot be multicore");
148
149   if (model->getUpdateMechanism() != UM_UNDEFINED) {
150     p_constraintCore = xbt_new(lmm_constraint_t, core);
151     p_constraintCoreId = xbt_new(void*, core);
152
153     for (int i = 0; i < core; i++) {
154       /* just for a unique id, never used as a string. */
155       p_constraintCoreId[i] = bprintf("%s:%i", host->name().c_str(), i);
156       p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], speed_.scale * speed_.peak);
157     }
158   }
159 }
160
161 Cpu::~Cpu()
162 {
163   if (p_constraintCoreId){
164     for (int i = 0; i < coresAmount_; i++)
165       xbt_free(p_constraintCoreId[i]);
166     xbt_free(p_constraintCore);
167   }
168   xbt_free(p_constraintCoreId);
169   xbt_dynar_free(&speedPerPstate_);
170 }
171
172 double Cpu::getCurrentPowerPeak()
173 {
174   return speed_.peak;
175 }
176
177 int Cpu::getNbPStates()
178 {
179   return xbt_dynar_length(speedPerPstate_);
180 }
181
182 void Cpu::setPState(int pstate_index)
183 {
184   xbt_dynar_t plist = speedPerPstate_;
185   xbt_assert(pstate_index <= (int)xbt_dynar_length(plist),
186       "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index, (int)xbt_dynar_length(plist));
187
188   double new_peak_speed = xbt_dynar_get_as(plist, pstate_index, double);
189   pstate_ = pstate_index;
190   speed_.peak = new_peak_speed;
191
192   onSpeedChange();
193 }
194
195 int Cpu::getPState()
196 {
197   return pstate_;
198 }
199
200 double Cpu::getPowerPeakAt(int pstate_index)
201 {
202   xbt_dynar_t plist = speedPerPstate_;
203   xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
204
205   return xbt_dynar_get_as(plist, pstate_index, double);
206 }
207
208 double Cpu::getSpeed(double load)
209 {
210   return load * speed_.peak;
211 }
212
213 double Cpu::getAvailableSpeed()
214 {
215 /* number between 0 and 1 */
216   return speed_.scale;
217 }
218
219 void Cpu::onSpeedChange() {
220   TRACE_surf_host_set_speed(surf_get_clock(), getName(), coresAmount_ * speed_.scale * speed_.peak);
221 }
222
223
224 int Cpu::getCore()
225 {
226   return coresAmount_;
227 }
228
229 void Cpu::setStateTrace(tmgr_trace_t trace)
230 {
231   xbt_assert(stateEvent_==NULL,"Cannot set a second state trace to Host %s", host_->name().c_str());
232
233   stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
234 }
235 void Cpu::setSpeedTrace(tmgr_trace_t trace)
236 {
237   xbt_assert(speed_.event==NULL,"Cannot set a second speed trace to Host %s", host_->name().c_str());
238
239   speed_.event = future_evt_set->add_trace(trace, 0.0, this);
240 }
241
242
243 /**********
244  * Action *
245  **********/
246
247 void CpuAction::updateRemainingLazy(double now)
248 {
249   xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
250   xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
251
252   double delta = now - lastUpdate_;
253
254   if (remains_ > 0) {
255     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
256     double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
257
258     if (TRACE_is_enabled()) {
259       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
260       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), lastValue_, lastUpdate_, now - lastUpdate_);
261     }
262     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, remains_);
263   }
264
265   lastUpdate_ = now;
266   lastValue_ = lmm_variable_getvalue(getVariable());
267 }
268
269 /*
270  *
271  * This function formulates a constraint problem that pins a given task to
272  * particular cores. Currently, it is possible to pin a task to an exactly one
273  * specific core. The system links the variable object of the task to the
274  * per-core constraint object.
275  *
276  * But, the taskset command on Linux takes a mask value specifying a CPU
277  * affinity setting of a given task. If the mask value is 0x03, the given task
278  * will be executed on the first core (CPU0) or the second core (CPU1) on the
279  * given PM. The schedular will determine appropriate placements of tasks,
280  * considering given CPU affinities and task activities.
281  *
282  * How should the system formulate constraint problems for an affinity to
283  * multiple cores?
284  *
285  * The cpu argument must be the host where the task is being executed. The
286  * action object does not have the information about the location where the
287  * action is being executed.
288  */
289 void CpuAction::setAffinity(Cpu *cpu, unsigned long mask)
290 {
291   lmm_variable_t var_obj = getVariable();
292   XBT_IN("(%p,%lx)", this, mask);
293
294   {
295     unsigned long nbits = 0;
296
297     /* FIXME: There is much faster algorithms doing this. */
298     for (int i = 0; i < cpu->coresAmount_; i++) {
299       unsigned long has_affinity = (1UL << i) & mask;
300       if (has_affinity)
301         nbits += 1;
302     }
303
304     xbt_assert(nbits <= 1, "Affinity mask cannot span over multiple cores.");
305   }
306
307   for (int i = 0; i < cpu->coresAmount_; 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 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
334
335 void CpuAction::setState(Action::State state){
336   Action::State previous = getState();
337   Action::setState(state);
338   onStateChange(this, previous);
339 }
340 std::list<Cpu*> CpuAction::cpus() {
341   std::list<Cpu*> retlist;
342   lmm_system_t sys = getModel()->getMaxminSystem();
343   int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
344
345   for(int i = 0; i<llen; i++)
346     retlist.push_back( (Cpu*)(lmm_constraint_id( lmm_get_cnst_from_var(sys, getVariable(), i) )) );
347
348   return retlist;
349 }
350
351 }
352 }