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/internal_config.h" // HAVE_SMPI
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/plugins/vm/VirtualMachineImpl.hpp"
13 #include "src/smpi/plugins/ampi/ampi.hpp"
15 #include <xbt/config.hpp>
17 #include <boost/algorithm/string.hpp>
19 SIMGRID_REGISTER_PLUGIN(host_dvfs, "Dvfs support", &sg_host_dvfs_plugin_init)
21 static simgrid::config::Flag<double> cfg_sampling_rate("plugin/dvfs/sampling-rate", {"plugin/dvfs/sampling_rate"},
22 "How often should the dvfs plugin check whether the frequency needs to be changed?", 0.1,
23 [](double val){if (val != 0.1) sg_host_dvfs_plugin_init();});
25 static simgrid::config::Flag<std::string> cfg_governor("plugin/dvfs/governor",
26 "Which Governor should be used that adapts the CPU frequency?", "performance",
28 std::map<std::string, std::string>({
29 {"adagio", "TODO: Doc"},
30 {"conservative", "TODO: Doc"},
31 {"ondemand", "TODO: Doc"},
32 {"performance", "TODO: Doc"},
33 {"powersave", "TODO: Doc"},
36 [](std::string val) { if (val != "performance") sg_host_dvfs_plugin_init(); });
38 static simgrid::config::Flag<int> cfg_min_pstate("plugin/dvfs/min-pstate", {"plugin/dvfs/min_pstate"},
39 "Which pstate is the minimum (and hence fastest) pstate for this governor?", 0,
42 static const int max_pstate_not_limited = -1;
43 static simgrid::config::Flag<int> cfg_max_pstate("plugin/dvfs/max-pstate", {"plugin/dvfs/max_pstate"},
44 "Which pstate is the maximum (and hence slowest) pstate for this governor?", max_pstate_not_limited,
47 /** @addtogroup SURF_plugin_load
49 This plugin makes it very simple for users to obtain the current load for each host.
53 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_dvfs, surf, "Logging specific to the SURF HostDvfs plugin");
61 * Add this to your host tag:
62 * - \<prop id="plugin/dvfs/governor" value="performance" /\>
64 * Valid values as of now are: performance, powersave, ondemand, conservative
65 * It doesn't matter if you use uppercase or lowercase.
67 * For the sampling rate, use this:
69 * - \<prop id="plugin/dvfs/sampling-rate" value="2" /\>
71 * This will run the update() method of the specified governor every 2 seconds
74 * These properties can also be used within the \<config\> tag to configure
75 * these values globally. Using them within the \<host\> will overwrite this
76 * global configuration
81 simgrid::s4u::Host* const host_;
82 double sampling_rate_;
83 int min_pstate; //< Never use a pstate less than this one
84 int max_pstate; //< Never use a pstate larger than this one
87 explicit Governor(simgrid::s4u::Host* ptr)
89 , min_pstate(cfg_min_pstate)
90 , max_pstate(cfg_max_pstate == max_pstate_not_limited ? host_->get_pstate_count() - 1 : cfg_max_pstate)
94 virtual ~Governor() = default;
95 virtual std::string get_name() const = 0;
96 simgrid::s4u::Host* get_host() const { return host_; }
97 int get_min_pstate() const { return min_pstate; }
98 int get_max_pstate() const { return max_pstate; }
102 const char* local_sampling_rate_config = host_->get_property(cfg_sampling_rate.get_name());
103 if (local_sampling_rate_config != nullptr) {
104 sampling_rate_ = std::stod(local_sampling_rate_config);
106 sampling_rate_ = cfg_sampling_rate;
108 const char* local_min_pstate_config = host_->get_property(cfg_min_pstate.get_name());
109 if (local_min_pstate_config != nullptr) {
110 min_pstate = std::stoi(local_min_pstate_config);
113 const char* local_max_pstate_config = host_->get_property(cfg_max_pstate.get_name());
114 if (local_max_pstate_config != nullptr) {
115 max_pstate = std::stod(local_max_pstate_config);
117 xbt_assert(max_pstate <= host_->get_pstate_count() - 1, "Value for max_pstate too large!");
118 xbt_assert(min_pstate <= max_pstate, "min_pstate is larger than max_pstate!");
119 xbt_assert(0 <= min_pstate, "min_pstate is negative!");
122 virtual void update() = 0;
123 double get_sampling_rate() const { return sampling_rate_; }
127 * The linux kernel doc describes this governor as follows:
128 * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
130 * > The CPUfreq governor "performance" sets the CPU statically to the
131 * > highest frequency within the borders of scaling_min_freq and
132 * > scaling_max_freq.
134 * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
136 class Performance : public Governor {
138 explicit Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
139 std::string get_name() const override { return "Performance"; }
141 void update() override { get_host()->set_pstate(get_min_pstate()); }
145 * The linux kernel doc describes this governor as follows:
146 * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
148 * > The CPUfreq governor "powersave" sets the CPU statically to the
149 * > lowest frequency within the borders of scaling_min_freq and
150 * > scaling_max_freq.
152 * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
154 class Powersave : public Governor {
156 explicit Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
157 std::string get_name() const override { return "Powersave"; }
159 void update() override { get_host()->set_pstate(get_max_pstate()); }
163 * The linux kernel doc describes this governor as follows:
164 * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
166 * > The CPUfreq governor "ondemand" sets the CPU frequency depending on the
167 * > current system load. [...] when triggered, cpufreq checks
168 * > the CPU-usage statistics over the last period and the governor sets the
171 class OnDemand : public Governor {
173 * See https://elixir.bootlin.com/linux/v4.15.4/source/drivers/cpufreq/cpufreq_ondemand.c
174 * DEF_FREQUENCY_UP_THRESHOLD and od_update()
176 double freq_up_threshold_ = 0.80;
179 explicit OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
180 std::string get_name() const override { return "OnDemand"; }
182 void update() override
184 double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
185 sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
187 if (load > freq_up_threshold_) {
188 get_host()->set_pstate(get_min_pstate()); /* Run at max. performance! */
189 XBT_INFO("Load: %f > threshold: %f --> changed to pstate %i", load, freq_up_threshold_, get_min_pstate());
191 /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
193 * freq_next = min_f + load * (max_f - min_f) / 100
195 * So they assume that frequency increases by 100 MHz. We will just use
196 * lowest_pstate - load*pstatesCount()
198 // Load is now < freq_up_threshold; exclude pstate 0 (the fastest)
199 // because pstate 0 can only be selected if load > freq_up_threshold_
200 int new_pstate = get_max_pstate() - load * (get_max_pstate() + 1);
201 if (new_pstate < get_min_pstate())
202 new_pstate = get_min_pstate();
203 get_host()->set_pstate(new_pstate);
205 XBT_DEBUG("Load: %f < threshold: %f --> changed to pstate %i", load, freq_up_threshold_, new_pstate);
211 * This is the conservative governor, which is very similar to the
212 * OnDemand governor. The Linux Kernel Documentation describes it
213 * very well, see https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt:
215 * > The CPUfreq governor "conservative", much like the "ondemand"
216 * > governor, sets the CPU frequency depending on the current usage. It
217 * > differs in behaviour in that it gracefully increases and decreases the
218 * > CPU speed rather than jumping to max speed the moment there is any load
219 * > on the CPU. This behaviour is more suitable in a battery powered
222 class Conservative : public Governor {
223 double freq_up_threshold_ = .8;
224 double freq_down_threshold_ = .2;
227 explicit Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
228 virtual std::string get_name() const override { return "Conservative"; }
230 virtual void update() override
232 double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
233 int pstate = get_host()->get_pstate();
234 sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
236 if (load > freq_up_threshold_) {
237 if (pstate != get_min_pstate()) {
238 get_host()->set_pstate(pstate - 1);
239 XBT_INFO("Load: %f > threshold: %f -> increasing performance to pstate %d", load, freq_up_threshold_,
242 XBT_DEBUG("Load: %f > threshold: %f -> but cannot speed up even more, already in highest pstate %d", load,
243 freq_up_threshold_, pstate);
245 } else if (load < freq_down_threshold_) {
246 if (pstate != get_max_pstate()) { // Are we in the slowest pstate already?
247 get_host()->set_pstate(pstate + 1);
248 XBT_INFO("Load: %f < threshold: %f -> slowing down to pstate %d", load, freq_down_threshold_, pstate + 1);
250 XBT_DEBUG("Load: %f < threshold: %f -> cannot slow down even more, already in slowest pstate %d", load,
251 freq_down_threshold_, pstate);
257 class Adagio : public Governor {
260 double start_time = 0;
261 double comp_counter = 0;
262 double comp_timer = 0;
264 std::vector<std::vector<double>> rates;
266 unsigned int task_id = 0;
267 bool iteration_running = false; /*< Are we currently between iteration_in and iteration_out calls? */
270 explicit Adagio(simgrid::s4u::Host* ptr)
271 : Governor(ptr), rates(100, std::vector<double>(ptr->get_pstate_count(), 0.0))
274 simgrid::smpi::plugin::ampi::on_iteration_in.connect([this](simgrid::s4u::ActorPtr actor) {
275 // Every instance of this class subscribes to this event, so one per host
276 // This means that for any actor, all 'hosts' are normally notified of these
277 // changes, even those who don't currently run the actor 'proc_id'.
278 // -> Let's check if this signal call is for us!
279 if (get_host() == actor->get_host()) {
280 iteration_running = true;
283 simgrid::smpi::plugin::ampi::on_iteration_out.connect([this](simgrid::s4u::ActorPtr actor) {
284 if (get_host() == actor->get_host()) {
285 iteration_running = false;
290 simgrid::kernel::activity::ExecImpl::on_creation.connect([this](simgrid::kernel::activity::ExecImplPtr activity) {
291 if (activity->host_ == get_host())
294 simgrid::kernel::activity::ExecImpl::on_completion.connect([this](simgrid::kernel::activity::ExecImplPtr activity) {
295 // For more than one host (not yet supported), we can access the host via
296 // simcalls_.front()->issuer->iface()->get_host()
297 if (activity->host_ == get_host() && iteration_running) {
298 comp_timer += activity->surf_action_->get_finish_time() - activity->surf_action_->get_start_time();
301 simgrid::s4u::Link::on_communicate.connect(
302 [this](kernel::resource::NetworkAction* action, s4u::Host* src, s4u::Host* dst) {
303 if ((get_host() == src || get_host() == dst) && iteration_running) {
309 virtual std::string get_name() const override { return "Adagio"; }
313 sg_host_load_reset(get_host());
314 comp_counter = sg_host_get_computed_flops(get_host()); // Should be 0 because of the reset
316 start_time = simgrid::s4u::Engine::get_clock();
317 if (rates.size() <= task_id)
318 rates.resize(task_id + 5, std::vector<double>(get_host()->get_pstate_count(), 0.0));
319 if (rates[task_id][best_pstate] == 0)
321 get_host()->set_pstate(best_pstate); // Load our schedule
322 XBT_DEBUG("Set pstate to %i", best_pstate);
327 double computed_flops = sg_host_get_computed_flops(get_host()) - comp_counter;
328 double target_time = (simgrid::s4u::Engine::get_clock() - start_time);
331 static_cast<double>(99.0 / 100.0); // FIXME We account for t_copy arbitrarily with 1% -- this needs to be fixed
333 bool is_initialized = rates[task_id][best_pstate] != 0;
334 rates[task_id][best_pstate] = computed_flops / comp_timer;
335 if (not is_initialized) {
336 for (int i = 1; i < get_host()->get_pstate_count(); i++) {
337 rates[task_id][i] = rates[task_id][0] * (get_host()->get_pstate_speed(i) / get_host()->get_speed());
341 for (int pstate = get_host()->get_pstate_count() - 1; pstate >= 0; pstate--) {
342 if (computed_flops / rates[task_id][pstate] <= target_time) {
343 // We just found the pstate we want to use!
344 best_pstate = pstate;
351 virtual void update() override {}
354 } // namespace plugin
355 } // namespace simgrid
357 /* **************************** events callback *************************** */
358 static void on_host_added(simgrid::s4u::Host& host)
360 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
363 std::string name = std::string("dvfs-daemon-") + host.get_cname();
364 simgrid::s4u::ActorPtr daemon = simgrid::s4u::Actor::create(name.c_str(), &host, []() {
366 * This lambda function is the function the actor (daemon) will execute
367 * all the time - in the case of the dvfs plugin, this controls when to
368 * lower/raise the frequency.
370 simgrid::s4u::ActorPtr daemon_proc = simgrid::s4u::Actor::self();
372 XBT_DEBUG("DVFS process on %s is a daemon: %d", daemon_proc->get_host()->get_cname(), daemon_proc->is_daemon());
374 std::string dvfs_governor;
375 const char* host_conf = daemon_proc->get_host()->get_property("plugin/dvfs/governor");
376 if (host_conf != nullptr) {
377 dvfs_governor = std::string(host_conf);
378 boost::algorithm::to_lower(dvfs_governor);
380 dvfs_governor = cfg_governor;
381 boost::algorithm::to_lower(dvfs_governor);
384 auto governor = [&dvfs_governor, &daemon_proc]() {
385 if (dvfs_governor == "conservative") {
386 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
387 new simgrid::plugin::dvfs::Conservative(daemon_proc->get_host()));
388 } else if (dvfs_governor == "ondemand") {
389 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
390 new simgrid::plugin::dvfs::OnDemand(daemon_proc->get_host()));
391 } else if (dvfs_governor == "adagio") {
392 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
393 new simgrid::plugin::dvfs::Adagio(daemon_proc->get_host()));
394 } else if (dvfs_governor == "performance") {
395 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
396 new simgrid::plugin::dvfs::Performance(daemon_proc->get_host()));
397 } else if (dvfs_governor == "powersave") {
398 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
399 new simgrid::plugin::dvfs::Powersave(daemon_proc->get_host()));
401 XBT_CRITICAL("No governor specified for host %s, falling back to Performance",
402 daemon_proc->get_host()->get_cname());
403 return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
404 new simgrid::plugin::dvfs::Performance(daemon_proc->get_host()));
409 // Sleep *before* updating; important for startup (i.e., t = 0).
410 // In the beginning, we want to go with the pstates specified in the platform file
411 // (so we sleep first)
412 simgrid::s4u::this_actor::sleep_for(governor->get_sampling_rate());
414 XBT_DEBUG("Governor (%s) just updated!", governor->get_name().c_str());
417 XBT_WARN("I should have never reached this point: daemons should be killed when all regular processes are done");
421 // This call must be placed in this function. Otherwise, the daemonize() call comes too late and
422 // SMPI will take this process as an MPI process!
426 /* **************************** Public interface *************************** */
428 /** @ingroup SURF_plugin_load
429 * @brief Initializes the HostDvfs plugin
430 * @details The HostDvfs plugin provides an API to get the current load of each host.
432 void sg_host_dvfs_plugin_init()
434 static bool inited = false;
439 sg_host_load_plugin_init();
441 simgrid::s4u::Host::on_creation.connect(&on_host_added);