Logo AND Algorithmique Numérique Distribuée

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