Logo AND Algorithmique Numérique Distribuée

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