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, "Logging specific to the SURF cpu module");
15 void_f_void_t surf_cpu_model_init_preparse = nullptr;
17 simgrid::surf::CpuModel *surf_cpu_model_pm;
18 simgrid::surf::CpuModel *surf_cpu_model_vm;
27 simgrid::xbt::signal<void(CpuAction*, Action::State, Action::State)> cpuActionStateChangedCallbacks;
33 CpuModel::~CpuModel() {}
35 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
38 while ((xbt_heap_size(getActionHeap()) > 0)
39 && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
40 action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
41 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
42 if (TRACE_is_enabled()) {
43 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
44 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
45 lmm_variable_getvalue(action->getVariable()),
46 action->getLastUpdate(),
47 now - action->getLastUpdate());
51 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
53 /* set the remains to 0 due to precision problems when updating the remaining amount */
54 action->setRemains(0);
55 action->setState(Action::State::done);
56 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
58 if (TRACE_is_enabled()) {
59 //defining the last timestamp that we can safely dump to trace file
60 //without losing the event ascending order (considering all CPU's)
62 ActionList *actionSet = getRunningActionSet();
63 for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
64 ; it != itend ; ++it) {
65 action = static_cast<CpuAction*>(&*it);
67 smaller = action->getLastUpdate();
70 if (action->getLastUpdate() < smaller) {
71 smaller = action->getLastUpdate();
75 TRACE_last_timestamp_to_dump = smaller;
81 void CpuModel::updateActionsStateFull(double now, double delta)
83 CpuAction *action = nullptr;
84 ActionList *running_actions = getRunningActionSet();
86 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
87 ; it != itend ; it=itNext) {
89 action = static_cast<CpuAction*>(&*it);
90 if (TRACE_is_enabled()) {
91 Cpu *cpu = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
93 TRACE_surf_host_set_utilization(cpu->getName(),
94 action->getCategory(),
95 lmm_variable_getvalue(action->getVariable()),
98 TRACE_last_timestamp_to_dump = now - delta;
101 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
104 if (action->getMaxDuration() != NO_MAX_DURATION)
105 action->updateMaxDuration(delta);
108 if ((action->getRemainsNoUpdate() <= 0) &&
109 (lmm_get_variable_weight(action->getVariable()) > 0)) {
111 action->setState(Action::State::done);
112 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
113 (action->getMaxDuration() <= 0)) {
115 action->setState(Action::State::done);
120 bool CpuModel::next_occuring_event_isIdempotent()
128 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
129 : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
133 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint,
134 std::vector<double> * speedPerPstate, int core)
135 : Resource(model, host->name().c_str(), constraint)
139 xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->name().c_str());
141 speed_.peak = speedPerPstate->front();
143 host->pimpl_cpu = this;
144 xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->name().c_str());
146 // Copy the power peak array:
147 for (double value : *speedPerPstate) {
148 speedPerPstate_.push_back(value);
151 xbt_assert(model == surf_cpu_model_pm || core==1, "Currently, VM cannot be multicore");
154 Cpu::~Cpu() = default;
156 double Cpu::getPstateSpeedCurrent()
161 int Cpu::getNbPStates()
163 return speedPerPstate_.size();
166 void Cpu::setPState(int pstate_index)
168 xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
169 "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index,
170 static_cast<int>(speedPerPstate_.size()));
172 double new_peak_speed = speedPerPstate_[pstate_index];
173 pstate_ = pstate_index;
174 speed_.peak = new_peak_speed;
184 double Cpu::getPstateSpeed(int pstate_index)
186 xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
188 return speedPerPstate_[pstate_index];
191 double Cpu::getSpeed(double load)
193 return load * speed_.peak;
196 double Cpu::getAvailableSpeed()
198 /* number between 0 and 1 */
202 void Cpu::onSpeedChange() {
203 TRACE_surf_host_set_speed(surf_get_clock(), getName(), coresAmount_ * speed_.scale * speed_.peak);
212 void Cpu::setStateTrace(tmgr_trace_t trace)
214 xbt_assert(stateEvent_==nullptr,"Cannot set a second state trace to Host %s", host_->name().c_str());
216 stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
218 void Cpu::setSpeedTrace(tmgr_trace_t trace)
220 xbt_assert(speed_.event==nullptr,"Cannot set a second speed trace to Host %s", host_->name().c_str());
222 speed_.event = future_evt_set->add_trace(trace, 0.0, this);
230 void CpuAction::updateRemainingLazy(double now)
232 xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
233 xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
235 double delta = now - lastUpdate_;
238 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
239 double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
241 if (TRACE_is_enabled()) {
242 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
243 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), lastValue_, lastUpdate_, now - lastUpdate_);
245 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, remains_);
249 lastValue_ = lmm_variable_getvalue(getVariable());
252 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
254 void CpuAction::setState(Action::State state){
255 Action::State previous = getState();
256 Action::setState(state);
257 onStateChange(this, previous);
259 std::list<Cpu*> CpuAction::cpus() {
260 std::list<Cpu*> retlist;
261 lmm_system_t sys = getModel()->getMaxminSystem();
262 int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
264 for(int i = 0; i<llen; i++)
265 retlist.push_back( (Cpu*)(lmm_constraint_id( lmm_get_cnst_from_var(sys, getVariable(), i) )) );