Skip to content

Commit 18c1a0a

Browse files
kvassall-unitymattwalsh-unity0xFA11
authored
feat: Adding in Performance Tick Data that stores data for external consumption (RFC #7) (#491)
* Adding in Performance Tick Data that can be propagated out for external consumption * Addressing all the current review feedback * Handle Null cases * Refactor so transports reset their counters at the beginning of a new tick * renaming this better * Simplify the code even more * Don't overflow to negative ticks Move transport constants to the appropriate location * Don't allocate strings * Making a static event so registering to it can take place at any time * removed extra toString() Co-authored-by: Matt Walsh <matt.walsh@unity3d.com> Co-authored-by: M. Fatih MAR <mfatihmar@gmail.com>
1 parent 76b2a3e commit 18c1a0a

20 files changed

Lines changed: 285 additions & 1 deletion

com.unity.multiplayer.mlapi/Runtime/Core/NetworkedBehaviour.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ internal static void HandleNetworkedVarDeltas(List<INetworkedVar> networkedVarLi
769769
long readStartPos = stream.Position;
770770

771771
networkedVarList[i].ReadDelta(stream, IsServer);
772+
PerformanceDataManager.Increment(ProfilerConstants.NumberNetworkVarsReceived);
772773
ProfilerStatManager.networkVarsRcvd.Record();
773774

774775
if (NetworkingManager.Singleton.NetworkConfig.EnsureNetworkedVarLengthSafety)
@@ -836,6 +837,7 @@ internal static void HandleNetworkedVarUpdate(List<INetworkedVar> networkedVarLi
836837
long readStartPos = stream.Position;
837838

838839
networkedVarList[i].ReadField(stream);
840+
PerformanceDataManager.Increment(ProfilerConstants.NumberNetworkVarsReceived);
839841
ProfilerStatManager.networkVarsRcvd.Record();
840842

841843
if (NetworkingManager.Singleton.NetworkConfig.EnsureNetworkedVarLengthSafety)

com.unity.multiplayer.mlapi/Runtime/Core/NetworkingManager.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public class NetworkingManager : MonoBehaviour, INetworkUpdateSystem
7373
internal RpcQueueContainer rpcQueueContainer { get; private set; }
7474
internal NetworkTickSystem networkTickSystem { get; private set; }
7575

76+
public delegate void PerformanceDataEventHandler(PerformanceTickData profilerData);
77+
78+
public static event PerformanceDataEventHandler OnPerformanceDataEvent;
79+
7680
/// <summary>
7781
/// A synchronized time, represents the time in seconds since the server application started. Is replicated across all clients
7882
/// </summary>
@@ -747,11 +751,18 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
747751

748752
private void OnNetworkEarlyUpdate()
749753
{
754+
PerformanceDataManager.BeginNewTick();
755+
if (NetworkConfig.NetworkTransport is ITransportProfilerData profileTransport)
756+
{
757+
profileTransport.BeginNewTick();
758+
}
759+
750760
if (IsListening)
751761
{
752762
// Process received data
753763
if ((NetworkTime - m_LastReceiveTickTime >= (1f / NetworkConfig.ReceiveTickrate)) || NetworkConfig.ReceiveTickrate <= 0)
754764
{
765+
PerformanceDataManager.Increment(ProfilerConstants.ReceiveTickRate);
755766
ProfilerStatManager.rcvTickRate.Record();
756767
#if DEVELOPMENT_BUILD || UNITY_EDITOR
757768
s_ReceiveTick.Begin();
@@ -861,6 +872,14 @@ private void OnNetworkPreUpdate()
861872
currentNetworkTimeOffset += Mathf.Clamp(networkTimeOffset - currentNetworkTimeOffset, -maxDelta, maxDelta);
862873
}
863874
}
875+
876+
if(NetworkConfig.NetworkTransport is ITransportProfilerData profileTransport)
877+
{
878+
var transportProfilerData = profileTransport.GetTransportProfilerData();
879+
PerformanceDataManager.AddTransportData(transportProfilerData);
880+
}
881+
882+
OnPerformanceDataEvent?.Invoke(PerformanceDataManager.GetData());
864883
}
865884

866885
internal void UpdateNetworkTime(ulong clientId, float netTime, float receiveTime, bool warp = false)
@@ -914,6 +933,7 @@ internal IEnumerator TimeOutSwitchSceneProgress(SceneSwitchProgress switchSceneP
914933

915934
private void HandleRawTransportPoll(NetEventType eventType, ulong clientId, Channel channel, ArraySegment<byte> payload, float receiveTime)
916935
{
936+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesReceived, payload.Count);
917937
ProfilerStatManager.bytesRcvd.Record(payload.Count);
918938
switch (eventType)
919939
{
@@ -1174,6 +1194,7 @@ internal void HandleIncomingData(ulong clientId, Channel channel, ArraySegment<b
11741194
{
11751195
m_RpcBatcher.ReceiveItems(messageStream, ReceiveCallback, RpcQueueContainer.QueueItemType.ServerRpc, clientId, receiveTime);
11761196
ProfilerStatManager.rpcBatchesRcvd.Record();
1197+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCBatchesReceived);
11771198
}
11781199
else
11791200
{
@@ -1196,6 +1217,7 @@ internal void HandleIncomingData(ulong clientId, Channel channel, ArraySegment<b
11961217
{
11971218
m_RpcBatcher.ReceiveItems(messageStream, ReceiveCallback, RpcQueueContainer.QueueItemType.ClientRpc, clientId, receiveTime);
11981219
ProfilerStatManager.rpcBatchesRcvd.Record();
1220+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCBatchesReceived);
11991221
}
12001222
else
12011223
{
@@ -1347,6 +1369,7 @@ public void DisconnectClient(ulong clientId)
13471369
{
13481370
if (ConnectedClientsList[i].ClientId == clientId) {
13491371
ConnectedClientsList.RemoveAt(i);
1372+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections, -1);
13501373
ProfilerStatManager.connections.Record(-1);
13511374
}
13521375
}
@@ -1412,6 +1435,7 @@ internal void OnClientDisconnectFromServer(ulong clientId)
14121435
if (ConnectedClientsList[i].ClientId == clientId)
14131436
{
14141437
ConnectedClientsList.RemoveAt(i);
1438+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections, -1);
14151439
ProfilerStatManager.connections.Record(-1);
14161440
break;
14171441
}
@@ -1460,6 +1484,7 @@ internal void HandleApproval(ulong clientId, bool createPlayerObject, ulong? pla
14601484
ConnectedClients.Add(clientId, client);
14611485
ConnectedClientsList.Add(client);
14621486

1487+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections);
14631488
ProfilerStatManager.connections.Record();
14641489

14651490
// This packet is unreliable, but if it gets through it should provide a much better sync than the potentially huge approval message.

com.unity.multiplayer.mlapi/Runtime/Messaging/InternalMessageHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,15 @@ internal static void RPCReceiveQueueItem(ulong clientId, Stream stream, float re
694694
}
695695

696696
ProfilerStatManager.rpcsRcvd.Record();
697+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsReceived);
697698

698699
var rpcQueueContainer = NetworkingManager.Singleton.rpcQueueContainer;
699700
rpcQueueContainer.AddQueueItemToInboundFrame(queueItemType, receiveTime, clientId, (BitStream)stream);
700701
}
701702

702703
internal static void HandleUnnamedMessage(ulong clientId, Stream stream)
703704
{
705+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfUnnamedMessages);
704706
ProfilerStatManager.unnamedMessage.Record();
705707
#if DEVELOPMENT_BUILD || UNITY_EDITOR
706708
s_HandleUnnamedMessage.Begin();
@@ -713,6 +715,7 @@ internal static void HandleUnnamedMessage(ulong clientId, Stream stream)
713715

714716
internal static void HandleNamedMessage(ulong clientId, Stream stream)
715717
{
718+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfNamedMessages);
716719
ProfilerStatManager.namedMessage.Record();
717720
#if DEVELOPMENT_BUILD || UNITY_EDITOR
718721
s_HandleNamedMessage.Begin();

com.unity.multiplayer.mlapi/Runtime/Messaging/InternalMessageSender.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal static void Send(ulong clientId, byte messageType, Channel channel, Bit
2626

2727
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientId, new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
2828
ProfilerStatManager.bytesSent.Record((int)stream.Length);
29+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
2930

3031
NetworkProfiler.EndEvent();
3132
}
@@ -57,6 +58,7 @@ internal static void Send(byte messageType, Channel channel, BitStream messageSt
5758

5859
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
5960
ProfilerStatManager.bytesSent.Record((int)stream.Length);
61+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
6062
}
6163
NetworkProfiler.EndEvent();
6264
}
@@ -95,6 +97,7 @@ internal static void Send(byte messageType, Channel channel, List<ulong> clientI
9597

9698
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientIds[i], new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
9799
ProfilerStatManager.bytesSent.Record((int)stream.Length);
100+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
98101
}
99102
NetworkProfiler.EndEvent();
100103
}
@@ -131,6 +134,7 @@ internal static void Send(byte messageType, Channel channel, ulong clientIdToIgn
131134

132135
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(NetworkingManager.Singleton.ConnectedClientsList[i].ClientId, new ArraySegment<byte>(stream.GetBuffer(), 0, (int)stream.Length), channel);
133136
ProfilerStatManager.bytesSent.Record((int)stream.Length);
137+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)stream.Length);
134138
}
135139
NetworkProfiler.EndEvent();
136140
}

com.unity.multiplayer.mlapi/Runtime/Messaging/RpcBatcher.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public void QueueItem(in RpcFrameQueueItem queueItem)
149149

150150
ProfilerStatManager.bytesSent.Record(queueItem.messageData.Count);
151151
ProfilerStatManager.rpcsSent.Record();
152+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, queueItem.messageData.Count);
153+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsSent);
152154
}
153155
}
154156

@@ -178,6 +180,7 @@ public void SendItems(int thresholdBytes, SendCallbackType sendCallback)
178180
entry.Value.Stream.Position = 0;
179181
entry.Value.IsEmpty = true;
180182
ProfilerStatManager.rpcBatchesSent.Record();
183+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCBatchesSent);
181184
}
182185
}
183186
}

com.unity.multiplayer.mlapi/Runtime/Messaging/RpcQueue/RpcQueueContainer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ public void AdvanceFrameHistory(QueueHistoryFrame.QueueFrameType queueType)
190190
if (queueType == QueueHistoryFrame.QueueFrameType.Inbound)
191191
{
192192
ProfilerStatManager.rpcInQueueSize.Record((int)queueHistoryItem.totalSize);
193+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsInQueueSize, (int)queueHistoryItem.totalSize);
193194
}
194195
else
195196
{
196197
ProfilerStatManager.rpcOutQueueSize.Record((int)queueHistoryItem.totalSize);
198+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsOutQueueSize, (int)queueHistoryItem.totalSize);
197199
}
198200
}
199201

com.unity.multiplayer.mlapi/Runtime/Messaging/RpcQueue/RpcQueueProcessor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void ProcessReceiveQueue(NetworkUpdateStage currentStage)
6262

6363
NetworkingManager.InvokeRpc(currentQueueItem);
6464
ProfilerStatManager.rpcsQueueProc.Record();
65+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCQueueProcessed);
6566
currentQueueItem = CurrentFrame.GetNextQueueItem();
6667
}
6768

@@ -130,6 +131,7 @@ public void InternalMessagesSendAndFlush()
130131
InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_ADD_OBJECT, queueItem.channel, PoolStream, queueItem.sendFlags);
131132
}
132133

134+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsSent, queueItem.clientIds.Length);
133135
ProfilerStatManager.rpcsSent.Record(queueItem.clientIds.Length);
134136
break;
135137
}
@@ -140,6 +142,7 @@ public void InternalMessagesSendAndFlush()
140142
InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_DESTROY_OBJECT, queueItem.channel, PoolStream, queueItem.sendFlags);
141143
}
142144

145+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsSent, queueItem.clientIds.Length);
143146
ProfilerStatManager.rpcsSent.Record(queueItem.clientIds.Length);
144147
break;
145148
}
@@ -228,6 +231,9 @@ private void SendFrameQueueItem(RpcFrameQueueItem queueItem)
228231
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(queueItem.networkId, queueItem.messageData, queueItem.channel);
229232

230233
//For each packet sent, we want to record how much data we have sent
234+
235+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)queueItem.streamSize);
236+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsSent);
231237
ProfilerStatManager.bytesSent.Record((int)queueItem.streamSize);
232238
ProfilerStatManager.rpcsSent.Record();
233239
break;
@@ -239,10 +245,12 @@ private void SendFrameQueueItem(RpcFrameQueueItem queueItem)
239245
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientid, queueItem.messageData, queueItem.channel);
240246

241247
//For each packet sent, we want to record how much data we have sent
248+
PerformanceDataManager.Increment(ProfilerConstants.NumberBytesSent, (int)queueItem.streamSize);
242249
ProfilerStatManager.bytesSent.Record((int)queueItem.streamSize);
243250
}
244251

245252
//For each client we send to, we want to record how many RPCs we have sent
253+
PerformanceDataManager.Increment(ProfilerConstants.NumberOfRPCsSent, queueItem.clientIds.Length);
246254
ProfilerStatManager.rpcsSent.Record(queueItem.clientIds.Length);
247255

248256
break;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace MLAPI.Profiling
4+
{
5+
public interface ITransportProfilerData
6+
{
7+
void BeginNewTick();
8+
IReadOnlyDictionary<string, int> GetTransportProfilerData();
9+
}
10+
}

com.unity.multiplayer.mlapi/Runtime/Profiling/ITransportProfilerData.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace MLAPI.Profiling
6+
{
7+
static class PerformanceDataManager
8+
{
9+
static PerformanceTickData s_ProfilerData;
10+
static int s_TickID;
11+
12+
internal static void BeginNewTick()
13+
{
14+
s_TickID = Math.Max(s_TickID, 0);
15+
s_ProfilerData = new PerformanceTickData
16+
{
17+
tickID = s_TickID++,
18+
};
19+
}
20+
21+
internal static void Increment(string fieldName, int count = 1)
22+
{
23+
s_ProfilerData?.Increment(fieldName, count);
24+
}
25+
26+
internal static void AddTransportData(IReadOnlyDictionary<string, int> transportProfilerData)
27+
{
28+
s_ProfilerData?.AddNonDuplicateData(transportProfilerData);
29+
}
30+
31+
internal static PerformanceTickData GetData()
32+
{
33+
return s_ProfilerData;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)