Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[EXAMPLES] Make the HostLoad example more difficult
[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 class Performance : public Governor {
65 public:
66   explicit Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
67
68   void update() override { host->setPstate(0); }
69   std::string getName() override { return "Performance"; }
70 };
71
72 class Powersave : public Governor {
73 public:
74   explicit Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
75
76   void update() override { host->setPstate(host->getPstatesCount() - 1); }
77   std::string getName() override { return "Powersave"; }
78 };
79
80 class OnDemand : public Governor {
81   double freq_up_threshold = 0.95;
82
83 public:
84   explicit OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
85
86   std::string getName() override { return "OnDemand"; }
87   void update() override
88   {
89     double load = sg_host_get_current_load(host);
90
91     // FIXME I don't like that we multiply with the getCoreCount() just here...
92     if (load*host->getCoreCount() > freq_up_threshold) {
93       host->setPstate(0); /* Run at max. performance! */
94       XBT_INFO("Load: %f > threshold: %f --> changed to pstate %i", load * host->getCoreCount(), freq_up_threshold, 0);
95     } else {
96       /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
97        *
98        *    freq_next = min_f + load * (max_f - min_f) / 100
99        *
100        * So they assume that frequency increases by 100 MHz. We will just use
101        * lowest_pstate - load*pstatesCount()
102        */
103       int max_pstate = host->getPstatesCount() - 1;
104       int new_pstate = max_pstate - load * max_pstate;
105       host->setPstate(new_pstate);
106
107       XBT_DEBUG("Load: %f --> changed to pstate %i", load*host->getCoreCount(), new_pstate);
108     }
109   }
110 };
111
112 class Conservative : public Governor {
113   double freq_up_threshold   = .8;
114   double freq_down_threshold = .2;
115
116 public:
117   explicit Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
118
119   virtual std::string getName() override { return "Conservative"; }
120   virtual void update() override
121   {
122     double load = sg_host_get_current_load(host)*host->getCoreCount();
123     int pstate  = host->getPstate();
124
125     if (load > freq_up_threshold) {
126       if (pstate != 0) {
127         host->setPstate(pstate - 1);
128         XBT_INFO("Load: %f > threshold: %f -> increasing performance to pstate %d", load, freq_up_threshold, pstate - 1);
129       }
130       else {
131         XBT_DEBUG("Load: %f > threshold: %f -> but cannot speed up even more, already in highest pstate %d", load, freq_up_threshold, pstate);
132       }
133     } else if (load < freq_down_threshold) {
134       int max_pstate = host->getPstatesCount() - 1;
135       if (pstate != max_pstate) { // Are we in the slowest pstate already?
136         host->setPstate(pstate + 1);
137         XBT_INFO("Load: %f < threshold: %f -> slowing down to pstate %d", load, freq_down_threshold, pstate + 1);
138       }
139       else {
140         XBT_DEBUG("Load: %f < threshold: %f -> cannot slow down even more, already in slowest pstate %d", load, freq_down_threshold, pstate);
141       }
142     }
143   }
144 };
145
146 /**
147  *  Add this to your host tag:
148  *    - <prop id="plugin/dvfs/governor" value="performance" />
149  *
150  *  Valid values as of now are: performance, powersave, ondemand, conservative
151  *  It doesn't matter if you use uppercase or lowercase.
152  *
153  *  For the sampling rate, use this:
154  *
155  *    - <prop id="plugin/dvfs/sampling_rate" value="2" />
156  *
157  *  This will run the update() method of the specified governor every 2 seconds
158  *  on that host.
159  *
160  *  These properties can also be used within the <config> tag to configure
161  *  these values globally. Using them within the <host> will overwrite this
162  *  global configuration
163  */
164 class HostDvfs {
165 public:
166   static simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> EXTENSION_ID;
167
168   explicit HostDvfs(simgrid::s4u::Host*);
169   ~HostDvfs();
170 };
171
172 simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> HostDvfs::EXTENSION_ID;
173
174 HostDvfs::HostDvfs(simgrid::s4u::Host* ptr) {}
175
176 HostDvfs::~HostDvfs() = default;
177 }
178 }
179 }
180
181 using simgrid::plugin::dvfs::HostDvfs;
182
183 /* **************************** events  callback *************************** */
184 static void on_host_added(simgrid::s4u::Host& host)
185 {
186   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
187     return;
188
189   std::string name              = std::string("dvfs-daemon-") + host.getCname();
190   simgrid::s4u::ActorPtr daemon = simgrid::s4u::Actor::createActor(name.c_str(), &host, []() {
191     /**
192      * This lambda function is the function the actor (daemon) will execute
193      * all the time - in the case of the dvfs plugin, this controls when to
194      * lower/raise the frequency.
195      */
196     simgrid::s4u::ActorPtr daemonProc = simgrid::s4u::Actor::self();
197
198     XBT_DEBUG("DVFS process on %s is a daemon: %d", daemonProc->getHost()->getName().c_str(), daemonProc->isDaemon());
199
200     std::string dvfs_governor;
201     const char* host_conf = daemonProc->getHost()->getProperty(property_governor);
202     if (host_conf != nullptr) {
203       dvfs_governor = std::string(daemonProc->getHost()->getProperty(property_governor));
204       boost::algorithm::to_lower(dvfs_governor);
205     } else {
206       dvfs_governor = xbt_cfg_get_string(property_governor);
207       boost::algorithm::to_lower(dvfs_governor);
208     }
209
210     auto governor = [&dvfs_governor, &daemonProc]() {
211       if (dvfs_governor == "conservative") {
212         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
213             new simgrid::plugin::dvfs::Conservative(daemonProc->getHost()));
214       } else if (dvfs_governor == "ondemand") {
215         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
216             new simgrid::plugin::dvfs::OnDemand(daemonProc->getHost()));
217       } else if (dvfs_governor == "performance") {
218         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
219             new simgrid::plugin::dvfs::Performance(daemonProc->getHost()));
220       } else if (dvfs_governor == "powersave") {
221         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
222             new simgrid::plugin::dvfs::Powersave(daemonProc->getHost()));
223       } else {
224         XBT_CRITICAL("No governor specified for host %s, falling back to Performance",
225                      daemonProc->getHost()->getCname());
226         return std::unique_ptr<simgrid::plugin::dvfs::Governor>(
227             new simgrid::plugin::dvfs::Performance(daemonProc->getHost()));
228       }
229     }();
230
231     while (1) {
232       // Sleep *before* updating; important for startup (i.e., t = 0).
233       // In the beginning, we want to go with the pstates specified in the platform file
234       // (so we sleep first)
235       simgrid::s4u::this_actor::sleep_for(governor->samplingRate());
236       governor->update();
237       XBT_DEBUG("Governor (%s) just updated!", governor->getName().c_str());
238     }
239
240     XBT_WARN("I should have never reached this point: daemons should be killed when all regular processes are done");
241     return 0;
242   });
243
244   // This call must be placed in this function. Otherweise, the daemonize() call comes too late and
245   // SMPI will take this process as an MPI process!
246   daemon->daemonize();
247 }
248
249 /* **************************** Public interface *************************** */
250 extern "C" {
251
252 /** \ingroup SURF_plugin_load
253  * \brief Initializes the HostDvfs plugin
254  * \details The HostDvfs plugin provides an API to get the current load of each host.
255  */
256 void sg_host_dvfs_plugin_init()
257 {
258   if (HostDvfs::EXTENSION_ID.valid())
259     return;
260
261   HostDvfs::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostDvfs>();
262
263   sg_host_load_plugin_init();
264
265   simgrid::s4u::Host::onCreation.connect(&on_host_added);
266   xbt_cfg_register_double(property_sampling_rate, 0.1, nullptr,
267                           "How often should the dvfs plugin check whether the frequency needs to be changed?");
268   xbt_cfg_register_string(property_governor, "performance", nullptr,
269                           "Which Governor should be used that adapts the CPU frequency?");
270 }
271 }