Logo AND Algorithmique Numérique Distribuée

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