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 "simgrid/simix.hpp"
8 #include "src/plugins/vm/VirtualMachineImpl.hpp"
9 #include "src/surf/cpu_interface.hpp"
11 #include "simgrid/s4u/Engine.hpp"
13 #include <boost/algorithm/string/classification.hpp>
14 #include <boost/algorithm/string/split.hpp>
19 /** @addtogroup plugin_load
21 This plugin makes it very simple for users to obtain the current load for each host.
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_load, surf, "Logging specific to the HostLoad plugin");
32 static simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> EXTENSION_ID;
34 explicit HostLoad(simgrid::s4u::Host* ptr);
37 double getCurrentLoad();
38 double getComputedFlops();
39 double getAverageLoad();
45 simgrid::s4u::Host* host = nullptr;
46 double last_updated = 0;
47 double last_reset = 0;
48 double current_speed = 0;
49 double current_flops = 0;
50 double computed_flops = 0;
52 double theor_max_flops = 0;
53 bool was_prev_idle = true; /* A host is idle at the beginning */
56 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
58 HostLoad::HostLoad(simgrid::s4u::Host* ptr)
60 , last_updated(surf_get_clock())
61 , last_reset(surf_get_clock())
62 , current_speed(host->getSpeed())
63 , current_flops(host->pimpl_cpu->get_constraint()->get_usage())
65 , was_prev_idle(current_flops == 0)
69 HostLoad::~HostLoad() = default;
71 void HostLoad::update()
73 double now = surf_get_clock();
75 /* Current flop per second computed by the cpu; current_flops = k * pstate_speed_in_flops, k \in {0, 1, ..., cores}
76 * number of active cores */
77 current_flops = host->pimpl_cpu->get_constraint()->get_usage();
79 /* flops == pstate_speed * cores_being_currently_used */
80 computed_flops += (now - last_updated) * current_flops;
83 idle_time += (now - last_updated);
86 theor_max_flops += current_speed * host->getCoreCount() * (now - last_updated);
87 current_speed = host->getSpeed();
89 was_prev_idle = (current_flops == 0);
93 * WARNING: This function does not guarantee that you have the real load at any time
94 * imagine all actions on your CPU terminate at time t. Your load is then 0. Then
95 * you query the load (still 0) and then another action starts (still at time t!).
96 * This means that the load was never really 0 (because the time didn't advance) but
97 * it will still be reported as 0.
99 * So, use at your own risk.
101 double HostLoad::getCurrentLoad()
103 // We don't need to call update() here because it is called everytime an
104 // action terminates or starts
105 // FIXME: Can this happen at the same time? stop -> call to getCurrentLoad, load = 0 -> next action starts?
106 return current_flops / static_cast<double>(host->getSpeed() * host->getCoreCount());
110 * Return idle time since last reset
112 double HostLoad::getIdleTime() {
116 double HostLoad::getAverageLoad()
118 if (theor_max_flops == 0) { // Avoid division by 0
122 return computed_flops / theor_max_flops;
125 double HostLoad::getComputedFlops()
127 return computed_flops;
131 * Resets the counters
133 void HostLoad::reset()
135 last_updated = surf_get_clock();
136 last_reset = surf_get_clock();
140 current_flops = host->pimpl_cpu->get_constraint()->get_usage();
141 current_speed = host->getSpeed();
142 was_prev_idle = (current_flops == 0);
147 using simgrid::plugin::HostLoad;
149 /* **************************** events callback *************************** */
150 /* This callback is fired either when the host changes its state (on/off) or its speed
151 * (because the user changed the pstate, or because of external trace events) */
152 static void onHostChange(simgrid::s4u::Host& host)
154 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
157 host.extension<HostLoad>()->update();
160 /* This callback is called when an action (computation, idle, ...) terminates */
161 static void onActionStateChange(simgrid::surf::CpuAction* action, simgrid::kernel::resource::Action::State /*previous*/)
163 for (simgrid::surf::Cpu* const& cpu : action->cpus()) {
164 simgrid::s4u::Host* host = cpu->getHost();
166 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(host)) // Ignore virtual machines
169 if (host != nullptr) {
170 host->extension<HostLoad>()->update();
175 /* **************************** Public interface *************************** */
177 /** \ingroup plugin_load
178 * \brief Initializes the HostLoad plugin
179 * \details The HostLoad plugin provides an API to get the current load of each host.
181 void sg_host_load_plugin_init()
183 if (HostLoad::EXTENSION_ID.valid())
186 HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
188 /* When attaching a callback into a signal, you can use a lambda as follows, or a regular function as done below */
190 simgrid::s4u::Host::onCreation.connect([](simgrid::s4u::Host& host) {
191 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
193 host.extension_set(new HostLoad(&host));
196 simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
197 simgrid::s4u::Host::onStateChange.connect(&onHostChange);
198 simgrid::s4u::Host::onSpeedChange.connect(&onHostChange);
201 /** @brief Returns the current load of the host passed as argument
203 * See also @ref plugin_load
205 double sg_host_get_current_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>()->getCurrentLoad();
213 /** @brief Returns the current load of the host passed as argument
215 * See also @ref plugin_load
217 double sg_host_get_avg_load(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>()->getAverageLoad();
225 /** @brief Returns the time this host was idle since the last reset
227 * See also @ref plugin_load
229 double sg_host_get_idle_time(sg_host_t host)
231 xbt_assert(HostLoad::EXTENSION_ID.valid(),
232 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
234 return host->extension<HostLoad>()->getIdleTime();
237 double sg_host_get_computed_flops(sg_host_t host)
239 xbt_assert(HostLoad::EXTENSION_ID.valid(),
240 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
242 return host->extension<HostLoad>()->getComputedFlops();
245 void sg_host_load_reset(sg_host_t host)
247 xbt_assert(HostLoad::EXTENSION_ID.valid(),
248 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
250 host->extension<HostLoad>()->reset();