Logo AND Algorithmique Numérique Distribuée

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