Logo AND Algorithmique Numérique Distribuée

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