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);
27 double getCurrentLoad();
28 double getComputedFlops();
29 double getAverageLoad();
35 simgrid::s4u::Host* host = nullptr;
36 double last_updated = 0;
37 double last_reset = 0;
38 double current_speed = 0;
39 double current_flops = 0;
40 double computed_flops = 0;
42 double theor_max_flops = 0;
43 bool was_prev_idle = true; /* A host is idle at the beginning */
46 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
48 HostLoad::HostLoad(simgrid::s4u::Host* ptr)
50 , last_updated(surf_get_clock())
51 , last_reset(surf_get_clock())
52 , current_speed(host->getSpeed())
53 , current_flops(host->pimpl_cpu->get_constraint()->get_usage())
55 , was_prev_idle(current_flops == 0)
59 HostLoad::~HostLoad() = default;
61 void HostLoad::update()
63 double now = surf_get_clock();
65 /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k \in {0, 1, ..., cores}
66 * number of active cores */
67 current_flops = host->pimpl_cpu->get_constraint()->get_usage();
69 /* flops == pstate_speed * cores_being_currently_used */
70 computed_flops += (now - last_updated) * current_flops;
73 idle_time += (now - last_updated);
76 theor_max_flops += current_speed * host->get_core_count() * (now - last_updated);
77 current_speed = host->getSpeed();
79 was_prev_idle = (current_flops == 0);
83 * WARNING: This function does not guarantee that you have the real load at any time
84 * imagine all actions on your CPU terminate at time t. Your load is then 0. Then
85 * you query the load (still 0) and then another action starts (still at time t!).
86 * This means that the load was never really 0 (because the time didn't advance) but
87 * it will still be reported as 0.
89 * So, use at your own risk.
91 double HostLoad::getCurrentLoad()
93 // We don't need to call update() here because it is called everytime an
94 // action terminates or starts
95 // FIXME: Can this happen at the same time? stop -> call to getCurrentLoad, load = 0 -> next action starts?
96 return current_flops / static_cast<double>(host->getSpeed() * host->get_core_count());
100 * Return idle time since last reset
102 double HostLoad::getIdleTime()
107 double HostLoad::getAverageLoad()
109 if (theor_max_flops == 0) { // Avoid division by 0
113 return computed_flops / theor_max_flops;
116 double HostLoad::getComputedFlops()
118 return computed_flops;
122 * Resets the counters
124 void HostLoad::reset()
126 last_updated = surf_get_clock();
127 last_reset = surf_get_clock();
131 current_flops = host->pimpl_cpu->get_constraint()->get_usage();
132 current_speed = host->getSpeed();
133 was_prev_idle = (current_flops == 0);
135 } // namespace plugin
136 } // namespace simgrid
138 using simgrid::plugin::HostLoad;
140 /* **************************** events callback *************************** */
141 /* This callback is fired either when the host changes its state (on/off) or its speed
142 * (because the user changed the pstate, or because of external trace events) */
143 static void onHostChange(simgrid::s4u::Host& host)
145 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
148 host.extension<HostLoad>()->update();
151 /* This callback is called when an action (computation, idle, ...) terminates */
152 static void onActionStateChange(simgrid::surf::CpuAction* action)
154 for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
155 simgrid::s4u::Host* host = cpu->get_host();
157 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
160 if (host != nullptr) {
161 host->extension<HostLoad>()->update();
166 /* **************************** Public interface *************************** */
168 /** \ingroup plugin_load
169 * \brief Initializes the HostLoad plugin
170 * \details The HostLoad plugin provides an API to get the current load of each host.
172 void sg_host_load_plugin_init()
174 if (HostLoad::EXTENSION_ID.valid())
177 HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
179 /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
181 simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& host) {
182 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
184 host.extension_set(new HostLoad(&host));
187 simgrid::surf::CpuAction::on_state_change.connect(&onActionStateChange);
188 simgrid::s4u::Host::on_state_change.connect(&onHostChange);
189 simgrid::s4u::Host::on_speed_change.connect(&onHostChange);
192 /** @brief Returns the current load of the host passed as argument
194 * See also @ref plugin_load
196 double sg_host_get_current_load(sg_host_t host)
198 xbt_assert(HostLoad::EXTENSION_ID.valid(),
199 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
201 return host->extension<HostLoad>()->getCurrentLoad();
204 /** @brief Returns the current load of the host passed as argument
206 * See also @ref plugin_load
208 double sg_host_get_avg_load(sg_host_t host)
210 xbt_assert(HostLoad::EXTENSION_ID.valid(),
211 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
213 return host->extension<HostLoad>()->getAverageLoad();
216 /** @brief Returns the time this host was idle since the last reset
218 * See also @ref plugin_load
220 double sg_host_get_idle_time(sg_host_t host)
222 xbt_assert(HostLoad::EXTENSION_ID.valid(),
223 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
225 return host->extension<HostLoad>()->getIdleTime();
228 double sg_host_get_computed_flops(sg_host_t host)
230 xbt_assert(HostLoad::EXTENSION_ID.valid(),
231 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
233 return host->extension<HostLoad>()->getComputedFlops();
236 void sg_host_load_reset(sg_host_t host)
238 xbt_assert(HostLoad::EXTENSION_ID.valid(),
239 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
241 host->extension<HostLoad>()->reset();