Logo AND Algorithmique Numérique Distribuée

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