Logo AND Algorithmique Numérique Distribuée

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