Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use references, and avoid disturbing expressions like &*it.
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "cpu_interface.hpp"
8 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
9 #include <xbt/dynar.h>
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::updateActionsStateLazy(double now, double /*delta*/)
25 {
26   while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
27
28     CpuAction* action = static_cast<CpuAction*>(actionHeapPop());
29     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
30     if (TRACE_is_enabled()) {
31       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
32       TRACE_surf_host_set_utilization(cpu->getCname(), action->getCategory(),
33                                       lmm_variable_getvalue(action->getVariable()), action->getLastUpdate(),
34                                       now - action->getLastUpdate());
35     }
36
37     action->finish(Action::State::done);
38     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
39
40     /* set the remains to 0 due to precision problems when updating the remaining amount */
41     action->setRemains(0);
42   }
43   if (TRACE_is_enabled()) {
44     //defining the last timestamp that we can safely dump to trace file
45     //without losing the event ascending order (considering all CPU's)
46     double smaller = -1;
47     for (Action const& action : *getRunningActionSet()) {
48       if (smaller < 0 || action.getLastUpdate() < smaller)
49         smaller = action.getLastUpdate();
50     }
51     if (smaller > 0) {
52       TRACE_last_timestamp_to_dump = smaller;
53     }
54   }
55 }
56
57 void CpuModel::updateActionsStateFull(double now, double delta)
58 {
59   for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
60     CpuAction& action = static_cast<CpuAction&>(*it);
61     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
62     if (TRACE_is_enabled()) {
63       Cpu* cpu =
64           static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action.getVariable(), 0)));
65
66       TRACE_surf_host_set_utilization(cpu->getCname(), action.getCategory(),
67                                       lmm_variable_getvalue(action.getVariable()), now - delta, delta);
68       TRACE_last_timestamp_to_dump = now - delta;
69     }
70
71     action.updateRemains(lmm_variable_getvalue(action.getVariable()) * delta);
72
73     if (action.getMaxDuration() != NO_MAX_DURATION)
74       action.updateMaxDuration(delta);
75
76     if (((action.getRemainsNoUpdate() <= 0) && (lmm_get_variable_weight(action.getVariable()) > 0)) ||
77         ((action.getMaxDuration() != NO_MAX_DURATION) && (action.getMaxDuration() <= 0))) {
78       action.finish(Action::State::done);
79     }
80   }
81 }
82
83 /************
84  * Resource *
85  ************/
86 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
87  : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
88 {
89 }
90
91 Cpu::Cpu(Model* model, simgrid::s4u::Host* host, lmm_constraint_t constraint, std::vector<double>* speedPerPstate,
92          int core)
93     : Resource(model, host->getCname(), constraint), coresAmount_(core), host_(host)
94 {
95   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->getCname());
96
97   speed_.peak = speedPerPstate->front();
98   speed_.scale = 1;
99   host->pimpl_cpu = this;
100   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->getCname());
101
102   // Copy the power peak array:
103   for (double const& value : *speedPerPstate) {
104     speedPerPstate_.push_back(value);
105   }
106 }
107
108 Cpu::~Cpu() = default;
109
110 int Cpu::getNbPStates()
111 {
112   return speedPerPstate_.size();
113 }
114
115 void Cpu::setPState(int pstate_index)
116 {
117   xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
118              "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
119              "call to change the pstate.",
120              getCname(), pstate_index, static_cast<int>(speedPerPstate_.size()));
121
122   double new_peak_speed = speedPerPstate_[pstate_index];
123   pstate_ = pstate_index;
124   speed_.peak = new_peak_speed;
125
126   onSpeedChange();
127 }
128
129 int Cpu::getPState()
130 {
131   return pstate_;
132 }
133
134 double Cpu::getPstateSpeed(int pstate_index)
135 {
136   xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
137
138   return speedPerPstate_[pstate_index];
139 }
140
141 double Cpu::getSpeed(double load)
142 {
143   return load * speed_.peak;
144 }
145
146 double Cpu::getAvailableSpeed()
147 {
148 /* number between 0 and 1 */
149   return speed_.scale;
150 }
151
152 void Cpu::onSpeedChange() {
153   TRACE_surf_host_set_speed(surf_get_clock(), getCname(), coresAmount_ * speed_.scale * speed_.peak);
154   s4u::Host::onSpeedChange(*host_);
155 }
156
157 int Cpu::coreCount()
158 {
159   return coresAmount_;
160 }
161
162 void Cpu::setStateTrace(tmgr_trace_t trace)
163 {
164   xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Host %s", host_->getCname());
165
166   stateEvent_ = future_evt_set->add_trace(trace, this);
167 }
168 void Cpu::setSpeedTrace(tmgr_trace_t trace)
169 {
170   xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->getCname());
171
172   speed_.event = future_evt_set->add_trace(trace, this);
173 }
174
175
176 /**********
177  * Action *
178  **********/
179
180 void CpuAction::updateRemainingLazy(double now)
181 {
182   xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
183   xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
184
185   double delta = now - getLastUpdate();
186
187   if (getRemainsNoUpdate() > 0) {
188     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, getRemainsNoUpdate(),
189                getLastUpdate());
190     updateRemains(getLastValue() * delta);
191
192     if (TRACE_is_enabled()) {
193       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
194       TRACE_surf_host_set_utilization(cpu->getCname(), getCategory(), getLastValue(), getLastUpdate(),
195                                       now - getLastUpdate());
196     }
197     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, getRemainsNoUpdate());
198   }
199
200   refreshLastUpdate();
201   setLastValue(lmm_variable_getvalue(getVariable()));
202 }
203
204 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
205
206 void CpuAction::suspend(){
207   Action::State previous = getState();
208   onStateChange(this, previous);
209   Action::suspend();
210 }
211
212 void CpuAction::resume(){
213   Action::State previous = getState();
214   onStateChange(this, previous);
215   Action::resume();
216 }
217
218 void CpuAction::setState(Action::State state){
219   Action::State previous = getState();
220   Action::setState(state);
221   onStateChange(this, previous);
222 }
223 /** @brief returns a list of all CPUs that this action is using */
224 std::list<Cpu*> CpuAction::cpus() {
225   std::list<Cpu*> retlist;
226   lmm_system_t sys = getModel()->getMaxminSystem();
227   int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
228
229   for (int i = 0; i < llen; i++) {
230     /* Beware of composite actions: ptasks put links and cpus together */
231     // extra pb: we cannot dynamic_cast from void*...
232     Resource* resource = static_cast<Resource*>(lmm_constraint_id(lmm_get_cnst_from_var(sys, getVariable(), i)));
233     Cpu* cpu           = dynamic_cast<Cpu*>(resource);
234     if (cpu != nullptr)
235       retlist.push_back(cpu);
236   }
237
238   return retlist;
239 }
240
241 }
242 }