Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case the s4u::Host signals
[simgrid.git] / src / surf / 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->getCoreCount() * (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->getCoreCount());
97 }
98
99 /**
100  * Return idle time since last reset
101  */
102 double HostLoad::getIdleTime() {
103   return idle_time;
104 }
105
106 double HostLoad::getAverageLoad()
107 {
108   if (theor_max_flops == 0) { // Avoid division by 0
109     return 0;
110   }
111
112   return computed_flops / theor_max_flops;
113 }
114
115 double HostLoad::getComputedFlops()
116 {
117   return computed_flops;
118 }
119
120 /*
121  * Resets the counters
122  */
123 void HostLoad::reset()
124 {
125   last_updated    = surf_get_clock();
126   last_reset      = surf_get_clock();
127   idle_time       = 0;
128   computed_flops  = 0;
129   theor_max_flops = 0;
130   current_flops   = host->pimpl_cpu->get_constraint()->get_usage();
131   current_speed   = host->getSpeed();
132   was_prev_idle   = (current_flops == 0);
133 }
134 }
135 }
136
137 using simgrid::plugin::HostLoad;
138
139 /* **************************** events  callback *************************** */
140 /* This callback is fired either when the host changes its state (on/off) or its speed
141  * (because the user changed the pstate, or because of external trace events) */
142 static void onHostChange(simgrid::s4u::Host& host)
143 {
144   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
145     return;
146
147   host.extension<HostLoad>()->update();
148 }
149
150 /* This callback is called when an action (computation, idle, ...) terminates */
151 static void onActionStateChange(simgrid::surf::CpuAction* action, simgrid::kernel::resource::Action::State /*previous*/)
152 {
153   for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
154     simgrid::s4u::Host* host = cpu->getHost();
155
156     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
157       return;
158
159     if (host != nullptr) {
160       host->extension<HostLoad>()->update();
161     }
162   }
163 }
164
165 /* **************************** Public interface *************************** */
166
167 /** \ingroup plugin_load
168  * \brief Initializes the HostLoad plugin
169  * \details The HostLoad plugin provides an API to get the current load of each host.
170  */
171 void sg_host_load_plugin_init()
172 {
173   if (HostLoad::EXTENSION_ID.valid())
174     return;
175
176   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
177
178   /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
179
180   simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
181     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
182       return;
183     host.extension_set(new HostLoad(&host));
184   });
185
186   simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
187   simgrid::s4u::Host::on_state_change.connect(&onHostChange);
188   simgrid::s4u::Host::on_speed_change.connect(&onHostChange);
189 }
190
191 /** @brief Returns the current load of the host passed as argument
192  *
193  *  See also @ref plugin_load
194  */
195 double sg_host_get_current_load(sg_host_t host)
196 {
197   xbt_assert(HostLoad::EXTENSION_ID.valid(),
198              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
199
200   return host->extension<HostLoad>()->getCurrentLoad();
201 }
202
203 /** @brief Returns the current load of the host passed as argument
204  *
205  *  See also @ref plugin_load
206  */
207 double sg_host_get_avg_load(sg_host_t host)
208 {
209   xbt_assert(HostLoad::EXTENSION_ID.valid(),
210              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
211
212   return host->extension<HostLoad>()->getAverageLoad();
213 }
214
215 /** @brief Returns the time this host was idle since the last reset
216  *
217  *  See also @ref plugin_load
218  */
219 double sg_host_get_idle_time(sg_host_t host)
220 {
221   xbt_assert(HostLoad::EXTENSION_ID.valid(),
222              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
223
224   return host->extension<HostLoad>()->getIdleTime();
225 }
226
227 double sg_host_get_computed_flops(sg_host_t host)
228 {
229   xbt_assert(HostLoad::EXTENSION_ID.valid(),
230              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
231
232   return host->extension<HostLoad>()->getComputedFlops();
233 }
234
235 void sg_host_load_reset(sg_host_t host)
236 {
237   xbt_assert(HostLoad::EXTENSION_ID.valid(),
238              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
239
240   host->extension<HostLoad>()->reset();
241 }