Logo AND Algorithmique Numérique Distribuée

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