Files
o3de/Code/Framework/AzNetworking/AzNetworking/ConnectionLayer/ConnectionMetrics.h
T
Steve Pham 38261d0800 Shorten copyright headers by splitting into 2 lines (#2213)
* Updated all copyright headers to split the longer original copyright line into 2 shorter lines

Signed-off-by: Steve Pham <spham@amazon.com>
2021-07-16 15:25:48 -07:00

132 lines
4.7 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#include <AzNetworking/Utilities/NetworkCommon.h>
#include <AzCore/Time/ITime.h>
namespace AzNetworking
{
//! @struct DatarateAtom
//! @brief basic unit for measuring socket datarate with connection to time.
struct DatarateAtom
{
DatarateAtom() = default;
uint32_t m_bytesTransmitted = 0;
AZ::TimeMs m_timeAccumulatorMs = AZ::TimeMs{0};
};
//! @class DatarateMetrics
//! @brief used to track datarate related metrics for a given connection with respect to time.
class DatarateMetrics
{
public:
DatarateMetrics() = default;
//! Constructor.
//! @param maxSampleTimeMs the period of time in milliseconds to attempt to smooth datarate over
DatarateMetrics(AZ::TimeMs maxSampleTimeMs);
//! Invoked whenever traffic is handled by the connection this instance is responsible for.
//! @param byteCount number of bytes sent through the connection
//! @param currentTimeMs current process time in milliseconds
void LogPacket(uint32_t byteCount, AZ::TimeMs currentTimeMs);
//! Retrieve a sample of the datarate being incurred by this connection in bytes per second.
//! @return datarate for traffic sent to or from the connection in bytes per second
float GetBytesPerSecond() const;
private:
//! Used internally to swap buffers used for metric gathering.
void SwapBuffers();
static constexpr AZ::TimeMs MaxSampleTimeMs = AZ::TimeMs{500};
AZ::TimeMs m_maxSampleTimeMs = MaxSampleTimeMs;
AZ::TimeMs m_lastLoggedTimeMs = MaxSampleTimeMs;
uint32_t m_activeAtom = 0;
DatarateAtom m_atoms[2];
};
//! @struct ConnectionPacketEntry
//! @brief basic data structure used to timestamp packet sequences.
struct ConnectionPacketEntry
{
ConnectionPacketEntry() = default;
//! Constructor.
//! @param packetId packet id of the packet this entry is tracking
//! @param sendTimeMs logged send time for the tracked packet in milliseconds
ConnectionPacketEntry(PacketId packetId, AZ::TimeMs sendTimeMs);
PacketId m_packetId = InvalidPacketId;
AZ::TimeMs m_sendTimeMs = AZ::TimeMs{0};
};
//! @class ConnectionComputeRtt
//! @brief helper class used to compute round trip time to an connection.
class ConnectionComputeRtt
{
public:
ConnectionComputeRtt() = default;
//! Invoked whenever traffic is sent through the connection this instance is responsible for.
//! @param packetId identifier of the packet being sent
//! @param currentTimeMs current process time in milliseconds
void LogPacketSent(PacketId packetId, AZ::TimeMs currentTimeMs);
//! Invoked whenever traffic is acknowledged from the connection this instance is responsible for.
//! @param packetId identifier of the packet being acked
//! @param currentTimeMs current process time in milliseconds
void LogPacketAcked(PacketId packetId, AZ::TimeMs currentTimeMs);
//! Invoked whenever traffic times out from the connection this instance is responsible for.
//! @param packetId identifier of the packet timing out
void LogPacketTimeout(PacketId packetId);
//! Retrieve a sample of the computed round trip time for this connection.
//! @return estimated round trip time (ping) for the given connection in seconds
float GetRoundTripTimeSeconds() const;
private:
static constexpr uint32_t MaxTrackableEntries = 4;
static constexpr float InitialRoundTripTime = 0.1f; //< Start off with a 100 millisecond estimate for Rtt
float m_roundTripTime = InitialRoundTripTime;
ConnectionPacketEntry m_entries[MaxTrackableEntries];
};
//! @struct ConnectionMetrics
//! @brief used to track general performance metrics for a given connection with respect to time.
struct ConnectionMetrics
{
ConnectionMetrics() = default;
ConnectionMetrics& operator=(const ConnectionMetrics& rhs) = default;
//! Resets all internal metrics to defaults.
void Reset();
uint32_t m_packetsSent = 0;
uint32_t m_packetsRecv = 0;
uint32_t m_packetsLost = 0;
uint32_t m_packetsAcked = 0;
DatarateMetrics m_sendDatarate;
DatarateMetrics m_recvDatarate;
ConnectionComputeRtt m_connectionRtt;
};
}
#include <AzNetworking/ConnectionLayer/ConnectionMetrics.inl>