Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
45b89e76fd575f622294681124514ac47b05aa3c
[simgrid.git] / src / plugins / host_load.cpp
1 /* Copyright (c) 2010-2019. 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/kernel/activity/ExecImpl.hpp"
9 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 #include <simgrid/s4u.hpp>
11
12 SIMGRID_REGISTER_PLUGIN(host_load, "Cpu load", &sg_host_load_plugin_init)
13
14 /** @addtogroup plugin_load
15
16 This plugin makes it very simple for users to obtain the current load for each host.
17
18 */
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_load, surf, "Logging specific to the HostLoad plugin");
21
22 namespace simgrid {
23 namespace plugin {
24
25 static const double activity_uninitialized_remaining_cost = -1;
26
27 class HostLoad {
28 public:
29   static simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> EXTENSION_ID;
30
31   explicit HostLoad(simgrid::s4u::Host* ptr)
32       : host_(ptr)
33       , last_updated_(surf_get_clock())
34       , last_reset_(surf_get_clock())
35       , current_speed_(host_->get_speed())
36       , current_flops_(host_->pimpl_cpu->get_constraint()->get_usage())
37       , theor_max_flops_(0)
38   {
39   }
40   ~HostLoad() = default;
41   HostLoad() = delete;
42   explicit HostLoad(simgrid::s4u::Host& ptr) = delete;
43   explicit HostLoad(simgrid::s4u::Host&& ptr) = delete;
44
45   double get_current_load();
46   /** Get the the average load since last reset(), as a ratio
47    *
48    * That's the ratio (amount of flops that were actually computed) / (amount of flops that could have been computed at full speed)
49    */
50   double get_average_load() { update(); return (theor_max_flops_ == 0) ? 0 : computed_flops_ / theor_max_flops_; };
51   /** Amount of flops computed since last reset() */
52   double get_computed_flops() { update(); return computed_flops_; }
53   /** Return idle time since last reset() */
54   double get_idle_time() { update(); return idle_time_; }
55   /** Return idle time over the whole simulation */
56   double get_total_idle_time() { update(); return total_idle_time_; }
57   void update();
58   void add_activity(simgrid::kernel::activity::ExecImplPtr activity);
59   void reset();
60
61 private:
62   simgrid::s4u::Host* host_ = nullptr;
63   /* Stores all currently ongoing activities (computations) on this machine */
64   std::map<simgrid::kernel::activity::ExecImplPtr, /* cost still remaining*/double> current_activities;
65   double last_updated_      = 0;
66   double last_reset_        = 0;
67   /**
68    * current_speed each core is running at; we need to store this as the speed
69    * will already have changed once we get notified
70    */
71   double current_speed_     = 0;
72   /**
73    * How many flops are currently used by all the processes running on this
74    * host?
75    */
76   double current_flops_     = 0;
77   double computed_flops_    = 0;
78   double idle_time_         = 0;
79   double total_idle_time_   = 0; /* This gets never reset */
80   double theor_max_flops_   = 0;
81 };
82
83 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
84
85 void HostLoad::add_activity(simgrid::kernel::activity::ExecImplPtr activity)
86 {
87   current_activities.insert({activity, activity_uninitialized_remaining_cost});
88 }
89
90 void HostLoad::update()
91 {
92   double now = surf_get_clock();
93
94   // This loop updates the flops that the host executed for the ongoing computations
95   auto iter = begin(current_activities);
96   while (iter != end(current_activities)) {
97     auto& activity                         = iter->first;  // Just an alias
98     auto& remaining_cost_after_last_update = iter->second; // Just an alias
99     auto& action                           = activity->surf_action_;
100     auto current_iter                      = iter;
101     ++iter;
102
103     if (action != nullptr && action->get_finish_time() != now && activity->state_ == e_smx_state_t::SIMIX_RUNNING) {
104       if (remaining_cost_after_last_update == activity_uninitialized_remaining_cost) {
105         remaining_cost_after_last_update = action->get_cost();
106       }
107       double computed_flops_since_last_update = remaining_cost_after_last_update - /*remaining now*/activity->get_remaining();
108       computed_flops_                        += computed_flops_since_last_update;
109       remaining_cost_after_last_update        = activity->get_remaining();
110     }
111     else if (activity->state_ == e_smx_state_t::SIMIX_DONE) {
112       computed_flops_ += remaining_cost_after_last_update;
113       current_activities.erase(current_iter);
114     }
115   }
116
117   /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k @in {0, 1, ..., cores-1}
118    * designates number of active cores; will be 0 if CPU is currently idle */
119   current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
120
121   if (current_flops_ == 0) {
122     idle_time_ += (now - last_updated_);
123     total_idle_time_ += (now - last_updated_);
124     XBT_DEBUG("[%s]: Currently idle -> Added %f seconds to idle time (totaling %fs)", host_->get_cname(), (now - last_updated_), idle_time_);
125   }
126
127   theor_max_flops_ += current_speed_ * host_->get_core_count() * (now - last_updated_);
128   current_speed_ = host_->get_speed();
129   last_updated_  = now;
130 }
131
132 /** @brief Get the current load as a ratio = achieved_flops / (core_current_speed * core_amount)
133  *
134  * You may also want to check simgrid::s4u::Host::get_load() that simply returns
135  * the achieved flop rate (in flops per seconds), ie the load that a new action arriving on
136  * that host would suffer.
137  *
138  * Please note that this function only returns an instantaneous load that may be deceiving
139  * in some scenarios. For example, imagine that an activity terminates at time t, and that
140  * another activity is created on the same host at the exact same timestamp. The load was
141  * never 0 on the simulated machine since the time did not advance between the two events.
142  * But still, if you call this function between the two events (in the simulator course), it
143  * returns 0 although there is no time (in the simulated time) where this value is valid.
144  */
145 double HostLoad::get_current_load()
146 {
147   // We don't need to call update() here because it is called every time an action terminates or starts
148   return current_flops_ / static_cast<double>(host_->get_speed() * host_->get_core_count());
149 }
150
151 /*
152  * Resets the counters
153  */
154 void HostLoad::reset()
155 {
156   last_updated_    = surf_get_clock();
157   last_reset_      = surf_get_clock();
158   idle_time_       = 0;
159   computed_flops_  = 0;
160   theor_max_flops_ = 0;
161   current_flops_   = host_->pimpl_cpu->get_constraint()->get_usage();
162   current_speed_   = host_->get_speed();
163 }
164 } // namespace plugin
165 } // namespace simgrid
166
167 using simgrid::plugin::HostLoad;
168
169 /* **************************** events  callback *************************** */
170 /* This callback is fired either when the host changes its state (on/off) or its speed
171  * (because the user changed the pstate, or because of external trace events) */
172 static void on_host_change(simgrid::s4u::Host const& host)
173 {
174   if (dynamic_cast<simgrid::s4u::VirtualMachine const*>(&host)) // Ignore virtual machines
175     return;
176
177   host.extension<HostLoad>()->update();
178 }
179
180 /* **************************** Public interface *************************** */
181
182 /** @brief Initializes the HostLoad plugin
183  * @details The HostLoad plugin provides an API to get the current load of each host.
184  */
185 void sg_host_load_plugin_init()
186 {
187   if (HostLoad::EXTENSION_ID.valid())
188     return;
189
190   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
191
192   if (simgrid::s4u::Engine::is_initialized()) { // If not yet initialized, this would create a new instance
193                                                 // which would cause seg faults...
194     simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
195     for (auto& host : e->get_all_hosts()) {
196       host->extension_set(new HostLoad(host));
197     }
198   }
199
200   /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
201
202   simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
203     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
204       return;
205     host.extension_set(new HostLoad(&host));
206   });
207
208   simgrid::kernel::activity::ExecImpl::on_creation.connect([](simgrid::kernel::activity::ExecImpl& activity) {
209     if (activity.get_host_number() == 1) { // We only run on one host
210       simgrid::s4u::Host* host         = activity.get_host();
211       simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(host);
212       if (vm != nullptr)
213         host = vm->get_pm();
214       xbt_assert(host != nullptr);
215       host->extension<HostLoad>()->add_activity(&activity);
216       host->extension<HostLoad>()->update(); // If the system was idle until now, we need to update *before*
217                                              // this computation starts running so we can keep track of the
218                                              // idle time. (Communication operations don't trigger this hook!)
219     }
220     else { // This runs on multiple hosts
221       XBT_DEBUG("HostLoad plugin currently does not support executions on several hosts");
222     }
223   });
224   simgrid::kernel::activity::ExecImpl::on_completion.connect([](simgrid::kernel::activity::ExecImpl const& activity) {
225     if (activity.get_host_number() == 1) { // We only run on one host
226       simgrid::s4u::Host* host         = activity.get_host();
227       simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(host);
228       if (vm != nullptr)
229         host = vm->get_pm();
230       xbt_assert(host != nullptr);
231       host->extension<HostLoad>()->update();
232     }
233     else { // This runs on multiple hosts
234       XBT_DEBUG("HostLoad plugin currently does not support executions on several hosts");
235     }
236   });
237   simgrid::s4u::Host::on_state_change.connect(&on_host_change);
238   simgrid::s4u::Host::on_speed_change.connect(&on_host_change);
239 }
240
241 /** @brief Returns the current load of that host, as a ratio = achieved_flops / (core_current_speed * core_amount)
242  *
243  *  See simgrid::plugin::HostLoad::get_current_load() for the full documentation.
244  */
245 double sg_host_get_current_load(sg_host_t host)
246 {
247   xbt_assert(HostLoad::EXTENSION_ID.valid(),
248              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
249
250   return host->extension<HostLoad>()->get_current_load();
251 }
252
253 /** @brief Returns the current load of the host passed as argument
254  *
255  *  See also @ref plugin_load
256  */
257 double sg_host_get_avg_load(sg_host_t host)
258 {
259   xbt_assert(HostLoad::EXTENSION_ID.valid(),
260              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
261
262   return host->extension<HostLoad>()->get_average_load();
263 }
264
265 /** @brief Returns the time this host was idle since the last reset
266  *
267  *  See also @ref plugin_load
268  */
269 double sg_host_get_idle_time(sg_host_t host)
270 {
271   xbt_assert(HostLoad::EXTENSION_ID.valid(),
272              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
273
274   return host->extension<HostLoad>()->get_idle_time();
275 }
276
277 double sg_host_get_total_idle_time(sg_host_t host)
278 {
279   xbt_assert(HostLoad::EXTENSION_ID.valid(),
280              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
281
282   return host->extension<HostLoad>()->get_total_idle_time();
283 }
284
285 double sg_host_get_computed_flops(sg_host_t host)
286 {
287   xbt_assert(HostLoad::EXTENSION_ID.valid(),
288              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
289
290   return host->extension<HostLoad>()->get_computed_flops();
291 }
292
293 void sg_host_load_reset(sg_host_t host)
294 {
295   xbt_assert(HostLoad::EXTENSION_ID.valid(),
296              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
297
298   host->extension<HostLoad>()->reset();
299 }