Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[HostLoad] Add method HostLoad::add_activity(ExecImplPtr)
[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   /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k @in {0, 1, ..., cores-1}
88    * designates number of active cores; will be 0 if CPU is currently idle */
89   current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
90
91   /* flops == pstate_speed * cores_being_currently_used */
92   computed_flops_ += (now - last_updated_) * current_flops_;
93
94   if (current_flops_ == 0) {
95     idle_time_ += (now - last_updated_);
96     total_idle_time_ += (now - last_updated_);
97     XBT_DEBUG("[%s]: Currently idle -> Added %f seconds to idle time (totaling %fs)", host_->get_cname(), (now - last_updated_), idle_time_);
98   }
99
100   theor_max_flops_ += current_speed_ * host_->get_core_count() * (now - last_updated_);
101   current_speed_ = host_->get_speed();
102   last_updated_  = now;
103 }
104
105 /**
106  * WARNING: This function does not guarantee that you have the real load at any time imagine all actions on your CPU
107  * terminate at time t. Your load is then 0. Then you query the load (still 0) and then another action starts (still at
108  * time t!). This means that the load was never really 0 (because the time didn't advance) but it will still be reported
109  * as 0.
110  *
111  * So, use at your own risk.
112  */
113 double HostLoad::get_current_load()
114 {
115   // We don't need to call update() here because it is called every time an action terminates or starts
116   // FIXME: Can this happen at the same time? stop -> call to getCurrentLoad, load = 0 -> next action starts?
117   return current_flops_ / static_cast<double>(host_->get_speed() * host_->get_core_count());
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_->get_speed();
132 }
133 } // namespace plugin
134 } // namespace simgrid
135
136 using simgrid::plugin::HostLoad;
137
138 /* **************************** events  callback *************************** */
139 /* This callback is fired either when the host changes its state (on/off) or its speed
140  * (because the user changed the pstate, or because of external trace events) */
141 static void on_host_change(simgrid::s4u::Host& host)
142 {
143   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
144     return;
145
146   host.extension<HostLoad>()->update();
147 }
148
149 /* This callback is called when an action (computation, idle, ...) terminates */
150 static void on_action_state_change(simgrid::surf::CpuAction* action, simgrid::kernel::resource::Action::State /*previous*/)
151 {
152   for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
153     simgrid::s4u::Host* host = cpu->get_host();
154
155     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
156       return;
157
158     if (host != nullptr) {
159       host->extension<HostLoad>()->update();
160     }
161   }
162 }
163
164 /* **************************** Public interface *************************** */
165
166 /** @ingroup plugin_load
167  * @brief Initializes the HostLoad plugin
168  * @details The HostLoad plugin provides an API to get the current load of each host.
169  */
170 void sg_host_load_plugin_init()
171 {
172   if (HostLoad::EXTENSION_ID.valid())
173     return;
174
175   HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
176
177   if (simgrid::s4u::Engine::is_initialized()) { // If not yet initialized, this would create a new instance
178                                                 // which would cause seg faults...
179     simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
180     for (auto& host : e->get_all_hosts()) {
181       host->extension_set(new HostLoad(host));
182     }
183   }
184
185   /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
186
187   simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
188     if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
189       return;
190     host.extension_set(new HostLoad(&host));
191   });
192
193   simgrid::surf::CpuAction::on_state_change.connect(&on_action_state_change);
194   simgrid::s4u::Host::on_state_change.connect(&on_host_change);
195   simgrid::s4u::Host::on_speed_change.connect(&on_host_change);
196 }
197
198 /** @brief Returns the current load of the host passed as argument
199  *
200  *  See also @ref plugin_load
201  */
202 double sg_host_get_current_load(sg_host_t host)
203 {
204   xbt_assert(HostLoad::EXTENSION_ID.valid(),
205              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
206
207   return host->extension<HostLoad>()->get_current_load();
208 }
209
210 /** @brief Returns the current load of the host passed as argument
211  *
212  *  See also @ref plugin_load
213  */
214 double sg_host_get_avg_load(sg_host_t host)
215 {
216   xbt_assert(HostLoad::EXTENSION_ID.valid(),
217              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
218
219   return host->extension<HostLoad>()->get_average_load();
220 }
221
222 /** @brief Returns the time this host was idle since the last reset
223  *
224  *  See also @ref plugin_load
225  */
226 double sg_host_get_idle_time(sg_host_t host)
227 {
228   xbt_assert(HostLoad::EXTENSION_ID.valid(),
229              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
230
231   return host->extension<HostLoad>()->get_idle_time();
232 }
233
234 double sg_host_get_total_idle_time(sg_host_t host)
235 {
236   xbt_assert(HostLoad::EXTENSION_ID.valid(),
237              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
238
239   return host->extension<HostLoad>()->get_total_idle_time();
240 }
241
242 double sg_host_get_computed_flops(sg_host_t host)
243 {
244   xbt_assert(HostLoad::EXTENSION_ID.valid(),
245              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
246
247   return host->extension<HostLoad>()->get_computed_flops();
248 }
249
250 void sg_host_load_reset(sg_host_t host)
251 {
252   xbt_assert(HostLoad::EXTENSION_ID.valid(),
253              "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
254
255   host->extension<HostLoad>()->reset();
256 }