Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add the Storage::read_async and Storage::write_async methods
[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/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   double get_average_load() { update(); return (theor_max_flops_ == 0) ? 0 : computed_flops_ / theor_max_flops_; };
47   double get_computed_flops() { update(); return computed_flops_; }
48   double get_idle_time() { update(); return idle_time_; } /** Return idle time since last reset */
49   double get_total_idle_time() { update(); return total_idle_time_; } /** Return idle time over the whole simulation */
50   void update();
51   void add_activity(simgrid::kernel::activity::ExecImplPtr activity);
52   void reset();
53
54 private:
55   simgrid::s4u::Host* host_ = nullptr;
56   /* Stores all currently ongoing activities (computations) on this machine */
57   std::map<simgrid::kernel::activity::ExecImplPtr, /* cost still remaining*/double> current_activities;
58   double last_updated_      = 0;
59   double last_reset_        = 0;
60   /**
61    * current_speed each core is running at; we need to store this as the speed
62    * will already have changed once we get notified
63    */
64   double current_speed_     = 0;
65   /**
66    * How many flops are currently used by all the processes running on this
67    * host?
68    */
69   double current_flops_     = 0;
70   double computed_flops_    = 0;
71   double idle_time_         = 0;
72   double total_idle_time_   = 0; /* This gets never reset */
73   double theor_max_flops_   = 0;
74 };
75
76 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
77
78 void HostLoad::add_activity(simgrid::kernel::activity::ExecImplPtr activity)
79 {
80   current_activities.insert({activity, activity_uninitialized_remaining_cost});
81 }
82
83 void HostLoad::update()
84 {
85   double now = surf_get_clock();
86
87   // This loop updates the flops that the host executed for the ongoing computations
88   auto iter = begin(current_activities);
89   while (iter != end(current_activities)) {
90     auto& activity                         = iter->first;  // Just an alias
91     auto& remaining_cost_after_last_update = iter->second; // Just an alias
92     auto current_iter                      = iter;
93     ++iter;
94
95     if (activity->surf_action_->get_finish_time() != now && activity->state_ == e_smx_state_t::SIMIX_RUNNING) {
96       if (remaining_cost_after_last_update == activity_uninitialized_remaining_cost) {
97         remaining_cost_after_last_update = activity->surf_action_->get_cost();
98       }
99       double computed_flops_since_last_update = remaining_cost_after_last_update - /*remaining now*/activity->get_remaining();
100       computed_flops_                        += computed_flops_since_last_update;
101       remaining_cost_after_last_update        = activity->get_remaining();
102     }
103     else if (activity->state_ == e_smx_state_t::SIMIX_DONE) {
104       computed_flops_ += remaining_cost_after_last_update;
105       current_activities.erase(current_iter);
106     }
107   }
108
109   /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k @in {0, 1, ..., cores-1}
110    * designates number of active cores; will be 0 if CPU is currently idle */
111   current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
112
113   if (current_flops_ == 0) {
114     idle_time_ += (now - last_updated_);
115     total_idle_time_ += (now - last_updated_);
116     XBT_DEBUG("[%s]: Currently idle -> Added %f seconds to idle time (totaling %fs)", host_->get_cname(), (now - last_updated_), idle_time_);
117   }
118
119   theor_max_flops_ += current_speed_ * host_->get_core_count() * (now - last_updated_);
120   current_speed_ = host_->get_speed();
121   last_updated_  = now;
122 }
123
124 /**
125  * WARNING: This function does not guarantee that you have the real load at any time imagine all actions on your CPU
126  * terminate at time t. Your load is then 0. Then you query the load (still 0) and then another action starts (still at
127  * time t!). This means that the load was never really 0 (because the time didn't advance) but it will still be reported
128  * as 0.
129  *
130  * So, use at your own risk.
131  */
132 double HostLoad::get_current_load()
133 {
134   // We don't need to call update() here because it is called every time an action terminates or starts
135   // FIXME: Can this happen at the same time? stop -> call to getCurrentLoad, load = 0 -> next action starts?
136   return current_flops_ / static_cast<double>(host_->get_speed() * host_->get_core_count());
137 }
138
139 /*
140  * Resets the counters
141  */
142 void HostLoad::reset()
143 {
144   last_updated_    = surf_get_clock();
145   last_reset_      = surf_get_clock();
146   idle_time_       = 0;
147   computed_flops_  = 0;
148   theor_max_flops_ = 0;
149   current_flops_   = host_->pimpl_cpu->get_constraint()->get_usage();
150   current_speed_   = host_->get_speed();
151 }
152 } // namespace plugin
153 } // namespace simgrid
154
155 using simgrid::plugin::HostLoad;
156
157 /* **************************** events  callback *************************** */
158 /* This callback is fired either when the host changes its state (on/off) or its speed
159  * (because the user changed the pstate, or because of external trace events) */
160 static void on_host_change(simgrid::s4u::Host& host)
161 {
162   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
163     return;
164
165   host.extension<HostLoad>()->update();
166 }
167
168 /* This callback is called when an action (computation, idle, ...) terminates */
169 static void on_action_state_change(simgrid::surf::CpuAction* action, simgrid::kernel::resource::Action::State /*previous*/)
170 {
171   for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
172     simgrid::s4u::Host* host = cpu->get_host();
173
174     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
175       return;
176
177     if (host != nullptr) {
178       host->extension<HostLoad>()->update();
179     }
180   }
181 }
182
183 /* **************************** Public interface *************************** */
184
185 /** @ingroup plugin_load
186  * @brief Initializes the HostLoad plugin
187  * @details The HostLoad plugin provides an API to get the current load of each host.
188  */
189 void sg_host_load_plugin_init()
190 {
191   if (HostLoad::EXTENSION_ID.valid())
192     return;
193
194   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
195
196   if (simgrid::s4u::Engine::is_initialized()) { // If not yet initialized, this would create a new instance
197                                                 // which would cause seg faults...
198     simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
199     for (auto& host : e->get_all_hosts()) {
200       host->extension_set(new HostLoad(host));
201     }
202   }
203
204   /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
205
206   simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
207     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
208       return;
209     host.extension_set(new HostLoad(&host));
210   });
211
212   simgrid::kernel::activity::ExecImpl::on_creation.connect([](simgrid::kernel::activity::ExecImplPtr activity){
213     if (activity->host_ != nullptr) { // We only run on one host
214       simgrid::s4u::Host* host = activity->host_;
215       if (dynamic_cast<simgrid::s4u::VirtualMachine*>(activity->host_))
216         host = dynamic_cast<simgrid::s4u::VirtualMachine*>(activity->host_)->get_pm();
217
218       host->extension<HostLoad>()->add_activity(activity);
219       host->extension<HostLoad>()->update(); // If the system was idle until now, we need to update *before*
220                                              // this computation starts running so we can keep track of the
221                                              // idle time. (Communication operations don't trigger this hook!)
222     }
223     else { // This runs on multiple hosts
224       XBT_DEBUG("HostLoad plugin currently does not support executions on several hosts");
225     }
226   });
227   simgrid::kernel::activity::ExecImpl::on_completion.connect([](simgrid::kernel::activity::ExecImplPtr activity){
228     if (activity->host_ != nullptr) { // We only run on one host
229       simgrid::s4u::Host* host = activity->host_;
230       if (dynamic_cast<simgrid::s4u::VirtualMachine*>(activity->host_))
231         host = dynamic_cast<simgrid::s4u::VirtualMachine*>(activity->host_)->get_pm();
232
233       host->extension<HostLoad>()->update();
234     }
235     else { // This runs on multiple hosts
236       XBT_DEBUG("HostLoad plugin currently does not support executions on several hosts");
237     }
238   });
239   simgrid::s4u::Host::on_state_change.connect(&on_host_change);
240   simgrid::s4u::Host::on_speed_change.connect(&on_host_change);
241 }
242
243 /** @brief Returns the current load of the host passed as argument
244  *
245  *  See also @ref plugin_load
246  */
247 double sg_host_get_current_load(sg_host_t host)
248 {
249   xbt_assert(HostLoad::EXTENSION_ID.valid(),
250              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
251
252   return host->extension<HostLoad>()->get_current_load();
253 }
254
255 /** @brief Returns the current load of the host passed as argument
256  *
257  *  See also @ref plugin_load
258  */
259 double sg_host_get_avg_load(sg_host_t host)
260 {
261   xbt_assert(HostLoad::EXTENSION_ID.valid(),
262              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
263
264   return host->extension<HostLoad>()->get_average_load();
265 }
266
267 /** @brief Returns the time this host was idle since the last reset
268  *
269  *  See also @ref plugin_load
270  */
271 double sg_host_get_idle_time(sg_host_t host)
272 {
273   xbt_assert(HostLoad::EXTENSION_ID.valid(),
274              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
275
276   return host->extension<HostLoad>()->get_idle_time();
277 }
278
279 double sg_host_get_total_idle_time(sg_host_t host)
280 {
281   xbt_assert(HostLoad::EXTENSION_ID.valid(),
282              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
283
284   return host->extension<HostLoad>()->get_total_idle_time();
285 }
286
287 double sg_host_get_computed_flops(sg_host_t host)
288 {
289   xbt_assert(HostLoad::EXTENSION_ID.valid(),
290              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
291
292   return host->extension<HostLoad>()->get_computed_flops();
293 }
294
295 void sg_host_load_reset(sg_host_t host)
296 {
297   xbt_assert(HostLoad::EXTENSION_ID.valid(),
298              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
299
300   host->extension<HostLoad>()->reset();
301 }