Logo AND Algorithmique Numérique Distribuée

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