changeset 232:50a9c8eb4cac

Make memory statistics available
author Chris Cannam
date Fri, 10 Jun 2016 11:50:31 +0100
parents 55bdec565e9d
children 4b272c839f7e
files src/Matcher.cpp src/Matcher.h
diffstat 2 files changed, 57 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/Matcher.cpp	Wed Sep 09 09:58:12 2015 +0100
+++ b/src/Matcher.cpp	Fri Jun 10 11:50:31 2016 +0100
@@ -513,6 +513,29 @@
     return double(sz) / 1024.0;
 }
 
+Matcher::MemoryStats
+Matcher::getMemoryStats() const
+{
+    MemoryStats stats;
+    stats.features_k =
+        k(m_features.size() * m_features[0].size() * sizeof(featurebin_t));
+    
+    size_t cells = 0;
+    for (const auto &d: m_distance) {
+        cells += d.size();
+    }
+    
+    stats.pathcosts_k = k(cells * sizeof(pathcost_t));
+    stats.distances_k = k(cells * sizeof(distance_t));
+    stats.advances_k = k(cells * sizeof(advance_t));
+
+    if (m_firstPM && m_otherMatcher) {
+        stats = stats + m_otherMatcher->getMemoryStats();
+    }
+    
+    return stats;
+}
+
 void
 Matcher::printStats()
 {
@@ -524,8 +547,7 @@
     if (m_features.empty()) {
         cerr << "- have no features yet" << endl;
     } else {
-        cerr << "- have " << m_features.size() << " features of " << m_features[0].size() << " bins each (= "
-             << k(m_features.size() * m_features[0].size() * sizeof(featurebin_t)) << "K)" << endl;
+        cerr << "- have " << m_features.size() << " features of " << m_features[0].size() << " bins each" << endl;
     }
 
     size_t cells = 0;
@@ -538,13 +560,18 @@
         cerr << "- have " << m_distance.size() << " cols in matrix with avg "
              << double(cells) / double(m_distance.size()) << " rows, total "
              << cells << " cells" << endl;
-        cerr << "- path costs " << k(cells * sizeof(pathcost_t))
-             << "K, distances " << k(cells * sizeof(distance_t))
-             << "K, advances " << k(cells * sizeof(advance_t)) << "K" << endl;
     }
 
     if (m_firstPM && m_otherMatcher) {
         m_otherMatcher->printStats();
+        MemoryStats stats = getMemoryStats();
+        cerr << "Memory: "
+             << "features " << stats.features_k << "K, "
+             << "path costs " << stats.pathcosts_k << "K, "
+             << "distances " << stats.distances_k << "K,\n        "
+             << "advances " << stats.advances_k << "K, "
+             << "total " << stats.total_k() << "K"
+             << endl;
         cerr << endl;
     }
 }
--- a/src/Matcher.h	Wed Sep 09 09:58:12 2015 +0100
+++ b/src/Matcher.h	Fri Jun 10 11:50:31 2016 +0100
@@ -255,6 +255,20 @@
      */
     advance_t getAdvance(int i, int j);
 
+    struct MemoryStats {
+        double features_k;
+        double pathcosts_k;
+        double distances_k;
+        double advances_k;
+        double total_k() const {
+            return features_k + pathcosts_k + distances_k + advances_k;
+        }
+    };
+    
+    /** Obtain some stats about memory consumption.
+     */
+    MemoryStats getMemoryStats() const;
+    
     /** Print some stats about memory consumption etc to stderr.
      */
     void printStats();
@@ -339,4 +353,15 @@
     DistanceMetric m_metric;
 };
 
+inline Matcher::MemoryStats operator+(const Matcher::MemoryStats &a,
+                                      const Matcher::MemoryStats &b)
+{
+    Matcher::MemoryStats m = a;
+    m.features_k += b.features_k;
+    m.pathcosts_k += b.pathcosts_k;
+    m.distances_k += b.distances_k;
+    m.advances_k += b.advances_k;
+    return m;
+}
+
 #endif