Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The creation of the pimpl needs no simcall
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 2013-2019. 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/surf/surf_interface.hpp"
8 #include "surf/surf.hpp"
9
10 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
12
13 simgrid::surf::CpuModel *surf_cpu_model_pm;
14 simgrid::surf::CpuModel *surf_cpu_model_vm;
15
16 namespace simgrid {
17 namespace surf {
18
19 /*********
20  * Model *
21  *********/
22
23 void CpuModel::update_actions_state_lazy(double now, double /*delta*/)
24 {
25   while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
26
27     CpuAction* action = static_cast<CpuAction*>(get_action_heap().pop());
28     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
29
30     action->finish(kernel::resource::Action::State::FINISHED);
31     XBT_CDEBUG(surf_kernel, "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     CpuAction& 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_weight() > 0)) ||
45         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
46       action.finish(kernel::resource::Action::State::FINISHED);
47     }
48   }
49 }
50
51 /************
52  * Resource *
53  ************/
54 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
55     : Cpu(model, host, nullptr /*constraint*/, speedPerPstate, core)
56 {
57 }
58
59 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, kernel::lmm::Constraint* constraint,
60          std::vector<double>* speedPerPstate, int core)
61     : Resource(model, host->get_cname(), constraint), core_count_(core), host_(host)
62 {
63   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->get_cname());
64
65   speed_.peak = speedPerPstate->front();
66   speed_.scale = 1;
67   host->pimpl_cpu = this;
68   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->get_cname());
69
70   // Copy the power peak array:
71   for (double const& value : *speedPerPstate) {
72     speed_per_pstate_.push_back(value);
73   }
74 }
75
76 Cpu::~Cpu()
77 {
78   if (get_model() == surf_cpu_model_pm)
79     speed_per_pstate_.clear();
80 }
81
82 int Cpu::get_pstate_count()
83 {
84   return speed_per_pstate_.size();
85 }
86
87 void Cpu::set_pstate(int pstate_index)
88 {
89   xbt_assert(pstate_index <= static_cast<int>(speed_per_pstate_.size()),
90              "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
91              "call to change the pstate.",
92              get_cname(), pstate_index, static_cast<int>(speed_per_pstate_.size()));
93
94   double new_peak_speed = speed_per_pstate_[pstate_index];
95   pstate_ = pstate_index;
96   speed_.peak = new_peak_speed;
97
98   on_speed_change();
99 }
100
101 int Cpu::get_pstate()
102 {
103   return pstate_;
104 }
105
106 double Cpu::get_pstate_peak_speed(int pstate_index)
107 {
108   xbt_assert((pstate_index <= static_cast<int>(speed_per_pstate_.size())),
109              "Invalid parameters (pstate index out of bounds)");
110
111   return speed_per_pstate_[pstate_index];
112 }
113
114 double Cpu::get_speed(double load)
115 {
116   return load * speed_.peak;
117 }
118
119 double Cpu::get_speed_ratio()
120 {
121 /* number between 0 and 1 */
122   return speed_.scale;
123 }
124
125 void Cpu::on_speed_change()
126 {
127   s4u::Host::on_speed_change(*host_);
128 }
129
130 int Cpu::get_core_count()
131 {
132   return core_count_;
133 }
134
135 void Cpu::set_speed_profile(kernel::profile::Profile* profile)
136 {
137   xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->get_cname());
138
139   speed_.event = profile->schedule(&future_evt_set, this);
140 }
141
142
143 /**********
144  * Action *
145  **********/
146
147 void CpuAction::update_remains_lazy(double now)
148 {
149   xbt_assert(get_state_set() == get_model()->get_started_action_set(),
150              "You're updating an action that is not running.");
151   xbt_assert(get_priority() > 0, "You're updating an action that seems suspended.");
152
153   double delta = now - get_last_update();
154
155   if (get_remains_no_update() > 0) {
156     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
157                get_last_update());
158     update_remains(get_last_value() * delta);
159
160     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, get_remains_no_update());
161   }
162
163   set_last_update();
164   set_last_value(get_variable()->get_value());
165 }
166
167 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, kernel::resource::Action::State)> CpuAction::on_state_change;
168
169 void CpuAction::suspend(){
170   Action::State previous = get_state();
171   on_state_change(this, previous);
172   Action::suspend();
173 }
174
175 void CpuAction::resume(){
176   Action::State previous = get_state();
177   on_state_change(this, previous);
178   Action::resume();
179 }
180
181 void CpuAction::set_state(Action::State state)
182 {
183   Action::State previous = get_state();
184   Action::set_state(state);
185   on_state_change(this, previous);
186 }
187
188 /** @brief returns a list of all CPUs that this action is using */
189 std::list<Cpu*> CpuAction::cpus() {
190   std::list<Cpu*> retlist;
191   int llen = get_variable()->get_number_of_constraint();
192
193   for (int i = 0; i < llen; i++) {
194     /* Beware of composite actions: ptasks put links and cpus together */
195     // extra pb: we cannot dynamic_cast from void*...
196     kernel::resource::Resource* resource =
197         static_cast<kernel::resource::Resource*>(get_variable()->get_constraint(i)->get_id());
198     Cpu* cpu           = dynamic_cast<Cpu*>(resource);
199     if (cpu != nullptr)
200       retlist.push_back(cpu);
201   }
202
203   return retlist;
204 }
205
206 }
207 }