changeset 205:05154c7bb90b

* Basics of an approximate way of managing memory that we've committed to using but haven't allocated yet
author Chris Cannam
date Fri, 15 Dec 2006 18:05:31 +0000
parents 29b70bdaacdc
children a75e678f5d37
files base/StorageAdviser.cpp base/StorageAdviser.h data/fft/FFTDataServer.cpp
diffstat 3 files changed, 80 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/base/StorageAdviser.cpp	Tue Dec 12 10:41:41 2006 +0000
+++ b/base/StorageAdviser.cpp	Fri Dec 15 18:05:31 2006 +0000
@@ -22,6 +22,9 @@
 
 #include <iostream>
 
+long StorageAdviser::m_discPlanned = 0;
+long StorageAdviser::m_memoryPlanned = 0;
+
 StorageAdviser::Recommendation
 StorageAdviser::recommend(Criteria criteria,
 			  int minimumSize,
@@ -36,6 +39,18 @@
     int memoryFree, memoryTotal;
     GetRealMemoryMBAvailable(memoryFree, memoryTotal);
 
+    if (discFree > m_discPlanned / 1024 + 1) {
+        discFree -= m_discPlanned / 1024 + 1;
+    } else if (discFree > 0) { // can also be -1 for unknown
+        discFree = 0;
+    }
+
+    if (memoryFree > m_memoryPlanned / 1024 + 1) {
+        memoryFree -= m_memoryPlanned / 1024 + 1;
+    } else if (memoryFree > 0) { // can also be -1 for unknown
+        memoryFree = 0;
+    }
+
     std::cerr << "Disc space: " << discFree << ", memory free: " << memoryFree << ", memory total: " << memoryTotal << std::endl;
 
     //!!! We have a potentially serious problem here if multiple
@@ -147,3 +162,26 @@
     return Recommendation(recommendation);
 }
 
+void
+StorageAdviser::notifyPlannedAllocation(AllocationArea area, int size)
+{
+    if (area == MemoryAllocation) m_memoryPlanned += size;
+    else if (area == DiscAllocation) m_discPlanned += size;
+    std::cerr << "storage planned up: memory: " << m_memoryPlanned << ", disc "
+              << m_discPlanned << std::endl;
+}
+
+void
+StorageAdviser::notifyDoneAllocation(AllocationArea area, int size)
+{
+    if (area == MemoryAllocation) {
+        if (m_memoryPlanned > size) m_memoryPlanned -= size;
+        else m_memoryPlanned = 0;
+    } else if (area == DiscAllocation) {
+        if (m_discPlanned > size) m_discPlanned -= size; 
+        else m_discPlanned = 0;
+    }
+    std::cerr << "storage planned down: memory: " << m_memoryPlanned << ", disc "
+              << m_discPlanned << std::endl;
+}
+
--- a/base/StorageAdviser.h	Tue Dec 12 10:41:41 2006 +0000
+++ b/base/StorageAdviser.h	Fri Dec 15 18:05:31 2006 +0000
@@ -58,6 +58,28 @@
     static Recommendation recommend(Criteria criteria,
                                     int minimumSize,
                                     int maximumSize);
+
+    enum AllocationArea {
+        MemoryAllocation,
+        DiscAllocation
+    };
+
+    /**
+     * Specify that we are planning to use a given amount of storage
+     * (in kilobytes), but haven't allocated it yet.
+     */
+    static void notifyPlannedAllocation(AllocationArea area, int size);
+
+    /**
+     * Specify that we have now allocated, or abandoned the allocation
+     * of, the given amount (in kilobytes) of a storage area that was
+     * previously notified using notifyPlannedAllocation.
+     */
+    static void notifyDoneAllocation(AllocationArea area, int size);
+
+private:
+    static long m_discPlanned;
+    static long m_memoryPlanned;
 };
 
 #endif
--- a/data/fft/FFTDataServer.cpp	Tue Dec 12 10:41:41 2006 +0000
+++ b/data/fft/FFTDataServer.cpp	Fri Dec 15 18:05:31 2006 +0000
@@ -393,6 +393,11 @@
     std::cerr << "Width " << m_width << ", cache width " << m_cacheWidth << " (size " << m_cacheWidth * columnSize << ")" << std::endl;
 #endif
 
+    StorageAdviser::notifyPlannedAllocation
+        (m_memoryCache ? StorageAdviser::MemoryAllocation :
+                         StorageAdviser::DiscAllocation,
+         m_compactCache ? minimumSize : maximumSize);
+
     for (size_t i = 0; i <= m_width / m_cacheWidth; ++i) {
         m_caches.push_back(0);
     }
@@ -436,7 +441,15 @@
     QMutexLocker locker(&m_writeMutex);
 
     for (CacheVector::iterator i = m_caches.begin(); i != m_caches.end(); ++i) {
-        delete *i;
+        if (*i) {
+            delete *i;
+        } else {
+            StorageAdviser::notifyDoneAllocation
+                (m_memoryCache ? StorageAdviser::MemoryAllocation :
+                                 StorageAdviser::DiscAllocation,
+                 m_cacheWidth * m_height *
+                 (m_compactCache ? sizeof(uint16_t) : sizeof(float)) / 1024 + 1);
+        }
     }
 
     deleteProcessingData();
@@ -583,6 +596,12 @@
         cache->resize(width, m_height);
         cache->reset();
 
+        StorageAdviser::notifyDoneAllocation
+            (m_memoryCache ? StorageAdviser::MemoryAllocation :
+                             StorageAdviser::DiscAllocation,
+             width * m_height *
+             (m_compactCache ? sizeof(uint16_t) : sizeof(float)) / 1024 + 1);
+
     } catch (std::bad_alloc) {
         std::cerr << "ERROR: Memory allocation failed in FFTFileCache::resize:"
                   << " abandoning this cache" << std::endl;