Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
privatize some fields of kernel::resource::Cpu
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 2013-2018. 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/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
8 #include "src/surf/surf_interface.hpp"
9 #include "surf/surf.hpp"
10
11 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
13
14 simgrid::surf::CpuModel *surf_cpu_model_pm;
15 simgrid::surf::CpuModel *surf_cpu_model_vm;
16
17 namespace simgrid {
18 namespace surf {
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
28     CpuAction* action = static_cast<CpuAction*>(get_action_heap().pop());
29     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
30
31     action->finish(kernel::resource::Action::State::FINISHED);
32     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
33   }
34   if (TRACE_is_enabled()) {
35     //defining the last timestamp that we can safely dump to trace file
36     //without losing the event ascending order (considering all CPU's)
37     double smaller = -1;
38     for (kernel::resource::Action const& action : *get_started_action_set()) {
39       if (smaller < 0 || action.get_last_update() < smaller)
40         smaller = action.get_last_update();
41     }
42     if (smaller > 0) {
43       TRACE_last_timestamp_to_dump = smaller;
44     }
45   }
46 }
47
48 void CpuModel::update_actions_state_full(double now, double delta)
49 {
50   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
51     CpuAction& action = static_cast<CpuAction&>(*it);
52     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
53
54     action.update_remains(action.get_variable()->get_value() * delta);
55
56     if (action.get_max_duration() != NO_MAX_DURATION)
57       action.update_max_duration(delta);
58
59     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_weight() > 0)) ||
60         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
61       action.finish(kernel::resource::Action::State::FINISHED);
62     }
63   }
64 }
65
66 /************
67  * Resource *
68  ************/
69 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
70     : Cpu(model, host, nullptr /*constraint*/, speedPerPstate, core)
71 {
72 }
73
74 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, kernel::lmm::Constraint* constraint,
75          std::vector<double>* speedPerPstate, int core)
76     : Resource(model, host->get_cname(), constraint), cores_count_(core), host_(host)
77 {
78   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->get_cname());
79
80   speed_.peak = speedPerPstate->front();
81   speed_.scale = 1;
82   host->pimpl_cpu = this;
83   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->get_cname());
84
85   // Copy the power peak array:
86   for (double const& value : *speedPerPstate) {
87     speed_per_pstate_.push_back(value);
88   }
89 }
90
91 Cpu::~Cpu() = default;
92
93 int Cpu::get_pstates_count()
94 {
95   return speed_per_pstate_.size();
96 }
97
98 void Cpu::set_pstate(int pstate_index)
99 {
100   xbt_assert(pstate_index <= static_cast<int>(speed_per_pstate_.size()),
101              "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
102              "call to change the pstate.",
103              get_cname(), pstate_index, static_cast<int>(speed_per_pstate_.size()));
104
105   double new_peak_speed = speed_per_pstate_[pstate_index];
106   pstate_ = pstate_index;
107   speed_.peak = new_peak_speed;
108
109   onSpeedChange();
110 }
111
112 int Cpu::get_pstate()
113 {
114   return pstate_;
115 }
116
117 double Cpu::getPstateSpeed(int pstate_index)
118 {
119   xbt_assert((pstate_index <= static_cast<int>(speed_per_pstate_.size())),
120              "Invalid parameters (pstate index out of bounds)");
121
122   return speed_per_pstate_[pstate_index];
123 }
124
125 double Cpu::getSpeed(double load)
126 {
127   return load * speed_.peak;
128 }
129
130 double Cpu::get_available_speed()
131 {
132 /* number between 0 and 1 */
133   return speed_.scale;
134 }
135
136 void Cpu::onSpeedChange() {
137   s4u::Host::on_speed_change(*host_);
138 }
139
140 int Cpu::get_cores_count()
141 {
142   return cores_count_;
143 }
144
145 void Cpu::setStateTrace(tmgr_trace_t trace)
146 {
147   xbt_assert(state_event_ == nullptr, "Cannot set a second state trace to Host %s", host_->get_cname());
148
149   state_event_ = future_evt_set->add_trace(trace, this);
150 }
151 void Cpu::set_speed_trace(tmgr_trace_t trace)
152 {
153   xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->get_cname());
154
155   speed_.event = future_evt_set->add_trace(trace, this);
156 }
157
158
159 /**********
160  * Action *
161  **********/
162
163 void CpuAction::update_remains_lazy(double now)
164 {
165   xbt_assert(get_state_set() == get_model()->get_started_action_set(),
166              "You're updating an action that is not running.");
167   xbt_assert(get_priority() > 0, "You're updating an action that seems suspended.");
168
169   double delta = now - get_last_update();
170
171   if (get_remains_no_update() > 0) {
172     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
173                get_last_update());
174     update_remains(get_last_value() * delta);
175
176     if (TRACE_is_enabled()) {
177       Cpu* cpu = static_cast<Cpu*>(get_variable()->get_constraint(0)->get_id());
178       TRACE_surf_resource_set_utilization("HOST", "power_used", cpu->get_cname(), get_category(), get_last_value(),
179                                           get_last_update(), now - get_last_update());
180     }
181     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, get_remains_no_update());
182   }
183
184   set_last_update();
185   set_last_value(get_variable()->get_value());
186 }
187
188 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, kernel::resource::Action::State)> CpuAction::onStateChange;
189
190 void CpuAction::suspend(){
191   Action::State previous = get_state();
192   onStateChange(this, previous);
193   Action::suspend();
194 }
195
196 void CpuAction::resume(){
197   Action::State previous = get_state();
198   onStateChange(this, previous);
199   Action::resume();
200 }
201
202 void CpuAction::set_state(Action::State state)
203 {
204   Action::State previous = get_state();
205   Action::set_state(state);
206   onStateChange(this, previous);
207 }
208 /** @brief returns a list of all CPUs that this action is using */
209 std::list<Cpu*> CpuAction::cpus() {
210   std::list<Cpu*> retlist;
211   int llen = get_variable()->get_number_of_constraint();
212
213   for (int i = 0; i < llen; i++) {
214     /* Beware of composite actions: ptasks put links and cpus together */
215     // extra pb: we cannot dynamic_cast from void*...
216     kernel::resource::Resource* resource =
217         static_cast<kernel::resource::Resource*>(get_variable()->get_constraint(i)->get_id());
218     Cpu* cpu           = dynamic_cast<Cpu*>(resource);
219     if (cpu != nullptr)
220       retlist.push_back(cpu);
221   }
222
223   return retlist;
224 }
225
226 }
227 }