1 /* Copyright (c) 2013-2021. 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"
8 #include "src/kernel/resource/profile/Profile.hpp"
9 #include "src/surf/surf_interface.hpp"
10 #include "surf/surf.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_cpu, ker_resource, "CPU resource, fueling execution activites");
22 void CpuModel::update_actions_state_lazy(double now, double /*delta*/)
24 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
25 auto* action = static_cast<CpuAction*>(get_action_heap().pop());
26 XBT_DEBUG("Something happened to action %p", action);
28 action->finish(kernel::resource::Action::State::FINISHED);
29 XBT_DEBUG("Action %p finished", action);
33 void CpuModel::update_actions_state_full(double /*now*/, double delta)
35 for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
36 auto& action = static_cast<CpuAction&>(*it);
37 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
39 action.update_remains(action.get_variable()->get_value() * delta);
40 action.update_max_duration(delta);
42 if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
43 ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
44 action.finish(Action::State::FINISHED);
52 Cpu::Cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate)
53 : Resource_T(host->get_cname()), piface_(host), speed_per_pstate_(speed_per_pstate)
56 speed_.peak = speed_per_pstate_.front();
57 host->pimpl_cpu = this;
60 void Cpu::reset_vcpu(Cpu* that)
62 this->pstate_ = that->pstate_;
63 this->speed_ = that->speed_;
64 this->speed_per_pstate_.clear();
65 this->speed_per_pstate_.assign(that->speed_per_pstate_.begin(), that->speed_per_pstate_.end());
68 Cpu* Cpu::set_pstate(int pstate_index)
70 xbt_assert(pstate_index <= static_cast<int>(speed_per_pstate_.size()),
71 "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
72 "call to change the pstate.",
73 get_cname(), pstate_index, static_cast<int>(speed_per_pstate_.size()));
75 double new_peak_speed = speed_per_pstate_[pstate_index];
76 pstate_ = pstate_index;
77 speed_.peak = new_peak_speed;
83 Cpu* Cpu::set_pstate_speed(const std::vector<double>& speed_per_state)
85 xbt_assert(speed_per_state.size() > 0, "CPU %s: processor speed vector cannot be empty", get_cname());
86 xbt_assert(not is_sealed(), "CPU %s: processor speed cannot be changed once CPU has been sealed", get_cname());
87 speed_per_pstate_ = speed_per_state;
88 speed_.peak = speed_per_pstate_.front();
92 double Cpu::get_pstate_peak_speed(int pstate_index) const
94 xbt_assert((pstate_index <= static_cast<int>(speed_per_pstate_.size())),
95 "Invalid parameters (pstate index out of bounds)");
97 return speed_per_pstate_[pstate_index];
100 void Cpu::on_speed_change()
102 s4u::Host::on_speed_change(*piface_);
105 Cpu* Cpu::set_core_count(int core_count)
107 xbt_assert(not is_sealed(), "Core count cannot be changed once CPU has been sealed");
108 xbt_assert(core_count > 0, "Host %s must have at least one core, not 0.", piface_->get_cname());
109 if (dynamic_cast<CpuTiModel*>(get_model()) != nullptr)
110 xbt_assert(core_count == 1, "Multi-core not handled by this model yet");
112 core_count_ = core_count;
116 int Cpu::get_core_count()
121 Cpu* Cpu::set_speed_profile(kernel::profile::Profile* profile)
124 xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", piface_->get_cname());
125 speed_.event = profile->schedule(&profile::future_evt_set, this);
132 lmm::System* lmm = get_model()->get_maxmin_system();
133 if (dynamic_cast<CpuTiModel*>(get_model()) == nullptr)
134 this->set_constraint(lmm->constraint_new(this, core_count_ * speed_per_pstate_.front()));
142 void CpuAction::update_remains_lazy(double now)
144 xbt_assert(get_state_set() == get_model()->get_started_action_set(),
145 "You're updating an action that is not running.");
146 xbt_assert(get_sharing_penalty() > 0, "You're updating an action that seems suspended.");
148 double delta = now - get_last_update();
150 if (get_remains_no_update() > 0) {
151 XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
153 update_remains(get_last_value() * delta);
155 XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
159 set_last_value(get_variable()->get_value());
162 xbt::signal<void(CpuAction const&, Action::State)> CpuAction::on_state_change;
164 void CpuAction::suspend()
166 Action::State previous = get_state();
167 on_state_change(*this, previous);
171 void CpuAction::resume()
173 Action::State previous = get_state();
174 on_state_change(*this, previous);
178 void CpuAction::set_state(Action::State state)
180 Action::State previous = get_state();
181 Action::set_state(state);
182 on_state_change(*this, previous);
185 /** @brief returns a list of all CPUs that this action is using */
186 std::list<Cpu*> CpuAction::cpus() const
188 std::list<Cpu*> retlist;
189 int llen = get_variable()->get_number_of_constraint();
191 for (int i = 0; i < llen; i++) {
192 /* Beware of composite actions: ptasks put links and cpus together */
193 // extra pb: we cannot dynamic_cast from void*...
194 Resource* resource = get_variable()->get_constraint(i)->get_id();
195 auto* cpu = dynamic_cast<Cpu*>(resource);
197 retlist.push_back(cpu);
202 } // namespace resource
203 } // namespace kernel
204 } // namespace simgrid