feat: remove CpuUsage calculation

This commit is contained in:
2025-10-27 10:38:56 +01:00
parent 4559e13d7d
commit ad879bfe7b
6 changed files with 10 additions and 334 deletions

View File

@@ -11,7 +11,6 @@
#include "core/TaskManager.h" #include "core/TaskManager.h"
#include "Service.h" #include "Service.h"
#include "util/Logging.h" #include "util/Logging.h"
#include "util/CpuUsage.h"
class Spore { class Spore {
public: public:
@@ -34,12 +33,6 @@ public:
TaskManager& getTaskManager() { return taskManager; } TaskManager& getTaskManager() { return taskManager; }
ClusterManager& getCluster() { return cluster; } ClusterManager& getCluster() { return cluster; }
ApiServer& getApiServer() { return apiServer; } ApiServer& getApiServer() { return apiServer; }
// CPU usage monitoring
CpuUsage& getCpuUsage() { return cpuUsage; }
float getCurrentCpuUsage() const { return cpuUsage.getCpuUsage(); }
float getAverageCpuUsage() const { return cpuUsage.getAverageCpuUsage(); }
private: private:
void initializeCore(); void initializeCore();
@@ -51,7 +44,6 @@ private:
TaskManager taskManager; TaskManager taskManager;
ClusterManager cluster; ClusterManager cluster;
ApiServer apiServer; ApiServer apiServer;
CpuUsage cpuUsage;
std::vector<std::shared_ptr<Service>> services; std::vector<std::shared_ptr<Service>> services;
bool initialized; bool initialized;

View File

@@ -1,11 +1,10 @@
#pragma once #pragma once
#include "spore/Service.h" #include "spore/Service.h"
#include "spore/util/CpuUsage.h"
#include <functional> #include <functional>
class MonitoringService : public Service { class MonitoringService : public Service {
public: public:
MonitoringService(CpuUsage& cpuUsage); MonitoringService();
void registerEndpoints(ApiServer& api) override; void registerEndpoints(ApiServer& api) override;
void registerTasks(TaskManager& taskManager) override; void registerTasks(TaskManager& taskManager) override;
const char* getName() const override { return "Monitoring"; } const char* getName() const override { return "Monitoring"; }
@@ -41,6 +40,4 @@ private:
// Helper methods // Helper methods
void getFilesystemInfo(size_t& totalBytes, size_t& usedBytes) const; void getFilesystemInfo(size_t& totalBytes, size_t& usedBytes) const;
CpuUsage& cpuUsage;
}; };

View File

@@ -1,118 +0,0 @@
#pragma once
#include <Arduino.h>
/**
* @brief CPU usage measurement utility for ESP32/ESP8266
*
* This class provides methods to measure CPU usage by tracking idle time
* and calculating the percentage of time the CPU is busy vs idle.
*/
class CpuUsage {
public:
/**
* @brief Construct a new CpuUsage object
*/
CpuUsage();
/**
* @brief Destructor
*/
~CpuUsage() = default;
/**
* @brief Initialize the CPU usage measurement
* Call this once during setup
*/
void begin();
/**
* @brief Start measuring CPU usage for the current cycle
* Call this at the beginning of your main loop
*/
void startMeasurement();
/**
* @brief End measuring CPU usage for the current cycle
* Call this at the end of your main loop
*/
void endMeasurement();
/**
* @brief Get the current CPU usage percentage
* @return float CPU usage percentage (0.0 to 100.0)
*/
float getCpuUsage() const;
/**
* @brief Get the average CPU usage over the measurement window
* @return float Average CPU usage percentage (0.0 to 100.0)
*/
float getAverageCpuUsage() const;
/**
* @brief Get the maximum CPU usage recorded
* @return float Maximum CPU usage percentage (0.0 to 100.0)
*/
float getMaxCpuUsage() const;
/**
* @brief Get the minimum CPU usage recorded
* @return float Minimum CPU usage percentage (0.0 to 100.0)
*/
float getMinCpuUsage() const;
/**
* @brief Reset all CPU usage statistics
*/
void reset();
/**
* @brief Check if measurement is currently active
* @return true if measurement is active, false otherwise
*/
bool isMeasuring() const;
/**
* @brief Get the number of measurements taken
* @return unsigned long Number of measurements
*/
unsigned long getMeasurementCount() const;
private:
// Measurement state
bool _initialized;
bool _measuring;
unsigned long _measurementCount;
// Timing variables
unsigned long _cycleStartTime;
unsigned long _idleStartTime;
unsigned long _totalIdleTime;
unsigned long _totalCycleTime;
// Statistics
float _currentCpuUsage;
float _averageCpuUsage;
float _maxCpuUsage;
float _minCpuUsage;
unsigned long _totalCpuTime;
// Rolling average window
static constexpr size_t ROLLING_WINDOW_SIZE = 10;
float _rollingWindow[ROLLING_WINDOW_SIZE];
size_t _rollingIndex;
bool _rollingWindowFull;
/**
* @brief Update rolling average calculation
* @param value New value to add to rolling average
*/
void updateRollingAverage(float value);
/**
* @brief Update min/max statistics
* @param value New value to check against min/max
*/
void updateMinMax(float value);
};

View File

@@ -9,7 +9,7 @@
Spore::Spore() : ctx(), network(ctx), taskManager(ctx), cluster(ctx, taskManager), Spore::Spore() : ctx(), network(ctx), taskManager(ctx), cluster(ctx, taskManager),
apiServer(ctx, taskManager, ctx.config.api_server_port), apiServer(ctx, taskManager, ctx.config.api_server_port),
cpuUsage(), initialized(false), apiServerStarted(false) { initialized(false), apiServerStarted(false) {
// Rebuild labels from constructor + config labels // Rebuild labels from constructor + config labels
ctx.rebuildLabels(); ctx.rebuildLabels();
@@ -18,7 +18,7 @@ Spore::Spore() : ctx(), network(ctx), taskManager(ctx), cluster(ctx, taskManager
Spore::Spore(std::initializer_list<std::pair<String, String>> initialLabels) Spore::Spore(std::initializer_list<std::pair<String, String>> initialLabels)
: ctx(initialLabels), network(ctx), taskManager(ctx), cluster(ctx, taskManager), : ctx(initialLabels), network(ctx), taskManager(ctx), cluster(ctx, taskManager),
apiServer(ctx, taskManager, ctx.config.api_server_port), apiServer(ctx, taskManager, ctx.config.api_server_port),
cpuUsage(), initialized(false), apiServerStarted(false) { initialized(false), apiServerStarted(false) {
// Rebuild labels from constructor + config labels (config takes precedence) // Rebuild labels from constructor + config labels (config takes precedence)
ctx.rebuildLabels(); ctx.rebuildLabels();
@@ -40,9 +40,6 @@ void Spore::setup() {
// Initialize core components // Initialize core components
initializeCore(); initializeCore();
// Initialize CPU usage monitoring
cpuUsage.begin();
// Register core services // Register core services
registerCoreServices(); registerCoreServices();
@@ -78,15 +75,9 @@ void Spore::loop() {
return; return;
} }
// Start CPU usage measurement
cpuUsage.startMeasurement();
// Execute main tasks // Execute main tasks
taskManager.execute(); taskManager.execute();
// End CPU usage measurement before yield
cpuUsage.endMeasurement();
// Yield to allow other tasks to run // Yield to allow other tasks to run
yield(); yield();
} }
@@ -141,7 +132,7 @@ void Spore::registerCoreServices() {
auto clusterService = std::make_shared<ClusterService>(ctx); auto clusterService = std::make_shared<ClusterService>(ctx);
auto taskService = std::make_shared<TaskService>(taskManager); auto taskService = std::make_shared<TaskService>(taskManager);
auto staticFileService = std::make_shared<StaticFileService>(ctx, apiServer); auto staticFileService = std::make_shared<StaticFileService>(ctx, apiServer);
auto monitoringService = std::make_shared<MonitoringService>(cpuUsage); auto monitoringService = std::make_shared<MonitoringService>();
// Add to services list // Add to services list
services.push_back(nodeService); services.push_back(nodeService);

View File

@@ -5,8 +5,7 @@
#include <FS.h> #include <FS.h>
#include <LittleFS.h> #include <LittleFS.h>
MonitoringService::MonitoringService(CpuUsage& cpuUsage) MonitoringService::MonitoringService() {
: cpuUsage(cpuUsage) {
} }
void MonitoringService::registerEndpoints(ApiServer& api) { void MonitoringService::registerEndpoints(ApiServer& api) {
@@ -22,11 +21,11 @@ void MonitoringService::registerTasks(TaskManager& taskManager) {
MonitoringService::SystemResources MonitoringService::getSystemResources() const { MonitoringService::SystemResources MonitoringService::getSystemResources() const {
SystemResources resources; SystemResources resources;
// CPU information // CPU information - sending fixed value of 100
resources.currentCpuUsage = cpuUsage.getCpuUsage(); resources.currentCpuUsage = 100.0f;
resources.averageCpuUsage = cpuUsage.getAverageCpuUsage(); resources.averageCpuUsage = 100.0f;
resources.measurementCount = cpuUsage.getMeasurementCount(); resources.measurementCount = 0;
resources.isMeasuring = cpuUsage.isMeasuring(); resources.isMeasuring = false;
// Memory information - ESP8266 compatible // Memory information - ESP8266 compatible
resources.freeHeap = ESP.getFreeHeap(); resources.freeHeap = ESP.getFreeHeap();

View File

@@ -1,185 +0,0 @@
#include "spore/util/CpuUsage.h"
CpuUsage::CpuUsage()
: _initialized(false)
, _measuring(false)
, _measurementCount(0)
, _cycleStartTime(0)
, _idleStartTime(0)
, _totalIdleTime(0)
, _totalCycleTime(0)
, _currentCpuUsage(0.0f)
, _averageCpuUsage(0.0f)
, _maxCpuUsage(0.0f)
, _minCpuUsage(100.0f)
, _totalCpuTime(0)
, _rollingIndex(0)
, _rollingWindowFull(false) {
// Initialize rolling window
for (size_t i = 0; i < ROLLING_WINDOW_SIZE; ++i) {
_rollingWindow[i] = 0.0f;
}
}
void CpuUsage::begin() {
if (_initialized) {
return;
}
_initialized = true;
_measurementCount = 0;
_totalIdleTime = 0;
_totalCycleTime = 0;
_totalCpuTime = 0;
_currentCpuUsage = 0.0f;
_averageCpuUsage = 0.0f;
_maxCpuUsage = 0.0f;
_minCpuUsage = 100.0f;
_rollingIndex = 0;
_rollingWindowFull = false;
// Initialize rolling window
for (size_t i = 0; i < ROLLING_WINDOW_SIZE; ++i) {
_rollingWindow[i] = 0.0f;
}
}
void CpuUsage::startMeasurement() {
if (!_initialized) {
return;
}
if (_measuring) {
// If already measuring, end the previous measurement first
endMeasurement();
}
_measuring = true;
_cycleStartTime = millis();
_idleStartTime = millis();
}
void CpuUsage::endMeasurement() {
if (!_initialized || !_measuring) {
return;
}
unsigned long cycleEndTime = millis();
unsigned long cycleDuration = cycleEndTime - _cycleStartTime;
// Calculate idle time (time spent in yield() calls)
unsigned long idleTime = cycleEndTime - _idleStartTime;
// Calculate CPU usage
if (cycleDuration > 0) {
_currentCpuUsage = ((float)(cycleDuration - idleTime) / (float)cycleDuration) * 100.0f;
// Clamp to valid range
if (_currentCpuUsage < 0.0f) {
_currentCpuUsage = 0.0f;
} else if (_currentCpuUsage > 100.0f) {
_currentCpuUsage = 100.0f;
}
// Update statistics
_totalCycleTime += cycleDuration;
_totalIdleTime += idleTime;
_totalCpuTime += (cycleDuration - idleTime);
_measurementCount++;
// Update rolling average
updateRollingAverage(_currentCpuUsage);
// Update min/max
updateMinMax(_currentCpuUsage);
// Calculate overall average
if (_measurementCount > 0) {
_averageCpuUsage = ((float)_totalCpuTime / (float)_totalCycleTime) * 100.0f;
}
}
_measuring = false;
}
float CpuUsage::getCpuUsage() const {
return _currentCpuUsage;
}
float CpuUsage::getAverageCpuUsage() const {
if (_rollingWindowFull) {
return _averageCpuUsage;
} else if (_measurementCount > 0) {
// Calculate average from rolling window
float sum = 0.0f;
for (size_t i = 0; i < _rollingIndex; ++i) {
sum += _rollingWindow[i];
}
return sum / (float)_rollingIndex;
}
return 0.0f;
}
float CpuUsage::getMaxCpuUsage() const {
return _maxCpuUsage;
}
float CpuUsage::getMinCpuUsage() const {
return _minCpuUsage;
}
void CpuUsage::reset() {
_measurementCount = 0;
_totalIdleTime = 0;
_totalCycleTime = 0;
_totalCpuTime = 0;
_currentCpuUsage = 0.0f;
_averageCpuUsage = 0.0f;
_maxCpuUsage = 0.0f;
_minCpuUsage = 100.0f;
_rollingIndex = 0;
_rollingWindowFull = false;
// Reset rolling window
for (size_t i = 0; i < ROLLING_WINDOW_SIZE; ++i) {
_rollingWindow[i] = 0.0f;
}
}
bool CpuUsage::isMeasuring() const {
return _measuring;
}
unsigned long CpuUsage::getMeasurementCount() const {
return _measurementCount;
}
void CpuUsage::updateRollingAverage(float value) {
_rollingWindow[_rollingIndex] = value;
_rollingIndex++;
if (_rollingIndex >= ROLLING_WINDOW_SIZE) {
_rollingIndex = 0;
_rollingWindowFull = true;
}
// Calculate rolling average
float sum = 0.0f;
size_t count = _rollingWindowFull ? ROLLING_WINDOW_SIZE : _rollingIndex;
for (size_t i = 0; i < count; ++i) {
sum += _rollingWindow[i];
}
_averageCpuUsage = sum / (float)count;
}
void CpuUsage::updateMinMax(float value) {
if (value > _maxCpuUsage) {
_maxCpuUsage = value;
}
if (value < _minCpuUsage) {
_minCpuUsage = value;
}
}