Skip to content

Commit

Permalink
Ubuntu temperature and cpu frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
rerdavies committed Nov 23, 2024
1 parent e79589e commit 44a0d8b
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 19 deletions.
16 changes: 14 additions & 2 deletions react/src/JackHostStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ function fmtCpuFreq(freq: number): string {
return freq + " KHz";
}

function areSameFrequency(max: number, min: number): boolean {
return true;
// // x86 cpus can show incredibly minor frequency variations between CPUs
// if (min === 0) return false;
// let ratio = max/min;
// if (ratio < 1.2)
// {
// return true;
// }
// return false;
}

export default class JackHostStatus {
deserialize(input: any): JackHostStatus {
this.active = input.active;
Expand Down Expand Up @@ -92,12 +104,12 @@ export default class JackHostStatus {
(
<Typography variant="caption" color="inherit">
{
(status.cpuFreqMax === status.cpuFreqMin) ?
(areSameFrequency(status.cpuFreqMax,status.cpuFreqMin)) ?
(
<span> {status.governor} {fmtCpuFreq(status.cpuFreqMax)} </span>
)
: (
<span> {status.governor} {fmtCpuFreq(status.cpuFreqMax)}-{fmtCpuFreq(status.cpuFreqMax)} </span>
<span> {status.governor} {fmtCpuFreq(status.cpuFreqMin)}-{fmtCpuFreq(status.cpuFreqMax)} </span>

)
}
Expand Down
23 changes: 6 additions & 17 deletions src/AudioHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <unordered_map>
#include "PluginHost.hpp"
#include "PatchPropertyWriter.hpp"
#include "CpuTemperatureMonitor.hpp"

using namespace pipedal;

Expand Down Expand Up @@ -387,6 +388,7 @@ class AudioHostImpl : public AudioHost, private AudioDriverHost, private IPatchW
std::mutex atomConverterMutex;
AtomConverter atomConverter;

CpuTemperatureMonitor::ptr cpuTemperatureMonitor;
static constexpr size_t DEFERRED_MIDI_BUFFER_SIZE = 1024;

uint8_t deferredMidiMessages[DEFERRED_MIDI_BUFFER_SIZE];
Expand Down Expand Up @@ -1231,6 +1233,8 @@ class AudioHostImpl : public AudioHost, private AudioDriverHost, private IPatchW
{
realtimeAtomBuffer.resize(32 * 1024);
lv2_atom_forge_init(&inputWriterForge, pHost->GetMapFeature().GetMap());

cpuTemperatureMonitor = CpuTemperatureMonitor::Get();
}
virtual ~AudioHostImpl()
{
Expand Down Expand Up @@ -2032,21 +2036,6 @@ class AudioHostImpl : public AudioHost, private AudioDriverHost, private IPatchW
this->hostWriter.ParameterRequest(pParameterRequest);
}

static int32_t GetRaspberryPiTemperature()
{
try
{
std::ifstream f("/sys/class/thermal/thermal_zone0/temp");
int32_t temp;
f >> temp;
return temp;
}
catch (std::exception &)
{
return -1000000;
}
}

virtual JackHostStatus getJackStatus()
{
CleanRestartThreads(false);
Expand All @@ -2062,7 +2051,7 @@ class AudioHostImpl : public AudioHost, private AudioDriverHost, private IPatchW

result.msSinceLastUnderrun_ = (uint64_t)dt;

result.temperaturemC_ = GetRaspberryPiTemperature();
result.temperaturemC_ = (int32_t)(std::round(cpuTemperatureMonitor->GetTemperatureC()*1000));

result.active_ = IsAudioRunning();
result.restarting_ = this->restarting;
Expand All @@ -2071,7 +2060,7 @@ class AudioHostImpl : public AudioHost, private AudioDriverHost, private IPatchW
{
result.cpuUsage_ = audioDriver->CpuUse();
}
GetCpuFrequency(&result.cpuFreqMax_, &result.cpuFreqMin_);
GetCpuFrequency(&result.cpuFreqMin_, &result.cpuFreqMax_);
result.hasCpuGovernor_ = HasCpuGovernor();
if (result.hasCpuGovernor_)
{
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ else()
endif()

set (PIPEDAL_SOURCES
CpuTemperatureMonitor.cpp CpuTemperatureMonitor.hpp
SchedulerPriority.hpp SchedulerPriority.cpp
ModFileTypes.cpp ModFileTypes.hpp
PatchPropertyWriter.hpp
Expand Down
118 changes: 118 additions & 0 deletions src/CpuTemperatureMonitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) 2022 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#include "CpuTemperatureMonitor.hpp"

#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>
#include <string_view>
#include <optional>
#include <system_error>

namespace fs = std::filesystem;
using namespace pipedal;



static constexpr std::string_view THERMAL_PATH = "/sys/class/thermal/";


static std::optional<std::string> readFile(const fs::path& path) {
std::ifstream file(path);
if (!file) {
return std::nullopt;
}

std::string content;
std::getline(file, content);
return content;
}


static bool isCpuThermal(const fs::path &zone_path) {
auto type = readFile(zone_path / "type");
return type && (*type == "cpu-thermal") || (*type == "x86_pkg_temp");
}




class CpuTemperatureMonitorImpl: public CpuTemperatureMonitor {
private:
std::vector<fs::path> cpuZones;
public:
explicit CpuTemperatureMonitorImpl(std::vector<fs::path>&&cpuZones)
: cpuZones(std::move(cpuZones)) {
}


virtual float GetTemperatureC() override {
float result = INVALID_TEMPERATURE*1000;

for (const auto&cpuZone: cpuZones)
{

std::ifstream file(cpuZone / "temp");
if (file) {
float v;
file >> v;
if (file)
{
if (v > result)
{
result = v;
}
}
}
}
return result/1000;
}
};

static std::vector<fs::path> findCpuThermalZones() {
std::vector<fs::path> cpu_zones;
std::error_code ec;

for (const auto& entry : fs::directory_iterator(THERMAL_PATH, ec)) {
if (ec) {
std::cerr << "Error reading directory: " << ec.message() << '\n';
continue;
}

const auto& path = entry.path();
if (path.filename().string().find("thermal_zone") == 0) {
if (isCpuThermal(path)) {
cpu_zones.push_back(path);
}
}
}
return cpu_zones;
}


CpuTemperatureMonitor::ptr CpuTemperatureMonitor::Get()
{

std::vector<fs::path> cpuZones = findCpuThermalZones();

return std::make_unique<CpuTemperatureMonitorImpl>(std::move(cpuZones));
}
43 changes: 43 additions & 0 deletions src/CpuTemperatureMonitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2024 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once


#include <filesystem>
#include <cstdint>
#include <memory>

namespace pipedal {

class CpuTemperatureMonitor {
protected:
CpuTemperatureMonitor() { }
public:
virtual ~CpuTemperatureMonitor() {};

static constexpr float INVALID_TEMPERATURE = -400;
using ptr = std::unique_ptr<CpuTemperatureMonitor>;

static ptr Get(); // may return empty pointer.

// degrees Celcius * 1000;
virtual float GetTemperatureC() = 0;

};
}

0 comments on commit 44a0d8b

Please sign in to comment.