1 /* Copyright (c) 2013-2017. 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. */
7 #include "cpu_interface.hpp"
8 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
11 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
14 simgrid::surf::CpuModel *surf_cpu_model_pm;
15 simgrid::surf::CpuModel *surf_cpu_model_vm;
24 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
26 while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
28 CpuAction* action = static_cast<CpuAction*>(actionHeapPop());
29 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
30 if (TRACE_is_enabled()) {
31 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
32 TRACE_surf_host_set_utilization(cpu->getCname(), action->getCategory(),
33 lmm_variable_getvalue(action->getVariable()), action->getLastUpdate(),
34 now - action->getLastUpdate());
37 action->finish(Action::State::done);
38 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
40 /* set the remains to 0 due to precision problems when updating the remaining amount */
41 action->setRemains(0);
43 if (TRACE_is_enabled()) {
44 //defining the last timestamp that we can safely dump to trace file
45 //without losing the event ascending order (considering all CPU's)
47 for (Action const& action : *getRunningActionSet()) {
48 if (smaller < 0 || action.getLastUpdate() < smaller)
49 smaller = action.getLastUpdate();
52 TRACE_last_timestamp_to_dump = smaller;
57 void CpuModel::updateActionsStateFull(double now, double delta)
59 for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
60 CpuAction& action = static_cast<CpuAction&>(*it);
61 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
62 if (TRACE_is_enabled()) {
64 static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action.getVariable(), 0)));
66 TRACE_surf_host_set_utilization(cpu->getCname(), action.getCategory(),
67 lmm_variable_getvalue(action.getVariable()), now - delta, delta);
68 TRACE_last_timestamp_to_dump = now - delta;
71 action.updateRemains(lmm_variable_getvalue(action.getVariable()) * delta);
73 if (action.getMaxDuration() != NO_MAX_DURATION)
74 action.updateMaxDuration(delta);
76 if (((action.getRemainsNoUpdate() <= 0) && (lmm_get_variable_weight(action.getVariable()) > 0)) ||
77 ((action.getMaxDuration() != NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
78 action.finish(Action::State::done);
86 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
87 : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
91 Cpu::Cpu(Model* model, simgrid::s4u::Host* host, lmm_constraint_t constraint, std::vector<double>* speedPerPstate,
93 : Resource(model, host->getCname(), constraint), coresAmount_(core), host_(host)
95 xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->getCname());
97 speed_.peak = speedPerPstate->front();
99 host->pimpl_cpu = this;
100 xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->getCname());
102 // Copy the power peak array:
103 for (double const& value : *speedPerPstate) {
104 speedPerPstate_.push_back(value);
108 Cpu::~Cpu() = default;
110 int Cpu::getNbPStates()
112 return speedPerPstate_.size();
115 void Cpu::setPState(int pstate_index)
117 xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
118 "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
119 "call to change the pstate.",
120 getCname(), pstate_index, static_cast<int>(speedPerPstate_.size()));
122 double new_peak_speed = speedPerPstate_[pstate_index];
123 pstate_ = pstate_index;
124 speed_.peak = new_peak_speed;
134 double Cpu::getPstateSpeed(int pstate_index)
136 xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
138 return speedPerPstate_[pstate_index];
141 double Cpu::getSpeed(double load)
143 return load * speed_.peak;
146 double Cpu::getAvailableSpeed()
148 /* number between 0 and 1 */
152 void Cpu::onSpeedChange() {
153 TRACE_surf_host_set_speed(surf_get_clock(), getCname(), coresAmount_ * speed_.scale * speed_.peak);
154 s4u::Host::onSpeedChange(*host_);
162 void Cpu::setStateTrace(tmgr_trace_t trace)
164 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Host %s", host_->getCname());
166 stateEvent_ = future_evt_set->add_trace(trace, this);
168 void Cpu::setSpeedTrace(tmgr_trace_t trace)
170 xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->getCname());
172 speed_.event = future_evt_set->add_trace(trace, this);
180 void CpuAction::updateRemainingLazy(double now)
182 xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
183 xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
185 double delta = now - getLastUpdate();
187 if (getRemainsNoUpdate() > 0) {
188 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(),
190 updateRemains(getLastValue() * delta);
192 if (TRACE_is_enabled()) {
193 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
194 TRACE_surf_host_set_utilization(cpu->getCname(), getCategory(), getLastValue(), getLastUpdate(),
195 now - getLastUpdate());
197 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
201 setLastValue(lmm_variable_getvalue(getVariable()));
204 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
206 void CpuAction::suspend(){
207 Action::State previous = getState();
208 onStateChange(this, previous);
212 void CpuAction::resume(){
213 Action::State previous = getState();
214 onStateChange(this, previous);
218 void CpuAction::setState(Action::State state){
219 Action::State previous = getState();
220 Action::setState(state);
221 onStateChange(this, previous);
223 /** @brief returns a list of all CPUs that this action is using */
224 std::list<Cpu*> CpuAction::cpus() {
225 std::list<Cpu*> retlist;
226 lmm_system_t sys = getModel()->getMaxminSystem();
227 int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
229 for (int i = 0; i < llen; i++) {
230 /* Beware of composite actions: ptasks put links and cpus together */
231 // extra pb: we cannot dynamic_cast from void*...
232 Resource* resource = static_cast<Resource*>(lmm_constraint_id(lmm_get_cnst_from_var(sys, getVariable(), i)));
233 Cpu* cpu = dynamic_cast<Cpu*>(resource);
235 retlist.push_back(cpu);