Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[DVFS] Move comment + change get_host() visibility
[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 static simgrid::config::Flag<double> cfg_sampling_rate("plugin/dvfs/sampling-rate", {"plugin/dvfs/sampling_rate"},
16     "How often should the dvfs plugin check whether the frequency needs to be changed?", 0.1,
17     [](double val){if (val != 0.1) sg_host_dvfs_plugin_init();});
18
19 static simgrid::config::Flag<std::string> cfg_governor("plugin/dvfs/governor",
20     "Which Governor should be used that adapts the CPU frequency?", "performance",
21
22     std::map<std::string, std::string>({
23         {"conservative", "TODO: Doc"},
24         {"ondemand", "TODO: Doc"},
25         {"performance", "TODO: Doc"},
26         {"powersave", "TODO: Doc"},
27     }),
28
29     [](std::string val){if (val != "performance") sg_host_dvfs_plugin_init();});
30
31 /** @addtogroup SURF_plugin_load
32
33   This plugin makes it very simple for users to obtain the current load for each host.
34
35 */
36
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_dvfs, surf, "Logging specific to the SURF HostDvfs plugin");
38
39 namespace simgrid {
40 namespace plugin {
41
42 namespace dvfs {
43
44 /**
45  *  Add this to your host tag:
46  *    - \<prop id="plugin/dvfs/governor" value="performance" /\>
47  *
48  *  Valid values as of now are: performance, powersave, ondemand, conservative
49  *  It doesn't matter if you use uppercase or lowercase.
50  *
51  *  For the sampling rate, use this:
52  *
53  *    - \<prop id="plugin/dvfs/sampling-rate" value="2" /\>
54  *
55  *  This will run the update() method of the specified governor every 2 seconds
56  *  on that host.
57  *
58  *  These properties can also be used within the \<config\> tag to configure
59  *  these values globally. Using them within the \<host\> will overwrite this
60  *  global configuration
61  */
62 class Governor {
63
64 protected:
65   simgrid::s4u::Host* const host_;
66   double sampling_rate_;
67
68 public:
69
70   explicit Governor(simgrid::s4u::Host* ptr) : host_(ptr) { init(); }
71   virtual ~Governor() = default;
72   virtual std::string get_name() = 0;
73   simgrid::s4u::Host* get_host() const { return host_; }
74
75   void init()
76   {
77     const char* local_sampling_rate_config = host_->get_property(cfg_sampling_rate.get_name());
78     double global_sampling_rate_config     = cfg_sampling_rate;
79     if (local_sampling_rate_config != nullptr) {
80       sampling_rate_ = std::stod(local_sampling_rate_config);
81     } else {
82       sampling_rate_ = global_sampling_rate_config;
83     }
84   }
85
86   virtual void update()         = 0;
87   double get_sampling_rate() { return sampling_rate_; }
88 };
89
90 /**
91  * The linux kernel doc describes this governor as follows:
92  * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
93  *
94  * > The CPUfreq governor "performance" sets the CPU statically to the
95  * > highest frequency within the borders of scaling_min_freq and
96  * > scaling_max_freq.
97  *
98  * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
99  */
100 class Performance : public Governor {
101 public:
102   explicit Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
103   std::string get_name() override { return "Performance"; }
104
105   void update() override { get_host()->set_pstate(0); }
106 };
107
108 /**
109  * The linux kernel doc describes this governor as follows:
110  * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
111  *
112  * > The CPUfreq governor "powersave" sets the CPU statically to the
113  * > lowest frequency within the borders of scaling_min_freq and
114  * > scaling_max_freq.
115  *
116  * We do not support scaling_min_freq/scaling_max_freq -- we just pick the lowest frequency.
117  */
118 class Powersave : public Governor {
119 public:
120   explicit Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
121   std::string get_name() override { return "Powersave"; }
122
123   void update() override { get_host()->set_pstate(get_host()->get_pstate_count() - 1); }
124 };
125
126 /**
127  * The linux kernel doc describes this governor as follows:
128  * https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
129  *
130  * > The CPUfreq governor "ondemand" sets the CPU frequency depending on the
131  * > current system load. [...] when triggered, cpufreq checks
132  * > the CPU-usage statistics over the last period and the governor sets the
133  * > CPU accordingly.
134  */
135 class OnDemand : public Governor {
136   /**
137    * See https://elixir.bootlin.com/linux/v4.15.4/source/drivers/cpufreq/cpufreq_ondemand.c
138    * DEF_FREQUENCY_UP_THRESHOLD and od_update()
139    */
140   double freq_up_threshold_ = 0.80;
141
142 public:
143   explicit OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
144   std::string get_name() override { return "OnDemand"; }
145
146   void update() override
147   {
148     double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
149     sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
150
151     if (load > freq_up_threshold_) {
152       get_host()->set_pstate(0); /* Run at max. performance! */
153       XBT_INFO("Load: %f > threshold: %f --> changed to pstate %i", load, freq_up_threshold_, 0);
154     } else {
155       /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
156        *
157        *    freq_next = min_f + load * (max_f - min_f) / 100
158        *
159        * So they assume that frequency increases by 100 MHz. We will just use
160        * lowest_pstate - load*pstatesCount()
161        */
162       int max_pstate = get_host()->get_pstate_count() - 1;
163       // Load is now < freq_up_threshold; exclude pstate 0 (the fastest)
164       // because pstate 0 can only be selected if load > freq_up_threshold_
165       int new_pstate = max_pstate - load * (max_pstate + 1);
166       get_host()->set_pstate(new_pstate);
167
168       XBT_DEBUG("Load: %f < threshold: %f --> changed to pstate %i", load, freq_up_threshold_, new_pstate);
169     }
170   }
171 };
172
173 /**
174  * This is the conservative governor, which is very similar to the
175  * OnDemand governor. The Linux Kernel Documentation describes it
176  * very well, see https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt:
177  *
178  * > The CPUfreq governor "conservative", much like the "ondemand"
179  * > governor, sets the CPU frequency depending on the current usage.  It
180  * > differs in behaviour in that it gracefully increases and decreases the
181  * > CPU speed rather than jumping to max speed the moment there is any load
182  * > on the CPU. This behaviour is more suitable in a battery powered
183  * > environment.
184  */
185 class Conservative : public Governor {
186   double freq_up_threshold_   = .8;
187   double freq_down_threshold_ = .2;
188
189 public:
190   explicit Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
191   virtual std::string get_name() override { return "Conservative"; }
192
193   virtual void update() override
194   {
195     double load = get_host()->get_core_count() * sg_host_get_avg_load(get_host());
196     int pstate  = get_host()->get_pstate();
197     sg_host_load_reset(get_host()); // Only consider the period between two calls to this method!
198
199     if (load > freq_up_threshold_) {
200       if (pstate != 0) {
201         get_host()->set_pstate(pstate - 1);
202         XBT_INFO("Load: %f > threshold: %f -> increasing performance to pstate %d", load, freq_up_threshold_,
203                  pstate - 1);
204       } else {
205         XBT_DEBUG("Load: %f > threshold: %f -> but cannot speed up even more, already in highest pstate %d", load,
206                   freq_up_threshold_, pstate);
207       }
208     } else if (load < freq_down_threshold_) {
209       int max_pstate = get_host()->get_pstate_count() - 1;
210       if (pstate != max_pstate) { // Are we in the slowest pstate already?
211         get_host()->set_pstate(pstate + 1);
212         XBT_INFO("Load: %f < threshold: %f -> slowing down to pstate %d", load, freq_down_threshold_, pstate + 1);
213       } else {
214         XBT_DEBUG("Load: %f < threshold: %f -> cannot slow down even more, already in slowest pstate %d", load,
215                   freq_down_threshold_, pstate);
216       }
217     }
218   }
219 };
220
221 } // namespace dvfs
222 } // namespace plugin
223 } // namespace simgrid
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("plugin/dvfs/governor");
244     if (host_conf != nullptr) {
245       dvfs_governor = std::string(host_conf);
246       boost::algorithm::to_lower(dvfs_governor);
247     } else {
248       dvfs_governor = cfg_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   static bool inited = false;
300   if (inited)
301     return;
302   inited = true;
303
304   sg_host_load_plugin_init();
305
306   simgrid::s4u::Host::on_creation.connect(&on_host_added);
307 }