Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
202442686efe30f62010e574a27ab3991aca1a53
[simgrid.git] / src / plugins / host_dvfs.cpp
1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/plugins/dvfs.h"
7 #include "simgrid/plugins/load.h"
8 #include "src/plugins/vm/VirtualMachineImpl.hpp"
9 #include <xbt/config.hpp>
10
11 #include <boost/algorithm/string.hpp>
12
13 SIMGRID_REGISTER_PLUGIN(host_dvfs, "Dvfs support", &sg_host_dvfs_plugin_init)
14
15 /** @addtogroup SURF_plugin_load
16
17   This plugin makes it very simple for users to obtain the current load for each host.
18
19 */
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_dvfs, surf, "Logging specific to the SURF HostDvfs plugin");
22
23 static const char* property_sampling_rate = "plugin/dvfs/sampling_rate";
24 static const char* property_governor      = "plugin/dvfs/governor";
25
26 namespace simgrid {
27 namespace plugin {
28
29 namespace dvfs {
30 class Governor {
31
32 private:
33   simgrid::s4u::Host* const host_;
34   double sampling_rate_;
35
36 protected:
37   simgrid::s4u::Host* get_host() const { return host_; }
38
39 public:
40
41   explicit Governor(simgrid::s4u::Host* ptr) : host_(ptr) { init(); }
42   virtual ~Governor() = default;
43   virtual std::string get_name() = 0;
44
45   void init()
46   {
47     const char* local_sampling_rate_config = host_->get_property(property_sampling_rate);
48     double global_sampling_rate_config     = simgrid::config::get_value<double>(property_sampling_rate);
49     if (local_sampling_rate_config != nullptr) {
50       sampling_rate_ = std::stod(local_sampling_rate_config);
51     } else {
52       sampling_rate_ = global_sampling_rate_config;
53     }
54   }
55
56   virtual void update()         = 0;
57   double get_sampling_rate() { return sampling_rate_; }
58 };
59
60 /**
61  * The linux kernel doc describes this governor as follows:
62  * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
63  *
64  * > The CPUfreq governor "performance" sets the CPU statically to the
65  * > highest frequency within the borders of scaling_min_freq and
66  * > scaling_max_freq.
67  *
68  * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
69  */
70 class Performance : public Governor {
71 public:
72   explicit Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
73   std::string get_name() override { return "Performance"; }
74
75   void update() override { get_host()->set_pstate(0); }
76 };
77
78 /**
79  * The linux kernel doc describes this governor as follows:
80  * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
81  *
82  * > The CPUfreq governor "powersave" sets the CPU statically to the
83  * > lowest frequency within the borders of scaling_min_freq and
84  * > scaling_max_freq.
85  *
86  * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
87  */
88 class Powersave : public Governor {
89 public:
90   explicit Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
91   std::string get_name() override { return "Powersave"; }
92
93   void update() override { get_host()->set_pstate(get_host()->get_pstate_count() - 1); }
94 };
95
96 /**
97  * The linux kernel doc describes this governor as follows:
98  * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
99  *
100  * > The CPUfreq governor "ondemand" sets the CPU frequency depending on the
101  * > current system load. [...] when triggered, cpufreq checks
102  * > the CPU-usage statistics over the last period and the governor sets the
103  * > CPU accordingly.
104  */
105 class OnDemand : public Governor {
106   /**
107    * See https://elixir.bootlin.com/linux/v4.15.4/source/drivers/cpufreq/cpufreq_ondemand.c
108    * DEF_FREQUENCY_UP_THRESHOLD and od_update()
109    */
110   double freq_up_threshold_ = 0.80;
111
112 public:
113   explicit OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
114   std::string get_name() override { return "OnDemand"; }
115
116   void update() override
117   {
118     double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
119     sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
120
121     if (load > freq_up_threshold_) {
122       get_host()->set_pstate(0); /* Run at max. performance! */
123       XBT_INFO("Load: %f > threshold: %f --> changed to pstate %i", load, freq_up_threshold_, 0);
124     } else {
125       /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
126        *
127        *    freq_next = min_f + load * (max_f - min_f) / 100
128        *
129        * So they assume that frequency increases by 100 MHz. We will just use
130        * lowest_pstate - load*pstatesCount()
131        */
132       int max_pstate = get_host()->get_pstate_count() - 1;
133       // Load is now < freq_up_threshold; exclude pstate 0 (the fastest)
134       // because pstate 0 can only be selected if load > freq_up_threshold_
135       int new_pstate = max_pstate - load * (max_pstate + 1);
136       get_host()->set_pstate(new_pstate);
137
138       XBT_DEBUG("Load: %f < threshold: %f --> changed to pstate %i", load, freq_up_threshold_, new_pstate);
139     }
140   }
141 };
142
143 /**
144  * This is the conservative governor, which is very similar to the
145  * OnDemand governor. The Linux Kernel Documentation describes it
146  * very well, see https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt:
147  *
148  * > The CPUfreq governor "conservative", much like the "ondemand"
149  * > governor, sets the CPU frequency depending on the current usage.  It
150  * > differs in behaviour in that it gracefully increases and decreases the
151  * > CPU speed rather than jumping to max speed the moment there is any load
152  * > on the CPU. This behaviour is more suitable in a battery powered
153  * > environment.
154  */
155 class Conservative : public Governor {
156   double freq_up_threshold_   = .8;
157   double freq_down_threshold_ = .2;
158
159 public:
160   explicit Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
161   virtual std::string get_name() override { return "Conservative"; }
162
163   virtual void update() override
164   {
165     double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
166     int pstate  = get_host()->get_pstate();
167     sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
168
169     if (load > freq_up_threshold_) {
170       if (pstate != 0) {
171         get_host()->set_pstate(pstate - 1);
172         XBT_INFO("Load: %f > threshold: %f -> increasing performance to pstate %d", load, freq_up_threshold_,
173                  pstate - 1);
174       } else {
175         XBT_DEBUG("Load: %f > threshold: %f -> but cannot speed up even more, already in highest pstate %d", load,
176                   freq_up_threshold_, pstate);
177       }
178     } else if (load < freq_down_threshold_) {
179       int max_pstate = get_host()->get_pstate_count() - 1;
180       if (pstate != max_pstate) { // Are we in the slowest pstate already?
181         get_host()->set_pstate(pstate + 1);
182         XBT_INFO("Load: %f < threshold: %f -> slowing down to pstate %d", load, freq_down_threshold_, pstate + 1);
183       } else {
184         XBT_DEBUG("Load: %f < threshold: %f -> cannot slow down even more, already in slowest pstate %d", load,
185                   freq_down_threshold_, pstate);
186       }
187     }
188   }
189 };
190
191 /**
192  *  Add this to your host tag:
193  *    - \<prop id="plugin/dvfs/governor" value="performance" /\>
194  *
195  *  Valid values as of now are: performance, powersave, ondemand, conservative
196  *  It doesn't matter if you use uppercase or lowercase.
197  *
198  *  For the sampling rate, use this:
199  *
200  *    - \<prop id="plugin/dvfs/sampling_rate" value="2" /\>
201  *
202  *  This will run the update() method of the specified governor every 2 seconds
203  *  on that host.
204  *
205  *  These properties can also be used within the \<config\> tag to configure
206  *  these values globally. Using them within the \<host\> will overwrite this
207  *  global configuration
208  */
209 class HostDvfs {
210 public:
211   static simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> EXTENSION_ID;
212
213   explicit HostDvfs(simgrid::s4u::Host*){};
214   ~HostDvfs() = default;
215 };
216
217 simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> HostDvfs::EXTENSION_ID;
218
219 } // namespace dvfs
220 } // namespace plugin
221 } // namespace simgrid
222
223 using simgrid::plugin::dvfs::HostDvfs;
224
225 /* **************************** events  callback *************************** */
226 static void on_host_added(simgrid::s4u::Host& host)
227 {
228   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
229     return;
230
231   std::string name              = std::string("dvfs-daemon-") + host.get_cname();
232   simgrid::s4u::ActorPtr daemon = simgrid::s4u::Actor::create(name.c_str(), &host, []() {
233     /**
234      * This lambda function is the function the actor (daemon) will execute
235      * all the time - in the case of the dvfs plugin, this controls when to
236      * lower/raise the frequency.
237      */
238     simgrid::s4u::ActorPtr daemon_proc = simgrid::s4u::Actor::self();
239
240     XBT_DEBUG("DVFS process on %s is a daemon: %d", daemon_proc->get_host()->get_cname(), daemon_proc->is_daemon());
241
242     std::string dvfs_governor;
243     const char* host_conf = daemon_proc->get_host()->get_property(property_governor);
244     if (host_conf != nullptr) {
245       dvfs_governor = std::string(daemon_proc->get_host()->get_property(property_governor));
246       boost::algorithm::to_lower(dvfs_governor);
247     } else {
248       dvfs_governor = simgrid::config::get_value<std::string>(property_governor);
249       boost::algorithm::to_lower(dvfs_governor);
250     }
251
252     auto governor = [&dvfs_governor, &daemon_proc]() {
253       if (dvfs_governor == "conservative") {
254         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
255             new simgrid::plugin::dvfs::Conservative(daemon_proc->get_host()));
256       } else if (dvfs_governor == "ondemand") {
257         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
258             new simgrid::plugin::dvfs::OnDemand(daemon_proc->get_host()));
259       } else if (dvfs_governor == "performance") {
260         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
261             new simgrid::plugin::dvfs::Performance(daemon_proc->get_host()));
262       } else if (dvfs_governor == "powersave") {
263         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
264             new simgrid::plugin::dvfs::Powersave(daemon_proc->get_host()));
265       } else {
266         XBT_CRITICAL("No governor specified for host %s, falling back to Performance",
267                      daemon_proc->get_host()->get_cname());
268         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
269             new simgrid::plugin::dvfs::Performance(daemon_proc->get_host()));
270       }
271     }();
272
273     while (1) {
274       // Sleep *before* updating; important for startup (i.e., t = 0).
275       // In the beginning, we want to go with the pstates specified in the platform file
276       // (so we sleep first)
277       simgrid::s4u::this_actor::sleep_for(governor->get_sampling_rate());
278       governor->update();
279       XBT_DEBUG("Governor (%s) just updated!", governor->get_name().c_str());
280     }
281
282     XBT_WARN("I should have never reached this point: daemons should be killed when all regular processes are done");
283     return 0;
284   });
285
286   // This call must be placed in this function. Otherwise, the daemonize() call comes too late and
287   // SMPI will take this process as an MPI process!
288   daemon->daemonize();
289 }
290
291 /* **************************** Public interface *************************** */
292
293 /** \ingroup SURF_plugin_load
294  * \brief Initializes the HostDvfs plugin
295  * \details The HostDvfs plugin provides an API to get the current load of each host.
296  */
297 void sg_host_dvfs_plugin_init()
298 {
299   if (HostDvfs::EXTENSION_ID.valid())
300     return;
301
302   HostDvfs::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostDvfs>();
303
304   sg_host_load_plugin_init();
305
306   simgrid::s4u::Host::on_creation.connect(&on_host_added);
307   simgrid::config::declare_flag<double>(
308       property_sampling_rate, "How often should the dvfs plugin check whether the frequency needs to be changed?", 0.1);
309   simgrid::config::declare_flag<std::string>(
310       property_governor, "Which Governor should be used that adapts the CPU frequency?", "performance");
311 }