Logo AND Algorithmique Numérique Distribuée

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