Logo AND Algorithmique Numérique Distribuée

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