1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "cpu_interface.hpp"
7 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
10 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
13 simgrid::surf::CpuModel *surf_cpu_model_pm;
14 simgrid::surf::CpuModel *surf_cpu_model_vm;
23 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
25 while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
27 CpuAction* action = static_cast<CpuAction*>(actionHeapPop());
28 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
29 if (TRACE_is_enabled()) {
30 Cpu* cpu = static_cast<Cpu*>(action->getVariable()->get_constraint(0)->get_id());
31 TRACE_surf_host_set_utilization(cpu->getCname(), action->getCategory(), action->getVariable()->get_value(),
32 action->getLastUpdate(), now - action->getLastUpdate());
35 action->finish(kernel::resource::Action::State::done);
36 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
38 /* set the remains to 0 due to precision problems when updating the remaining amount */
39 action->setRemains(0);
41 if (TRACE_is_enabled()) {
42 //defining the last timestamp that we can safely dump to trace file
43 //without losing the event ascending order (considering all CPU's)
45 for (kernel::resource::Action const& action : *getRunningActionSet()) {
46 if (smaller < 0 || action.getLastUpdate() < smaller)
47 smaller = action.getLastUpdate();
50 TRACE_last_timestamp_to_dump = smaller;
55 void CpuModel::updateActionsStateFull(double now, double delta)
57 for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
58 CpuAction& action = static_cast<CpuAction&>(*it);
59 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
60 if (TRACE_is_enabled()) {
61 Cpu* cpu = static_cast<Cpu*>(action.getVariable()->get_constraint(0)->get_id());
62 TRACE_surf_host_set_utilization(cpu->getCname(), action.getCategory(), action.getVariable()->get_value(),
64 TRACE_last_timestamp_to_dump = now - delta;
67 action.updateRemains(action.getVariable()->get_value() * delta);
69 if (action.getMaxDuration() != NO_MAX_DURATION)
70 action.updateMaxDuration(delta);
72 if (((action.getRemainsNoUpdate() <= 0) && (action.getVariable()->get_weight() > 0)) ||
73 ((action.getMaxDuration() != NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
74 action.finish(kernel::resource::Action::State::done);
82 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
83 : Cpu(model, host, nullptr /*constraint*/, speedPerPstate, core)
87 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, kernel::lmm::Constraint* constraint,
88 std::vector<double>* speedPerPstate, int core)
89 : Resource(model, host->getCname(), constraint), coresAmount_(core), host_(host)
91 xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->getCname());
93 speed_.peak = speedPerPstate->front();
95 host->pimpl_cpu = this;
96 xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->getCname());
98 // Copy the power peak array:
99 for (double const& value : *speedPerPstate) {
100 speedPerPstate_.push_back(value);
104 Cpu::~Cpu() = default;
106 int Cpu::getNbPStates()
108 return speedPerPstate_.size();
111 void Cpu::setPState(int pstate_index)
113 xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
114 "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
115 "call to change the pstate.",
116 getCname(), pstate_index, static_cast<int>(speedPerPstate_.size()));
118 double new_peak_speed = speedPerPstate_[pstate_index];
119 pstate_ = pstate_index;
120 speed_.peak = new_peak_speed;
130 double Cpu::getPstateSpeed(int pstate_index)
132 xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
134 return speedPerPstate_[pstate_index];
137 double Cpu::getSpeed(double load)
139 return load * speed_.peak;
142 double Cpu::getAvailableSpeed()
144 /* number between 0 and 1 */
148 void Cpu::onSpeedChange() {
149 TRACE_surf_host_set_speed(surf_get_clock(), getCname(), coresAmount_ * speed_.scale * speed_.peak);
150 s4u::Host::onSpeedChange(*host_);
158 void Cpu::setStateTrace(tmgr_trace_t trace)
160 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Host %s", host_->getCname());
162 stateEvent_ = future_evt_set->add_trace(trace, this);
164 void Cpu::setSpeedTrace(tmgr_trace_t trace)
166 xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->getCname());
168 speed_.event = future_evt_set->add_trace(trace, this);
176 void CpuAction::updateRemainingLazy(double now)
178 xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
179 xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
181 double delta = now - getLastUpdate();
183 if (getRemainsNoUpdate() > 0) {
184 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(),
186 updateRemains(getLastValue() * delta);
188 if (TRACE_is_enabled()) {
189 Cpu* cpu = static_cast<Cpu*>(getVariable()->get_constraint(0)->get_id());
190 TRACE_surf_host_set_utilization(cpu->getCname(), getCategory(), getLastValue(), getLastUpdate(),
191 now - getLastUpdate());
193 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
197 setLastValue(getVariable()->get_value());
200 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, kernel::resource::Action::State)> CpuAction::onStateChange;
202 void CpuAction::suspend(){
203 Action::State previous = get_state();
204 onStateChange(this, previous);
208 void CpuAction::resume(){
209 Action::State previous = get_state();
210 onStateChange(this, previous);
214 void CpuAction::set_state(Action::State state)
216 Action::State previous = get_state();
217 Action::set_state(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 kernel::resource::Resource* resource =
229 static_cast<kernel::resource::Resource*>(getVariable()->get_constraint(i)->get_id());
230 Cpu* cpu = dynamic_cast<Cpu*>(resource);
232 retlist.push_back(cpu);