Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[PLUGINS] Initial commit for the Dvfs plugin (frequency governors)
[simgrid.git] / src / surf / plugins / host_dvfs.cpp
1 /* Copyright (c) 2010, 2012-2016. 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/classification.hpp>
13 #include <boost/algorithm/string/split.hpp>
14 #include <simgrid/msg.h>
15 #include <simgrid/s4u/Engine.hpp>
16 #include <string>
17 #include <utility>
18 #include <vector>
19
20 /** @addtogroup SURF_plugin_load
21
22   This plugin makes it very simple for users to obtain the current load for each host.
23
24 */
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_dvfs, surf, "Logging specific to the SURF HostDvfs plugin");
27
28 namespace simgrid {
29 namespace plugin {
30
31 namespace dvfs {
32 class Governor {
33
34 protected:
35   simgrid::s4u::Host* host;
36
37 public:
38   double sampling_rate;
39
40   Governor(simgrid::s4u::Host* ptr) : host(ptr) { init(); }
41
42   void init()
43   {
44     const char* local_sampling_rate_config = host->getProperty("plugin/dvfs/sampling_rate");
45     double global_sampling_rate_config     = xbt_cfg_get_double("plugin/dvfs/sampling_rate");
46     if (local_sampling_rate_config != nullptr) {
47       sampling_rate = std::stod(local_sampling_rate_config);
48     } else {
49       sampling_rate = global_sampling_rate_config;
50     }
51   }
52
53   virtual void update() = 0;
54   double samplingRate() { return sampling_rate; }
55 };
56
57 class Performance : public Governor {
58 public:
59   Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
60
61   void update() { host->setPstate(0); }
62 };
63
64 class Powersave : public Governor {
65 public:
66   Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
67
68   void update() { host->setPstate(host->getPstatesCount() - 1); }
69 };
70
71 class OnDemand : public Governor {
72   double freq_up_threshold = 0.95;
73
74 public:
75   OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
76
77   void update()
78   {
79     double load = sg_host_get_current_load(host);
80
81     if (load > freq_up_threshold) {
82       host->setPstate(0); /* Run at max. performance! */
83       XBT_INFO("Changed to pstate %f", 0.0);
84     } else {
85       /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
86        *
87        *    freq_next = min_f + load * (max_f - min_f) / 100;
88        *
89        * So they assume that frequency increases by 100 MHz. We will just use
90        * lowest_pstate - load*pstatesCount();
91        */
92       int max_pstate = host->getPstatesCount() - 1;
93
94       host->setPstate(max_pstate - load * max_pstate);
95       XBT_INFO("Changed to pstate %f -- check: %i", max_pstate - load * max_pstate, host->getPstate());
96     }
97   }
98 };
99
100 class Conservative : public Governor {
101   double freq_up_threshold   = .8;
102   double freq_down_threshold = .2;
103
104 public:
105   Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
106
107   void update()
108   {
109     double load = sg_host_get_current_load(host);
110     int pstate  = host->getPstate();
111
112     if (load > freq_up_threshold) {
113       if (pstate != 0) {
114         host->setPstate(pstate - 1);
115         XBT_INFO("Reducing pstate to %d", pstate - 1);
116       }
117     }
118
119     if (load < freq_down_threshold) {
120       int max_pstate = host->getPstatesCount() - 1;
121       if (pstate != max_pstate) { // Are we in the slowest pstate already?
122         host->setPstate(pstate + 1);
123         XBT_INFO("Increasing pstate to %d", pstate + 1);
124       }
125     }
126   }
127 };
128 }
129
130 class HostDvfs {
131 public:
132   static simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> EXTENSION_ID;
133
134   explicit HostDvfs(simgrid::s4u::Host* ptr);
135   ~HostDvfs();
136
137 private:
138   simgrid::s4u::Host* host = nullptr;
139 };
140
141 simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> HostDvfs::EXTENSION_ID;
142
143 HostDvfs::HostDvfs(simgrid::s4u::Host* ptr) : host(ptr)
144 {
145 }
146
147 HostDvfs::~HostDvfs() = default;
148 }
149 }
150
151 using simgrid::plugin::HostDvfs;
152
153 /* **************************** events  callback *************************** */
154 static int check(int argc, char* argv[]);
155
156 static void on_host_added(simgrid::s4u::Host& host)
157 {
158   if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
159     return;
160
161   // host.extension_set(new HostDvfs(&host));
162   // This call must be placed in this function. Otherweise, the daemonize() call comes too late and
163   // SMPI will take this process as an MPI process!
164   MSG_process_daemonize(MSG_process_create("daemon", check, NULL, &host));
165 }
166
167 static int check(int argc, char* argv[])
168 {
169   msg_host_t host = MSG_host_self();
170
171   int isDaemon = (MSG_process_self())->getImpl()->isDaemon();
172   XBT_INFO("Bin ein Daemon: %d", isDaemon);
173
174   simgrid::plugin::dvfs::Conservative governor(host);
175   while (1) {
176     // Sleep before updating; important for startup (i.e., t = 0).
177     // In the beginning, we want to go with the pstates specified in the platform file
178     // (so we sleep first)
179     MSG_process_sleep(governor.samplingRate());
180     governor.update();
181     XBT_INFO("Governor just updated!");
182   }
183
184   // const char* dvfs_governor = host->property("plugin/dvfs/governor");
185
186   XBT_INFO("I will never reach that point: daemons are killed when regular processes are done");
187   return 0;
188 }
189
190 /* **************************** Public interface *************************** */
191 SG_BEGIN_DECL()
192
193 /** \ingroup SURF_plugin_load
194  * \brief Initializes the HostDvfs plugin
195  * \details The HostDvfs plugin provides an API to get the current load of each host.
196  */
197 void sg_host_dvfs_plugin_init()
198 {
199   if (HostDvfs::EXTENSION_ID.valid())
200     return;
201
202   HostDvfs::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostDvfs>();
203
204   sg_host_load_plugin_init();
205
206   simgrid::s4u::Host::onCreation.connect(&on_host_added);
207   xbt_cfg_register_double("plugin/dvfs/sampling_rate", 0.1, nullptr,
208                           "How often should the dvfs plugin check whether the frequency needs to be changed?");
209 }
210
211 SG_END_DECL()