Skip to content

fix: Implement a max number of metric values dispatched per frame #1287

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 13 commits into from
Oct 18, 2021
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
161 changes: 132 additions & 29 deletions com.unity.netcode.gameobjects/Runtime/Metrics/NetworkMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
using Unity.Multiplayer.Tools;
using Unity.Multiplayer.Tools.MetricTypes;
using Unity.Multiplayer.Tools.NetStats;
using UnityEngine;

namespace Unity.Netcode
{
internal class NetworkMetrics : INetworkMetrics
{
private static Dictionary<uint, string> s_SceneEventTypeNames;
const ulong k_MaxMetricsPerFrame = 1000L;

static Dictionary<uint, string> s_SceneEventTypeNames;

static NetworkMetrics()
{
Expand Down Expand Up @@ -59,7 +62,8 @@ private static string GetSceneEventTypeName(uint typeCode)
private readonly EventMetric<ServerLogEvent> m_ServerLogReceivedEvent = new EventMetric<ServerLogEvent>(NetworkMetricTypes.ServerLogReceived.Id);
private readonly EventMetric<SceneEventMetric> m_SceneEventSentEvent = new EventMetric<SceneEventMetric>(NetworkMetricTypes.SceneEventSent.Id);
private readonly EventMetric<SceneEventMetric> m_SceneEventReceivedEvent = new EventMetric<SceneEventMetric>(NetworkMetricTypes.SceneEventReceived.Id);
private bool m_Dirty;

private ulong m_NumberOfMetricsThisFrame;

public NetworkMetrics()
{
Expand All @@ -82,6 +86,8 @@ public NetworkMetrics()

internal IMetricDispatcher Dispatcher { get; }

private bool CanSendMetrics => m_NumberOfMetricsThisFrame < k_MaxMetricsPerFrame;

public void SetConnectionId(ulong connectionId)
{
Dispatcher.SetConnectionId(connectionId);
Expand All @@ -99,20 +105,35 @@ public void TrackTransportBytesReceived(long bytesCount)

public void TrackNetworkMessageSent(ulong receivedClientId, string messageType, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_NetworkMessageSentEvent.Mark(new NetworkMessageEvent(new ConnectionInfo(receivedClientId), messageType, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackNetworkMessageReceived(ulong senderClientId, string messageType, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_NetworkMessageReceivedEvent.Mark(new NetworkMessageEvent(new ConnectionInfo(senderClientId), messageType, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackNamedMessageSent(ulong receiverClientId, string messageName, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_NamedMessageSentEvent.Mark(new NamedMessageEvent(new ConnectionInfo(receiverClientId), messageName, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackNamedMessageSent(IReadOnlyCollection<ulong> receiverClientIds, string messageName, long bytesCount)
Expand All @@ -125,14 +146,24 @@ public void TrackNamedMessageSent(IReadOnlyCollection<ulong> receiverClientIds,

public void TrackNamedMessageReceived(ulong senderClientId, string messageName, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_NamedMessageReceivedEvent.Mark(new NamedMessageEvent(new ConnectionInfo(senderClientId), messageName, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackUnnamedMessageSent(ulong receiverClientId, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_UnnamedMessageSentEvent.Mark(new UnnamedMessageEvent(new ConnectionInfo(receiverClientId), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackUnnamedMessageSent(IReadOnlyCollection<ulong> receiverClientIds, long bytesCount)
Expand All @@ -145,8 +176,13 @@ public void TrackUnnamedMessageSent(IReadOnlyCollection<ulong> receiverClientIds

public void TrackUnnamedMessageReceived(ulong senderClientId, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_UnnamedMessageReceivedEvent.Mark(new UnnamedMessageEvent(new ConnectionInfo(senderClientId), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackNetworkVariableDeltaSent(
Expand All @@ -156,14 +192,19 @@ public void TrackNetworkVariableDeltaSent(
string networkBehaviourName,
long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_NetworkVariableDeltaSentEvent.Mark(
new NetworkVariableEvent(
new ConnectionInfo(receiverClientId),
GetObjectIdentifier(networkObject),
variableName,
networkBehaviourName,
bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackNetworkVariableDeltaReceived(
Expand All @@ -173,51 +214,86 @@ public void TrackNetworkVariableDeltaReceived(
string networkBehaviourName,
long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_NetworkVariableDeltaReceivedEvent.Mark(
new NetworkVariableEvent(
new ConnectionInfo(senderClientId),
GetObjectIdentifier(networkObject),
variableName,
networkBehaviourName,
bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackOwnershipChangeSent(ulong receiverClientId, NetworkObject networkObject, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_OwnershipChangeSentEvent.Mark(new OwnershipChangeEvent(new ConnectionInfo(receiverClientId), GetObjectIdentifier(networkObject), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackOwnershipChangeReceived(ulong senderClientId, NetworkObject networkObject, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_OwnershipChangeReceivedEvent.Mark(new OwnershipChangeEvent(new ConnectionInfo(senderClientId),
GetObjectIdentifier(networkObject), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackObjectSpawnSent(ulong receiverClientId, NetworkObject networkObject, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_ObjectSpawnSentEvent.Mark(new ObjectSpawnedEvent(new ConnectionInfo(receiverClientId), GetObjectIdentifier(networkObject), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackObjectSpawnReceived(ulong senderClientId, NetworkObject networkObject, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_ObjectSpawnReceivedEvent.Mark(new ObjectSpawnedEvent(new ConnectionInfo(senderClientId), GetObjectIdentifier(networkObject), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackObjectDestroySent(ulong receiverClientId, NetworkObject networkObject, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_ObjectDestroySentEvent.Mark(new ObjectDestroyedEvent(new ConnectionInfo(receiverClientId), GetObjectIdentifier(networkObject), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackObjectDestroyReceived(ulong senderClientId, NetworkObject networkObject, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_ObjectDestroyReceivedEvent.Mark(new ObjectDestroyedEvent(new ConnectionInfo(senderClientId), GetObjectIdentifier(networkObject), bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackRpcSent(
Expand All @@ -227,14 +303,19 @@ public void TrackRpcSent(
string networkBehaviourName,
long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_RpcSentEvent.Mark(
new RpcEvent(
new ConnectionInfo(receiverClientId),
GetObjectIdentifier(networkObject),
rpcName,
networkBehaviourName,
bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackRpcSent(
Expand All @@ -257,25 +338,40 @@ public void TrackRpcReceived(
string networkBehaviourName,
long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_RpcReceivedEvent.Mark(
new RpcEvent(new ConnectionInfo(senderClientId),
GetObjectIdentifier(networkObject),
rpcName,
networkBehaviourName,
bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackServerLogSent(ulong receiverClientId, uint logType, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_ServerLogSentEvent.Mark(new ServerLogEvent(new ConnectionInfo(receiverClientId), (Multiplayer.Tools.MetricTypes.LogLevel)logType, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackServerLogReceived(ulong senderClientId, uint logType, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_ServerLogReceivedEvent.Mark(new ServerLogEvent(new ConnectionInfo(senderClientId), (Multiplayer.Tools.MetricTypes.LogLevel)logType, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackSceneEventSent(IReadOnlyList<ulong> receiverClientIds, uint sceneEventType, string sceneName, long bytesCount)
Expand All @@ -288,28 +384,35 @@ public void TrackSceneEventSent(IReadOnlyList<ulong> receiverClientIds, uint sce

public void TrackSceneEventSent(ulong receiverClientId, uint sceneEventType, string sceneName, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_SceneEventSentEvent.Mark(new SceneEventMetric(new ConnectionInfo(receiverClientId), GetSceneEventTypeName(sceneEventType), sceneName, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void TrackSceneEventReceived(ulong senderClientId, uint sceneEventType, string sceneName, long bytesCount)
{
if (!CanSendMetrics)
{
return;
}

m_SceneEventReceivedEvent.Mark(new SceneEventMetric(new ConnectionInfo(senderClientId), GetSceneEventTypeName(sceneEventType), sceneName, bytesCount));
MarkDirty();
IncrementMetricCount();
}

public void DispatchFrame()
{
if (m_Dirty)
{
Dispatcher.Dispatch();
m_Dirty = false;
}
Dispatcher.Dispatch();
m_NumberOfMetricsThisFrame = 0;
}

private void MarkDirty()
private void IncrementMetricCount()
{
m_Dirty = true;
m_NumberOfMetricsThisFrame++;
}

private static NetworkObjectIdentifier GetObjectIdentifier(NetworkObject networkObject)
Expand Down
Loading