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*>(action->getVariable()->get_constraint(0)->get_id());
32 TRACE_surf_host_set_utilization(cpu->getCname(), action->getCategory(), action->getVariable()->get_value(),
33 action->getLastUpdate(), now - action->getLastUpdate());
36 action->finish(Action::State::done);
37 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
39 /* set the remains to 0 due to precision problems when updating the remaining amount */
40 action->setRemains(0);
42 if (TRACE_is_enabled()) {
43 //defining the last timestamp that we can safely dump to trace file
44 //without losing the event ascending order (considering all CPU's)
46 for (Action const& action : *getRunningActionSet()) {
47 if (smaller < 0 || action.getLastUpdate() < smaller)
48 smaller = action.getLastUpdate();
51 TRACE_last_timestamp_to_dump = smaller;
56 void CpuModel::updateActionsStateFull(double now, double delta)
58 for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
59 CpuAction& action = static_cast<CpuAction&>(*it);
60 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
61 if (TRACE_is_enabled()) {
62 Cpu* cpu = static_cast<Cpu*>(action.getVariable()->get_constraint(0)->get_id());
63 TRACE_surf_host_set_utilization(cpu->getCname(), action.getCategory(), action.getVariable()->get_value(),
65 TRACE_last_timestamp_to_dump = now - delta;
68 action.updateRemains(action.getVariable()->get_value() * delta);
70 if (action.getMaxDuration() != NO_MAX_DURATION)
71 action.updateMaxDuration(delta);
73 if (((action.getRemainsNoUpdate() <= 0) && (action.getVariable()->get_weight() > 0)) ||
74 ((action.getMaxDuration() != NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
75 action.finish(Action::State::done);
83 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
84 : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
88 Cpu::Cpu(Model* model, simgrid::s4u::Host* host, lmm_constraint_t constraint, std::vector<double>* speedPerPstate,
90 : Resource(model, host->getCname(), constraint), coresAmount_(core), host_(host)
92 xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->getCname());
94 speed_.peak = speedPerPstate->front();
96 host->pimpl_cpu = this;
97 xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->getCname());
99 // Copy the power peak array:
100 for (double const& value : *speedPerPstate) {
101 speedPerPstate_.push_back(value);
105 Cpu::~Cpu() = default;
107 int Cpu::getNbPStates()
109 return speedPerPstate_.size();
112 void Cpu::setPState(int pstate_index)
114 xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
115 "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
116 "call to change the pstate.",
117 getCname(), pstate_index, static_cast<int>(speedPerPstate_.size()));
119 double new_peak_speed = speedPerPstate_[pstate_index];
120 pstate_ = pstate_index;
121 speed_.peak = new_peak_speed;
131 double Cpu::getPstateSpeed(int pstate_index)
133 xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
135 return speedPerPstate_[pstate_index];
138 double Cpu::getSpeed(double load)
140 return load * speed_.peak;
143 double Cpu::getAvailableSpeed()
145 /* number between 0 and 1 */
149 void Cpu::onSpeedChange() {
150 TRACE_surf_host_set_speed(surf_get_clock(), getCname(), coresAmount_ * speed_.scale * speed_.peak);
151 s4u::Host::onSpeedChange(*host_);
159 void Cpu::setStateTrace(tmgr_trace_t trace)
161 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Host %s", host_->getCname());
163 stateEvent_ = future_evt_set->add_trace(trace, this);
165 void Cpu::setSpeedTrace(tmgr_trace_t trace)
167 xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->getCname());
169 speed_.event = future_evt_set->add_trace(trace, this);
177 void CpuAction::updateRemainingLazy(double now)
179 xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
180 xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
182 double delta = now - getLastUpdate();
184 if (getRemainsNoUpdate() > 0) {
185 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(),
187 updateRemains(getLastValue() * delta);
189 if (TRACE_is_enabled()) {
190 Cpu* cpu = static_cast<Cpu*>(getVariable()->get_constraint(0)->get_id());
191 TRACE_surf_host_set_utilization(cpu->getCname(), getCategory(), getLastValue(), getLastUpdate(),
192 now - getLastUpdate());
194 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
198 setLastValue(getVariable()->get_value());
201 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
203 void CpuAction::suspend(){
204 Action::State previous = getState();
205 onStateChange(this, previous);
209 void CpuAction::resume(){
210 Action::State previous = getState();
211 onStateChange(this, previous);
215 void CpuAction::setState(Action::State state){
216 Action::State previous = getState();
217 Action::setState(state);
218 onStateChange(this, previous);
220 /** @brief returns a list of all CPUs that this action is using */
221 std::list<Cpu*> CpuAction::cpus() {
222 std::list<Cpu*> retlist;
223 int llen = getVariable()->get_number_of_constraint();
225 for (int i = 0; i < llen; i++) {
226 /* Beware of composite actions: ptasks put links and cpus together */
227 // extra pb: we cannot dynamic_cast from void*...
228 Resource* resource = static_cast<Resource*>(getVariable()->get_constraint(i)->get_id());
229 Cpu* cpu = dynamic_cast<Cpu*>(resource);
231 retlist.push_back(cpu);