From 0f118362c8946ef33320bf7a5c8af274d21849fe Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Sun, 7 Jun 2015 20:50:21 +0200 Subject: [PATCH] use KFormat for byte formatting in Qt model implementation. --- accumulatedtracedata.cpp | 26 ------------------------ accumulatedtracedata.h | 14 ------------- gui/model.cpp | 18 ++++++++++------- heaptrack_print.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/accumulatedtracedata.cpp b/accumulatedtracedata.cpp index 2e097ae..fd936b2 100644 --- a/accumulatedtracedata.cpp +++ b/accumulatedtracedata.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include @@ -33,31 +32,6 @@ using namespace std; -ostream& operator<<(ostream& out, const formatBytes data) -{ - if (data.m_bytes < 1000) { - // no fancy formatting for plain byte values, esp. no .00 factions - return out << data.m_bytes << 'B'; - } - - static const auto units = { - "B", - "KB", - "MB", - "GB", - "TB" - }; - auto unit = units.begin(); - size_t i = 0; - double bytes = data.m_bytes; - while (i < units.size() - 1 && bytes > 1000.) { - bytes /= 1000.; - ++i; - ++unit; - } - return out << fixed << setprecision(2) << bytes << *unit; -} - namespace { template diff --git a/accumulatedtracedata.h b/accumulatedtracedata.h index bdb2e73..75d2dc5 100644 --- a/accumulatedtracedata.h +++ b/accumulatedtracedata.h @@ -28,20 +28,6 @@ #include #include -class formatBytes -{ -public: - formatBytes(size_t bytes) - : m_bytes(bytes) - { - } - - friend std::ostream& operator<<(std::ostream& out, const formatBytes data); - -private: - size_t m_bytes; -}; - // sadly, C++ doesn't yet have opaque typedefs template struct Index diff --git a/gui/model.cpp b/gui/model.cpp index 5c146eb..99a27f4 100644 --- a/gui/model.cpp +++ b/gui/model.cpp @@ -20,7 +20,9 @@ #include "model.h" #include +#include +#include #include #include @@ -30,18 +32,20 @@ using namespace std; namespace { QString generateSummary(const AccumulatedTraceData& data) { - stringstream stream; + QString ret; + KFormat format; + QTextStream stream(&ret); const double totalTimeS = 0.001 * data.totalTime; stream << "" - << "total runtime: " << fixed << totalTimeS << "s.
" - << "bytes allocated in total (ignoring deallocations): " << formatBytes(data.totalAllocated) - << " (" << formatBytes(data.totalAllocated / totalTimeS) << "/s)
" + << "total runtime: " << totalTimeS << "s.
" + << "bytes allocated in total (ignoring deallocations): " << format.formatByteSize(data.totalAllocated, 2) + << " (" << format.formatByteSize(data.totalAllocated / totalTimeS) << "/s)
" << "calls to allocation functions: " << data.totalAllocations << " (" << size_t(data.totalAllocations / totalTimeS) << "/s)
" - << "peak heap memory consumption: " << formatBytes(data.peak) << "
" - << "total memory leaked: " << formatBytes(data.leaked) << "
"; + << "peak heap memory consumption: " << format.formatByteSize(data.peak) << "
" + << "total memory leaked: " << format.formatByteSize(data.leaked) << "
"; stream << "
"; - return QString::fromStdString(stream.str()); + return ret; } int parentRow(const QModelIndex& child) diff --git a/heaptrack_print.cpp b/heaptrack_print.cpp index 53c81a2..920d7a4 100644 --- a/heaptrack_print.cpp +++ b/heaptrack_print.cpp @@ -28,12 +28,54 @@ #include "accumulatedtracedata.h" #include +#include #include "config.h" using namespace std; namespace po = boost::program_options; +namespace { + +class formatBytes +{ +public: + formatBytes(size_t bytes) + : m_bytes(bytes) + { + } + + friend std::ostream& operator<<(std::ostream& out, const formatBytes data); + +private: + size_t m_bytes; +}; + +ostream& operator<<(ostream& out, const formatBytes data) +{ + if (data.m_bytes < 1000) { + // no fancy formatting for plain byte values, esp. no .00 factions + return out << data.m_bytes << 'B'; + } + + static const auto units = { + "B", + "KB", + "MB", + "GB", + "TB" + }; + auto unit = units.begin(); + size_t i = 0; + double bytes = data.m_bytes; + while (i < units.size() - 1 && bytes > 1000.) { + bytes /= 1000.; + ++i; + ++unit; + } + return out << fixed << setprecision(2) << bytes << *unit; +} + struct Printer final : public AccumulatedTraceData { template @@ -246,6 +288,7 @@ struct Printer final : public AccumulatedTraceData size_t lastMassifPeak = 0; vector massifAllocations; }; +} int main(int argc, char** argv) { -- 2.34.1