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("Reducing pstate to %d", pstate - 1);
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);
132 static simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> EXTENSION_ID;
134 explicit HostDvfs(simgrid::s4u::Host* ptr);
138 simgrid::s4u::Host* host = nullptr;
141 simgrid::xbt::Extension<simgrid::s4u::Host, HostDvfs> HostDvfs::EXTENSION_ID;
143 HostDvfs::HostDvfs(simgrid::s4u::Host* ptr) : host(ptr)
147 HostDvfs::~HostDvfs() = default;
151 using simgrid::plugin::HostDvfs;
153 /* **************************** events callback *************************** */
154 static int check(int argc, char* argv[]);
156 static void on_host_added(simgrid::s4u::Host& host)
158 if (dynamic_cast<simgrid::s4u::VirtualMachine*>(&host)) // Ignore virtual machines
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));
167 static int check(int argc, char* argv[])
169 msg_host_t host = MSG_host_self();
171 int isDaemon = (MSG_process_self())->getImpl()->isDaemon();
172 XBT_INFO("Bin ein Daemon: %d", isDaemon);
174 simgrid::plugin::dvfs::Conservative governor(host);
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());
181 XBT_INFO("Governor just updated!");
184 // const char* dvfs_governor = host->property("plugin/dvfs/governor");
186 XBT_INFO("I will never reach that point: daemons are killed when regular processes are done");
190 /* **************************** Public interface *************************** */
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.
197 void sg_host_dvfs_plugin_init()
199 if (HostDvfs::EXTENSION_ID.valid())
202 HostDvfs::EXTENSION_ID = simgrid::s4u::Host::extension_create<HostDvfs>();
204 sg_host_load_plugin_init();
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?");