|
|
@@ -20,6 +20,16 @@
|
|
|
|
|
|
namespace smartbotic::database {
|
|
|
|
|
|
+const char* memoryPressureToString(MemoryPressure p) {
|
|
|
+ switch (p) {
|
|
|
+ case MemoryPressure::Normal: return "normal";
|
|
|
+ case MemoryPressure::Soft: return "soft";
|
|
|
+ case MemoryPressure::Hard: return "hard";
|
|
|
+ case MemoryPressure::Emergency: return "emergency";
|
|
|
+ }
|
|
|
+ return "normal";
|
|
|
+}
|
|
|
+
|
|
|
MemoryStore::MemoryStore(Config config)
|
|
|
: config_(std::move(config)) {
|
|
|
}
|
|
|
@@ -1501,6 +1511,26 @@ MemoryStore::Stats MemoryStore::getStats() const {
|
|
|
return stats;
|
|
|
}
|
|
|
|
|
|
+MemoryPressure MemoryStore::pressure() const {
|
|
|
+ uint64_t used = estimatedMemoryBytes_.load(std::memory_order_relaxed);
|
|
|
+ uint64_t max = config_.maxMemoryBytes;
|
|
|
+ if (max == 0) return MemoryPressure::Normal; // unbounded — always normal
|
|
|
+
|
|
|
+ uint32_t percent = static_cast<uint32_t>(std::min<uint64_t>(100, used * 100 / max));
|
|
|
+
|
|
|
+ if (percent >= config_.memoryEmergencyPercent) return MemoryPressure::Emergency;
|
|
|
+ if (percent >= config_.memoryHardPercent) return MemoryPressure::Hard;
|
|
|
+ if (percent >= config_.memorySoftPercent) return MemoryPressure::Soft;
|
|
|
+ return MemoryPressure::Normal;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t MemoryStore::pressurePercent() const {
|
|
|
+ uint64_t used = estimatedMemoryBytes_.load(std::memory_order_relaxed);
|
|
|
+ uint64_t max = config_.maxMemoryBytes;
|
|
|
+ if (max == 0) return 0;
|
|
|
+ return static_cast<uint32_t>(std::min<uint64_t>(100, used * 100 / max));
|
|
|
+}
|
|
|
+
|
|
|
// ===== Version History =====
|
|
|
|
|
|
void MemoryStore::saveToHistory(CollectionData& coll, const Document& currentDoc) {
|
|
|
@@ -2343,9 +2373,10 @@ void MemoryStore::evictionLoop() {
|
|
|
|
|
|
// Log memory status every 12 checks (~60 seconds with 5s interval)
|
|
|
if (checkCount % 12 == 1) {
|
|
|
- spdlog::info("Memory check: {} MB estimated, {} MB threshold, {} docs, {} evicted",
|
|
|
+ spdlog::info("Memory check: {} MB estimated ({}%, pressure={}), {} docs, {} evicted",
|
|
|
stats.estimatedMemoryBytes / (1024 * 1024),
|
|
|
- threshold / (1024 * 1024),
|
|
|
+ pressurePercent(),
|
|
|
+ memoryPressureToString(pressure()),
|
|
|
stats.totalDocuments,
|
|
|
stats.evictedCount);
|
|
|
}
|