Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
628944948870da8ab0dedadc568e542c5c665e01
[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 namespace simgrid {
31 namespace plugin {
32
33 namespace dvfs {
34 class Governor {
35
36 protected:
37   simgrid::s4u::Host* host;
38
39 public:
40   double sampling_rate;
41
42   explicit Governor(simgrid::s4u::Host* ptr) : host(ptr) { init(); }
43   virtual ~Governor() = default;
44
45   void init()
46   {
47     const char* local_sampling_rate_config = host->getProperty("plugin/dvfs/sampling_rate");
48     double global_sampling_rate_config     = xbt_cfg_get_double("plugin/dvfs/sampling_rate");
49     if (local_sampling_rate_config != nullptr) {
50       sampling_rate = std::stod(local_sampling_rate_config);
51     } else {
52       sampling_rate = global_sampling_rate_config;
53     }
54   }
55
56   virtual void update()         = 0;
57   virtual std::string getName() = 0;
58   double samplingRate() { return sampling_rate; }
59 };
60
61 class Performance : public Governor {
62 public:
63   explicit Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
64
65   void update() override { host->setPstate(0); }
66   std::string getName() override { return "Performance"; }
67 };
68
69 class Powersave : public Governor {
70 public:
71   explicit Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
72
73   void update() override { host->setPstate(host->getPstatesCount() - 1); }
74   std::string getName() override { return "Powersave"; }
75 };
76
77 class OnDemand : public Governor {
78   double freq_up_threshold = 0.95;
79
80 public:
81   explicit OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
82
83   std::string getName() override { return "OnDemand"; }
84   void update() override
85   {
86     double load = sg_host_get_current_load(host);
87
88     // FIXME I don't like that we multiply with the getCoreCount() just here...
89     if (load*host->getCoreCount() > freq_up_threshold) {
90       host->setPstate(0); /* Run at max. performance! */
91       XBT_INFO("Load: %f > threshold: %f --> changed to pstate %i", load * host->getCoreCount(), freq_up_threshold, 0);
92     } else {
93       /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
94        *
95        *    freq_next = min_f + load * (max_f - min_f) / 100
96        *
97        * So they assume that frequency increases by 100 MHz. We will just use
98        * lowest_pstate - load*pstatesCount()
99        */
100       int max_pstate = host->getPstatesCount() - 1;
101       int new_pstate = max_pstate - load * max_pstate;
102       host->setPstate(new_pstate);
103
104       XBT_DEBUG("Load: %f --> changed to pstate %i", load*host->getCoreCount(), new_pstate);
105     }
106   }
107 };
108
109 class Conservative : public Governor {
110   double freq_up_threshold   = .8;
111   double freq_down_threshold = .2;
112
113 public:
114   explicit Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
115
116   virtual std::string getName() override { return "Conservative"; }
117   virtual void update() override
118   {
119     double load = sg_host_get_current_load(host)*host->getCoreCount();
120     int pstate  = host->getPstate();
121
122     if (load > freq_up_threshold) {
123       if (pstate != 0) {
124         host->setPstate(pstate - 1);
125         XBT_INFO("Load: %f > threshold: %f -> increasing performance to pstate %d", load, freq_up_threshold, pstate - 1);
126       }
127       else {
128         XBT_DEBUG("Load: %f > threshold: %f -> but cannot speed up even more, already in highest pstate %d", load, freq_up_threshold, pstate);
129       }
130     } else if (load < freq_down_threshold) {
131       int max_pstate = host->getPstatesCount() - 1;
132       if (pstate != max_pstate) { // Are we in the slowest pstate already?
133         host->setPstate(pstate + 1);
134         XBT_INFO("Load: %f < threshold: %f -> slowing down to pstate %d", load, freq_down_threshold, pstate + 1);
135       }
136       else {
137         XBT_DEBUG("Load: %f < threshold: %f -> cannot slow down even more, already in slowest pstate %d", load, freq_down_threshold, pstate);
138       }
139     }
140   }
141 };
142 }
143
144 class HostDvfs {
145 public:
146   static simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> EXTENSION_ID;
147
148   explicit HostDvfs(simgrid::s4u::Host*);
149   ~HostDvfs();
150 };
151
152 simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> HostDvfs::EXTENSION_ID;
153
154 HostDvfs::HostDvfs(simgrid::s4u::Host* ptr) {}
155
156 HostDvfs::~HostDvfs() = default;
157 }
158 }
159
160 using simgrid::plugin::HostDvfs;
161
162 /* **************************** events  callback *************************** */
163 static void on_host_added(simgrid::s4u::Host& host)
164 {
165   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
166     return;
167
168   std::string name              = std::string("dvfs-daemon-") + host.getCname();
169   simgrid::s4u::ActorPtr daemon = simgrid::s4u::Actor::createActor(name.c_str(), &host, []() {
170     /**
171      * This lambda function is the function the actor (daemon) will execute
172      * all the time - in the case of the dvfs plugin, this controls when to
173      * lower/raise the frequency.
174      */
175     simgrid::s4u::ActorPtr daemonProc = simgrid::s4u::Actor::self();
176
177     XBT_DEBUG("DVFS process on %s is a daemon: %d", daemonProc->getHost()->getName().c_str(), daemonProc->isDaemon());
178
179     std::string dvfs_governor;
180     const char* host_conf = daemonProc->getHost()->getProperty("plugin/dvfs/governor");
181     if (host_conf != nullptr) {
182       dvfs_governor = std::string(daemonProc->getHost()->getProperty("plugin/dvfs/governor"));
183       boost::algorithm::to_lower(dvfs_governor);
184     } else {
185       dvfs_governor = xbt_cfg_get_string("plugin/dvfs/governor");
186       boost::algorithm::to_lower(dvfs_governor);
187     }
188
189     // FIXME This is really ugly. When do we free the governor? Actually, never, because
190     // daemons never stop to run - they will be killed when the simulation is over. :(
191     simgrid::plugin::dvfs::Governor* governor;
192     if (dvfs_governor == "conservative") {
193       governor = new simgrid::plugin::dvfs::Conservative(daemonProc->getHost());
194     } else if (dvfs_governor == "ondemand") {
195       governor = new simgrid::plugin::dvfs::OnDemand(daemonProc->getHost());
196     } else if (dvfs_governor == "performance") {
197       governor = new simgrid::plugin::dvfs::Performance(daemonProc->getHost());
198     } else if (dvfs_governor == "powersave") {
199       governor = new simgrid::plugin::dvfs::Powersave(daemonProc->getHost());
200     } else {
201       XBT_CRITICAL("No governor specified for host %s", daemonProc->getHost()->getCname());
202     }
203
204     while (1) {
205       // Sleep *before* updating; important for startup (i.e., t = 0).
206       // In the beginning, we want to go with the pstates specified in the platform file
207       // (so we sleep first)
208       simgrid::s4u::this_actor::sleep_for(governor->samplingRate());
209       governor->update();
210       XBT_DEBUG("Governor (%s) just updated!", governor->getName().c_str());
211     }
212
213     XBT_WARN("I should have never reached this point: daemons should be killed when all regular processes are done");
214     return 0;
215   });
216
217   // This call must be placed in this function. Otherweise, the daemonize() call comes too late and
218   // SMPI will take this process as an MPI process!
219   daemon->daemonize();
220 }
221
222 /* **************************** Public interface *************************** */
223 extern "C" {
224
225 /** \ingroup SURF_plugin_load
226  * \brief Initializes the HostDvfs plugin
227  * \details The HostDvfs plugin provides an API to get the current load of each host.
228  */
229 void sg_host_dvfs_plugin_init()
230 {
231   if (HostDvfs::EXTENSION_ID.valid())
232     return;
233
234   HostDvfs::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostDvfs>();
235
236   sg_host_load_plugin_init();
237
238   simgrid::s4u::Host::onCreation.connect(&on_host_added);
239   xbt_cfg_register_double("plugin/dvfs/sampling_rate", 0.1, nullptr,
240                           "How often should the dvfs plugin check whether the frequency needs to be changed?");
241   xbt_cfg_register_string("plugin/dvfs/governor", "performance", nullptr,
242                           "Which Governor should be used that adapts the CPU frequency?");
243 }
244 }