Refactor caching and memory management components
All checks were successful
Release Tag / release (push) Successful in 9s
All checks were successful
Release Tag / release (push) Successful in 9s
- Updated the caching logic to utilize a predictive cache warmer, enhancing content prefetching based on access patterns. - Replaced the legacy warming system with a more efficient predictive approach, allowing for better performance and resource management. - Refactored memory management to integrate dynamic cache size adjustments based on system memory usage, improving overall efficiency. - Simplified the VFS interface and improved concurrency handling with sharded locks for better performance in multi-threaded environments. - Enhanced tests to validate the new caching and memory management behaviors, ensuring reliability and performance improvements.
This commit is contained in:
@@ -17,6 +17,15 @@ type MemoryMonitor struct {
|
||||
ctx chan struct{}
|
||||
stopChan chan struct{}
|
||||
isMonitoring int32
|
||||
|
||||
// Dynamic cache management fields
|
||||
originalCacheSize uint64
|
||||
currentCacheSize uint64
|
||||
cache interface{} // Generic cache interface
|
||||
adjustmentInterval time.Duration
|
||||
lastAdjustment time.Time
|
||||
adjustmentCount int64
|
||||
isAdjusting int32
|
||||
}
|
||||
|
||||
// NewMemoryMonitor creates a new memory monitor
|
||||
@@ -27,9 +36,19 @@ func NewMemoryMonitor(targetMemoryUsage uint64, monitoringInterval time.Duration
|
||||
adjustmentThreshold: adjustmentThreshold,
|
||||
ctx: make(chan struct{}),
|
||||
stopChan: make(chan struct{}),
|
||||
adjustmentInterval: 30 * time.Second, // Default adjustment interval
|
||||
}
|
||||
}
|
||||
|
||||
// NewMemoryMonitorWithCache creates a new memory monitor with cache management
|
||||
func NewMemoryMonitorWithCache(targetMemoryUsage uint64, monitoringInterval time.Duration, adjustmentThreshold float64, cache interface{}, originalCacheSize uint64) *MemoryMonitor {
|
||||
mm := NewMemoryMonitor(targetMemoryUsage, monitoringInterval, adjustmentThreshold)
|
||||
mm.cache = cache
|
||||
mm.originalCacheSize = originalCacheSize
|
||||
mm.currentCacheSize = originalCacheSize
|
||||
return mm
|
||||
}
|
||||
|
||||
// Start begins monitoring memory usage
|
||||
func (mm *MemoryMonitor) Start() {
|
||||
if atomic.CompareAndSwapInt32(&mm.isMonitoring, 0, 1) {
|
||||
@@ -151,3 +170,105 @@ func (mm *MemoryMonitor) GetMemoryStats() map[string]interface{} {
|
||||
"gc_pause_total": m.PauseTotalNs,
|
||||
}
|
||||
}
|
||||
|
||||
// Dynamic Cache Management Methods
|
||||
|
||||
// StartDynamicAdjustment begins the dynamic cache size adjustment process
|
||||
func (mm *MemoryMonitor) StartDynamicAdjustment() {
|
||||
if mm.cache != nil {
|
||||
go mm.adjustmentLoop()
|
||||
}
|
||||
}
|
||||
|
||||
// GetCurrentCacheSize returns the current cache size
|
||||
func (mm *MemoryMonitor) GetCurrentCacheSize() uint64 {
|
||||
mm.mu.RLock()
|
||||
defer mm.mu.RUnlock()
|
||||
return atomic.LoadUint64(&mm.currentCacheSize)
|
||||
}
|
||||
|
||||
// GetOriginalCacheSize returns the original cache size
|
||||
func (mm *MemoryMonitor) GetOriginalCacheSize() uint64 {
|
||||
mm.mu.RLock()
|
||||
defer mm.mu.RUnlock()
|
||||
return mm.originalCacheSize
|
||||
}
|
||||
|
||||
// GetAdjustmentCount returns the number of adjustments made
|
||||
func (mm *MemoryMonitor) GetAdjustmentCount() int64 {
|
||||
return atomic.LoadInt64(&mm.adjustmentCount)
|
||||
}
|
||||
|
||||
// adjustmentLoop runs the cache size adjustment loop
|
||||
func (mm *MemoryMonitor) adjustmentLoop() {
|
||||
ticker := time.NewTicker(mm.adjustmentInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
mm.performAdjustment()
|
||||
}
|
||||
}
|
||||
|
||||
// performAdjustment performs a cache size adjustment if needed
|
||||
func (mm *MemoryMonitor) performAdjustment() {
|
||||
// Prevent concurrent adjustments
|
||||
if !atomic.CompareAndSwapInt32(&mm.isAdjusting, 0, 1) {
|
||||
return
|
||||
}
|
||||
defer atomic.StoreInt32(&mm.isAdjusting, 0)
|
||||
|
||||
// Check if enough time has passed since last adjustment
|
||||
if time.Since(mm.lastAdjustment) < mm.adjustmentInterval {
|
||||
return
|
||||
}
|
||||
|
||||
// Get recommended cache size
|
||||
recommendedSize := mm.GetRecommendedCacheSize(mm.originalCacheSize)
|
||||
currentSize := atomic.LoadUint64(&mm.currentCacheSize)
|
||||
|
||||
// Only adjust if there's a significant difference (more than 5%)
|
||||
sizeDiff := float64(recommendedSize) / float64(currentSize)
|
||||
if sizeDiff < 0.95 || sizeDiff > 1.05 {
|
||||
mm.adjustCacheSize(recommendedSize)
|
||||
mm.lastAdjustment = time.Now()
|
||||
atomic.AddInt64(&mm.adjustmentCount, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// adjustCacheSize adjusts the cache size to the recommended size
|
||||
func (mm *MemoryMonitor) adjustCacheSize(newSize uint64) {
|
||||
mm.mu.Lock()
|
||||
defer mm.mu.Unlock()
|
||||
|
||||
oldSize := atomic.LoadUint64(&mm.currentCacheSize)
|
||||
atomic.StoreUint64(&mm.currentCacheSize, newSize)
|
||||
|
||||
// If we're reducing the cache size, trigger GC to free up memory
|
||||
if newSize < oldSize {
|
||||
// Calculate how much to free
|
||||
bytesToFree := oldSize - newSize
|
||||
|
||||
// Trigger GC on the cache to free up the excess memory
|
||||
// This is a simplified approach - in practice, you'd want to integrate
|
||||
// with the actual GC system to free the right amount
|
||||
if gcCache, ok := mm.cache.(interface{ ForceGC(uint) }); ok {
|
||||
gcCache.ForceGC(uint(bytesToFree))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetDynamicStats returns statistics about the dynamic cache manager
|
||||
func (mm *MemoryMonitor) GetDynamicStats() map[string]interface{} {
|
||||
mm.mu.RLock()
|
||||
defer mm.mu.RUnlock()
|
||||
|
||||
return map[string]interface{}{
|
||||
"original_cache_size": mm.originalCacheSize,
|
||||
"current_cache_size": atomic.LoadUint64(&mm.currentCacheSize),
|
||||
"adjustment_count": atomic.LoadInt64(&mm.adjustmentCount),
|
||||
"last_adjustment": mm.lastAdjustment,
|
||||
"memory_utilization": mm.GetMemoryUtilization(),
|
||||
"target_memory_usage": mm.GetTargetMemoryUsage(),
|
||||
"current_memory_usage": mm.GetCurrentMemoryUsage(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user