Logo AND Algorithmique Numérique Distribuée

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