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/dvfs.h"
7 #include "simgrid/plugins/load.h"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10 #include "src/plugins/vm/VirtualMachineImpl.hpp"
11 #include "src/smpi/plugins/ampi/ampi.hpp"
12 #include <xbt/config.hpp>
14 #include <boost/algorithm/string.hpp>
16 SIMGRID_REGISTER_PLUGIN(host_dvfs, "Dvfs support", &sg_host_dvfs_plugin_init)
18 static simgrid::config::Flag<double> cfg_sampling_rate("plugin/dvfs/sampling-rate", {"plugin/dvfs/sampling_rate"},
19 "How often should the dvfs plugin check whether the frequency needs to be changed?", 0.1,
20 [](double val){if (val != 0.1) sg_host_dvfs_plugin_init();});
22 static simgrid::config::Flag<std::string> cfg_governor("plugin/dvfs/governor",
23 "Which Governor should be used that adapts the CPU frequency?", "performance",
25 std::map<std::string, std::string>({
26 {"adagio", "TODO: Doc"},
27 {"conservative", "TODO: Doc"},
28 {"ondemand", "TODO: Doc"},
29 {"performance", "TODO: Doc"},
30 {"powersave", "TODO: Doc"},
33 [](std::string val) { if (val != "performance") sg_host_dvfs_plugin_init(); });
35 /** @addtogroup SURF_plugin_load
37 This plugin makes it very simple for users to obtain the current load for each host.
41 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_dvfs, surf, "Logging specific to the SURF HostDvfs plugin");
49 * Add this to your host tag:
50 * - \<prop id="plugin/dvfs/governor" value="performance" /\>
52 * Valid values as of now are: performance, powersave, ondemand, conservative
53 * It doesn't matter if you use uppercase or lowercase.
55 * For the sampling rate, use this:
57 * - \<prop id="plugin/dvfs/sampling-rate" value="2" /\>
59 * This will run the update() method of the specified governor every 2 seconds
62 * These properties can also be used within the \<config\> tag to configure
63 * these values globally. Using them within the \<host\> will overwrite this
64 * global configuration
69 simgrid::s4u::Host* const host_;
70 double sampling_rate_;
74 explicit Governor(simgrid::s4u::Host* ptr) : host_(ptr) { init(); }
75 virtual ~Governor() = default;
76 virtual std::string get_name() const = 0;
77 simgrid::s4u::Host* get_host() const { return host_; }
81 const char* local_sampling_rate_config = host_->get_property(cfg_sampling_rate.get_name());
82 double global_sampling_rate_config = cfg_sampling_rate;
83 if (local_sampling_rate_config != nullptr) {
84 sampling_rate_ = std::stod(local_sampling_rate_config);
86 sampling_rate_ = global_sampling_rate_config;
90 virtual void update() = 0;
91 double get_sampling_rate() const { return sampling_rate_; }
95 * The linux kernel doc describes this governor as follows:
96 * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
98 * > The CPUfreq governor "performance" sets the CPU statically to the
99 * > highest frequency within the borders of scaling_min_freq and
100 * > scaling_max_freq.
102 * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
104 class Performance : public Governor {
106 explicit Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
107 std::string get_name() const override { return "Performance"; }
109 void update() override { get_host()->set_pstate(0); }
113 * The linux kernel doc describes this governor as follows:
114 * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
116 * > The CPUfreq governor "powersave" sets the CPU statically to the
117 * > lowest frequency within the borders of scaling_min_freq and
118 * > scaling_max_freq.
120 * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
122 class Powersave : public Governor {
124 explicit Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
125 std::string get_name() const override { return "Powersave"; }
127 void update() override { get_host()->set_pstate(get_host()->get_pstate_count() - 1); }
131 * The linux kernel doc describes this governor as follows:
132 * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
134 * > The CPUfreq governor "ondemand" sets the CPU frequency depending on the
135 * > current system load. [...] when triggered, cpufreq checks
136 * > the CPU-usage statistics over the last period and the governor sets the
139 class OnDemand : public Governor {
141 * See https://elixir.bootlin.com/linux/v4.15.4/source/drivers/cpufreq/cpufreq_ondemand.c
142 * DEF_FREQUENCY_UP_THRESHOLD and od_update()
144 double freq_up_threshold_ = 0.80;
147 explicit OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
148 std::string get_name() const override { return "OnDemand"; }
150 void update() override
152 double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
153 sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
155 if (load > freq_up_threshold_) {
156 get_host()->set_pstate(0); /* Run at max. performance! */
157 XBT_INFO("Load: %f > threshold: %f --> changed to pstate %i", load, freq_up_threshold_, 0);
159 /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
161 * freq_next = min_f + load * (max_f - min_f) / 100
163 * So they assume that frequency increases by 100 MHz. We will just use
164 * lowest_pstate - load*pstatesCount()
166 int max_pstate = get_host()->get_pstate_count() - 1;
167 // Load is now < freq_up_threshold; exclude pstate 0 (the fastest)
168 // because pstate 0 can only be selected if load > freq_up_threshold_
169 int new_pstate = max_pstate - load * (max_pstate + 1);
170 get_host()->set_pstate(new_pstate);
172 XBT_DEBUG("Load: %f < threshold: %f --> changed to pstate %i", load, freq_up_threshold_, new_pstate);
179 * This is the conservative governor, which is very similar to the
180 * OnDemand governor. The Linux Kernel Documentation describes it
181 * very well, see https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt:
183 * > The CPUfreq governor "conservative", much like the "ondemand"
184 * > governor, sets the CPU frequency depending on the current usage. It
185 * > differs in behaviour in that it gracefully increases and decreases the
186 * > CPU speed rather than jumping to max speed the moment there is any load
187 * > on the CPU. This behaviour is more suitable in a battery powered
190 class Conservative : public Governor {
191 double freq_up_threshold_ = .8;
192 double freq_down_threshold_ = .2;
195 explicit Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
196 virtual std::string get_name() const override { return "Conservative"; }
198 virtual void update() override
200 double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
201 int pstate = get_host()->get_pstate();
202 sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
204 if (load > freq_up_threshold_) {
206 get_host()->set_pstate(pstate - 1);
207 XBT_INFO("Load: %f > threshold: %f -> increasing performance to pstate %d", load, freq_up_threshold_,
210 XBT_DEBUG("Load: %f > threshold: %f -> but cannot speed up even more, already in highest pstate %d", load,
211 freq_up_threshold_, pstate);
213 } else if (load < freq_down_threshold_) {
214 int max_pstate = get_host()->get_pstate_count() - 1;
215 if (pstate != max_pstate) { // Are we in the slowest pstate already?
216 get_host()->set_pstate(pstate + 1);
217 XBT_INFO("Load: %f < threshold: %f -> slowing down to pstate %d", load, freq_down_threshold_, pstate + 1);
219 XBT_DEBUG("Load: %f < threshold: %f -> cannot slow down even more, already in slowest pstate %d", load,
220 freq_down_threshold_, pstate);
226 class Adagio : public Governor {
229 double start_time = 0;
230 double comp_counter = 0;
231 double comp_timer = 0;
233 std::vector<std::vector<double>> rates;
235 unsigned int task_id = 0;
236 bool iteration_running = false; /*< Are we currently between iteration_in and iteration_out calls? */
239 explicit Adagio(simgrid::s4u::Host* ptr)
240 : Governor(ptr), rates(100, std::vector<double>(host_->get_pstate_count(), 0.0))
242 simgrid::smpi::plugin::ampi::on_iteration_in.connect([this](simgrid::s4u::ActorPtr actor) {
243 // Every instance of this class subscribes to this event, so one per host
244 // This means that for any actor, all 'hosts' are normally notified of these
245 // changes, even those who don't currently run the actor 'proc_id'.
246 // -> Let's check if this signal call is for us!
247 if (get_host() == actor->get_host()) {
248 iteration_running = true;
251 simgrid::smpi::plugin::ampi::on_iteration_out.connect([this](simgrid::s4u::ActorPtr actor) {
252 if (get_host() == actor->get_host()) {
253 iteration_running = false;
257 simgrid::kernel::activity::ExecImpl::on_creation.connect([this](simgrid::kernel::activity::ExecImplPtr activity) {
258 if (activity->host_ == get_host())
261 simgrid::kernel::activity::ExecImpl::on_completion.connect([this](simgrid::kernel::activity::ExecImplPtr activity) {
262 // For more than one host (not yet supported), we can access the host via
263 // simcalls_.front()->issuer->iface()->get_host()
264 if (activity->host_ == get_host() && iteration_running) {
265 comp_timer += activity->surf_action_->get_finish_time() - activity->surf_action_->get_start_time();
268 simgrid::s4u::Link::on_communicate.connect(
269 [this](kernel::resource::NetworkAction* action, s4u::Host* src, s4u::Host* dst) {
270 if ((get_host() == src || get_host() == dst) && iteration_running) {
276 virtual std::string get_name() const override { return "Adagio"; }
280 sg_host_load_reset(host_);
281 comp_counter = sg_host_get_computed_flops(host_); // Should be 0 because of the reset
283 start_time = simgrid::s4u::Engine::get_clock();
284 if (rates.size() <= task_id)
285 rates.resize(task_id + 5, std::vector<double>(host_->get_pstate_count(), 0.0));
286 if (rates[task_id][best_pstate] == 0)
288 host_->set_pstate(best_pstate); // Load our schedule
289 XBT_DEBUG("Set pstate to %i", best_pstate);
294 double computed_flops = sg_host_get_computed_flops(host_) - comp_counter;
295 double target_time = (simgrid::s4u::Engine::get_clock() - start_time);
298 static_cast<double>(99.0 / 100.0); // FIXME We account for t_copy arbitrarily with 1% -- this needs to be fixed
300 bool is_initialized = rates[task_id][best_pstate] != 0;
301 rates[task_id][best_pstate] = computed_flops / comp_timer;
302 if (not is_initialized) {
303 for (int i = 1; i < host_->get_pstate_count(); i++) {
304 rates[task_id][i] = rates[task_id][0] * (host_->get_pstate_speed(i) / host_->get_speed());
306 is_initialized = true;
309 for (int pstate = host_->get_pstate_count() - 1; pstate >= 0; pstate--) {
310 if (computed_flops / rates[task_id][pstate] <= target_time) {
311 // We just found the pstate we want to use!
312 best_pstate = pstate;
319 virtual void update() override {}
322 } // namespace plugin
323 } // namespace simgrid
325 /* **************************** events callback *************************** */
326 static void on_host_added(simgrid::s4u::Host& host)
328 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
331 std::string name = std::string("dvfs-daemon-") + host.get_cname();
332 simgrid::s4u::ActorPtr daemon = simgrid::s4u::Actor::create(name.c_str(), &host, []() {
334 * This lambda function is the function the actor (daemon) will execute
335 * all the time - in the case of the dvfs plugin, this controls when to
336 * lower/raise the frequency.
338 simgrid::s4u::ActorPtr daemon_proc = simgrid::s4u::Actor::self();
340 XBT_DEBUG("DVFS process on %s is a daemon: %d", daemon_proc->get_host()->get_cname(), daemon_proc->is_daemon());
342 std::string dvfs_governor;
343 const char* host_conf = daemon_proc->get_host()->get_property("plugin/dvfs/governor");
344 if (host_conf != nullptr) {
345 dvfs_governor = std::string(host_conf);
346 boost::algorithm::to_lower(dvfs_governor);
348 dvfs_governor = cfg_governor;
349 boost::algorithm::to_lower(dvfs_governor);
352 auto governor = [&dvfs_governor, &daemon_proc]() {
353 if (dvfs_governor == "conservative") {
354 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
355 new simgrid::plugin::dvfs::Conservative(daemon_proc->get_host()));
356 } else if (dvfs_governor == "ondemand") {
357 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
358 new simgrid::plugin::dvfs::OnDemand(daemon_proc->get_host()));
359 } else if (dvfs_governor == "adagio") {
360 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
361 new simgrid::plugin::dvfs::Adagio(daemon_proc->get_host()));
362 } else if (dvfs_governor == "performance") {
363 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
364 new simgrid::plugin::dvfs::Performance(daemon_proc->get_host()));
365 } else if (dvfs_governor == "powersave") {
366 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
367 new simgrid::plugin::dvfs::Powersave(daemon_proc->get_host()));
369 XBT_CRITICAL("No governor specified for host %s, falling back to Performance",
370 daemon_proc->get_host()->get_cname());
371 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
372 new simgrid::plugin::dvfs::Performance(daemon_proc->get_host()));
377 // Sleep *before* updating; important for startup (i.e., t = 0).
378 // In the beginning, we want to go with the pstates specified in the platform file
379 // (so we sleep first)
380 simgrid::s4u::this_actor::sleep_for(governor->get_sampling_rate());
382 XBT_DEBUG("Governor (%s) just updated!", governor->get_name().c_str());
385 XBT_WARN("I should have never reached this point: daemons should be killed when all regular processes are done");
389 // This call must be placed in this function. Otherwise, the daemonize() call comes too late and
390 // SMPI will take this process as an MPI process!
394 /* **************************** Public interface *************************** */
396 /** @ingroup SURF_plugin_load
397 * @brief Initializes the HostDvfs plugin
398 * @details The HostDvfs plugin provides an API to get the current load of each host.
400 void sg_host_dvfs_plugin_init()
402 static bool inited = false;
407 sg_host_load_plugin_init();
409 simgrid::s4u::Host::on_creation.connect(&on_host_added);