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/surf/surf_interface.hpp"
8 #include "surf/surf.hpp"
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::update_actions_state_lazy(double now, double /*delta*/)
25 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
27 CpuAction* action = static_cast<CpuAction*>(get_action_heap().pop());
28 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
30 action->finish(kernel::resource::Action::State::FINISHED);
31 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
35 void CpuModel::update_actions_state_full(double now, double delta)
37 for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
38 CpuAction& action = static_cast<CpuAction&>(*it);
39 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
41 action.update_remains(action.get_variable()->get_value() * delta);
43 if (action.get_max_duration() != NO_MAX_DURATION)
44 action.update_max_duration(delta);
46 if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_weight() > 0)) ||
47 ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
48 action.finish(kernel::resource::Action::State::FINISHED);
56 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
57 : Cpu(model, host, nullptr /*constraint*/, speedPerPstate, core)
61 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, kernel::lmm::Constraint* constraint,
62 std::vector<double>* speedPerPstate, int core)
63 : Resource(model, host->get_cname(), constraint), core_count_(core), host_(host)
65 xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->get_cname());
67 speed_.peak = speedPerPstate->front();
69 host->pimpl_cpu = this;
70 xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->get_cname());
72 // Copy the power peak array:
73 for (double const& value : *speedPerPstate) {
74 speed_per_pstate_.push_back(value);
80 if (get_model() == surf_cpu_model_pm)
81 speed_per_pstate_.clear();
84 int Cpu::get_pstate_count()
86 return speed_per_pstate_.size();
89 void Cpu::set_pstate(int pstate_index)
91 xbt_assert(pstate_index <= static_cast<int>(speed_per_pstate_.size()),
92 "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
93 "call to change the pstate.",
94 get_cname(), pstate_index, static_cast<int>(speed_per_pstate_.size()));
96 double new_peak_speed = speed_per_pstate_[pstate_index];
97 pstate_ = pstate_index;
98 speed_.peak = new_peak_speed;
103 int Cpu::get_pstate()
108 double Cpu::get_pstate_peak_speed(int pstate_index)
110 xbt_assert((pstate_index <= static_cast<int>(speed_per_pstate_.size())),
111 "Invalid parameters (pstate index out of bounds)");
113 return speed_per_pstate_[pstate_index];
116 double Cpu::get_speed(double load)
118 return load * speed_.peak;
121 double Cpu::get_speed_ratio()
123 /* number between 0 and 1 */
127 void Cpu::on_speed_change()
129 s4u::Host::on_speed_change(*host_);
132 int Cpu::get_core_count()
137 void Cpu::set_speed_trace(tmgr_trace_t trace)
139 xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->get_cname());
141 speed_.event = future_evt_set.add_trace(trace, this);
149 void CpuAction::update_remains_lazy(double now)
151 xbt_assert(get_state_set() == get_model()->get_started_action_set(),
152 "You're updating an action that is not running.");
153 xbt_assert(get_priority() > 0, "You're updating an action that seems suspended.");
155 double delta = now - get_last_update();
157 if (get_remains_no_update() > 0) {
158 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
160 update_remains(get_last_value() * delta);
162 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, get_remains_no_update());
166 set_last_value(get_variable()->get_value());
169 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, kernel::resource::Action::State)> CpuAction::on_state_change;
171 void CpuAction::suspend(){
172 Action::State previous = get_state();
173 on_state_change(this, previous);
177 void CpuAction::resume(){
178 Action::State previous = get_state();
179 on_state_change(this, previous);
183 void CpuAction::set_state(Action::State state)
185 Action::State previous = get_state();
186 Action::set_state(state);
187 on_state_change(this, previous);
190 /** @brief returns a list of all CPUs that this action is using */
191 std::list<Cpu*> CpuAction::cpus() {
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 kernel::resource::Resource* resource =
199 static_cast<kernel::resource::Resource*>(get_variable()->get_constraint(i)->get_id());
200 Cpu* cpu = dynamic_cast<Cpu*>(resource);
202 retlist.push_back(cpu);