Logo AND Algorithmique Numérique Distribuée

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