Skip to content

Commit

Permalink
Network|libcore: More detailed network traffic statistics
Browse files Browse the repository at this point in the history
"net info" will now show more accurate statistics, and the current
output rate.
  • Loading branch information
skyjake committed Oct 15, 2017
1 parent 6aaa6f5 commit 0736e2d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doomsday/apps/client/include/network/net_buf.h
Expand Up @@ -87,7 +87,7 @@ int N_IdentifyPlayer(nodeid_t id);
void N_PrintBufferInfo(void);
void N_PrintTransmissionStats(void);
void N_PostMessage(netmessage_t *msg);
void N_AddSentBytes(size_t bytes);
//void N_AddSentBytes(size_t bytes);

#ifdef __cplusplus
} // extern "C"
Expand Down
32 changes: 19 additions & 13 deletions doomsday/apps/client/src/network/base/net_buf.cpp
Expand Up @@ -58,10 +58,10 @@ static dint entryCount;
static mutex_t msgMutex;

// Number of bytes of outgoing data transmitted.
static dsize numOutBytes;
//static dsize numOutBytes;

// Number of bytes sent over the network (compressed).
static dsize numSentBytes;
//static dsize numSentBytes;

reader_s *Reader_NewWithNetworkBuffer()
{
Expand Down Expand Up @@ -293,7 +293,7 @@ void N_SendPacket(dint flags)
#endif

// This is what will be sent.
::numOutBytes += ::netBuffer.headerLength + ::netBuffer.length;
//::numOutBytes += ::netBuffer.headerLength + ::netBuffer.length;

try
{
Expand All @@ -311,10 +311,10 @@ void N_SendPacket(dint flags)
}
}

void N_AddSentBytes(dsize bytes)
{
::numSentBytes += bytes;
}
//void N_AddSentBytes(dsize bytes)
//{
// ::numSentBytes += bytes;
//}

/**
* @return The player number that corresponds network node @a id.
Expand Down Expand Up @@ -415,15 +415,21 @@ void N_PrintBufferInfo()
*/
void N_PrintTransmissionStats()
{
if(::numOutBytes == 0)
auto const sentBytes = Socket::sentUncompressedBytes();
auto const outBytes = Socket::sentBytes();
auto const outRate = Socket::outputBytesPerSecond();

if (outBytes == 0)
{
LOG_NET_MSG("Transmission efficiency: Nothing has been sent yet");
LOG_NET_MSG("Nothing has been sent yet over the network");
}
else
{
LOG_NET_MSG("Transmission efficiency: %.3f%% (data: %i bytes, sent: %i bytes)")
<< (100 - (100.0f * ::numSentBytes) / ::numOutBytes)
<< ::numOutBytes
<< ::numSentBytes;
LOG_NET_MSG("Average compression: %.3f%% (data: %.1f KB, out: %.1f KB)\n"
"Current output: %.1f KB/s")
<< 100 * (1.0 - double(outBytes) / double(sentBytes))
<< sentBytes/1000.0
<< outBytes/1000.0
<< outRate/1000.0;
}
}
6 changes: 6 additions & 0 deletions doomsday/sdk/libcore/include/de/net/socket.h
Expand Up @@ -218,6 +218,12 @@ class DENG2_PUBLIC Socket : public QObject, public Transmitter
*/
void setQuiet(bool noLogOutput);

// Statistics:
static void resetCounters();
static duint64 sentUncompressedBytes();
static duint64 sentBytes();
static double outputBytesPerSecond();

signals:
void addressResolved();
void connected();
Expand Down
59 changes: 56 additions & 3 deletions doomsday/sdk/libcore/src/net/socket.cpp
Expand Up @@ -77,6 +77,17 @@

namespace de {

struct Counters
{
duint64 sentUncompressedBytes = 0;
duint64 sentBytes = 0;
duint64 sentPeriodBytes = 0;
double outputBytesPerSecond = 0;
Time periodStartedAt;
};
static LockableT<Counters> counters;
static TimeDelta const sendPeriodDuration = 5;

/// Maximum number of channels.
static duint const MAX_CHANNELS = 2;

Expand All @@ -88,9 +99,7 @@ static int const MAX_SIZE_LARGE = DENG2_SOCKET_MAX_PAYLOAD_SIZE;
/// Threshold for input data size: messages smaller than this are first compressed
/// with Doomsday's Huffman codes. If the result is smaller than the deflated data,
/// the Huffman coded payload is used (unless it doesn't fit in a medium-sized packet).
#define MAX_HUFFMAN_INPUT_SIZE 4096 // bytes

#define DEFAULT_TRANSMISSION_SIZE 4096
static int const MAX_HUFFMAN_INPUT_SIZE = 4096; // bytes

#define TRMF_CONTINUE 0x80
#define TRMF_DEFLATED 0x40
Expand Down Expand Up @@ -293,11 +302,31 @@ DENG2_PIMPL_NOREF(Socket)
totalBytesWritten += total;

socket->write(payload);

// Update total counters, too.
{
DENG2_GUARD(counters);
counters.value.sentPeriodBytes += total;
counters.value.sentBytes += total;
// Update Bps counter.
if (!counters.value.periodStartedAt.isValid()
|| counters.value.periodStartedAt.since() > sendPeriodDuration)
{
counters.value.outputBytesPerSecond = double(counters.value.sentPeriodBytes)
/ sendPeriodDuration;
counters.value.sentPeriodBytes = 0;
counters.value.periodStartedAt = Time::currentHighPerformanceTime();
}
}
}

void serializeAndSendMessage(IByteArray const &packet)
{
Block payload = packet;
{
DENG2_GUARD(counters);
counters.value.sentUncompressedBytes += payload.size();
}

if (!retainOrder && packet.size() >= MAX_SIZE_BIG)
{
Expand Down Expand Up @@ -552,6 +581,30 @@ void Socket::setQuiet(bool noLogOutput)
d->quiet = noLogOutput;
}

void Socket::resetCounters()
{
DENG2_GUARD(counters);
counters.value = Counters();
}

duint64 Socket::sentUncompressedBytes()
{
DENG2_GUARD(counters);
return counters.value.sentUncompressedBytes;
}

duint64 Socket::sentBytes()
{
DENG2_GUARD(counters);
return counters.value.sentBytes;
}

double Socket::outputBytesPerSecond()
{
DENG2_GUARD(counters);
return counters.value.outputBytesPerSecond;
}

duint Socket::channel() const
{
return d->activeChannel;
Expand Down

0 comments on commit 0736e2d

Please sign in to comment.