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/plugins/vm/VirtualMachineImpl.hpp"
9 /** @addtogroup plugin_load
11 This plugin makes it very simple for users to obtain the current load for each host.
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_load, surf, "Logging specific to the HostLoad plugin");
22 static simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> EXTENSION_ID;
24 explicit HostLoad(simgrid::s4u::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())
31 , was_prev_idle_(current_flops_ == 0)
34 ~HostLoad() = default;
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 */
44 simgrid::s4u::Host* host_ = nullptr;
45 double last_updated_ = 0;
46 double last_reset_ = 0;
48 * current_speed each core is running at right now
50 double current_speed_ = 0;
52 * How many flops are currently used by all the processes running on this
55 double current_flops_ = 0;
56 double computed_flops_ = 0;
57 double idle_time_ = 0;
58 double theor_max_flops_ = 0;
59 bool was_prev_idle_ = true; /* A host is idle at the beginning */
62 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
64 void HostLoad::update()
66 double now = surf_get_clock();
68 /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k \in {0, 1, ..., cores}
69 * number of active cores */
70 current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
72 /* flops == pstate_speed * cores_being_currently_used */
73 computed_flops_ += (now - last_updated_) * current_flops_;
76 idle_time_ += (now - last_updated_);
79 theor_max_flops_ += current_speed_ * host_->get_core_count() * (now - last_updated_);
80 current_speed_ = host_->getSpeed();
82 was_prev_idle_ = (current_flops_ == 0);
86 * WARNING: This function does not guarantee that you have the real load at any time imagine all actions on your CPU
87 * terminate at time t. Your load is then 0. Then you query the load (still 0) and then another action starts (still at
88 * time t!). This means that the load was never really 0 (because the time didn't advance) but it will still be reported
91 * So, use at your own risk.
93 double HostLoad::get_current_load()
95 // We don't need to call update() here because it is called every time an action terminates or starts
96 // FIXME: Can this happen at the same time? stop -> call to getCurrentLoad, load = 0 -> next action starts?
97 return current_flops_ / static_cast<double>(host_->getSpeed() * host_->get_core_count());
101 * Resets the counters
103 void HostLoad::reset()
105 last_updated_ = surf_get_clock();
106 last_reset_ = surf_get_clock();
109 theor_max_flops_ = 0;
110 current_flops_ = host_->pimpl_cpu->get_constraint()->get_usage();
111 current_speed_ = host_->getSpeed();
112 was_prev_idle_ = (current_flops_ == 0);
114 } // namespace plugin
115 } // namespace simgrid
117 using simgrid::plugin::HostLoad;
119 /* **************************** events callback *************************** */
120 /* This callback is fired either when the host changes its state (on/off) or its speed
121 * (because the user changed the pstate, or because of external trace events) */
122 static void on_host_change(simgrid::s4u::Host& host)
124 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
127 host.extension<HostLoad>()->update();
130 /* This callback is called when an action (computation, idle, ...) terminates */
131 static void on_action_state_change(simgrid::surf::CpuAction* action)
133 for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
134 simgrid::s4u::Host* host = cpu->get_host();
136 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
139 if (host != nullptr) {
140 host->extension<HostLoad>()->update();
145 /* **************************** Public interface *************************** */
147 /** \ingroup plugin_load
148 * \brief Initializes the HostLoad plugin
149 * \details The HostLoad plugin provides an API to get the current load of each host.
151 void sg_host_load_plugin_init()
153 if (HostLoad::EXTENSION_ID.valid())
156 HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
158 /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
160 simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
161 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
163 host.extension_set(new HostLoad(&host));
166 simgrid::surf::CpuAction::on_state_change.connect(&on_action_state_change);
167 simgrid::s4u::Host::on_state_change.connect(&on_host_change);
168 simgrid::s4u::Host::on_speed_change.connect(&on_host_change);
171 /** @brief Returns the current load of the host passed as argument
173 * See also @ref plugin_load
175 double sg_host_get_current_load(sg_host_t host)
177 xbt_assert(HostLoad::EXTENSION_ID.valid(),
178 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
180 return host->extension<HostLoad>()->get_current_load();
183 /** @brief Returns the current load of the host passed as argument
185 * See also @ref plugin_load
187 double sg_host_get_avg_load(sg_host_t host)
189 xbt_assert(HostLoad::EXTENSION_ID.valid(),
190 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
192 return host->extension<HostLoad>()->get_average_load();
195 /** @brief Returns the time this host was idle since the last reset
197 * See also @ref plugin_load
199 double sg_host_get_idle_time(sg_host_t host)
201 xbt_assert(HostLoad::EXTENSION_ID.valid(),
202 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
204 return host->extension<HostLoad>()->get_idle_time();
207 double sg_host_get_computed_flops(sg_host_t host)
209 xbt_assert(HostLoad::EXTENSION_ID.valid(),
210 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
212 return host->extension<HostLoad>()->get_computed_flops();
215 void sg_host_load_reset(sg_host_t host)
217 xbt_assert(HostLoad::EXTENSION_ID.valid(),
218 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
220 host->extension<HostLoad>()->reset();