Logo AND Algorithmique Numérique Distribuée

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