1 /* Copyright (c) 2010, 2012-2016. The SimGrid Team. All rights reserved. */
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. */
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"
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>
20 /** @addtogroup SURF_plugin_load
22 This plugin makes it very simple for users to obtain the current load for each host.
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_plugin_dvfs, surf, "Logging specific to the SURF HostDvfs plugin");
35 simgrid::s4u::Host* host;
40 Governor(simgrid::s4u::Host* ptr) : host(ptr) { init(); }
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);
49 sampling_rate = global_sampling_rate_config;
53 virtual void update() = 0;
54 double samplingRate() { return sampling_rate; }
57 class Performance : public Governor {
59 Performance(simgrid::s4u::Host* ptr) : Governor(ptr) {}
61 void update() { host->setPstate(0); }
64 class Powersave : public Governor {
66 Powersave(simgrid::s4u::Host* ptr) : Governor(ptr) {}
68 void update() { host->setPstate(host->getPstatesCount() - 1); }
71 class OnDemand : public Governor {
72 double freq_up_threshold = 0.95;
75 OnDemand(simgrid::s4u::Host* ptr) : Governor(ptr) {}
79 double load = sg_host_get_current_load(host);
81 if (load > freq_up_threshold) {
82 host->setPstate(0); /* Run at max. performance! */
83 XBT_INFO("Changed to pstate %f", 0.0);
85 /* The actual implementation uses a formula here: (See Kernel file cpufreq_ondemand.c:158)
87 * freq_next = min_f + load * (max_f - min_f) / 100;
89 * So they assume that frequency increases by 100 MHz. We will just use
90 * lowest_pstate - load*pstatesCount();
92 int max_pstate = host->getPstatesCount() - 1;
94 host->setPstate(max_pstate - load * max_pstate);
95 XBT_INFO("Changed to pstate %f -- check: %i", max_pstate - load * max_pstate, host->getPstate());
100 class Conservative : public Governor {
101 double freq_up_threshold = .8;
102 double freq_down_threshold = .2;
105 Conservative(simgrid::s4u::Host* ptr) : Governor(ptr) {}
109 double load = sg_host_get_current_load(host);
110 int pstate = host->getPstate();
112 if (load > freq_up_threshold) {
114 host->setPstate(pstate - 1);
115 XBT_INFO("Increasing performance to pstate %d", pstate - 1);
118 XBT_DEBUG("Cannot speed up even more, already in slowest pstate %d", pstate);
122 if (load < freq_down_threshold) {
123 int max_pstate = host->getPstatesCount() - 1;
124 if (pstate != max_pstate) { // Are we in the slowest pstate already?
125 host->setPstate(pstate + 1);
126 XBT_INFO("Slowing down to pstate %d", pstate + 1);
129 XBT_DEBUG("Cannot slow down even more, already in slowest pstate %d", pstate);
138 static simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> EXTENSION_ID;
140 explicit HostDvfs(simgrid::s4u::Host* ptr);
144 simgrid::s4u::Host* host = nullptr;
147 simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> HostDvfs::EXTENSION_ID;
149 HostDvfs::HostDvfs(simgrid::s4u::Host* ptr) : host(ptr)
153 HostDvfs::~HostDvfs() = default;
157 using simgrid::plugin::HostDvfs;
159 /* **************************** events callback *************************** */
160 static int check(int argc, char* argv[]);
162 static void on_host_added(simgrid::s4u::Host& host)
164 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
167 // host.extension_set(new HostDvfs(&host));
168 // This call must be placed in this function. Otherweise, the daemonize() call comes too late and
169 // SMPI will take this process as an MPI process!
170 MSG_process_daemonize(MSG_process_create("daemon", check, NULL, &host));
173 static int check(int argc, char* argv[])
175 msg_host_t host = MSG_host_self();
177 int isDaemon = (MSG_process_self())->getImpl()->isDaemon();
178 XBT_INFO("Bin ein Daemon: %d", isDaemon);
180 simgrid::plugin::dvfs::Conservative governor(host);
182 // Sleep before updating; important for startup (i.e., t = 0).
183 // In the beginning, we want to go with the pstates specified in the platform file
184 // (so we sleep first)
185 MSG_process_sleep(governor.samplingRate());
187 XBT_INFO("Governor just updated!");
190 // const char* dvfs_governor = host->property("plugin/dvfs/governor");
192 XBT_INFO("I will never reach that point: daemons are killed when regular processes are done");
196 /* **************************** Public interface *************************** */
199 /** \ingroup SURF_plugin_load
200 * \brief Initializes the HostDvfs plugin
201 * \details The HostDvfs plugin provides an API to get the current load of each host.
203 void sg_host_dvfs_plugin_init()
205 if (HostDvfs::EXTENSION_ID.valid())
208 HostDvfs::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostDvfs>();
210 sg_host_load_plugin_init();
212 simgrid::s4u::Host::onCreation.connect(&on_host_added);
213 xbt_cfg_register_double("plugin/dvfs/sampling_rate", 0.1, nullptr,
214 "How often should the dvfs plugin check whether the frequency needs to be changed?");