1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved. */
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. */
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>
11 SIMGRID_REGISTER_PLUGIN(host_load, "Cpu load", &sg_host_load_plugin_init)
13 /** @addtogroup plugin_load
15 This plugin makes it very simple for users to obtain the current load for each host.
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_load, surf, "Logging specific to the HostLoad plugin");
26 static simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> EXTENSION_ID;
28 explicit HostLoad(simgrid::s4u::Host* ptr)
30 , last_updated_(surf_get_clock())
31 , last_reset_(surf_get_clock())
32 , current_speed_(host_->get_speed())
33 , current_flops_(host_->pimpl_cpu->get_constraint()->get_usage())
35 , was_prev_idle_(current_flops_ == 0)
38 ~HostLoad() = default;
40 explicit HostLoad(simgrid::s4u::Host& ptr) = delete;
41 explicit HostLoad(simgrid::s4u::Host&& ptr) = delete;
43 double get_current_load();
44 double get_average_load() { return (theor_max_flops_ == 0) ? 0 : computed_flops_ / theor_max_flops_; };
45 double get_computed_flops() { return computed_flops_; }
46 double get_idle_time() { return idle_time_; } /** Return idle time since last reset */
47 double get_total_idle_time() { return total_idle_time_; } /** Return idle time over the whole simulation */
52 simgrid::s4u::Host* host_ = nullptr;
53 double last_updated_ = 0;
54 double last_reset_ = 0;
56 * current_speed each core is running at right now
58 double current_speed_ = 0;
60 * How many flops are currently used by all the processes running on this
63 double current_flops_ = 0;
64 double computed_flops_ = 0;
65 double idle_time_ = 0;
66 double total_idle_time_ = 0; /* This gets never reset */
67 double theor_max_flops_ = 0;
68 bool was_prev_idle_ = true; /* A host is idle at the beginning */
71 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
73 void HostLoad::update()
75 double now = surf_get_clock();
77 /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k @in {0, 1, ..., cores}
78 * number of active cores */
79 current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
81 /* flops == pstate_speed * cores_being_currently_used */
82 computed_flops_ += (now - last_updated_) * current_flops_;
85 idle_time_ += (now - last_updated_);
86 total_idle_time_ += (now - last_updated_);
89 theor_max_flops_ += current_speed_ * host_->get_core_count() * (now - last_updated_);
90 current_speed_ = host_->get_speed();
92 was_prev_idle_ = (current_flops_ == 0);
96 * WARNING: This function does not guarantee that you have the real load at any time imagine all actions on your CPU
97 * terminate at time t. Your load is then 0. Then you query the load (still 0) and then another action starts (still at
98 * time t!). This means that the load was never really 0 (because the time didn't advance) but it will still be reported
101 * So, use at your own risk.
103 double HostLoad::get_current_load()
105 // We don't need to call update() here because it is called every time an action terminates or starts
106 // FIXME: Can this happen at the same time? stop -> call to getCurrentLoad, load = 0 -> next action starts?
107 return current_flops_ / static_cast<double>(host_->get_speed() * host_->get_core_count());
111 * Resets the counters
113 void HostLoad::reset()
115 last_updated_ = surf_get_clock();
116 last_reset_ = surf_get_clock();
119 theor_max_flops_ = 0;
120 current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
121 current_speed_ = host_->get_speed();
122 was_prev_idle_ = (current_flops_ == 0);
124 } // namespace plugin
125 } // namespace simgrid
127 using simgrid::plugin::HostLoad;
129 /* **************************** events callback *************************** */
130 /* This callback is fired either when the host changes its state (on/off) or its speed
131 * (because the user changed the pstate, or because of external trace events) */
132 static void on_host_change(simgrid::s4u::Host& host)
134 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
137 host.extension<HostLoad>()->update();
140 /* This callback is called when an action (computation, idle, ...) terminates */
141 static void on_action_state_change(simgrid::surf::CpuAction* action, simgrid::kernel::resource::Action::State /*previous*/)
143 for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
144 simgrid::s4u::Host* host = cpu->get_host();
146 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
149 if (host != nullptr) {
150 host->extension<HostLoad>()->update();
155 /* **************************** Public interface *************************** */
157 /** @ingroup plugin_load
158 * @brief Initializes the HostLoad plugin
159 * @details The HostLoad plugin provides an API to get the current load of each host.
161 void sg_host_load_plugin_init()
163 if (HostLoad::EXTENSION_ID.valid())
166 HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
168 if (simgrid::s4u::Engine::is_initialized()) { // If not yet initialized, this would create a new instance
169 // which would cause seg faults...
170 simgrid::s4u::Engine* e = simgrid::s4u::Engine::get_instance();
171 for (auto& host : e->get_all_hosts()) {
172 host->extension_set(new HostLoad(host));
176 /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
178 simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
179 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
181 host.extension_set(new HostLoad(&host));
184 simgrid::surf::CpuAction::on_state_change.connect(&on_action_state_change);
185 simgrid::s4u::Host::on_state_change.connect(&on_host_change);
186 simgrid::s4u::Host::on_speed_change.connect(&on_host_change);
189 /** @brief Returns the current load of the host passed as argument
191 * See also @ref plugin_load
193 double sg_host_get_current_load(sg_host_t host)
195 xbt_assert(HostLoad::EXTENSION_ID.valid(),
196 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
198 return host->extension<HostLoad>()->get_current_load();
201 /** @brief Returns the current load of the host passed as argument
203 * See also @ref plugin_load
205 double sg_host_get_avg_load(sg_host_t host)
207 xbt_assert(HostLoad::EXTENSION_ID.valid(),
208 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
210 return host->extension<HostLoad>()->get_average_load();
213 /** @brief Returns the time this host was idle since the last reset
215 * See also @ref plugin_load
217 double sg_host_get_idle_time(sg_host_t host)
219 xbt_assert(HostLoad::EXTENSION_ID.valid(),
220 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
222 return host->extension<HostLoad>()->get_idle_time();
225 double sg_host_get_total_idle_time(sg_host_t host)
227 xbt_assert(HostLoad::EXTENSION_ID.valid(),
228 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
230 return host->extension<HostLoad>()->get_total_idle_time();
233 double sg_host_get_computed_flops(sg_host_t host)
235 xbt_assert(HostLoad::EXTENSION_ID.valid(),
236 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
238 return host->extension<HostLoad>()->get_computed_flops();
241 void sg_host_load_reset(sg_host_t host)
243 xbt_assert(HostLoad::EXTENSION_ID.valid(),
244 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
246 host->extension<HostLoad>()->reset();