Ubuntu doens't have a CPU governor.

This commit is contained in:
Robin Davies
2024-11-11 19:31:43 -05:00
parent bd994031bd
commit 64062fbbd7
17 changed files with 161 additions and 88 deletions
+13
View File
@@ -28,6 +28,7 @@
#include <sys/types.h>
#include <ifaddrs.h>
#include "Ipv6Helpers.hpp"
#include "CpuGovernor.hpp"
using namespace pipedal;
@@ -143,6 +144,10 @@ void AdminClient::SetGovernorSettings(const std::string &settings)
{
throw PiPedalException("Can't use AdminClient when running interactively.");
}
if (!HasCpuGovernor())
{
return;
}
std::stringstream cmd;
cmd << "GovernorSettings ";
json_writer writer(cmd, true);
@@ -161,6 +166,10 @@ void AdminClient::MonitorGovernor(const std::string &governor)
{
return;
}
if (!HasCpuGovernor())
{
return;
}
std::stringstream cmd;
cmd << "MonitorGovernor ";
json_writer writer(cmd, true);
@@ -178,6 +187,10 @@ void AdminClient::UnmonitorGovernor()
{
return;
}
if (!HasCpuGovernor())
{
return;
}
std::stringstream cmd;
cmd << "UnmonitorGovernor";
cmd << '\n';
+4 -4
View File
@@ -76,7 +76,7 @@ static int exec(const std::string &command)
if ((pid = fork()) == 0)
{
execv(argv[0], (char *const *)argv.data());
write(1,"!\n",2);
auto _ = write(1,"!\n",2);
exit(EXIT_FAILURE);
}
else
@@ -84,7 +84,7 @@ static int exec(const std::string &command)
if (pid == -1)
{
write(1,"*",1);
auto _ = write(1,"*",1);
perror("execv");
return EXIT_FAILURE;
}
@@ -99,8 +99,8 @@ static int exec(const std::string &command)
void updateLog(const std::string &message)
{
write(1,message.c_str(),message.length());
write(1,"\n",1);
auto _ = write(1,message.c_str(),message.length());
_ = write(1,"\n",1);
}
+16 -1
View File
@@ -63,6 +63,10 @@ public:
void Start(std::string governor)
{
std::unique_lock<std::mutex> lock(mutex);
if (!HasCpuGovernor())
{
return;
}
if (!pThread)
{
this->governor = governor;
@@ -81,6 +85,10 @@ public:
}
void Stop()
{
if (!HasCpuGovernor())
{
return;
}
std::unique_lock<std::mutex> lock(mutex);
if (pThread)
{
@@ -109,6 +117,10 @@ private:
std::string savedGovernor;
void ServiceProc()
{
if (!HasCpuGovernor())
{
return;
}
savedGovernor = pipedal::GetCpuGovernor();
pipedal::SetCpuGovernor(this->governor);
while (true)
@@ -303,7 +315,10 @@ private:
{
throw PiPedalArgumentException("Invalid arguments.");
}
governorMonitorThread.SetGovernor(governor);
if (HasCpuGovernor())
{
governorMonitorThread.SetGovernor(governor);
}
result = 0;
}
else if (command == "WifiConfigSettings")
+10 -3
View File
@@ -101,8 +101,8 @@ static void GetCpuFrequency(uint64_t *freqMin, uint64_t *freqMax)
catch (const std::exception &)
{
}
if (fMin == 0)
fMax = 0;
if (fMax == 0)
fMin = 0;
*freqMin = fMin;
*freqMax = fMax;
}
@@ -2091,7 +2091,13 @@ public:
result.cpuUsage_ = audioDriver->CpuUse();
}
GetCpuFrequency(&result.cpuFreqMax_, &result.cpuFreqMin_);
result.governor_ = GetGovernor();
result.hasCpuGovernor_ = HasCpuGovernor();
if (result.hasCpuGovernor_)
{
result.governor_ = GetGovernor();
} else {
result.governor_ = "";
}
return result;
}
@@ -2278,5 +2284,6 @@ JSON_MAP_REFERENCE(JackHostStatus, msSinceLastUnderrun)
JSON_MAP_REFERENCE(JackHostStatus, temperaturemC)
JSON_MAP_REFERENCE(JackHostStatus, cpuFreqMin)
JSON_MAP_REFERENCE(JackHostStatus, cpuFreqMax)
JSON_MAP_REFERENCE(JackHostStatus, hasCpuGovernor)
JSON_MAP_REFERENCE(JackHostStatus, governor)
JSON_MAP_END()
+1
View File
@@ -184,6 +184,7 @@ namespace pipedal
int32_t temperaturemC_ = -100000;
uint64_t cpuFreqMax_ = 0;
uint64_t cpuFreqMin_ = 0;
bool hasCpuGovernor_ = true;
std::string governor_;
DECLARE_JSON_MAP(JackHostStatus);
+4 -2
View File
@@ -737,14 +737,16 @@ set (DEBIAN_COPYRIGHT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../debian/copyright)
# generate Copyright section of settings page.
# warning: there may be multiple versions. Pick the latest.
if(EXISTS "/usr/share/doc/libboost1.74-dev")
if(EXISTS "/usr/share/doc/libboost1.83-dev")
set (BOOST_COPYRIGHT_DIR "libboost1.83-dev")
elseif(EXISTS "/usr/share/doc/libboost1.74-dev")
set (BOOST_COPYRIGHT_DIR "libboost1.74-dev")
elseif(EXISTS "/usr/share/doc/libboost1.71-dev")
set (BOOST_COPYRIGHT_DIR "libboost1.71-dev")
elseif(EXISTS "/usr/share/doc/libboost1.67-dev")
set (BOOST_COPYRIGHT_DIR "libboost1.67-dev")
else()
message(ERROR "Boost libary version has changed. Please update me.")
message(FATAL_ERROR "Boost libary version has changed. Please update me.")
endif()
+24 -16
View File
@@ -26,26 +26,34 @@
using namespace pipedal;
#ifdef __WIN32__
// not a feature on Win32.
void pipedal::SetCpuGovernor(const std::string &governor) { }
std::vector<std::string> pipedal::GetAvailableGovernors() { return std::vector<std::string>(); }
#else
static const int SYSFS_RETRIES = 3;
bool pipedal::HasCpuGovernor()
{
#ifdef __WIN32__
return false;
#else
std::filesystem::path sysFsPath = SS("/sys/devices/system/cpu/cpu" << 0 << "/cpufreq/scaling_governor");
return std::filesystem::exists(sysFsPath);
#endif
}
std::string pipedal::GetCpuGovernor()
{
std::string result;
try {
std::ifstream f("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
if (!f.is_open())
{
return "";
}
f >> result;
} catch (const std::exception &)
{
return result;
}
return result;
@@ -115,6 +123,10 @@ void pipedal::SetCpuGovernor(const std::string &governor) {
break;
std::filesystem::path sysFsPath = SS("/sys/devices/system/cpu/cpu" << nCpu << "/cpufreq/scaling_governor");
if (!std::filesystem::exists(sysFsPath))
{
return;
}
if (!writeAndVerify(sysFsPath,governor))
{
@@ -125,17 +137,13 @@ void pipedal::SetCpuGovernor(const std::string &governor) {
}
}
std::vector<std::string> pipedal::GetAvailableGovernors() {
if (!HasCpuGovernor())
{
return std::vector<std::string>();
}
return std::vector<std::string> {
"performance",
"ondemand",
"powersave"
};
}
#endif
void SetCpuGovernor(const std::string &governor);
std::vector<std::string> GetAvailableGovernors();
+1
View File
@@ -23,6 +23,7 @@
#include <vector>
namespace pipedal {
bool HasCpuGovernor();
std::string GetCpuGovernor();
void SetCpuGovernor(const std::string &governor);
+4 -3
View File
@@ -50,6 +50,7 @@ namespace fs = std::filesystem;
#include "GithubResponseHeaders.hpp"
class pipedal::UpdaterImpl : public Updater
{
public:
@@ -236,7 +237,7 @@ void UpdaterImpl::Stop()
if (event_writer != -1)
{
uint64_t value = CLOSE_EVENT;
write(this->event_writer, &value, sizeof(uint64_t));
auto _ = write(this->event_writer, &value, sizeof(uint64_t));
}
if (thread)
{
@@ -256,7 +257,7 @@ void UpdaterImpl::Stop()
void UpdaterImpl::CheckNow()
{
uint64_t value = CHECK_NOW_EVENT;
write(this->event_writer, &value, sizeof(uint64_t));
auto _ = write(this->event_writer, &value, sizeof(uint64_t));
}
void UpdaterImpl::SetUpdateListener(UpdateListener &&listener)
@@ -912,7 +913,7 @@ void UpdaterImpl::SetUpdatePolicy(UpdatePolicyT updatePolicy)
void UpdaterImpl::ForceUpdateCheck()
{
uint64_t value = UNCACHED_CHECK_NOW_EVENT;
write(this->event_writer, &value, sizeof(uint64_t));
auto _ = write(this->event_writer, &value, sizeof(uint64_t));
}
UpdateStatus::UpdateStatus()
+1 -1
View File
@@ -97,7 +97,7 @@ void segvHandler(int sig) {
// Print out all the frames to stderr
const char *message = "Error: SEGV signal received.\n";
write(STDERR_FILENO,message,strlen(message));
auto _ = write(STDERR_FILENO,message,strlen(message));
backtrace_symbols_fd(array+2, size-2, STDERR_FILENO);
_exit(EXIT_FAILURE);
+6 -2
View File
@@ -14,6 +14,7 @@
#include "TemporaryFile.hpp"
#include "json_variant.hpp"
#include "GithubResponseHeaders.hpp"
#include "Finally.hpp"
using namespace std;
using namespace pipedal;
@@ -37,12 +38,15 @@ std::string psystem(const std::string &command)
std::string cmdRedirected = command + " 2>&1";
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmdRedirected.c_str(), "r"), pclose);
FILE *pipe = popen(cmdRedirected.c_str(), "r");
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
Finally ([pipe]() {
pclose(pipe);
});
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
{
result += buffer.data();
}