Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a solve() fun pointer to maxmin datatype (+minor cleanups)
[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 /************
111  * Resource *
112  ************/
113 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
114  : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
115 {
116 }
117
118 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, lmm_constraint_t constraint,
119     std::vector<double> * speedPerPstate, int core)
120  : Resource(model, host->name().c_str(), constraint)
121  , coresAmount_(core)
122  , host_(host)
123 {
124   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->name().c_str());
125
126   speed_.peak = speedPerPstate->front();
127   speed_.scale = 1;
128   host->pimpl_cpu = this;
129   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->name().c_str());
130
131   // Copy the power peak array:
132   for (double value : *speedPerPstate) {
133     speedPerPstate_.push_back(value);
134   }
135
136   xbt_assert(model == surf_cpu_model_pm || core==1, "Currently, VM cannot be multicore");
137 }
138
139 Cpu::~Cpu() = default;
140
141 double Cpu::getPstateSpeedCurrent()
142 {
143   return speed_.peak;
144 }
145
146 int Cpu::getNbPStates()
147 {
148   return speedPerPstate_.size();
149 }
150
151 void Cpu::setPState(int pstate_index)
152 {
153   xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
154       "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index,
155       static_cast<int>(speedPerPstate_.size()));
156
157   double new_peak_speed = speedPerPstate_[pstate_index];
158   pstate_ = pstate_index;
159   speed_.peak = new_peak_speed;
160
161   onSpeedChange();
162 }
163
164 int Cpu::getPState()
165 {
166   return pstate_;
167 }
168
169 double Cpu::getPstateSpeed(int pstate_index)
170 {
171   xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
172
173   return speedPerPstate_[pstate_index];
174 }
175
176 double Cpu::getSpeed(double load)
177 {
178   return load * speed_.peak;
179 }
180
181 double Cpu::getAvailableSpeed()
182 {
183 /* number between 0 and 1 */
184   return speed_.scale;
185 }
186
187 void Cpu::onSpeedChange() {
188   TRACE_surf_host_set_speed(surf_get_clock(), getName(), coresAmount_ * speed_.scale * speed_.peak);
189 }
190
191 int Cpu::getCoreCount()
192 {
193   return coresAmount_;
194 }
195
196 void Cpu::setStateTrace(tmgr_trace_t trace)
197 {
198   xbt_assert(stateEvent_==nullptr,"Cannot set a second state trace to Host %s", host_->name().c_str());
199
200   stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
201 }
202 void Cpu::setSpeedTrace(tmgr_trace_t trace)
203 {
204   xbt_assert(speed_.event==nullptr,"Cannot set a second speed trace to Host %s", host_->name().c_str());
205
206   speed_.event = future_evt_set->add_trace(trace, 0.0, this);
207 }
208
209
210 /**********
211  * Action *
212  **********/
213
214 void CpuAction::updateRemainingLazy(double now)
215 {
216   xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
217   xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
218
219   double delta = now - lastUpdate_;
220
221   if (remains_ > 0) {
222     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
223     double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
224
225     if (TRACE_is_enabled()) {
226       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
227       TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), lastValue_, lastUpdate_, now - lastUpdate_);
228     }
229     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, remains_);
230   }
231
232   lastUpdate_ = now;
233   lastValue_ = lmm_variable_getvalue(getVariable());
234 }
235
236 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
237
238 void CpuAction::setState(Action::State state){
239   Action::State previous = getState();
240   Action::setState(state);
241   onStateChange(this, previous);
242 }
243 std::list<Cpu*> CpuAction::cpus() {
244   std::list<Cpu*> retlist;
245   lmm_system_t sys = getModel()->getMaxminSystem();
246   int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
247
248   for(int i = 0; i<llen; i++)
249     retlist.push_back( (Cpu*)(lmm_constraint_id( lmm_get_cnst_from_var(sys, getVariable(), i) )) );
250
251   return retlist;
252 }
253
254 }
255 }