Logo AND Algorithmique Numérique Distribuée

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