From cd0c7d0e9f5aa802d5d1048b52c849c14dba57fc Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Sun, 7 Sep 2014 00:42:18 +0200 Subject: [PATCH] Write histogram to file specified by cli argument --- heaptrack_print.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/heaptrack_print.cpp b/heaptrack_print.cpp index e96d806..fa4b1ad 100644 --- a/heaptrack_print.cpp +++ b/heaptrack_print.cpp @@ -593,8 +593,8 @@ int main(int argc, char** argv) "Print backtraces to top allocators, sorted by number of calls to allocation functions.") ("print-leaks,l", po::value()->default_value(false)->implicit_value(true), "Print backtraces to leaked memory allocations.") - ("print-histogram,H", po::value()->default_value(false)->implicit_value(true), - "Print allocation size histogram.") + ("print-histogram,H", po::value()->default_value(string()), + "Path to output file where an allocation size histogram will be written to.") ("print-overall-allocated,o", po::value()->default_value(false)->implicit_value(true), "Print top overall allocators, ignoring memory frees.") ("help,h", @@ -627,7 +627,7 @@ int main(int argc, char** argv) const auto inputFile = vm["file"].as(); data.shortenTemplates = vm["shorten-templates"].as(); data.mergeBacktraces = vm["merge-backtraces"].as(); - const bool printHistogram = vm["print-histogram"].as(); + const string printHistogram = vm["print-histogram"].as(); const bool printLeaks = vm["print-leaks"].as(); const bool printOverallAlloc = vm["print-overall-allocated"].as(); const bool printPeaks = vm["print-peaks"].as(); @@ -706,10 +706,14 @@ int main(int argc, char** argv) << "peak heap memory consumption: " << data.peak << " bytes\n" << "total memory leaked: " << data.leaked << " bytes\n"; - if (printHistogram) { - cout << "size histogram: " << endl; - for (auto entry : data.sizeHistogram) { - cout << entry.first << "\t" << entry.second << endl; + if (!printHistogram.empty()) { + ofstream histogram(printHistogram, ios_base::out); + if (!histogram.is_open()) { + cerr << "Failed to open histogram output file \"" << histogram << "\"." << endl; + } else { + for (auto entry : data.sizeHistogram) { + histogram << entry.first << '\t' << entry.second << '\n'; + } } } -- 2.7.4