From 703e517baf613a4bad82af7e362c63001d2e8bc5 Mon Sep 17 00:00:00 2001
From: Extremesecrecy <10959169+extremesecrecy@users.noreply.github.com>
Date: Fri, 25 Jul 2025 14:24:10 -0700
Subject: [PATCH] RAM usage +
Introduced a new field to track memory usage in the backend status structure, allowing the server to report memory utilization as a percentage
Implemented a Linux-specific routine to read /proc/meminfo and calculate the memory usage percentage, and integrated this into the status query
Added serialization for the memory usage field so it is included in JSON responses
Updated the frontend class to deserialize and display memory usage alongside CPU usage on the status readout
---
src/AudioHost.cpp | 34 ++++++++++++++++++++++++++++-
src/AudioHost.hpp | 1 +
vite/src/pipedal/JackHostStatus.tsx | 7 ++++++
3 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/src/AudioHost.cpp b/src/AudioHost.cpp
index cf5f30e..99bcc9f 100644
--- a/src/AudioHost.cpp
+++ b/src/AudioHost.cpp
@@ -110,6 +110,36 @@ static void GetCpuFrequency(uint64_t *freqMin, uint64_t *freqMax)
*freqMin = fMin;
*freqMax = fMax;
}
+static float GetMemoryUsagePercent()
+{
+ try {
+ std::ifstream f("/proc/meminfo");
+ if (!f.is_open()) return 0.0f;
+ std::string label;
+ uint64_t memTotal = 0;
+ uint64_t memAvailable = 0;
+ while (f >> label)
+ {
+ if (label == "MemTotal:")
+ {
+ f >> memTotal;
+ } else if (label == "MemAvailable:")
+ {
+ f >> memAvailable;
+ } else {
+ std::string rest;
+ std::getline(f, rest);
+ }
+ if (memTotal && memAvailable) break;
+ }
+ if (memTotal == 0) return 0.0f;
+ return (float)(memTotal - memAvailable) * 100.0f / (float)memTotal;
+ } catch (const std::exception &)
+ {
+ return 0.0f;
+ }
+}
+
static std::string GetGovernor()
{
return pipedal::GetCpuGovernor();
@@ -2119,12 +2149,13 @@ public:
if (this->audioDriver != nullptr)
{
- result.cpuUsage_ = audioDriver->CpuUse();
+ result.cpuUsage_ = audioDriver->CpuUse();
if (!std::isfinite(result.cpuUsage_))
{
result.cpuUsage_ = 0.0f;
}
}
+ result.memoryUsage_ = GetMemoryUsagePercent();
GetCpuFrequency(&result.cpuFreqMin_, &result.cpuFreqMax_);
result.hasCpuGovernor_ = HasCpuGovernor();
if (result.hasCpuGovernor_)
@@ -2324,6 +2355,7 @@ JSON_MAP_REFERENCE(JackHostStatus, errorMessage)
JSON_MAP_REFERENCE(JackHostStatus, restarting)
JSON_MAP_REFERENCE(JackHostStatus, underruns)
JSON_MAP_REFERENCE(JackHostStatus, cpuUsage)
+JSON_MAP_REFERENCE(JackHostStatus, memoryUsage)
JSON_MAP_REFERENCE(JackHostStatus, msSinceLastUnderrun)
JSON_MAP_REFERENCE(JackHostStatus, temperaturemC)
JSON_MAP_REFERENCE(JackHostStatus, cpuFreqMin)
diff --git a/src/AudioHost.hpp b/src/AudioHost.hpp
index 16a9fb3..f2e23ba 100644
--- a/src/AudioHost.hpp
+++ b/src/AudioHost.hpp
@@ -191,6 +191,7 @@ namespace pipedal
bool restarting_;
uint64_t underruns_;
float cpuUsage_ = 0;
+ float memoryUsage_ = 0;
uint64_t msSinceLastUnderrun_ = 0;
int32_t temperaturemC_ = -100000;
uint64_t cpuFreqMax_ = 0;
diff --git a/vite/src/pipedal/JackHostStatus.tsx b/vite/src/pipedal/JackHostStatus.tsx
index 67239e9..fcccbea 100644
--- a/vite/src/pipedal/JackHostStatus.tsx
+++ b/vite/src/pipedal/JackHostStatus.tsx
@@ -68,6 +68,7 @@ export default class JackHostStatus {
this.errorMessage = input.errorMessage;
this.underruns = input.underruns;
this.cpuUsage = input.cpuUsage;
+ this.memoryUsage = input.memoryUsage;
this.msSinceLastUnderrun = input.msSinceLastUnderrun;
this.temperaturemC = input.temperaturemC;
this.cpuFreqMax = input.cpuFreqMax;
@@ -84,6 +85,7 @@ export default class JackHostStatus {
restarting: boolean = false;
underruns: number = 0;
cpuUsage: number = 0;
+ memoryUsage: number = 0;
msSinceLastUnderrun: number = -5000 * 1000;
temperaturemC: number = -1000000;
cpuFreqMax: number = 0;
@@ -177,6 +179,11 @@ export default class JackHostStatus {
CPU: {cpuDisplay(status.cpuUsage)}
+
+
+ MEM: {cpuDisplay(status.memoryUsage)}
+
+
{tempDisplay(status.temperaturemC)}