Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename Action::priority into Action::sharing_penalty in surf as it should be
[simgrid.git] / src / surf / cpu_interface.cpp
1 /* Copyright (c) 2013-2019. 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/kernel/resource/profile/Profile.hpp"
8 #include "src/surf/surf_interface.hpp"
9 #include "surf/surf.hpp"
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::kernel::resource::CpuModel* surf_cpu_model_pm;
15 simgrid::kernel::resource::CpuModel* surf_cpu_model_vm;
16
17 namespace simgrid {
18 namespace kernel {
19 namespace resource {
20
21 /*********
22  * Model *
23  *********/
24
25 void CpuModel::update_actions_state_lazy(double now, double /*delta*/)
26 {
27   while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
28
29     CpuAction* action = static_cast<CpuAction*>(get_action_heap().pop());
30     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
31
32     action->finish(kernel::resource::Action::State::FINISHED);
33     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
34   }
35 }
36
37 void CpuModel::update_actions_state_full(double /*now*/, double delta)
38 {
39   for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
40     CpuAction& action = static_cast<CpuAction&>(*it);
41     ++it; // increment iterator here since the following calls to action.finish() may invalidate it
42
43     action.update_remains(action.get_variable()->get_value() * delta);
44     action.update_max_duration(delta);
45
46     if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_weight() > 0)) ||
47         ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
48       action.finish(Action::State::FINISHED);
49     }
50   }
51 }
52
53 /************
54  * Resource *
55  ************/
56 Cpu::Cpu(Model* model, s4u::Host* host, const std::vector<double>& speed_per_pstate, int core)
57     : Cpu(model, host, nullptr /*constraint*/, speed_per_pstate, core)
58 {
59 }
60
61 Cpu::Cpu(Model* model, s4u::Host* host, lmm::Constraint* constraint, const std::vector<double>& speed_per_pstate,
62          int core)
63     : Resource(model, host->get_cname(), constraint)
64     , core_count_(core)
65     , host_(host)
66     , speed_per_pstate_(speed_per_pstate)
67 {
68   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->get_cname());
69
70   speed_.peak     = speed_per_pstate_.front();
71   speed_.scale = 1;
72   host->pimpl_cpu = this;
73   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->get_cname());
74 }
75
76 int Cpu::get_pstate_count() const
77 {
78   return speed_per_pstate_.size();
79 }
80
81 void Cpu::set_pstate(int pstate_index)
82 {
83   xbt_assert(pstate_index <= static_cast<int>(speed_per_pstate_.size()),
84              "Invalid parameters for CPU %s (pstate %d > length of pstates %d). Please fix your platform file, or your "
85              "call to change the pstate.",
86              get_cname(), pstate_index, static_cast<int>(speed_per_pstate_.size()));
87
88   double new_peak_speed = speed_per_pstate_[pstate_index];
89   pstate_ = pstate_index;
90   speed_.peak = new_peak_speed;
91
92   on_speed_change();
93 }
94
95 int Cpu::get_pstate() const
96 {
97   return pstate_;
98 }
99
100 double Cpu::get_pstate_peak_speed(int pstate_index) const
101 {
102   xbt_assert((pstate_index <= static_cast<int>(speed_per_pstate_.size())),
103              "Invalid parameters (pstate index out of bounds)");
104
105   return speed_per_pstate_[pstate_index];
106 }
107
108 double Cpu::get_speed(double load) const
109 {
110   return load * speed_.peak;
111 }
112
113 double Cpu::get_speed_ratio()
114 {
115 /* number between 0 and 1 */
116   return speed_.scale;
117 }
118
119 void Cpu::on_speed_change()
120 {
121   s4u::Host::on_speed_change(*host_);
122 }
123
124 int Cpu::get_core_count()
125 {
126   return core_count_;
127 }
128
129 void Cpu::set_speed_profile(kernel::profile::Profile* profile)
130 {
131   xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->get_cname());
132
133   speed_.event = profile->schedule(&profile::future_evt_set, this);
134 }
135
136
137 /**********
138  * Action *
139  **********/
140
141 void CpuAction::update_remains_lazy(double now)
142 {
143   xbt_assert(get_state_set() == get_model()->get_started_action_set(),
144              "You're updating an action that is not running.");
145   xbt_assert(get_sharing_penalty() > 0, "You're updating an action that seems suspended.");
146
147   double delta = now - get_last_update();
148
149   if (get_remains_no_update() > 0) {
150     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, get_remains_no_update(),
151                get_last_update());
152     update_remains(get_last_value() * delta);
153
154     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, get_remains_no_update());
155   }
156
157   set_last_update();
158   set_last_value(get_variable()->get_value());
159 }
160
161 xbt::signal<void(CpuAction const&, Action::State)> CpuAction::on_state_change;
162
163 void CpuAction::suspend(){
164   Action::State previous = get_state();
165   on_state_change(*this, previous);
166   Action::suspend();
167 }
168
169 void CpuAction::resume(){
170   Action::State previous = get_state();
171   on_state_change(*this, previous);
172   Action::resume();
173 }
174
175 void CpuAction::set_state(Action::State state)
176 {
177   Action::State previous = get_state();
178   Action::set_state(state);
179   on_state_change(*this, previous);
180 }
181
182 /** @brief returns a list of all CPUs that this action is using */
183 std::list<Cpu*> CpuAction::cpus() const
184 {
185   std::list<Cpu*> retlist;
186   int llen = get_variable()->get_number_of_constraint();
187
188   for (int i = 0; i < llen; i++) {
189     /* Beware of composite actions: ptasks put links and cpus together */
190     // extra pb: we cannot dynamic_cast from void*...
191     Resource* resource = static_cast<Resource*>(get_variable()->get_constraint(i)->get_id());
192     Cpu* cpu           = dynamic_cast<Cpu*>(resource);
193     if (cpu != nullptr)
194       retlist.push_back(cpu);
195   }
196
197   return retlist;
198 }
199 } // namespace resource
200 } // namespace kernel
201 } // namespace simgrid