Find temporary allocations in heaptrack_print.
authorMilian Wolff <mail@milianw.de>
Thu, 10 Dec 2015 14:35:43 +0000 (15:35 +0100)
committerMilian Wolff <mail@milianw.de>
Thu, 10 Dec 2015 14:35:43 +0000 (15:35 +0100)
accumulatedtracedata.cpp
accumulatedtracedata.h
heaptrack_print.cpp

index c734d93..c95b776 100644 (file)
@@ -152,10 +152,12 @@ bool AccumulatedTraceData::read(istream& in)
     m_maxAllocationTraceIndex.index = 0;
     totalAllocated = 0;
     totalAllocations = 0;
+    totalTemporary = 0;
     peak = 0;
     leaked = 0;
     allocations.clear();
     sizeHistogram.clear();
+    uint64_t lastAllocationPtr = 0;
 
     while (reader.getLine(in)) {
         if (reader.mode() == 's') {
@@ -235,6 +237,7 @@ bool AccumulatedTraceData::read(istream& in)
             if (leaked > peak) {
                 peak = leaked;
             }
+            lastAllocationPtr = ptr;
             handleAllocation();
             if (printHistogram) {
                 ++sizeHistogram[size];
@@ -262,6 +265,11 @@ bool AccumulatedTraceData::read(istream& in)
                 allocation.leaked -= info.size;
             }
             leaked -= info.size;
+            if (lastAllocationPtr == ptr) {
+                ++allocation.temporary;
+                ++totalTemporary;
+            }
+            lastAllocationPtr = 0;
         } else if (reader.mode() == '#') {
             // comment or empty line
             continue;
index f5c2709..2be87a1 100644 (file)
@@ -105,6 +105,8 @@ struct AllocationData
     uint64_t leaked = 0;
     // largest amount of bytes allocated
     uint64_t peak = 0;
+    // number of temporary allocations
+    uint64_t temporary = 0;
 };
 
 struct Allocation : public AllocationData
@@ -173,6 +175,7 @@ struct AccumulatedTraceData
     std::map<uint64_t, uint64_t> sizeHistogram;
     uint64_t totalAllocated = 0;
     uint64_t totalAllocations = 0;
+    uint64_t totalTemporary = 0;
     uint64_t peak = 0;
     uint64_t leaked = 0;
     uint64_t totalTime = 0;
index dc88276..3fe2135 100644 (file)
@@ -126,6 +126,7 @@ struct Printer final : public AccumulatedTraceData
                 merged.allocations += allocation.allocations;
                 merged.leaked += allocation.leaked;
                 merged.peak += allocation.peak;
+                merged.temporary += allocation.temporary;
             }
         }
         return ret;
@@ -482,6 +483,8 @@ int main(int argc, char** argv)
             "Print backtraces to top allocators, sorted by peak consumption.")
         ("print-allocators,a", po::value<bool>()->default_value(true)->implicit_value(true),
             "Print backtraces to top allocators, sorted by number of calls to allocation functions.")
+        ("print-temporary,T", po::value<bool>()->default_value(true)->implicit_value(true),
+            "Print backtraces to top allocators, sorted by number of temporary allocations.")
         ("print-leaks,l", po::value<bool>()->default_value(false)->implicit_value(true),
             "Print backtraces to leaked memory allocations.")
         ("print-overall-allocated,o", po::value<bool>()->default_value(false)->implicit_value(true),
@@ -567,6 +570,7 @@ int main(int argc, char** argv)
     const bool printOverallAlloc = vm["print-overall-allocated"].as<bool>();
     const bool printPeaks = vm["print-peaks"].as<bool>();
     const bool printAllocs = vm["print-allocators"].as<bool>();
+    const bool printTemporary = vm["print-temporary"].as<bool>();
 
     cout << "reading file \"" << inputFile << "\" - please wait, this might take some time..." << endl;
     if (!data.read(inputFile)) {
@@ -628,12 +632,27 @@ int main(int argc, char** argv)
         cout << endl;
     }
 
+    if (printTemporary) {
+        // sort by amount of temporary allocations
+        cout << "MOST TEMPORARY ALLOCATIONS\n";
+        data.printAllocations(&AllocationData::temporary, [] (const AllocationData& data) {
+            cout << data.temporary << " temporary allocations of " << data.allocations << " allocations in total ("
+                 << setprecision(2) << (float(data.temporary)  * 100.f / data.allocations) << "%) from\n";
+        }, [] (const AllocationData& data) {
+            cout << data.temporary << " temporary allocations of " << data.allocations << " allocations in total ("
+                 << setprecision(2) << (float(data.temporary)  * 100.f / data.allocations) << "%) from:\n";
+        });
+        cout << endl;
+    }
+
     const double totalTimeS = 0.001 * data.totalTime;
     cout << "total runtime: " << fixed << totalTimeS << "s.\n"
          << "bytes allocated in total (ignoring deallocations): " << formatBytes(data.totalAllocated)
             << " (" << formatBytes(data.totalAllocated / totalTimeS) << "/s)" << '\n'
          << "calls to allocation functions: " << data.totalAllocations
             << " (" << size_t(data.totalAllocations / totalTimeS) << "/s)\n"
+         << "temporary memory allocations: " << data.totalTemporary
+            << " (" << size_t(data.totalTemporary / totalTimeS) << "/s)\n"
          << "peak heap memory consumption: " << formatBytes(data.peak) << '\n'
          << "total memory leaked: " << formatBytes(data.leaked) << '\n';