Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename a field for cosmetics
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 2013-2015. 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 <xbt/dynar.h>
8 #include "cpu_interface.hpp"
9 #include "plugins/energy.hpp"
10 #include "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
11
12 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
14
15 simgrid::surf::CpuModel *surf_cpu_model_pm;
16 simgrid::surf::CpuModel *surf_cpu_model_vm;
17
18 namespace simgrid {
19 namespace surf {
20
21 /*********
22  * Model *
23  *********/
24
25 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
26 {
27   while ((xbt_heap_size(getActionHeap()) > 0)
28          && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
29
30     CpuAction *action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
31     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
32     if (TRACE_is_enabled()) {
33       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
34       TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
35                                       lmm_variable_getvalue(action->getVariable()),
36                                       action->getLastUpdate(),
37                                       now - action->getLastUpdate());
38     }
39
40     action->finish();
41     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
42
43     /* set the remains to 0 due to precision problems when updating the remaining amount */
44     action->setRemains(0);
45     action->setState(Action::State::done);
46   }
47   if (TRACE_is_enabled()) {
48     //defining the last timestamp that we can safely dump to trace file
49     //without losing the event ascending order (considering all CPU's)
50     double smaller = -1;
51     ActionList *actionSet = getRunningActionSet();
52     for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
53        ; it != itend ; ++it) {
54       CpuAction *action = static_cast<CpuAction*>(&*it);
55         if (smaller < 0) {
56           smaller = action->getLastUpdate();
57           continue;
58         }
59         if (action->getLastUpdate() < smaller) {
60           smaller = action->getLastUpdate();
61         }
62     }
63     if (smaller > 0) {
64       TRACE_last_timestamp_to_dump = smaller;
65     }
66   }
67 }
68
69 void CpuModel::updateActionsStateFull(double now, double delta)
70 {
71   CpuAction *action = nullptr;
72   ActionList *running_actions = getRunningActionSet();
73
74   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
75      ; it != itend ; it=itNext) {
76     ++itNext;
77     action = static_cast<CpuAction*>(&*it);
78     if (TRACE_is_enabled()) {
79       Cpu *cpu = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
80
81       TRACE_surf_host_set_utilization(cpu->getName(),
82                                       action->getCategory(),
83                                       lmm_variable_getvalue(action->getVariable()),
84                                       now - delta,
85                                       delta);
86       TRACE_last_timestamp_to_dump = now - delta;
87     }
88
89     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
90
91
92     if (action->getMaxDuration() != NO_MAX_DURATION)
93       action->updateMaxDuration(delta);
94
95
96     if ((action->getRemainsNoUpdate() <= 0) &&
97         (lmm_get_variable_weight(action->getVariable()) > 0)) {
98       action->finish();
99       action->setState(Action::State::done);
100     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
101                (action->getMaxDuration() <= 0)) {
102       action->finish();
103       action->setState(Action::State::done);
104     }
105   }
106 }
107
108 /************
109  * Resource *
110  ************/
111 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
112  : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
113 {
114 }
115
116 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint,
117     std::vector<double> * speedPerPstate, int core)
118  : Resource(model, host->name().c_str(), constraint)
119  , coresAmount_(core)
120  , host_(host)
121 {
122   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->name().c_str());
123
124   speed_.peak = speedPerPstate->front();
125   speed_.scale = 1;
126   host->pimpl_cpu = this;
127   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->name().c_str());
128
129   // Copy the power peak array:
130   for (double value : *speedPerPstate) {
131     speedPerPstate_.push_back(value);
132   }
133
134   xbt_assert(model == surf_cpu_model_pm || core==1, "Currently, VM cannot be multicore");
135 }
136
137 Cpu::~Cpu() = default;
138
139 double Cpu::getPstateSpeedCurrent()
140 {
141   return speed_.peak;
142 }
143
144 int Cpu::getNbPStates()
145 {
146   return speedPerPstate_.size();
147 }
148
149 void Cpu::setPState(int pstate_index)
150 {
151   xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
152       "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index,
153       static_cast<int>(speedPerPstate_.size()));
154
155   double new_peak_speed = speedPerPstate_[pstate_index];
156   pstate_ = pstate_index;
157   speed_.peak = new_peak_speed;
158
159   onSpeedChange();
160 }
161
162 int Cpu::getPState()
163 {
164   return pstate_;
165 }
166
167 double Cpu::getPstateSpeed(int pstate_index)
168 {
169   xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
170
171   return speedPerPstate_[pstate_index];
172 }
173
174 double Cpu::getSpeed(double load)
175 {
176   return load * speed_.peak;
177 }
178
179 double Cpu::getAvailableSpeed()
180 {
181 /* number between 0 and 1 */
182   return speed_.scale;
183 }
184
185 void Cpu::onSpeedChange() {
186   TRACE_surf_host_set_speed(surf_get_clock(), getName(), coresAmount_ * speed_.scale * speed_.peak);
187 }
188
189 int Cpu::coreCount()
190 {
191   return coresAmount_;
192 }
193
194 void Cpu::setStateTrace(tmgr_trace_t trace)
195 {
196   xbt_assert(stateEvent_==nullptr,"Cannot set a second state trace to Host %s", host_->name().c_str());
197
198   stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
199 }
200 void Cpu::setSpeedTrace(tmgr_trace_t trace)
201 {
202   xbt_assert(speed_.event==nullptr,"Cannot set a second speed trace to Host %s", host_->name().c_str());
203
204   speed_.event = future_evt_set->add_trace(trace, 0.0, this);
205 }
206
207
208 /**********
209  * Action *
210  **********/
211
212 void CpuAction::updateRemainingLazy(double now)
213 {
214   xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
215   xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
216
217   double delta = now - lastUpdate_;
218
219   if (remains_ > 0) {
220     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
221     double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
222
223     if (TRACE_is_enabled()) {
224       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
225       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), lastValue_, lastUpdate_, now - lastUpdate_);
226     }
227     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, remains_);
228   }
229
230   lastUpdate_ = now;
231   lastValue_ = lmm_variable_getvalue(getVariable());
232 }
233
234 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
235
236 void CpuAction::setState(Action::State state){
237   Action::State previous = getState();
238   Action::setState(state);
239   onStateChange(this, previous);
240 }
241 std::list<Cpu*> CpuAction::cpus() {
242   std::list<Cpu*> retlist;
243   lmm_system_t sys = getModel()->getMaxminSystem();
244   int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
245
246   for(int i = 0; i<llen; i++)
247     retlist.push_back( (Cpu*)(lmm_constraint_id( lmm_get_cnst_from_var(sys, getVariable(), i) )) );
248
249   return retlist;
250 }
251
252 }
253 }