Logo AND Algorithmique Numérique Distribuée

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