Logo AND Algorithmique Numérique Distribuée

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