Logo AND Algorithmique Numérique Distribuée

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