Logo AND Algorithmique Numérique Distribuée

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