Logo AND Algorithmique Numérique Distribuée

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