Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
finish snake_casing resource::Action
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "cpu_interface.hpp"
7 #include "src/instr/instr_private.hpp" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
8 #include <xbt/dynar.h>
9
10 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
12
13 simgrid::surf::CpuModel *surf_cpu_model_pm;
14 simgrid::surf::CpuModel *surf_cpu_model_vm;
15
16 namespace simgrid {
17 namespace surf {
18
19 /*********
20  * Model *
21  *********/
22
23 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
24 {
25   while (not actionHeapIsEmpty() && double_equals(actionHeapTopDate(), now, sg_surf_precision)) {
26
27     CpuAction* action = static_cast<CpuAction*>(actionHeapPop());
28     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
29     if (TRACE_is_enabled()) {
30       Cpu* cpu = static_cast<Cpu*>(action->getVariable()->get_constraint(0)->get_id());
31       TRACE_surf_host_set_utilization(cpu->getCname(), action->get_category(), action->getVariable()->get_value(),
32                                       action->getLastUpdate(), now - action->getLastUpdate());
33     }
34
35     action->finish(kernel::resource::Action::State::done);
36     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
37
38     /* set the remains to 0 due to precision problems when updating the remaining amount */
39     action->set_remains(0);
40   }
41   if (TRACE_is_enabled()) {
42     //defining the last timestamp that we can safely dump to trace file
43     //without losing the event ascending order (considering all CPU's)
44     double smaller = -1;
45     for (kernel::resource::Action const& action : *getRunningActionSet()) {
46       if (smaller < 0 || action.getLastUpdate() < smaller)
47         smaller = action.getLastUpdate();
48     }
49     if (smaller > 0) {
50       TRACE_last_timestamp_to_dump = smaller;
51     }
52   }
53 }
54
55 void CpuModel::updateActionsStateFull(double now, double delta)
56 {
57   for (auto it = std::begin(*getRunningActionSet()); it != std::end(*getRunningActionSet());) {
58     CpuAction& action = static_cast<CpuAction&>(*it);
59     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
60     if (TRACE_is_enabled()) {
61       Cpu* cpu = static_cast<Cpu*>(action.getVariable()->get_constraint(0)->get_id());
62       TRACE_surf_host_set_utilization(cpu->getCname(), action.get_category(), action.getVariable()->get_value(),
63                                       now - delta, delta);
64       TRACE_last_timestamp_to_dump = now - delta;
65     }
66
67     action.update_remains(action.getVariable()->get_value() * delta);
68
69     if (action.get_max_duration() != NO_MAX_DURATION)
70       action.update_max_duration(delta);
71
72     if (((action.get_remains_no_update() <= 0) && (action.getVariable()->get_weight() > 0)) ||
73         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
74       action.finish(kernel::resource::Action::State::done);
75     }
76   }
77 }
78
79 /************
80  * Resource *
81  ************/
82 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
83     : Cpu(model, host, nullptr /*constraint*/, speedPerPstate, core)
84 {
85 }
86
87 Cpu::Cpu(kernel::resource::Model* model, simgrid::s4u::Host* host, kernel::lmm::Constraint* constraint,
88          std::vector<double>* speedPerPstate, int core)
89     : Resource(model, host->getCname(), constraint), coresAmount_(core), host_(host)
90 {
91   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->getCname());
92
93   speed_.peak = speedPerPstate->front();
94   speed_.scale = 1;
95   host->pimpl_cpu = this;
96   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->getCname());
97
98   // Copy the power peak array:
99   for (double const& value : *speedPerPstate) {
100     speedPerPstate_.push_back(value);
101   }
102 }
103
104 Cpu::~Cpu() = default;
105
106 int Cpu::getNbPStates()
107 {
108   return speedPerPstate_.size();
109 }
110
111 void Cpu::setPState(int pstate_index)
112 {
113   xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
114              "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
115              "call to change the pstate.",
116              getCname(), pstate_index, static_cast<int>(speedPerPstate_.size()));
117
118   double new_peak_speed = speedPerPstate_[pstate_index];
119   pstate_ = pstate_index;
120   speed_.peak = new_peak_speed;
121
122   onSpeedChange();
123 }
124
125 int Cpu::getPState()
126 {
127   return pstate_;
128 }
129
130 double Cpu::getPstateSpeed(int pstate_index)
131 {
132   xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
133
134   return speedPerPstate_[pstate_index];
135 }
136
137 double Cpu::getSpeed(double load)
138 {
139   return load * speed_.peak;
140 }
141
142 double Cpu::getAvailableSpeed()
143 {
144 /* number between 0 and 1 */
145   return speed_.scale;
146 }
147
148 void Cpu::onSpeedChange() {
149   TRACE_surf_host_set_speed(surf_get_clock(), getCname(), coresAmount_ * speed_.scale * speed_.peak);
150   s4u::Host::onSpeedChange(*host_);
151 }
152
153 int Cpu::coreCount()
154 {
155   return coresAmount_;
156 }
157
158 void Cpu::setStateTrace(tmgr_trace_t trace)
159 {
160   xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Host %s", host_->getCname());
161
162   stateEvent_ = future_evt_set->add_trace(trace, this);
163 }
164 void Cpu::setSpeedTrace(tmgr_trace_t trace)
165 {
166   xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->getCname());
167
168   speed_.event = future_evt_set->add_trace(trace, this);
169 }
170
171
172 /**********
173  * Action *
174  **********/
175
176 void CpuAction::updateRemainingLazy(double now)
177 {
178   xbt_assert(get_state_set() == get_model()->getRunningActionSet(), "You're updating an action that is not running.");
179   xbt_assert(get_priority() > 0, "You're updating an action that seems suspended.");
180
181   double delta = now - getLastUpdate();
182
183   if (get_remains_no_update() > 0) {
184     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
185                getLastUpdate());
186     update_remains(getLastValue() * delta);
187
188     if (TRACE_is_enabled()) {
189       Cpu* cpu = static_cast<Cpu*>(getVariable()->get_constraint(0)->get_id());
190       TRACE_surf_host_set_utilization(cpu->getCname(), get_category(), getLastValue(), getLastUpdate(),
191                                       now - getLastUpdate());
192     }
193     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, get_remains_no_update());
194   }
195
196   refreshLastUpdate();
197   setLastValue(getVariable()->get_value());
198 }
199
200 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, kernel::resource::Action::State)> CpuAction::onStateChange;
201
202 void CpuAction::suspend(){
203   Action::State previous = get_state();
204   onStateChange(this, previous);
205   Action::suspend();
206 }
207
208 void CpuAction::resume(){
209   Action::State previous = get_state();
210   onStateChange(this, previous);
211   Action::resume();
212 }
213
214 void CpuAction::set_state(Action::State state)
215 {
216   Action::State previous = get_state();
217   Action::set_state(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     kernel::resource::Resource* resource =
229         static_cast<kernel::resource::Resource*>(getVariable()->get_constraint(i)->get_id());
230     Cpu* cpu           = dynamic_cast<Cpu*>(resource);
231     if (cpu != nullptr)
232       retlist.push_back(cpu);
233   }
234
235   return retlist;
236 }
237
238 }
239 }