-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDebugViewCurrentSceneSystem.cs
More file actions
227 lines (188 loc) · 12.2 KB
/
Copy pathDebugViewCurrentSceneSystem.cs
File metadata and controls
227 lines (188 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using Arch.Core;
using Arch.SystemGroups;
using Arch.SystemGroups.DefaultSystemGroups;
using DCL.DebugUtilities;
using DCL.DebugUtilities.UIBindings;
using ECS;
using ECS.Abstract;
using ECS.SceneLifeCycle;
using SceneRunner.Scene;
using System;
using UnityEngine;
namespace DCL.Profiling.ECS
{
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial class DebugViewCurrentSceneSystem : BaseUnityLoopSystem
{
private const int FPS_CHART_CAPACITY = 120;
private const int TICK_CHART_CAPACITY = SampledCounter.BUFFER_CAPACITY;
private const int FRAME_STATS_COOLDOWN = 30;
private const int RECENT_TICK_WINDOW = 32;
private const long HICCUP_THRESHOLD_NS = 50_000_000L; // 50 ms ~ 20 FPS
private const float MESSAGE_HICCUP_MEAN_MULTIPLIER = 2f; // a tick that produces > 2x avg messages is a spike
private readonly IRealmData realmData;
private readonly IScenesCache scenesCache;
private readonly bool perfWidgetEnabled;
private readonly bool contentWidgetEnabled;
private readonly DebugWidgetVisibilityBinding visibility;
private readonly DebugWidgetVisibilityBinding contentVisibility;
private readonly StringBindings stringBindings;
private readonly ContentStatsBindings contentStatsBindings;
private readonly ElementBinding<LineChartBuffer> fpsChart;
private readonly ElementBinding<LineChartBuffer> bytesFromChart;
private readonly ElementBinding<LineChartBuffer> bytesToChart;
private readonly ElementBinding<LineChartBuffer> messagesFromChart;
private readonly ElementBinding<LineChartBuffer> messagesToChart;
private readonly long[] longScratch = new long[SampledCounter.BUFFER_CAPACITY];
private readonly float[] fpsRing = new float[FPS_CHART_CAPACITY];
private readonly float[] bytesFromRing = new float[TICK_CHART_CAPACITY];
private readonly float[] bytesToRing = new float[TICK_CHART_CAPACITY];
private readonly float[] messagesFromRing = new float[TICK_CHART_CAPACITY];
private readonly float[] messagesToRing = new float[TICK_CHART_CAPACITY];
private int fpsRingIndex;
private int fpsRingCount;
private readonly Action<ISceneFacade?>? onCurrentSceneChanged;
private ISceneFacade? currentScene;
private SceneContentCaps contentCaps;
private long lastContentCollectionCount = -1;
private long lastBytesFromScene;
private long lastBytesToScene;
private long lastMessagesFromScene;
private long lastMessagesToScene;
private float lastSampleTime;
private int framesSinceMetricsUpdate;
internal DebugViewCurrentSceneSystem(World world, IDebugContainerBuilder debugBuilder, IScenesCache scenesCache, IRealmData realmData) : base(world)
{
this.realmData = realmData;
this.scenesCache = scenesCache;
visibility = new DebugWidgetVisibilityBinding(true);
contentVisibility = new DebugWidgetVisibilityBinding(true);
stringBindings = StringBindings.Create();
contentStatsBindings = ContentStatsBindings.Create();
fpsChart = new ElementBinding<LineChartBuffer>(new LineChartBuffer(fpsRing, 0, 0, 0));
bytesFromChart = new ElementBinding<LineChartBuffer>(new LineChartBuffer(bytesFromRing, 0, 0, 0));
bytesToChart = new ElementBinding<LineChartBuffer>(new LineChartBuffer(bytesToRing, 0, 0, 0));
messagesFromChart = new ElementBinding<LineChartBuffer>(new LineChartBuffer(messagesFromRing, 0, 0, 0));
messagesToChart = new ElementBinding<LineChartBuffer>(new LineChartBuffer(messagesToRing, 0, 0, 0));
DebugWidgetBuilder? widgetBuilder = debugBuilder.TryAddWidget(IDebugContainerBuilder.Categories.CURRENT_SCENE);
DebugWidgetBuilder? contentWidgetBuilder = debugBuilder.TryAddWidget(IDebugContainerBuilder.Categories.SCENE_CONTENT);
if (widgetBuilder == null && contentWidgetBuilder == null)
return;
perfWidgetEnabled = widgetBuilder != null;
contentWidgetEnabled = contentWidgetBuilder != null;
onCurrentSceneChanged = OnCurrentSceneChanged;
scenesCache.CurrentScene.OnUpdate += onCurrentSceneChanged;
OnCurrentSceneChanged(scenesCache.CurrentScene.Value);
contentWidgetBuilder?.SetVisibilityBinding(contentVisibility)
.AddCustomMarker("Entities:", contentStatsBindings.Entities)
.AddCustomMarker("Triangles:", contentStatsBindings.Triangles)
.AddCustomMarker("Meshes (bodies):", contentStatsBindings.Bodies)
.AddCustomMarker("Textures:", contentStatsBindings.Textures)
.AddCustomMarker("Geometries:", contentStatsBindings.Geometries)
.AddCustomMarker("Materials:", contentStatsBindings.Materials)
.AddCustomMarker("Colliders:", contentStatsBindings.Colliders)
.AddCustomMarker("External videos/audios:", contentStatsBindings.Videos);
widgetBuilder?.SetVisibilityBinding(visibility)
.AddCustomMarker("Real tick FPS:", stringBindings.RealFps)
.AddCustomMarker("Min FPS (last 256 ticks):", stringBindings.MinFps)
.AddCustomMarker("Max FPS (last 256 ticks):", stringBindings.MaxFps)
.AddCustomMarker("Hiccups (last 256 ticks):", stringBindings.Hiccups)
.AddControl(new DebugLineChartDef(fpsChart, "Tick FPS", new Color(0.18f, 0.80f, 0.44f)), null)
.AddCustomMarker("Bytes from scene:", stringBindings.BytesFromTotal)
.AddCustomMarker("Bytes/s from scene:", stringBindings.BytesFromPerSec)
.AddControl(new DebugLineChartDef(bytesFromChart, "Bytes/tick from scene", new Color(0.20f, 0.60f, 0.86f), DebugLongMarkerDef.Unit.Bytes), null)
.AddCustomMarker("Msgs from scene:", stringBindings.MessagesFromTotal)
.AddCustomMarker("Msgs/s from scene:", stringBindings.MessagesFromPerSec)
.AddCustomMarker("Msgs/call min/max from scene:", stringBindings.MessagesFromMinMax)
.AddCustomMarker("Msg hiccups from scene:", stringBindings.MessagesFromHiccups)
.AddControl(new DebugLineChartDef(messagesFromChart, "Msgs/tick from scene", new Color(0.40f, 0.80f, 0.95f)), null)
.AddCustomMarker("Bytes to scene:", stringBindings.BytesToTotal)
.AddCustomMarker("Bytes/s to scene:", stringBindings.BytesToPerSec)
.AddControl(new DebugLineChartDef(bytesToChart, "Bytes/tick to scene", new Color(0.91f, 0.30f, 0.55f), DebugLongMarkerDef.Unit.Bytes), null)
.AddCustomMarker("Msgs to scene:", stringBindings.MessagesToTotal)
.AddCustomMarker("Msgs/s to scene:", stringBindings.MessagesToPerSec)
.AddCustomMarker("Msgs/call min/max to scene:", stringBindings.MessagesToMinMax)
.AddCustomMarker("Msg hiccups to scene:", stringBindings.MessagesToHiccups)
.AddControl(new DebugLineChartDef(messagesToChart, "Msgs/tick to scene", new Color(0.98f, 0.55f, 0.75f)), null);
}
protected override void Update(float t)
{
if (!perfWidgetEnabled && !contentWidgetEnabled) return;
if (!realmData.Configured) return;
if (currentScene == null) return;
SceneRuntimeMetrics metrics = currentScene.RuntimeMetrics;
bool contentExpanded = contentWidgetEnabled && contentVisibility.IsConnectedAndExpanded;
metrics.ContentStats.RequestedByDebugWidget = contentExpanded;
if (contentExpanded && metrics.ContentStats.CollectionCount != lastContentCollectionCount)
{
lastContentCollectionCount = metrics.ContentStats.CollectionCount;
UpdateContentStatsBindings(in contentStatsBindings, metrics.ContentStats, in contentCaps);
}
if (!perfWidgetEnabled || !visibility.IsConnectedAndExpanded) return;
long bytesFrom = metrics.BytesFromScene.Total;
long bytesTo = metrics.BytesToScene.Total;
long messagesFrom = metrics.MessagesFromScene.Total;
long messagesTo = metrics.MessagesToScene.Total;
float now = UnityEngine.Time.unscaledTime;
float dt = Mathf.Max(1e-3f, now - lastSampleTime);
long deltaBytesFrom = bytesFrom - lastBytesFromScene;
long deltaBytesTo = bytesTo - lastBytesToScene;
long deltaMessagesFrom = messagesFrom - lastMessagesFromScene;
long deltaMessagesTo = messagesTo - lastMessagesToScene;
lastBytesFromScene = bytesFrom;
lastBytesToScene = bytesTo;
lastMessagesFromScene = messagesFrom;
lastMessagesToScene = messagesTo;
lastSampleTime = now;
int tickSampleCount = metrics.TickTimesNs.CopySnapshot(longScratch);
ComputeTickFps(longScratch, tickSampleCount, out float currentFpsValue, out float minFpsValue, out float maxFpsValue, out int hiccupCount);
PushSample(fpsRing, ref fpsRingIndex, ref fpsRingCount, currentFpsValue);
fpsChart.SetAndUpdate(new LineChartBuffer(fpsRing, fpsRingIndex, fpsRingCount, currentFpsValue));
PopulatePerTickChart(metrics.BytesFromScene, bytesFromChart, bytesFromRing, longScratch);
PopulatePerTickChart(metrics.BytesToScene, bytesToChart, bytesToRing, longScratch);
PopulatePerTickChart(metrics.MessagesFromScene, messagesFromChart, messagesFromRing, longScratch);
PopulatePerTickChart(metrics.MessagesToScene, messagesToChart, messagesToRing, longScratch);
if (++framesSinceMetricsUpdate >= FRAME_STATS_COOLDOWN)
{
framesSinceMetricsUpdate = 0;
UpdateStringBindings(in stringBindings, metrics, currentFpsValue, minFpsValue, maxFpsValue, hiccupCount,
deltaBytesFrom, deltaBytesTo, deltaMessagesFrom, deltaMessagesTo, dt);
}
}
protected override void OnDispose()
{
if (onCurrentSceneChanged != null)
scenesCache.CurrentScene.OnUpdate -= onCurrentSceneChanged;
}
private void OnCurrentSceneChanged(ISceneFacade? scene)
{
if (currentScene != null)
currentScene.RuntimeMetrics.ContentStats.RequestedByDebugWidget = false;
currentScene = scene;
contentCaps = scene != null ? SceneContentCaps.ForParcelCount(scene.SceneData.Parcels.Count) : default(SceneContentCaps);
ResetLocalState();
if (scene != null)
{
SceneRuntimeMetrics initial = scene.RuntimeMetrics;
lastBytesFromScene = initial.BytesFromScene.Total;
lastBytesToScene = initial.BytesToScene.Total;
lastMessagesFromScene = initial.MessagesFromScene.Total;
lastMessagesToScene = initial.MessagesToScene.Total;
}
lastSampleTime = UnityEngine.Time.unscaledTime;
}
private void ResetLocalState()
{
fpsRingIndex = fpsRingCount = 0;
lastBytesFromScene = lastBytesToScene = lastMessagesFromScene = lastMessagesToScene = 0;
framesSinceMetricsUpdate = 0;
lastContentCollectionCount = -1;
// Stage the cleared buffer; flush happens via SetAndUpdate in Update once the binding is connected.
fpsChart.Value = new LineChartBuffer(fpsRing, 0, 0, 0);
bytesFromChart.Value = new LineChartBuffer(bytesFromRing, 0, 0, 0);
bytesToChart.Value = new LineChartBuffer(bytesToRing, 0, 0, 0);
messagesFromChart.Value = new LineChartBuffer(messagesFromRing, 0, 0, 0);
messagesToChart.Value = new LineChartBuffer(messagesToRing, 0, 0, 0);
}
}
}