1 /* Copyright (c) 2013-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
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
12 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
14 "Logging specific to the SURF cpu module");
16 int autoload_surf_cpu_model = 1;
17 void_f_void_t surf_cpu_model_init_preparse = NULL;
19 simgrid::surf::CpuModel *surf_cpu_model_pm;
20 simgrid::surf::CpuModel *surf_cpu_model_vm;
29 std::list<Cpu*> getActionCpus(CpuAction *action) {
30 std::list<Cpu*> retlist;
31 lmm_system_t sys = action->getModel()->getMaxminSystem();
32 int llen = lmm_get_number_of_cnst_from_var(sys, action->getVariable());
34 for(int i = 0; i<llen; i++) {
35 retlist.push_back( (Cpu*)(lmm_constraint_id( lmm_get_cnst_from_var(sys, action->getVariable(), i) )) );
40 simgrid::xbt::signal<void(CpuAction*, e_surf_action_state_t, e_surf_action_state_t)> cpuActionStateChangedCallbacks;
46 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
49 while ((xbt_heap_size(getActionHeap()) > 0)
50 && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
51 action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
52 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
53 if (TRACE_is_enabled()) {
54 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
55 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
56 lmm_variable_getvalue(action->getVariable()),
57 action->getLastUpdate(),
58 now - action->getLastUpdate());
62 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
64 /* set the remains to 0 due to precision problems when updating the remaining amount */
65 action->setRemains(0);
66 action->setState(SURF_ACTION_DONE);
67 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
69 if (TRACE_is_enabled()) {
70 //defining the last timestamp that we can safely dump to trace file
71 //without losing the event ascending order (considering all CPU's)
73 ActionList *actionSet = getRunningActionSet();
74 for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
75 ; it != itend ; ++it) {
76 action = static_cast<CpuAction*>(&*it);
78 smaller = action->getLastUpdate();
81 if (action->getLastUpdate() < smaller) {
82 smaller = action->getLastUpdate();
86 TRACE_last_timestamp_to_dump = smaller;
92 void CpuModel::updateActionsStateFull(double now, double delta)
94 CpuAction *action = NULL;
95 ActionList *running_actions = getRunningActionSet();
97 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
98 ; it != itend ; it=itNext) {
100 action = static_cast<CpuAction*>(&*it);
101 if (TRACE_is_enabled()) {
102 Cpu *x = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
104 TRACE_surf_host_set_utilization(x->getName(),
105 action->getCategory(),
106 lmm_variable_getvalue(action->getVariable()),
109 TRACE_last_timestamp_to_dump = now - delta;
112 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
115 if (action->getMaxDuration() != NO_MAX_DURATION)
116 action->updateMaxDuration(delta);
119 if ((action->getRemainsNoUpdate() <= 0) &&
120 (lmm_get_variable_weight(action->getVariable()) > 0)) {
122 action->setState(SURF_ACTION_DONE);
123 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
124 (action->getMaxDuration() <= 0)) {
126 action->setState(SURF_ACTION_DONE);
136 Cpu::Cpu(Model *model, simgrid::s4u::Host *host,
137 xbt_dynar_t speedPerPstate, int core)
138 : Cpu(model, host, NULL/*constraint*/, speedPerPstate, core)
142 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint,
143 xbt_dynar_t speedPerPstate, int core)
144 : Resource(model, host->name().c_str(), constraint)
148 speed_.peak = xbt_dynar_get_as(speedPerPstate, 0/*pstate*/, double);
150 host->pimpl_cpu = this;
151 xbt_assert(speed_.scale > 0, "Available speed has to be >0");
153 // Copy the power peak array:
154 speedPerPstate_ = xbt_dynar_new(sizeof(double), nullptr);
155 unsigned long n = xbt_dynar_length(speedPerPstate);
156 for (unsigned long i = 0; i != n; ++i) {
157 double value = xbt_dynar_get_as(speedPerPstate, i, double);
158 xbt_dynar_push(speedPerPstate_, &value);
161 /* Currently, we assume that a VM does not have a multicore CPU. */
163 xbt_assert(model == surf_cpu_model_pm);
165 if (model->getUpdateMechanism() != UM_UNDEFINED) {
166 p_constraintCore = xbt_new(lmm_constraint_t, core);
167 p_constraintCoreId = xbt_new(void*, core);
170 for (i = 0; i < core; i++) {
171 /* just for a unique id, never used as a string. */
172 p_constraintCoreId[i] = bprintf("%s:%i", host->name().c_str(), i);
173 p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], speed_.scale * speed_.peak);
180 if (p_constraintCoreId){
181 for (int i = 0; i < coresAmount_; i++) {
182 xbt_free(p_constraintCoreId[i]);
184 xbt_free(p_constraintCore);
186 if (p_constraintCoreId)
187 xbt_free(p_constraintCoreId);
189 xbt_dynar_free(&speedPerPstate_);
192 double Cpu::getCurrentPowerPeak()
197 int Cpu::getNbPStates()
199 return xbt_dynar_length(speedPerPstate_);
202 void Cpu::setPState(int pstate_index)
204 xbt_dynar_t plist = speedPerPstate_;
205 xbt_assert(pstate_index <= (int)xbt_dynar_length(plist),
206 "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index, (int)xbt_dynar_length(plist));
208 double new_peak_speed = xbt_dynar_get_as(plist, pstate_index, double);
209 pstate_ = pstate_index;
210 speed_.peak = new_peak_speed;
220 double Cpu::getPowerPeakAt(int pstate_index)
222 xbt_dynar_t plist = speedPerPstate_;
223 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
225 return xbt_dynar_get_as(plist, pstate_index, double);
228 double Cpu::getSpeed(double load)
230 return load * speed_.peak;
233 double Cpu::getAvailableSpeed()
235 /* number between 0 and 1 */
239 void Cpu::onSpeedChange() {
240 TRACE_surf_host_set_speed(surf_get_clock(), getName(),
241 coresAmount_ * speed_.scale * speed_.peak);
250 void Cpu::setStateTrace(tmgr_trace_t trace)
252 xbt_assert(stateEvent_==NULL,"Cannot set a second state trace to Host %s", host_->name().c_str());
254 stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
256 void Cpu::setSpeedTrace(tmgr_trace_t trace)
258 xbt_assert(speed_.event==NULL,"Cannot set a second speed trace to Host %s", host_->name().c_str());
260 speed_.event = future_evt_set->add_trace(trace, 0.0, this);
268 void CpuAction::updateRemainingLazy(double now)
272 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
273 "You're updating an action that is not running.");
275 /* bogus priority, skip it */
276 xbt_assert(getPriority() > 0,
277 "You're updating an action that seems suspended.");
279 delta = now - m_lastUpdate;
282 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
283 double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
285 if (TRACE_is_enabled()) {
286 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
287 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
289 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, m_remains);
293 m_lastValue = lmm_variable_getvalue(getVariable());
298 * This function formulates a constraint problem that pins a given task to
299 * particular cores. Currently, it is possible to pin a task to an exactly one
300 * specific core. The system links the variable object of the task to the
301 * per-core constraint object.
303 * But, the taskset command on Linux takes a mask value specifying a CPU
304 * affinity setting of a given task. If the mask value is 0x03, the given task
305 * will be executed on the first core (CPU0) or the second core (CPU1) on the
306 * given PM. The schedular will determine appropriate placements of tasks,
307 * considering given CPU affinities and task activities.
309 * How should the system formulate constraint problems for an affinity to
312 * The cpu argument must be the host where the task is being executed. The
313 * action object does not have the information about the location where the
314 * action is being executed.
316 void CpuAction::setAffinity(Cpu *cpu, unsigned long mask)
318 lmm_variable_t var_obj = getVariable();
319 XBT_IN("(%p,%lx)", this, mask);
322 unsigned long nbits = 0;
324 /* FIXME: There is much faster algorithms doing this. */
325 for (int i = 0; i < cpu->coresAmount_; i++) {
326 unsigned long has_affinity = (1UL << i) & mask;
332 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
333 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
338 for (int i = 0; i < cpu->coresAmount_; i++) {
339 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
340 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
342 unsigned long has_affinity = (1UL << i) & mask;
344 /* This function only accepts an affinity setting on the host where the
345 * task is now running. In future, a task might move to another host.
346 * But, at this moment, this function cannot take an affinity setting on
349 * It might be possible to extend the code to allow this function to
350 * accept affinity settings on a future host. We might be able to assign
351 * zero to elem->value to maintain such inactive affinity settings in the
352 * system. But, this will make the system complex. */
353 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
354 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
358 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
359 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
364 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, e_surf_action_state_t)> CpuAction::onStateChange;
366 void CpuAction::setState(e_surf_action_state_t state){
367 e_surf_action_state_t previous = getState();
368 Action::setState(state);
369 onStateChange(this, previous);