Logo AND Algorithmique Numérique Distribuée

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