Logo AND Algorithmique Numérique Distribuée

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