Index: source/network/NetStats.cpp =================================================================== --- source/network/NetStats.cpp +++ source/network/NetStats.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -21,6 +21,31 @@ #include "lib/external_libraries/enet.h" +#include +#include + +namespace +{ +float PacketLossNormalize(const enet_uint32 v) noexcept +{ + static_assert(ENET_PEER_PACKET_LOSS_SCALE != 0); + return static_cast(v) / static_cast(ENET_PEER_PACKET_LOSS_SCALE); +} + +std::string PacketLossStats(const enet_uint32 mean, const enet_uint32 variance) +{ + // {:5.2} shows 5 digits with 2 after the decimal point, this is enough to represent a percentage + // with 2 digits for precision and does not change the text width + return fmt::format("{:5.2f} +/- {:5.2f} %", PacketLossNormalize(mean), PacketLossNormalize(variance)); +} + +std::string FormatRTT(const enet_uint32 time, const enet_uint32 variance) +{ + // Use `{:4}` so the "+/-" of the package loss and the rtt is aligned. + return fmt::format("{} +/- {:4} ms", time, variance); +} +} + enum { Row_InData, @@ -30,8 +55,8 @@ Row_NextTimeout, Row_PacketsSent, Row_PacketsLost, - Row_LastRTT, Row_RTT, + Row_LastRTT, Row_MTU, Row_ReliableInTransit, NumberRows @@ -71,13 +96,13 @@ m_ColumnDescriptions.push_back(ProfileColumn("Name", 200)); if (m_Peer) - m_ColumnDescriptions.push_back(ProfileColumn("Value", 80)); + m_ColumnDescriptions.push_back(ProfileColumn("Value", 108)); else { std::lock_guard lock(m_Mutex); for (size_t i = 0; i < m_LatchedData.size(); ++i) - m_ColumnDescriptions.push_back(ProfileColumn("Peer "+CStr::FromUInt(i), 80)); + m_ColumnDescriptions.push_back(ProfileColumn("Peer "+CStr::FromUInt(i), 108)); } return m_ColumnDescriptions; @@ -106,9 +131,23 @@ ROW(Row_LastRecvTime, "last receive time", lastReceiveTime); ROW(Row_NextTimeout, "next timeout", nextTimeout); ROW(Row_PacketsSent, "packets sent", packetsSent); - ROW(Row_PacketsLost, "packets lost", packetsLost); - ROW(Row_LastRTT, "last RTT", lastRoundTripTime); - ROW(Row_RTT, "mean RTT", roundTripTime); + case Row_PacketsLost: + if (col == 0) return "packet lost"; + + if (m_Peer) return PacketLossStats(m_Peer->packetLoss, m_Peer->packetLossVariance); + return "???"; + case Row_RTT: + if (col == 0) return "mean RTT"; + + if (m_Peer) return FormatRTT(m_Peer->roundTripTime, m_Peer->roundTripTimeVariance); + + return "???"; + case Row_LastRTT: + if (col == 0) return "last RTT"; + + if (m_Peer) return FormatRTT(m_Peer->lastRoundTripTime, m_Peer->lastRoundTripTimeVariance); + + return "???"; ROW(Row_MTU, "MTU", mtu); ROW(Row_ReliableInTransit, "reliable data in transit", reliableDataInTransit); @@ -142,9 +181,13 @@ ROW(Row_LastRecvTime, "last receive time", lastReceiveTime); ROW(Row_NextTimeout, "next timeout", nextTimeout); ROW(Row_PacketsSent, "packets sent", packetsSent); - ROW(Row_PacketsLost, "packets lost", packetsLost); - ROW(Row_LastRTT, "last RTT", lastRoundTripTime); - ROW(Row_RTT, "mean RTT", roundTripTime); + + m_LatchedData[i].push_back(PacketLossStats(host->peers[i].packetLoss, host->peers[i].packetLossVariance)); + // RTT stats + m_LatchedData[i].push_back( + FormatRTT(host->peers[i].roundTripTime, host->peers[i].roundTripTimeVariance)); + m_LatchedData[i].push_back( + FormatRTT(host->peers[i].lastRoundTripTime, host->peers[i].lastRoundTripTimeVariance)); ROW(Row_MTU, "MTU", mtu); ROW(Row_ReliableInTransit, "reliable data in transit", reliableDataInTransit); }