Logo AND Algorithmique Numérique Distribuée

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