Skip to content

feat: Extract Metrics from Network Pipeline Stage #1583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions com.unity.netcode.adapter.utp/Runtime/Metrics.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Unity.Netcode
{
public struct NetworkMetricsContext
{
public uint PacketSentCount;
public uint PacketReceivedCount;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public NetworkPipelineStage StaticInitialize(byte* staticInstanceBuffer,
ReceiveCapacity: 0,
SendCapacity: 0,
HeaderCapacity: 0,
0);
SharedStateCapacity: UnsafeUtility.SizeOf<NetworkMetricsContext>());
}

public int StaticSize => 0;
Expand All @@ -36,7 +36,8 @@ private static void Receive(ref NetworkPipelineContext networkPipelineContext,
ref NetworkPipelineStage.Requests requests,
int systemHeaderSize)
{

var networkMetricContext = (NetworkMetricsContext*)networkPipelineContext.internalSharedProcessBuffer;
networkMetricContext->PacketReceivedCount++;
}

[BurstCompile(DisableDirectCall = true)]
Expand All @@ -46,6 +47,8 @@ private static int Send(ref NetworkPipelineContext networkPipelineContext,
ref NetworkPipelineStage.Requests requests,
int systemHeaderSize)
{
var networkMetricContext = (NetworkMetricsContext*)networkPipelineContext.internalSharedProcessBuffer;
networkMetricContext->PacketSentCount++;
return 0;
}

Expand All @@ -55,6 +58,9 @@ private static void InitializeConnection(byte* staticInstanceBuffer, int staticI
byte* sendProcessBuffer, int sendProcessBufferLength, byte* receiveProcessBuffer, int receiveProcessBufferLength,
byte* sharedProcessBuffer, int sharedProcessBufferLength)
{
var networkMetricContext = (NetworkMetricsContext*)sharedProcessBuffer;
networkMetricContext->PacketSentCount = 0;
networkMetricContext->PacketReceivedCount = 0;
}
}
}
Expand Down
62 changes: 61 additions & 1 deletion com.unity.netcode.adapter.utp/Runtime/UnityTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,10 @@ private void Update()
{
;
}

#if MULTIPLAYER_TOOLS
ExtractNetworkMetrics();
#endif
}
}

Expand All @@ -566,6 +570,58 @@ private void OnDestroy()
DisposeDriver();
}

#if MULTIPLAYER_TOOLS
private void ExtractNetworkMetrics()
{
if (NetworkManager.Singleton.IsServer)
{
var ngoConnectionIds = NetworkManager.Singleton.ConnectedClients.Keys;
foreach (var ngoConnectionId in ngoConnectionIds)
{
if (ngoConnectionId == 0 && NetworkManager.Singleton.IsHost)
{
continue;
}
ExtractNetworkMetricsForClient(NetworkManager.Singleton.ClientIdToTransportId(ngoConnectionId));
}
}
else
{
ExtractNetworkMetricsForClient(NetworkManager.Singleton.ClientIdToTransportId(NetworkManager.Singleton.ServerClientId));
}
}

private void ExtractNetworkMetricsForClient(ulong transportClientId)
{
var networkConnection = ParseClientId(transportClientId);
ExtractNetworkMetricsFromPipeline(m_UnreliableFragmentedPipeline, networkConnection);
ExtractNetworkMetricsFromPipeline(m_UnreliableSequencedFragmentedPipeline, networkConnection);
ExtractNetworkMetricsFromPipeline(m_ReliableSequencedFragmentedPipeline, networkConnection);
}

private void ExtractNetworkMetricsFromPipeline(NetworkPipeline pipeline, NetworkConnection networkConnection)
{
//Don't need to dispose of the buffers, they are filled with data pointers.
m_Driver.GetPipelineBuffers(pipeline,
NetworkPipelineStageCollection.GetStageId(typeof(NetworkMetricsPipelineStage)),
networkConnection,
out _,
out _,
out var sharedBuffer);

unsafe
{
var networkMetricsContext = (NetworkMetricsContext*)sharedBuffer.GetUnsafePtr();

NetworkMetrics.TrackPacketSent(networkMetricsContext->PacketSentCount);
NetworkMetrics.TrackPacketReceived(networkMetricsContext->PacketReceivedCount);

networkMetricsContext->PacketSentCount = 0;
networkMetricsContext->PacketReceivedCount = 0;
}
}
#endif

private static unsafe ulong ParseClientId(NetworkConnection utpConnectionId)
{
return *(ulong*)&utpConnectionId;
Expand Down Expand Up @@ -773,7 +829,11 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
unreliableFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage),
typeof(SimulatorPipelineStage),
typeof(SimulatorPipelineStageInSend));
typeof(SimulatorPipelineStageInSend)
#if MULTIPLAYER_TOOLS
,typeof(NetworkMetricsPipelineStage)
#endif
);
unreliableSequencedFragmentedPipeline = driver.CreatePipeline(
typeof(FragmentationPipelineStage),
typeof(UnreliableSequencedPipelineStage),
Expand Down
2 changes: 1 addition & 1 deletion com.unity.netcode.gameobjects/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
[assembly: InternalsVisibleTo("Unity.Netcode.EditorTests")]
[assembly: InternalsVisibleTo("Unity.Netcode.Editor.CodeGen")]
[assembly: InternalsVisibleTo("Unity.Netcode.Editor")]
[assembly: InternalsVisibleTo("Unity.Netcode.Adapter.UTP")]
[assembly: InternalsVisibleTo("TestProject.EditorTests")]
[assembly: InternalsVisibleTo("TestProject.RuntimeTests")]
[assembly: InternalsVisibleTo("TestProject.ToolsIntegration.RuntimeTests")]
#endif
[assembly: InternalsVisibleTo("Unity.Netcode.RuntimeTests")]
[assembly: InternalsVisibleTo("Unity.Netcode.Adapter.UTP")]

Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ private ulong TransportIdToClientId(ulong transportId)
return transportId == m_ServerTransportId ? ServerClientId : m_TransportIdToClientIdMap[transportId];
}

private ulong ClientIdToTransportId(ulong clientId)
internal ulong ClientIdToTransportId(ulong clientId)
{
return clientId == ServerClientId ? m_ServerTransportId : m_ClientIdToTransportIdMap[clientId];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ void TrackRpcReceived(

void TrackSceneEventReceived(ulong senderClientId, uint sceneEventType, string sceneName, long bytesCount);

void TrackPacketSent(uint packetCount);

void TrackPacketReceived(uint packetCount);

void DispatchFrame();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ public void TrackSceneEventReceived(ulong senderClientId, uint sceneEventType, s
IncrementMetricCount();
}

public void TrackPacketSent(uint packetCount)
{
}

public void TrackPacketReceived(uint packetCount)
{
}

Comment on lines +410 to +417
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this waiting for some changes in the tools package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be part of the proper JIRA Ticket that dispatch it. With tests and all. It's there just to make sure we have functions.

public void DispatchFrame()
{
s_FrameDispatch.Begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ public void TrackSceneEventReceived(ulong senderClientId, uint sceneEventType, s
{
}

public void TrackPacketSent(uint packetCount)
{
}

public void TrackPacketReceived(uint packetCount)
{
}

public void DispatchFrame()
{
}
Expand Down