1 /* Copyright (c) 2010, 2012-2016. 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 <boost/algorithm/string/classification.hpp>
12 #include <boost/algorithm/string/split.hpp>
13 #include <simgrid/s4u/engine.hpp>
18 /** @addtogroup SURF_plugin_load
20 This plugin makes it very simple for users to obtain the current load for each host.
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_load, surf, "Logging specific to the SURF HostLoad plugin");
31 static simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> EXTENSION_ID;
33 explicit HostLoad(simgrid::s4u::Host* ptr);
36 double getCurrentLoad();
37 double getComputedFlops();
42 simgrid::s4u::Host* host = nullptr;
43 double last_updated = 0;
44 double current_flops = 0;
45 double computed_flops = 0;
48 simgrid::xbt::Extension<simgrid::s4u::Host, HostLoad> HostLoad::EXTENSION_ID;
50 HostLoad::HostLoad(simgrid::s4u::Host* ptr)
51 : host(ptr), last_updated(surf_get_clock()), current_flops(lmm_constraint_get_usage(host->pimpl_cpu->constraint()))
55 HostLoad::~HostLoad() = default;
57 void HostLoad::update()
59 double now = surf_get_clock();
60 if (last_updated < now) {
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 = lmm_constraint_get_usage(host->pimpl_cpu->constraint());
65 /* flops == pstate_speed * cores_being_currently_used */
66 computed_flops += (now - last_updated) * current_flops;
71 double HostLoad::getCurrentLoad()
73 return current_flops / (host->speed() * host->coreCount());
76 double HostLoad::getComputedFlops()
80 return computed_flops;
86 void HostLoad::reset()
88 last_updated = surf_get_clock();
94 using simgrid::plugin::HostLoad;
96 /* **************************** events callback *************************** */
97 static void on_host_added(simgrid::s4u::Host& host)
99 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
101 host.extension_set(new HostLoad(&host));
104 /* This callback is fired either when the host changes its state (on/off) or its speed
105 * (because the user changed the pstate, or because of external trace events) */
106 static void onHostChange(simgrid::s4u::Host& host)
108 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
111 HostLoad* host_load = host.extension<HostLoad>();
115 /* This callback is called when an action (computation, idle, ...) terminates */
116 static void onActionStateChange(simgrid::surf::CpuAction* action, simgrid::surf::Action::State previous)
118 for (simgrid::surf::Cpu* cpu : action->cpus()) {
119 simgrid::s4u::Host* host = cpu->getHost();
124 // Get the host_load extension for the relevant host
125 HostLoad* host_load = host->extension<HostLoad>();
130 /* **************************** Public interface *************************** */
133 /** \ingroup SURF_plugin_load
134 * \brief Initializes the HostLoad plugin
135 * \details The HostLoad plugin provides an API to get the current load of each host.
137 void sg_host_load_plugin_init()
139 if (HostLoad::EXTENSION_ID.valid())
142 HostLoad::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostLoad>();
144 simgrid::s4u::Host::onCreation.connect(&on_host_added);
145 simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange);
146 simgrid::s4u::Host::onStateChange.connect(&onHostChange);
147 simgrid::s4u::Host::onSpeedChange.connect(&onHostChange);
150 /** @brief Returns the current load of the host passed as argument
152 * See also @ref SURF_plugin_load
154 double sg_host_get_current_load(sg_host_t host)
156 xbt_assert(HostLoad::EXTENSION_ID.valid(),
157 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
159 return host->extension<HostLoad>()->getCurrentLoad();
162 double sg_host_get_computed_flops(sg_host_t host)
164 xbt_assert(HostLoad::EXTENSION_ID.valid(),
165 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
167 return host->extension<HostLoad>()->getComputedFlops();
170 void sg_host_load_reset(sg_host_t host)
172 xbt_assert(HostLoad::EXTENSION_ID.valid(),
173 "The Load plugin is not active. Please call sg_host_load_plugin_init() during initialization.");
175 host->extension<HostLoad>()->reset();