From c1a692b69de89cc1d7ee7f4e4faabc117f766bff Mon Sep 17 00:00:00 2001 From: R E Broadley Date: Thu, 29 Apr 2021 18:03:43 +0100 Subject: [PATCH] Display send/recv in bps instead of totals in the debug window --- src/net.h | 1 - src/qt/guiutil.cpp | 34 ++++++++++++++++++++++++++++++++++ src/qt/guiutil.h | 1 + src/qt/peertablemodel.cpp | 20 +++++++++++++++----- src/qt/peertablemodel.h | 8 ++++---- src/qt/peertablesortproxy.cpp | 17 +++++++++++++---- 6 files changed, 67 insertions(+), 14 deletions(-) diff --git a/src/net.h b/src/net.h index 0a72ca888db..c6fc5ef830d 100644 --- a/src/net.h +++ b/src/net.h @@ -419,7 +419,6 @@ class CNode RecursiveMutex cs_sendProcessing; uint64_t nRecvBytes GUARDED_BY(cs_vRecv){0}; - std::atomic nLastSend{0}; std::atomic nLastRecv{0}; //! Unix epoch time at peer connection, in seconds. diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index e98e50ba148..85eb35fdabc 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -781,6 +781,40 @@ QString formatBytes(uint64_t bytes) return QObject::tr("%1 GB").arg(bytes / 1'000'000'000); } +QString formatBps(float bits) +{ + if (bits < 10) + //: "Bits per second" + return QObject::tr("%1 bps").arg(0.01 * int(bits * 100)); + if (bits < 100) + //: "Bits per second" + return QObject::tr("%1 bps").arg(0.1 * int(bits * 10)); + if (bits < 1'000) + //: "Bits per second" + return QObject::tr("%1 bps").arg((int)bits); + if (bits < 10'000) + //: "Kilobits per second" + return QObject::tr("%1 kbps").arg(0.01 * ((int)bits / 10)); + if (bits < 100'000) + //: "Kilobits per second" + return QObject::tr("%1 kbps").arg(0.1 * ((int)bits / 100)); + if (bits < 1'000'000) + //: "Kilobits per second" + return QObject::tr("%1 kbps").arg((int)bits / 1'000); + if (bits < 10'000'000) + //: "Megabits per second" + return QObject::tr("%1 Mbps").arg(0.01 * ((int)bits / 10'000)); + if (bits < 100'000'000) + //: "Megabits per second" + return QObject::tr("%1 Mbps").arg(0.1 * ((int)bits / 100'000)); + if (bits < 10'000'000'000) + //: "Megabits per second" + return QObject::tr("%1 Mbps").arg((long)bits / 1'000'000); + + //: "Gigabits per second" + return QObject::tr("%1 Gbps").arg((long)bits / 1'000'000'000); +} + qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize, qreal font_size) { while(font_size >= minPointSize) { font.setPointSizeF(font_size); diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index 06a3b636686..f771426a2ac 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -229,6 +229,7 @@ namespace GUIUtil QString formatNiceTimeOffset(qint64 secs); QString formatBytes(uint64_t bytes); + QString formatBps(float bits); qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal minPointSize = 4, qreal startPointSize = 14); diff --git a/src/qt/peertablemodel.cpp b/src/qt/peertablemodel.cpp index 433a1ea934e..d8fe98bc6b4 100644 --- a/src/qt/peertablemodel.cpp +++ b/src/qt/peertablemodel.cpp @@ -85,10 +85,20 @@ QVariant PeerTableModel::data(const QModelIndex& index, int role) const return GUIUtil::NetworkToQString(rec->nodeStats.m_network); case Ping: return GUIUtil::formatPingTime(rec->nodeStats.m_min_ping_time); - case Sent: - return GUIUtil::formatBytes(rec->nodeStats.nSendBytes); - case Received: - return GUIUtil::formatBytes(rec->nodeStats.nRecvBytes); + case Sent: { + int64_t now = GetTimeSeconds(); + if (now != rec->nodeStats.nTimeConnected) // Avoid division by zero + return GUIUtil::formatBps(rec->nodeStats.nSendBytes * 8.0 / (now - rec->nodeStats.nTimeConnected)); + else + return QString::fromStdString(""); + } + case Recv: { + int64_t now = GetTimeSeconds(); + if (rec->nodeStats.nTimeConnected != now) + return GUIUtil::formatBps(rec->nodeStats.nRecvBytes * 8.0 / (now - rec->nodeStats.nTimeConnected)); + else + return QString::fromStdString(""); + } case Subversion: return QString::fromStdString(rec->nodeStats.cleanSubVer); } // no default case, so the compiler can warn about missing cases @@ -105,7 +115,7 @@ QVariant PeerTableModel::data(const QModelIndex& index, int role) const return QVariant(Qt::AlignCenter); case Ping: case Sent: - case Received: + case Recv: return QVariant(Qt::AlignRight | Qt::AlignVCenter); case Subversion: return {}; diff --git a/src/qt/peertablemodel.h b/src/qt/peertablemodel.h index 40265ee2668..36adbc81d8d 100644 --- a/src/qt/peertablemodel.h +++ b/src/qt/peertablemodel.h @@ -53,7 +53,7 @@ class PeerTableModel : public QAbstractTableModel Network, Ping, Sent, - Received, + Recv, Subversion }; @@ -97,12 +97,12 @@ public Q_SLOTS: /*: Title of Peers Table column which indicates the current latency of the connection with the peer. */ tr("Ping"), - /*: Title of Peers Table column which indicates the total amount of + /*: Title of Peers Table column which indicates the speed of network information we have sent to the peer. */ tr("Sent"), - /*: Title of Peers Table column which indicates the total amount of + /*: Title of Peers Table column which indicates the speed of network information we have received from the peer. */ - tr("Received"), + tr("Recv"), /*: Title of Peers Table column which contains the peer's User Agent string. */ tr("User Agent")}; diff --git a/src/qt/peertablesortproxy.cpp b/src/qt/peertablesortproxy.cpp index 419133bc32f..c416b15e4a3 100644 --- a/src/qt/peertablesortproxy.cpp +++ b/src/qt/peertablesortproxy.cpp @@ -34,10 +34,19 @@ bool PeerTableSortProxy::lessThan(const QModelIndex& left_index, const QModelInd return left_stats.m_network < right_stats.m_network; case PeerTableModel::Ping: return left_stats.m_min_ping_time < right_stats.m_min_ping_time; - case PeerTableModel::Sent: - return left_stats.nSendBytes < right_stats.nSendBytes; - case PeerTableModel::Received: - return left_stats.nRecvBytes < right_stats.nRecvBytes; + case PeerTableModel::Sent: { + int64_t now = GetTimeSeconds(); + int Right = right_stats.nSendBytes * 8 / (now + 1 - right_stats.nTimeConnected); + int Left = left_stats.nSendBytes * 8 / (now + 1 - left_stats.nTimeConnected); + return Left < Right; + } + case PeerTableModel::Recv: { + int64_t now = GetTimeSeconds(); + int Right = right_stats.nRecvBytes * 8 / (now + 1 - right_stats.nTimeConnected); + int Left = left_stats.nRecvBytes * 8 / (now + 1 - left_stats.nTimeConnected); + return Left < Right; + } + case PeerTableModel::Subversion: return left_stats.cleanSubVer.compare(right_stats.cleanSubVer) < 0; } // no default case, so the compiler can warn about missing cases