Logo AND Algorithmique Numérique Distribuée

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