| | 1 | | using System; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | | using UnityEngine.Profiling; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public sealed class SceneMetricsCounter : ISceneMetricsCounter |
| | 14 | | { |
| | 15 | | public static class LimitsConfig |
| | 16 | | { |
| | 17 | | // number of entities |
| | 18 | | public const int entities = 200; |
| | 19 | |
|
| | 20 | | // Number of faces (per parcel) |
| | 21 | | public const int triangles = 10000; |
| | 22 | | public const int bodies = 300; |
| | 23 | | public const int textures = 10; |
| | 24 | | public const int materials = 20; |
| | 25 | | public const int meshes = 200; |
| | 26 | |
|
| | 27 | | public const float height = 20; |
| | 28 | | public const float visibleRadius = 10; |
| | 29 | | } |
| | 30 | |
|
| 1 | 31 | | private static bool VERBOSE = false; |
| 1 | 32 | | private static Logger logger = new Logger("SceneMetricsCounter") { verboseEnabled = VERBOSE }; |
| | 33 | | public event Action<ISceneMetricsCounter> OnMetricsUpdated; |
| | 34 | |
|
| 549 | 35 | | private SceneMetricsModel maxCountValue = new SceneMetricsModel(); |
| 549 | 36 | | private SceneMetricsModel currentCountValue = new SceneMetricsModel(); |
| 549 | 37 | | private SceneMetricsModel lastCountValue = new SceneMetricsModel(); |
| | 38 | |
|
| | 39 | | // TODO: We should handle this better, right now if we get the current amount of limits, we update the metrics |
| | 40 | | // So if someone check the current amount when subscribed to the OnMetricsUpdated, we will try to Update the met |
| | 41 | | public SceneMetricsModel currentCount |
| | 42 | | { |
| | 43 | | get |
| | 44 | | { |
| 100 | 45 | | UpdateMetrics(); |
| 100 | 46 | | return currentCountValue.Clone(); |
| | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| 575 | 50 | | public SceneMetricsModel maxCount => maxCountValue.Clone(); |
| | 51 | |
|
| 0 | 52 | | public bool dirty { get; private set; } |
| | 53 | |
|
| | 54 | | private string sceneId; |
| | 55 | |
|
| | 56 | | private Vector2Int scenePosition; |
| | 57 | |
|
| | 58 | | private int sceneParcelCount; |
| | 59 | |
|
| | 60 | | private DataStore_WorldObjects data; |
| | 61 | | private bool enabled = false; |
| | 62 | |
|
| 10 | 63 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore, string sceneId, Vector2Int scenePosition, int scene |
| | 64 | | { |
| 10 | 65 | | this.data = dataStore; |
| 10 | 66 | | Configure(sceneId, scenePosition, sceneParcelCount); |
| 10 | 67 | | } |
| | 68 | |
|
| 539 | 69 | | public SceneMetricsCounter(DataStore_WorldObjects dataStore) |
| | 70 | | { |
| 539 | 71 | | this.data = dataStore; |
| 539 | 72 | | } |
| | 73 | |
|
| | 74 | | public void Configure(string sceneId, Vector2Int scenePosition, int sceneParcelCount) |
| | 75 | | { |
| 542 | 76 | | this.sceneId = sceneId; |
| 542 | 77 | | this.scenePosition = scenePosition; |
| 542 | 78 | | this.sceneParcelCount = sceneParcelCount; |
| | 79 | |
|
| 542 | 80 | | Assert.IsTrue( !string.IsNullOrEmpty(sceneId), "Scene must have an ID!" ); |
| 542 | 81 | | maxCountValue = ComputeMaxCount(); |
| 542 | 82 | | } |
| | 83 | |
|
| | 84 | | public void Dispose() |
| | 85 | | { |
| 539 | 86 | | } |
| | 87 | |
|
| | 88 | |
|
| | 89 | | public void Enable() |
| | 90 | | { |
| 542 | 91 | | if ( enabled ) |
| 0 | 92 | | return; |
| | 93 | |
|
| 542 | 94 | | var sceneData = data.sceneData[sceneId]; |
| | 95 | |
|
| 542 | 96 | | sceneData.materials.OnAdded += OnDataChanged; |
| 542 | 97 | | sceneData.materials.OnRemoved += OnDataChanged; |
| | 98 | |
|
| 542 | 99 | | sceneData.textures.OnAdded += OnTextureAdded; |
| 542 | 100 | | sceneData.textures.OnRemoved += OnTextureRemoved; |
| | 101 | |
|
| 542 | 102 | | sceneData.meshes.OnAdded += OnMeshAdded; |
| 542 | 103 | | sceneData.meshes.OnRemoved += OnMeshRemoved; |
| | 104 | |
|
| 542 | 105 | | sceneData.animationClips.OnAdded += OnAnimationClipAdded; |
| 542 | 106 | | sceneData.animationClips.OnRemoved += OnAnimationClipRemoved; |
| | 107 | |
|
| 542 | 108 | | sceneData.animationClipSize.OnChange += OnAnimationClipSizeChange; |
| 542 | 109 | | sceneData.meshDataSize.OnChange += OnMeshDataSizeChange; |
| | 110 | |
|
| 542 | 111 | | sceneData.audioClips.OnAdded += OnAudioClipAdded; |
| 542 | 112 | | sceneData.audioClips.OnRemoved += OnAudioClipRemoved; |
| | 113 | |
|
| 542 | 114 | | sceneData.renderers.OnAdded += OnDataChanged; |
| 542 | 115 | | sceneData.renderers.OnRemoved += OnDataChanged; |
| | 116 | |
|
| 542 | 117 | | sceneData.owners.OnAdded += OnDataChanged; |
| 542 | 118 | | sceneData.owners.OnRemoved += OnDataChanged; |
| | 119 | |
|
| 542 | 120 | | sceneData.triangles.OnChange += OnDataChanged; |
| | 121 | |
|
| 542 | 122 | | enabled = true; |
| 542 | 123 | | } |
| | 124 | |
|
| | 125 | | public void Disable() |
| | 126 | | { |
| 543 | 127 | | if ( !enabled ) |
| 11 | 128 | | return; |
| | 129 | |
|
| 532 | 130 | | var sceneData = data.sceneData[sceneId]; |
| | 131 | |
|
| 532 | 132 | | sceneData.materials.OnAdded -= OnDataChanged; |
| 532 | 133 | | sceneData.materials.OnRemoved -= OnDataChanged; |
| | 134 | |
|
| 532 | 135 | | sceneData.textures.OnAdded -= OnTextureAdded; |
| 532 | 136 | | sceneData.textures.OnRemoved -= OnTextureRemoved; |
| | 137 | |
|
| 532 | 138 | | sceneData.meshes.OnAdded -= OnMeshAdded; |
| 532 | 139 | | sceneData.meshes.OnRemoved -= OnMeshRemoved; |
| | 140 | |
|
| 532 | 141 | | sceneData.animationClipSize.OnChange -= OnAnimationClipSizeChange; |
| 532 | 142 | | sceneData.meshDataSize.OnChange -= OnMeshDataSizeChange; |
| | 143 | |
|
| 532 | 144 | | sceneData.audioClips.OnAdded -= OnAudioClipAdded; |
| 532 | 145 | | sceneData.audioClips.OnRemoved -= OnAudioClipRemoved; |
| | 146 | |
|
| 532 | 147 | | sceneData.animationClips.OnAdded -= OnDataChanged; |
| 532 | 148 | | sceneData.animationClips.OnRemoved -= OnDataChanged; |
| | 149 | |
|
| 532 | 150 | | sceneData.renderers.OnAdded -= OnDataChanged; |
| 532 | 151 | | sceneData.renderers.OnRemoved -= OnDataChanged; |
| | 152 | |
|
| 532 | 153 | | sceneData.owners.OnAdded -= OnDataChanged; |
| 532 | 154 | | sceneData.owners.OnRemoved -= OnDataChanged; |
| | 155 | |
|
| 532 | 156 | | sceneData.triangles.OnChange -= OnDataChanged; |
| | 157 | |
|
| 532 | 158 | | enabled = false; |
| 532 | 159 | | } |
| | 160 | |
|
| | 161 | | private SceneMetricsModel ComputeMaxCount() |
| | 162 | | { |
| 547 | 163 | | var result = new SceneMetricsModel(); |
| | 164 | |
|
| 547 | 165 | | float log = Mathf.Log(sceneParcelCount + 1, 2); |
| 547 | 166 | | float lineal = sceneParcelCount; |
| | 167 | |
|
| 547 | 168 | | result.triangles = (int) (lineal * LimitsConfig.triangles); |
| 547 | 169 | | result.bodies = (int) (lineal * LimitsConfig.bodies); |
| 547 | 170 | | result.entities = (int) (lineal * LimitsConfig.entities); |
| 547 | 171 | | result.materials = (int) (log * LimitsConfig.materials); |
| 547 | 172 | | result.textures = (int) (log * LimitsConfig.textures); |
| 547 | 173 | | result.meshes = (int) (log * LimitsConfig.meshes); |
| 547 | 174 | | result.sceneHeight = (int) (log * LimitsConfig.height); |
| | 175 | |
|
| 547 | 176 | | return result; |
| | 177 | | } |
| | 178 | |
|
| | 179 | | public bool IsInsideTheLimits() |
| | 180 | | { |
| 5 | 181 | | UpdateMetrics(); |
| 5 | 182 | | SceneMetricsModel limits = ComputeMaxCount(); |
| 5 | 183 | | SceneMetricsModel usage = currentCountValue; |
| | 184 | |
|
| 5 | 185 | | if (usage.triangles > limits.triangles) |
| 0 | 186 | | return false; |
| | 187 | |
|
| 5 | 188 | | if (usage.bodies > limits.bodies) |
| 0 | 189 | | return false; |
| | 190 | |
|
| 5 | 191 | | if (usage.entities > limits.entities) |
| 1 | 192 | | return false; |
| | 193 | |
|
| 4 | 194 | | if (usage.materials > limits.materials) |
| 0 | 195 | | return false; |
| | 196 | |
|
| 4 | 197 | | if (usage.textures > limits.textures) |
| 0 | 198 | | return false; |
| | 199 | |
|
| 4 | 200 | | if (usage.meshes > limits.meshes) |
| 0 | 201 | | return false; |
| | 202 | |
|
| 4 | 203 | | return true; |
| | 204 | | } |
| | 205 | |
|
| | 206 | | private void MarkDirty() |
| | 207 | | { |
| 0 | 208 | | dirty = true; |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | public void SendEvent() |
| | 212 | | { |
| 759 | 213 | | if (!dirty) |
| 581 | 214 | | return; |
| | 215 | |
|
| 178 | 216 | | dirty = false; |
| | 217 | |
|
| 178 | 218 | | UpdateMetrics(); |
| | 219 | |
|
| 178 | 220 | | Interface.WebInterface.ReportOnMetricsUpdate(sceneId, currentCountValue.ToMetricsModel(), maxCount.ToMetrics |
| 178 | 221 | | } |
| | 222 | |
|
| | 223 | | void OnMeshAdded(Mesh mesh) |
| | 224 | | { |
| 414 | 225 | | if (mesh == null) |
| 0 | 226 | | return; |
| | 227 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 414 | 228 | | currentCountValue.meshMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(mesh); |
| | 229 | | #endif |
| 414 | 230 | | MarkDirty(); |
| 414 | 231 | | } |
| | 232 | |
|
| | 233 | | void OnMeshRemoved(Mesh mesh) |
| | 234 | | { |
| 134 | 235 | | if (mesh == null) |
| 0 | 236 | | return; |
| | 237 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 134 | 238 | | currentCountValue.meshMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(mesh); |
| | 239 | | #endif |
| 134 | 240 | | MarkDirty(); |
| 134 | 241 | | } |
| | 242 | |
|
| | 243 | | void OnAnimationClipAdded(AnimationClip animationClip) |
| | 244 | | { |
| 12 | 245 | | if (animationClip == null) |
| 0 | 246 | | return; |
| | 247 | |
|
| | 248 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 12 | 249 | | currentCountValue.animationClipMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(animationClip); |
| | 250 | | #endif |
| 12 | 251 | | MarkDirty(); |
| 12 | 252 | | } |
| | 253 | |
|
| | 254 | | void OnAnimationClipRemoved(AnimationClip animationClip) |
| | 255 | | { |
| 2 | 256 | | if (animationClip == null) |
| 0 | 257 | | return; |
| | 258 | |
|
| | 259 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 2 | 260 | | currentCountValue.animationClipMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(animationClip); |
| | 261 | | #endif |
| 2 | 262 | | MarkDirty(); |
| 2 | 263 | | } |
| | 264 | |
|
| | 265 | | void OnAudioClipAdded(AudioClip audioClip) |
| | 266 | | { |
| 18 | 267 | | if (audioClip == null) |
| 0 | 268 | | return; |
| | 269 | |
|
| 18 | 270 | | MarkDirty(); |
| | 271 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 18 | 272 | | currentCountValue.audioClipMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(audioClip); |
| | 273 | | #endif |
| 18 | 274 | | currentCountValue.audioClipMemoryScore += MetricsScoreUtils.ComputeAudioClipScore(audioClip); |
| 18 | 275 | | } |
| | 276 | |
|
| | 277 | | void OnAudioClipRemoved(AudioClip audioClip) |
| | 278 | | { |
| 3 | 279 | | if (audioClip == null) |
| 0 | 280 | | return; |
| | 281 | |
|
| 3 | 282 | | MarkDirty(); |
| | 283 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 3 | 284 | | currentCountValue.audioClipMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(audioClip); |
| | 285 | | #endif |
| 3 | 286 | | currentCountValue.audioClipMemoryScore -= MetricsScoreUtils.ComputeAudioClipScore(audioClip); |
| 3 | 287 | | } |
| | 288 | |
|
| | 289 | |
|
| | 290 | | private void OnMeshDataSizeChange(long current, long previous) |
| | 291 | | { |
| 93 | 292 | | MarkDirty(); |
| 93 | 293 | | currentCountValue.meshMemoryScore = current; |
| 93 | 294 | | } |
| | 295 | |
|
| | 296 | | void OnAnimationClipSizeChange(long animationClipSize, long previous) |
| | 297 | | { |
| 15 | 298 | | MarkDirty(); |
| 15 | 299 | | currentCountValue.animationClipMemoryScore = animationClipSize; |
| 15 | 300 | | } |
| | 301 | |
|
| | 302 | | void OnTextureAdded(Texture texture) |
| | 303 | | { |
| 203 | 304 | | if (texture == null) |
| 0 | 305 | | return; |
| | 306 | |
|
| 203 | 307 | | MarkDirty(); |
| | 308 | |
|
| 203 | 309 | | if (texture is Texture2D tex2D) |
| | 310 | | { |
| | 311 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 203 | 312 | | currentCountValue.textureMemoryProfiler += Profiler.GetRuntimeMemorySizeLong(tex2D); |
| | 313 | | #endif |
| 203 | 314 | | currentCountValue.textureMemoryScore += MetricsScoreUtils.ComputeTextureScore(tex2D); |
| | 315 | | } |
| 203 | 316 | | } |
| | 317 | |
|
| | 318 | | void OnTextureRemoved(Texture texture) |
| | 319 | | { |
| 48 | 320 | | if (texture == null) |
| 0 | 321 | | return; |
| | 322 | |
|
| 48 | 323 | | MarkDirty(); |
| | 324 | |
|
| 48 | 325 | | if (texture is Texture2D tex2D) |
| | 326 | | { |
| | 327 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 48 | 328 | | currentCountValue.textureMemoryProfiler -= Profiler.GetRuntimeMemorySizeLong(tex2D); |
| | 329 | | #endif |
| 48 | 330 | | currentCountValue.textureMemoryScore -= MetricsScoreUtils.ComputeTextureScore(tex2D); |
| | 331 | | } |
| 48 | 332 | | } |
| | 333 | |
|
| | 334 | | void OnDataChanged<T>(T obj) |
| | 335 | | where T : class |
| | 336 | | { |
| 873 | 337 | | MarkDirty(); |
| 873 | 338 | | } |
| | 339 | |
|
| | 340 | | void OnDataChanged(int obj1, int obj2) |
| | 341 | | { |
| 496 | 342 | | MarkDirty(); |
| 496 | 343 | | } |
| | 344 | |
|
| | 345 | | void OnDataChanged(long obj1) |
| | 346 | | { |
| 1355 | 347 | | MarkDirty(); |
| 1355 | 348 | | } |
| | 349 | |
|
| | 350 | | private void UpdateMetrics() |
| | 351 | | { |
| 283 | 352 | | if (string.IsNullOrEmpty(sceneId) || data == null || !data.sceneData.ContainsKey(sceneId)) |
| 0 | 353 | | return; |
| | 354 | |
|
| 283 | 355 | | if (data != null && data.sceneData.ContainsKey(sceneId)) |
| | 356 | | { |
| 283 | 357 | | var sceneData = data.sceneData[sceneId]; |
| | 358 | |
|
| 283 | 359 | | if (sceneData != null) |
| | 360 | | { |
| 283 | 361 | | currentCountValue.materials = sceneData.materials.Count(); |
| 283 | 362 | | currentCountValue.textures = sceneData.textures.Count(); |
| 283 | 363 | | currentCountValue.meshes = sceneData.meshes.Count(); |
| 283 | 364 | | currentCountValue.entities = sceneData.owners.Count(); |
| 283 | 365 | | currentCountValue.bodies = sceneData.renderers.Count(); |
| 283 | 366 | | currentCountValue.triangles = sceneData.triangles.Get() / 3; |
| | 367 | | } |
| | 368 | | } |
| | 369 | |
|
| 283 | 370 | | logger.Verbose($"Current metrics: {currentCountValue}"); |
| 283 | 371 | | RaiseMetricsUpdate(); |
| 283 | 372 | | } |
| | 373 | |
|
| | 374 | | private void UpdateWorstMetricsOffense() |
| | 375 | | { |
| 283 | 376 | | DataStore_SceneMetrics metricsData = DataStore.i.Get<DataStore_SceneMetrics>(); |
| | 377 | |
|
| 283 | 378 | | if ( maxCountValue != null && metricsData.worstMetricOffenseComputeEnabled.Get() ) |
| | 379 | | { |
| 283 | 380 | | bool isOffending = currentCountValue > maxCountValue; |
| | 381 | |
|
| 283 | 382 | | if ( !isOffending ) |
| 282 | 383 | | return; |
| | 384 | |
|
| 1 | 385 | | bool firstOffense = false; |
| | 386 | |
|
| 1 | 387 | | if (!metricsData.worstMetricOffenses.ContainsKey(sceneId)) |
| | 388 | | { |
| 1 | 389 | | firstOffense = true; |
| 1 | 390 | | metricsData.worstMetricOffenses[sceneId] = currentCountValue.Clone(); |
| | 391 | | } |
| | 392 | |
|
| 1 | 393 | | SceneMetricsModel worstOffense = metricsData.worstMetricOffenses[sceneId]; |
| 1 | 394 | | SceneMetricsModel currentOffense = maxCountValue - currentCountValue; |
| | 395 | |
|
| 1 | 396 | | if ( firstOffense ) |
| 1 | 397 | | logger.Verbose($"New offending scene {sceneId} ({scenePosition})!\n{currentCountValue}"); |
| | 398 | |
|
| 1 | 399 | | if ( currentOffense < worstOffense ) |
| 1 | 400 | | return; |
| | 401 | |
|
| 0 | 402 | | metricsData.worstMetricOffenses[sceneId] = currentOffense; |
| 0 | 403 | | logger.Verbose($"New offending scene {sceneId} {scenePosition}!\nmetrics: {currentCountValue}\nlimits: { |
| | 404 | | } |
| 0 | 405 | | } |
| | 406 | |
|
| | 407 | | private void RaiseMetricsUpdate() |
| | 408 | | { |
| 283 | 409 | | UpdateWorstMetricsOffense(); |
| 283 | 410 | | if (!currentCountValue.Equals(lastCountValue)) |
| | 411 | | { |
| 163 | 412 | | lastCountValue = currentCountValue; |
| 163 | 413 | | OnMetricsUpdated?.Invoke(this); |
| | 414 | | } |
| 120 | 415 | | } |
| | 416 | | } |
| | 417 | | } |