Logo AND Algorithmique Numérique Distribuée

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