use KFormat for byte formatting in Qt model implementation.
authorMilian Wolff <mail@milianw.de>
Sun, 7 Jun 2015 18:50:21 +0000 (20:50 +0200)
committerMilian Wolff <mail@milianw.de>
Sun, 7 Jun 2015 18:50:21 +0000 (20:50 +0200)
accumulatedtracedata.cpp
accumulatedtracedata.h
gui/model.cpp
heaptrack_print.cpp

index 2e097aef32a1631e29283d4c9bb7ba4f10fabfc1..fd936b2a9170f08389123bd7af0fc183dee8943a 100644 (file)
@@ -23,7 +23,6 @@
 #include <memory>
 #include <algorithm>
 #include <cassert>
-#include <iomanip>
 
 #include <boost/iostreams/filtering_stream.hpp>
 #include <boost/iostreams/filter/gzip.hpp>
 
 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<typename Base>
index bdb2e7379404e74aa35650229489777314f185d4..75d2dc527039a3cfc01d8bb3197567c992b5b61a 100644 (file)
 #include <unordered_map>
 #include <map>
 
-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<typename Base>
 struct Index
index 5c146eb94c1c19f501a68041fa4c22da68e1beae..99a27f4ce49451efcac1b6e01a19e16358e2169f 100644 (file)
@@ -20,7 +20,9 @@
 #include "model.h"
 
 #include <QDebug>
+#include <QTextStream>
 
+#include <KFormat>
 #include <ThreadWeaver/ThreadWeaver>
 
 #include <sstream>
@@ -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 << "<qt>"
-           << "<strong>total runtime</strong>: " << fixed << totalTimeS << "s.<br/>"
-           << "<strong>bytes allocated in total</strong> (ignoring deallocations): " << formatBytes(data.totalAllocated)
-             << " (" << formatBytes(data.totalAllocated / totalTimeS) << "/s)<br/>"
+           << "<strong>total runtime</strong>: " << totalTimeS << "s.<br/>"
+           << "<strong>bytes allocated in total</strong> (ignoring deallocations): " << format.formatByteSize(data.totalAllocated, 2)
+             << " (" << format.formatByteSize(data.totalAllocated / totalTimeS) << "/s)<br/>"
            << "<strong>calls to allocation functions</strong>: " << data.totalAllocations
              << " (" << size_t(data.totalAllocations / totalTimeS) << "/s)<br/>"
-           << "<strong>peak heap memory consumption</strong>: " << formatBytes(data.peak) << "<br/>"
-           << "<strong>total memory leaked</strong>: " << formatBytes(data.leaked) << "<br/>";
+           << "<strong>peak heap memory consumption</strong>: " << format.formatByteSize(data.peak) << "<br/>"
+           << "<strong>total memory leaked</strong>: " << format.formatByteSize(data.leaked) << "<br/>";
     stream << "</qt>";
-    return QString::fromStdString(stream.str());
+    return ret;
 }
 
 int parentRow(const QModelIndex& child)
index 53c81a2ecae999192df631f1d0d6b2b1edabce71..920d7a4fbdb2721c3f3b5e3acd7404cd8d2257bf 100644 (file)
 #include "accumulatedtracedata.h"
 
 #include <iostream>
+#include <iomanip>
 
 #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<typename T, typename LabelPrinter, typename SubLabelPrinter>
@@ -246,6 +288,7 @@ struct Printer final : public AccumulatedTraceData
     size_t lastMassifPeak = 0;
     vector<Allocation> massifAllocations;
 };
+}
 
 int main(int argc, char** argv)
 {