-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsystem_monitor.cpp
More file actions
158 lines (132 loc) · 4.86 KB
/
system_monitor.cpp
File metadata and controls
158 lines (132 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "system_monitor.h"
#include "logging.h"
// Global instance
SystemMonitor systemMonitor;
SystemMonitor::SystemMonitor() :
lastWatchdogReset(0),
lastMemoryCheck(0),
lastSerialFlush(0),
minFreeHeap(SIZE_MAX),
minFreePsram(SIZE_MAX),
systemHealthy(true)
{
}
bool SystemMonitor::begin(unsigned long timeoutMs) {
LOG_DEBUG("[SystemMonitor] Initializing system monitor and watchdog");
// Try to initialize watchdog timer for system stability
esp_task_wdt_config_t wdt_config = {
.timeout_ms = timeoutMs,
.idle_core_mask = WATCHDOG_IDLE_CORE_MASK,
.trigger_panic = WATCHDOG_TRIGGER_PANIC
};
LOG_DEBUG_F("[SystemMonitor] Watchdog timeout: %lu ms\n", timeoutMs);
esp_err_t result = esp_task_wdt_init(&wdt_config);
if (result == ESP_ERR_INVALID_STATE) {
LOG_DEBUG("[SystemMonitor] Watchdog already initialized");
} else if (result != ESP_OK) {
LOG_ERROR_F("[SystemMonitor] Watchdog init failed: %s\n", esp_err_to_name(result));
return false;
}
// Add current task to watchdog
result = esp_task_wdt_add(NULL);
if (result == ESP_ERR_INVALID_ARG) {
LOG_DEBUG("[SystemMonitor] Task already added to watchdog");
} else if (result != ESP_OK) {
LOG_ERROR_F("[SystemMonitor] Watchdog add task failed: %s\n", esp_err_to_name(result));
return false;
}
// Initialize memory tracking
minFreeHeap = ESP.getFreeHeap();
minFreePsram = ESP.getFreePsram();
LOG_DEBUG_F("[SystemMonitor] Initial heap: %d bytes, PSRAM: %d bytes\n", minFreeHeap, minFreePsram);
LOG_DEBUG("[SystemMonitor] System monitor initialization complete");
return true;
}
void SystemMonitor::resetWatchdog() {
unsigned long now = millis();
if (now - lastWatchdogReset >= WATCHDOG_RESET_INTERVAL) {
esp_task_wdt_reset();
lastWatchdogReset = now;
}
}
void SystemMonitor::forceResetWatchdog() {
esp_task_wdt_reset();
lastWatchdogReset = millis();
}
void SystemMonitor::checkSystemHealth() {
unsigned long now = millis();
if (now - lastMemoryCheck >= MEMORY_CHECK_INTERVAL) {
size_t freeHeap = ESP.getFreeHeap();
size_t freePsram = ESP.getFreePsram();
// Track minimum memory levels
if (freeHeap < minFreeHeap) minFreeHeap = freeHeap;
if (freePsram < minFreePsram) minFreePsram = freePsram;
// Check for critical memory levels
bool heapCritical = freeHeap < CRITICAL_HEAP_THRESHOLD;
bool psramCritical = freePsram < CRITICAL_PSRAM_THRESHOLD;
if (heapCritical || psramCritical) {
systemHealthy = false;
LOG_CRITICAL_F("[SystemMonitor] Low memory detected - Heap: %d bytes (threshold: %d), PSRAM: %d bytes (threshold: %d)\n",
freeHeap, CRITICAL_HEAP_THRESHOLD, freePsram, CRITICAL_PSRAM_THRESHOLD);
// Force garbage collection
if (heapCritical) {
LOG_WARNING("[SystemMonitor] Attempting heap cleanup");
// Could add specific cleanup here if needed
}
} else {
if (!systemHealthy) {
LOG_INFO("[SystemMonitor] System health recovered");
}
systemHealthy = true;
}
lastMemoryCheck = now;
}
}
bool SystemMonitor::isSystemHealthy() const {
return systemHealthy;
}
size_t SystemMonitor::getMinFreeHeap() const {
return minFreeHeap;
}
size_t SystemMonitor::getMinFreePsram() const {
return minFreePsram;
}
size_t SystemMonitor::getCurrentFreeHeap() const {
return ESP.getFreeHeap();
}
size_t SystemMonitor::getCurrentFreePsram() const {
return ESP.getFreePsram();
}
void SystemMonitor::flushSerial() {
unsigned long now = millis();
if (now - lastSerialFlush >= SERIAL_FLUSH_INTERVAL) {
Serial.flush();
lastSerialFlush = now;
}
}
void SystemMonitor::safeYield() {
resetWatchdog();
yield();
vTaskDelay(1); // Give other tasks a chance to run
}
void SystemMonitor::safeDelay(unsigned long ms) {
unsigned long start = millis();
while (millis() - start < ms) {
resetWatchdog();
delay(min(100UL, ms - (millis() - start)));
}
}
void SystemMonitor::update() {
forceResetWatchdog();
checkSystemHealth();
flushSerial();
}
void SystemMonitor::printMemoryStatus() {
size_t freeHeap = getCurrentFreeHeap();
size_t freePsram = getCurrentFreePsram();
LOG_INFO("[SystemMonitor] === Memory Status ===");
LOG_INFO_F("[SystemMonitor] Current - Heap: %d bytes, PSRAM: %d bytes\n", freeHeap, freePsram);
LOG_INFO_F("[SystemMonitor] Minimum - Heap: %d bytes, PSRAM: %d bytes\n", minFreeHeap, minFreePsram);
LOG_INFO_F("[SystemMonitor] System Health: %s\n", systemHealthy ? "HEALTHY" : "CRITICAL");
LOG_INFO("[SystemMonitor] =======================");
}