Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[HostLoad] Elaborate on comment
[simgrid.git] / src / plugins / host_load.cpp
1 /* Copyright (c) 2010-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 "simgrid/plugins/load.h"
7 #include "src/include/surf/surf.hpp"
8 #include "src/plugins/vm/VirtualMachineImpl.hpp"
9 #include <simgrid/s4u.hpp>
10
11 SIMGRID_REGISTER_PLUGIN(host_load, "Cpu load", &sg_host_load_plugin_init)
12
13 /** @addtogroup plugin_load
14
15 This plugin makes it very simple for users to obtain the current load for each host.
16
17 */
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_load, surf, "Logging specific to the HostLoad plugin");
20
21 namespace simgrid {
22 namespace plugin {
23
24 class HostLoad {
25 public:
26   static simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> EXTENSION_ID;
27
28   explicit HostLoad(simgrid::s4u::Host* ptr)
29       : host_(ptr)
30       , last_updated_(surf_get_clock())
31       , last_reset_(surf_get_clock())
32       , current_speed_(host_->get_speed())
33       , current_flops_(host_->pimpl_cpu->get_constraint()->get_usage())
34       , theor_max_flops_(0)
35   {
36   }
37   ~HostLoad() = default;
38   HostLoad() = delete;
39   explicit HostLoad(simgrid::s4u::Host& ptr) = delete;
40   explicit HostLoad(simgrid::s4u::Host&& ptr) = delete;
41
42   double get_current_load();
43   double get_average_load() { return (theor_max_flops_ == 0) ? 0 : computed_flops_ / theor_max_flops_; };
44   double get_computed_flops() { return computed_flops_; }
45   double get_idle_time() { return idle_time_; } /** Return idle time since last reset */
46   double get_total_idle_time() { return total_idle_time_; } /** Return idle time over the whole simulation */
47   void update();
48   void reset();
49
50 private:
51   simgrid::s4u::Host* host_ = nullptr;
52   double last_updated_      = 0;
53   double last_reset_        = 0;
54   /**
55    * current_speed each core is running at right now
56    */
57   double current_speed_     = 0;
58   /**
59    * How many flops are currently used by all the processes running on this
60    * host?
61    */
62   double current_flops_     = 0;
63   double computed_flops_    = 0;
64   double idle_time_         = 0;
65   double total_idle_time_   = 0; /* This gets never reset */
66   double theor_max_flops_   = 0;
67 };
68
69 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
70
71 void HostLoad::update()
72 {
73   double now = surf_get_clock();
74
75   /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k @in {0, 1, ..., cores-1}
76    * designates number of active cores; will be 0 if CPU is currently idle */
77   current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
78
79   /* flops == pstate_speed * cores_being_currently_used */
80   computed_flops_ += (now - last_updated_) * current_flops_;
81
82   if (current_flops_ == 0) {
83     idle_time_ += (now - last_updated_);
84     total_idle_time_ += (now - last_updated_);
85   }
86
87   theor_max_flops_ += current_speed_ * host_->get_core_count() * (now - last_updated_);
88   current_speed_ = host_->get_speed();
89   last_updated_  = now;
90 }
91
92 /**
93  * WARNING: This function does not guarantee that you have the real load at any time imagine all actions on your CPU
94  * terminate at time t. Your load is then 0. Then you query the load (still 0) and then another action starts (still at
95  * time t!). This means that the load was never really 0 (because the time didn't advance) but it will still be reported
96  * as 0.
97  *
98  * So, use at your own risk.
99  */
100 double HostLoad::get_current_load()
101 {
102   // We don't need to call update() here because it is called every time an action terminates or starts
103   // FIXME: Can this happen at the same time? stop -> call to getCurrentLoad, load = 0 -> next action starts?
104   return current_flops_ / static_cast<double>(host_->get_speed() * host_->get_core_count());
105 }
106
107 /*
108  * Resets the counters
109  */
110 void HostLoad::reset()
111 {
112   last_updated_    = surf_get_clock();
113   last_reset_      = surf_get_clock();
114   idle_time_       = 0;
115   computed_flops_  = 0;
116   theor_max_flops_ = 0;
117   current_flops_   = host_->pimpl_cpu->get_constraint()->get_usage();
118   current_speed_   = host_->get_speed();
119 }
120 } // namespace plugin
121 } // namespace simgrid
122
123 using simgrid::plugin::HostLoad;
124
125 /* **************************** events  callback *************************** */
126 /* This callback is fired either when the host changes its state (on/off) or its speed
127  * (because the user changed the pstate, or because of external trace events) */
128 static void on_host_change(simgrid::s4u::Host& host)
129 {
130   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
131     return;
132
133   host.extension<HostLoad>()->update();
134 }
135
136 /* This callback is called when an action (computation, idle, ...) terminates */
137 static void on_action_state_change(simgrid::surf::CpuAction* action, simgrid::kernel::resource::Action::State /*previous*/)
138 {
139   for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
140     simgrid::s4u::Host* host = cpu->get_host();
141
142     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
143       return;
144
145     if (host != nullptr) {
146       host->extension<HostLoad>()->update();
147     }
148   }
149 }
150
151 /* **************************** Public interface *************************** */
152
153 /** @ingroup plugin_load
154  * @brief Initializes the HostLoad plugin
155  * @details The HostLoad plugin provides an API to get the current load of each host.
156  */
157 void sg_host_load_plugin_init()
158 {
159   if (HostLoad::EXTENSION_ID.valid())
160     return;
161
162   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
163
164   if (simgrid::s4u::Engine::is_initialized()) { // If not yet initialized, this would create a new instance
165                                                 // which would cause seg faults...
166     simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
167     for (auto& host : e->get_all_hosts()) {
168       host->extension_set(new HostLoad(host));
169     }
170   }
171
172   /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
173
174   simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
175     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
176       return;
177     host.extension_set(new HostLoad(&host));
178   });
179
180   simgrid::surf::CpuAction::on_state_change.connect(&on_action_state_change);
181   simgrid::s4u::Host::on_state_change.connect(&on_host_change);
182   simgrid::s4u::Host::on_speed_change.connect(&on_host_change);
183 }
184
185 /** @brief Returns the current load of the host passed as argument
186  *
187  *  See also @ref plugin_load
188  */
189 double sg_host_get_current_load(sg_host_t host)
190 {
191   xbt_assert(HostLoad::EXTENSION_ID.valid(),
192              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
193
194   return host->extension<HostLoad>()->get_current_load();
195 }
196
197 /** @brief Returns the current load of the host passed as argument
198  *
199  *  See also @ref plugin_load
200  */
201 double sg_host_get_avg_load(sg_host_t host)
202 {
203   xbt_assert(HostLoad::EXTENSION_ID.valid(),
204              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
205
206   return host->extension<HostLoad>()->get_average_load();
207 }
208
209 /** @brief Returns the time this host was idle since the last reset
210  *
211  *  See also @ref plugin_load
212  */
213 double sg_host_get_idle_time(sg_host_t host)
214 {
215   xbt_assert(HostLoad::EXTENSION_ID.valid(),
216              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
217
218   return host->extension<HostLoad>()->get_idle_time();
219 }
220
221 double sg_host_get_total_idle_time(sg_host_t host)
222 {
223   xbt_assert(HostLoad::EXTENSION_ID.valid(),
224              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
225
226   return host->extension<HostLoad>()->get_total_idle_time();
227 }
228
229 double sg_host_get_computed_flops(sg_host_t host)
230 {
231   xbt_assert(HostLoad::EXTENSION_ID.valid(),
232              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
233
234   return host->extension<HostLoad>()->get_computed_flops();
235 }
236
237 void sg_host_load_reset(sg_host_t host)
238 {
239   xbt_assert(HostLoad::EXTENSION_ID.valid(),
240              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
241
242   host->extension<HostLoad>()->reset();
243 }