Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix bound checking in assert.
[simgrid.git] / src / kernel / resource / CpuImpl.cpp
1 /* Copyright (c) 2013-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/kernel/resource/CpuImpl.hpp"
7 #include "src/kernel/EngineImpl.hpp"
8 #include "src/kernel/resource/models/cpu_ti.hpp"
9 #include "src/kernel/resource/profile/Profile.hpp"
10 #include "src/simgrid/math_utils.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_cpu, ker_resource, "CPU resource, fueling execution activites");
13
14 namespace simgrid::kernel::resource {
15
16 /*********
17  * Model *
18  *********/
19
20 void CpuModel::update_actions_state_lazy(double now, double /*delta*/)
21 {
22   while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_precision_timing)) {
23     auto* action = static_cast<CpuAction*>(get_action_heap().pop());
24     XBT_DEBUG("Something happened to action %p", action);
25
26     action->finish(kernel::resource::Action::State::FINISHED);
27     XBT_DEBUG("Action %p finished", action);
28   }
29 }
30
31 void CpuModel::update_actions_state_full(double /*now*/, double delta)
32 {
33   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
34     auto& action = static_cast<CpuAction&>(*it);
35     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
36
37     action.update_remains(action.get_rate() * delta);
38     action.update_max_duration(delta);
39
40     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
41         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
42       action.finish(Action::State::FINISHED);
43     }
44   }
45 }
46
47 /************
48  * Resource *
49  ************/
50 CpuImpl::CpuImpl(s4u::Host* host, const std::vector<double>& speed_per_pstate)
51     : Resource_T(host->get_cname()), piface_(host), speed_per_pstate_(speed_per_pstate)
52 {
53   speed_.scale = 1;
54   speed_.peak  = speed_per_pstate_.front();
55   host->set_cpu(this);
56 }
57
58 void CpuImpl::reset_vcpu(CpuImpl* that)
59 {
60   this->pstate_ = that->pstate_;
61   this->speed_  = that->speed_;
62   this->speed_per_pstate_.clear();
63   this->speed_per_pstate_.assign(that->speed_per_pstate_.begin(), that->speed_per_pstate_.end());
64 }
65
66 CpuImpl* CpuImpl::set_pstate(unsigned long pstate_index)
67 {
68   xbt_assert(
69       pstate_index < speed_per_pstate_.size(),
70       "Invalid parameters for CPU %s (pstate %lu >= length of pstates %zu). Please fix your platform file, or your "
71       "call to change the pstate.",
72       get_cname(), pstate_index, speed_per_pstate_.size());
73
74   double new_peak_speed = speed_per_pstate_[pstate_index];
75   pstate_               = pstate_index;
76   speed_.peak           = new_peak_speed;
77
78   on_speed_change();
79   return this;
80 }
81
82 CpuImpl* CpuImpl::set_pstate_speed(const std::vector<double>& speed_per_state)
83 {
84   xbt_assert(not speed_per_state.empty(), "CPU %s: processor speed vector cannot be empty", get_cname());
85   xbt_assert(not is_sealed(), "CPU %s: processor speed cannot be changed once CPU has been sealed", get_cname());
86   speed_per_pstate_ = speed_per_state;
87   speed_.peak       = speed_per_pstate_.front();
88   return this;
89 }
90
91 double CpuImpl::get_pstate_peak_speed(unsigned long pstate_index) const
92 {
93   xbt_assert(pstate_index < speed_per_pstate_.size(), "Invalid parameters (pstate index %lu out of bounds %zu)",
94              pstate_index, speed_per_pstate_.size());
95   return speed_per_pstate_[pstate_index];
96 }
97
98 void CpuImpl::on_speed_change()
99 {
100   s4u::Host::on_speed_change(*piface_);
101 }
102
103 CpuImpl* CpuImpl::set_core_count(int core_count)
104 {
105   xbt_assert(not is_sealed(), "Core count cannot be changed once CPU has been sealed");
106   xbt_assert(core_count > 0, "Host %s must have at least one core, not 0.", piface_->get_cname());
107   if (dynamic_cast<CpuTiModel*>(get_model()) != nullptr)
108     xbt_assert(core_count == 1, "Multi-core not handled by this model yet");
109
110   core_count_ = core_count;
111   return this;
112 }
113
114 void CpuImpl::apply_sharing_policy_cfg() const
115 {
116   if (not get_constraint())
117     return;
118
119   kernel::lmm::Constraint::SharingPolicy lmm_policy = kernel::lmm::Constraint::SharingPolicy::SHARED;
120   if (sharing_policy_ == s4u::Host::SharingPolicy::NONLINEAR)
121     lmm_policy = kernel::lmm::Constraint::SharingPolicy::NONLINEAR;
122
123   get_constraint()->set_sharing_policy(lmm_policy, sharing_policy_cb_);
124 }
125
126 void CpuImpl::set_sharing_policy(s4u::Host::SharingPolicy policy, const s4u::NonLinearResourceCb& cb)
127 {
128   xbt_assert(dynamic_cast<CpuTiModel*>(get_model()) == nullptr, "Cannot change sharing policy with CPU:TI model");
129   sharing_policy_    = policy;
130   sharing_policy_cb_ = cb;
131   apply_sharing_policy_cfg();
132 }
133
134 CpuImpl* CpuImpl::set_speed_profile(kernel::profile::Profile* profile)
135 {
136   if (profile) {
137     xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", piface_->get_cname());
138     speed_.event = profile->schedule(&profile::future_evt_set, this);
139   }
140   return this;
141 }
142
143 void CpuImpl::seal()
144 {
145   if (is_sealed()) {
146     return;
147   }
148   lmm::System* lmm = get_model()->get_maxmin_system();
149   if (dynamic_cast<CpuTiModel*>(get_model()) == nullptr)
150     this->set_constraint(lmm->constraint_new(this, core_count_ * speed_per_pstate_.front()));
151   apply_sharing_policy_cfg();
152   Resource::seal();
153 }
154
155 void CpuImpl::turn_off()
156 {
157   if (is_on()) {
158     Resource::turn_off();
159
160     const kernel::lmm::Element* elem = nullptr;
161     double now                       = EngineImpl::get_clock();
162     while (const auto* var = get_constraint()->get_variable(&elem)) {
163       Action* action = var->get_id();
164       if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
165         action->set_finish_time(now);
166         action->set_state(Action::State::FAILED);
167       }
168     }
169   }
170 }
171
172 /**********
173  * Action *
174  **********/
175
176 void CpuAction::update_remains_lazy(double now)
177 {
178   xbt_assert(get_state_set() == get_model()->get_started_action_set(),
179              "You're updating an action that is not running.");
180   xbt_assert(get_sharing_penalty() > 0, "You're updating an action that seems suspended.");
181
182   double delta = now - get_last_update();
183
184   if (get_remains_no_update() > 0) {
185     XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
186               get_last_update());
187     update_remains(get_last_value() * delta);
188
189     XBT_DEBUG("Updating action(%p): remains is now %f", this, get_remains_no_update());
190   }
191
192   set_last_update();
193   set_last_value(get_rate());
194 }
195
196 xbt::signal<void(CpuAction const&, Action::State)> CpuAction::on_state_change;
197
198 void CpuAction::suspend()
199 {
200   Action::State previous = get_state();
201   on_state_change(*this, previous);
202   Action::suspend();
203 }
204
205 void CpuAction::resume()
206 {
207   Action::State previous = get_state();
208   on_state_change(*this, previous);
209   Action::resume();
210 }
211
212 void CpuAction::set_state(Action::State state)
213 {
214   Action::State previous = get_state();
215   Action::set_state(state);
216   on_state_change(*this, previous);
217 }
218
219 /** @brief returns a list of all CPUs that this action is using */
220 std::list<CpuImpl*> CpuAction::cpus() const
221 {
222   std::list<CpuImpl*> retlist;
223   int llen = get_variable()->get_number_of_constraint();
224
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 = get_variable()->get_constraint(i)->get_id();
229     auto* cpu          = dynamic_cast<CpuImpl*>(resource);
230     if (cpu != nullptr)
231       retlist.push_back(cpu);
232   }
233
234   return retlist;
235 }
236 } // namespace simgrid::kernel::resource