1 /* Copyright (c) 2013-2020. 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/kernel/resource/profile/Profile.hpp"
8 #include "src/surf/surf_interface.hpp"
9 #include "surf/surf.hpp"
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::kernel::resource::CpuModel* surf_cpu_model_pm;
15 simgrid::kernel::resource::CpuModel* surf_cpu_model_vm;
25 void CpuModel::update_actions_state_lazy(double now, double /*delta*/)
27 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
28 auto* action = static_cast<CpuAction*>(get_action_heap().pop());
29 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
31 action->finish(kernel::resource::Action::State::FINISHED);
32 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
36 void CpuModel::update_actions_state_full(double /*now*/, double delta)
38 for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
39 auto& action = static_cast<CpuAction&>(*it);
40 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
42 action.update_remains(action.get_variable()->get_value() * delta);
43 action.update_max_duration(delta);
45 if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
46 ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
47 action.finish(Action::State::FINISHED);
55 Cpu::Cpu(Model* model, s4u::Host* host, const std::vector<double>& speed_per_pstate, int core)
56 : Cpu(model, host, nullptr /*constraint*/, speed_per_pstate, core)
60 Cpu::Cpu(Model* model, s4u::Host* host, lmm::Constraint* constraint, const std::vector<double>& speed_per_pstate,
62 : Resource(model, host->get_cname(), constraint)
65 , speed_per_pstate_(speed_per_pstate)
67 xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->get_cname());
69 speed_.peak = speed_per_pstate_.front();
71 host->pimpl_cpu = this;
72 xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->get_cname());
75 void Cpu::reset_vcpu(Cpu* that)
77 this->pstate_ = that->pstate_;
78 this->speed_ = that->speed_;
79 this->speed_per_pstate_.clear();
80 this->speed_per_pstate_.assign(that->speed_per_pstate_.begin(), that->speed_per_pstate_.end());
83 int Cpu::get_pstate_count() const
85 return speed_per_pstate_.size();
88 void Cpu::set_pstate(int pstate_index)
90 xbt_assert(pstate_index <= static_cast<int>(speed_per_pstate_.size()),
91 "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
92 "call to change the pstate.",
93 get_cname(), pstate_index, static_cast<int>(speed_per_pstate_.size()));
95 double new_peak_speed = speed_per_pstate_[pstate_index];
96 pstate_ = pstate_index;
97 speed_.peak = new_peak_speed;
102 int Cpu::get_pstate() const
107 double Cpu::get_pstate_peak_speed(int pstate_index) const
109 xbt_assert((pstate_index <= static_cast<int>(speed_per_pstate_.size())),
110 "Invalid parameters (pstate index out of bounds)");
112 return speed_per_pstate_[pstate_index];
115 double Cpu::get_speed(double load) const
117 return load * speed_.peak;
120 double Cpu::get_speed_ratio()
122 /* number between 0 and 1 */
126 void Cpu::on_speed_change()
128 s4u::Host::on_speed_change(*host_);
131 int Cpu::get_core_count()
136 void Cpu::set_speed_profile(kernel::profile::Profile* profile)
138 xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->get_cname());
140 speed_.event = profile->schedule(&profile::future_evt_set, this);
148 void CpuAction::update_remains_lazy(double now)
150 xbt_assert(get_state_set() == get_model()->get_started_action_set(),
151 "You're updating an action that is not running.");
152 xbt_assert(get_sharing_penalty() > 0, "You're updating an action that seems suspended.");
154 double delta = now - get_last_update();
156 if (get_remains_no_update() > 0) {
157 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
159 update_remains(get_last_value() * delta);
161 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, get_remains_no_update());
165 set_last_value(get_variable()->get_value());
168 xbt::signal<void(CpuAction const&, Action::State)> CpuAction::on_state_change;
170 void CpuAction::suspend(){
171 Action::State previous = get_state();
172 on_state_change(*this, previous);
176 void CpuAction::resume(){
177 Action::State previous = get_state();
178 on_state_change(*this, previous);
182 void CpuAction::set_state(Action::State state)
184 Action::State previous = get_state();
185 Action::set_state(state);
186 on_state_change(*this, previous);
189 /** @brief returns a list of all CPUs that this action is using */
190 std::list<Cpu*> CpuAction::cpus() const
192 std::list<Cpu*> retlist;
193 int llen = get_variable()->get_number_of_constraint();
195 for (int i = 0; i < llen; i++) {
196 /* Beware of composite actions: ptasks put links and cpus together */
197 // extra pb: we cannot dynamic_cast from void*...
198 Resource* resource = get_variable()->get_constraint(i)->get_id();
199 auto* cpu = dynamic_cast<Cpu*>(resource);
201 retlist.push_back(cpu);
206 } // namespace resource
207 } // namespace kernel
208 } // namespace simgrid