| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.FPSDisplay; |
| | 5 | | using DCL.SettingsCommon; |
| | 6 | | using MainScripts.DCL.Analytics.PerformanceAnalytics; |
| | 7 | | using MainScripts.DCL.WorldRuntime.Debugging.Performance; |
| | 8 | | using Newtonsoft.Json; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace DCL |
| | 12 | | { |
| | 13 | | public class PerformanceMetricsController |
| | 14 | | { |
| | 15 | | private const int SAMPLES_SIZE = 1000; // Send performance report every 1000 samples |
| | 16 | | private const string PROFILER_METRICS_FEATURE_FLAG = "profiler_metrics"; |
| | 17 | |
|
| 0 | 18 | | private readonly char[] encodedSamples = new char[SAMPLES_SIZE]; |
| 0 | 19 | | private readonly Dictionary<int, long> scenesMemoryScore = new (); |
| | 20 | |
|
| | 21 | | private readonly PerformanceMetricsDataVariable performanceMetricsDataVariable; |
| | 22 | | private readonly LinealBufferHiccupCounter tracker; |
| | 23 | | private readonly IProfilerRecordsService profilerRecordsService; |
| | 24 | |
|
| 0 | 25 | | private BaseVariable<FeatureFlag> featureFlags => DataStore.i.featureFlags.flags; |
| 0 | 26 | | private IWorldState worldState => Environment.i.world.state; |
| 0 | 27 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| 0 | 28 | | private GeneralSettings generalSettings => Settings.i.generalSettings.Data; |
| | 29 | |
|
| | 30 | | private bool trackProfileRecords; |
| | 31 | | private int currentIndex; |
| | 32 | | private long totalAllocSample; |
| | 33 | |
|
| 0 | 34 | | public PerformanceMetricsController() |
| | 35 | | { |
| 0 | 36 | | performanceMetricsDataVariable = Resources.Load<PerformanceMetricsDataVariable>("ScriptableObjects/Performan |
| | 37 | |
|
| 0 | 38 | | profilerRecordsService = Environment.i.serviceLocator.Get<IProfilerRecordsService>(); |
| 0 | 39 | | tracker = new LinealBufferHiccupCounter(SAMPLES_SIZE); |
| | 40 | |
|
| 0 | 41 | | featureFlags.OnChange += OnFeatureFlagChange; |
| 0 | 42 | | OnFeatureFlagChange(featureFlags.Get(), null); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | private void OnFeatureFlagChange(FeatureFlag current, FeatureFlag previous) |
| | 46 | | { |
| 0 | 47 | | trackProfileRecords = current.IsFeatureEnabled(PROFILER_METRICS_FEATURE_FLAG); |
| | 48 | |
|
| 0 | 49 | | if (trackProfileRecords) |
| 0 | 50 | | profilerRecordsService.RecordAdditionalProfilerMetrics(); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void Update() |
| | 54 | | { |
| | 55 | | #if !UNITY_EDITOR && UNITY_WEBGL |
| | 56 | | if (!CommonScriptableObjects.focusState.Get()) |
| | 57 | | return; |
| | 58 | | #endif |
| 0 | 59 | | if (!CommonScriptableObjects.rendererState.Get() && !CommonScriptableObjects.forcePerformanceMeter.Get()) |
| 0 | 60 | | return; |
| | 61 | |
|
| 0 | 62 | | tracker.AddDeltaTime(Time.unscaledDeltaTime); |
| 0 | 63 | | performanceMetricsDataVariable.Set(profilerRecordsService.LastFPS, tracker.HiccupsCountInBuffer, tracker.Hic |
| | 64 | |
|
| 0 | 65 | | encodedSamples[currentIndex++] = (char)profilerRecordsService.LastFrameTimeInMS; |
| | 66 | |
|
| 0 | 67 | | if (trackProfileRecords) |
| 0 | 68 | | totalAllocSample += profilerRecordsService.GcAllocatedInFrame; |
| | 69 | |
|
| 0 | 70 | | if (currentIndex == SAMPLES_SIZE) |
| | 71 | | { |
| 0 | 72 | | currentIndex = 0; |
| 0 | 73 | | Report(new string(encodedSamples)); |
| 0 | 74 | | totalAllocSample = 0; |
| | 75 | | } |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | private void Report(string encodedSamples) |
| | 79 | | { |
| 0 | 80 | | var loadedScenesValues = worldState.GetLoadedScenes(); |
| 0 | 81 | | scenesMemoryScore.Clear(); |
| | 82 | |
|
| 0 | 83 | | foreach (var parcelScene in loadedScenesValues) |
| | 84 | | { |
| | 85 | | // we ignore global scene |
| 0 | 86 | | IParcelScene parcelSceneValue = parcelScene.Value; |
| | 87 | |
|
| 0 | 88 | | if (parcelSceneValue.isPersistent) |
| | 89 | | continue; |
| | 90 | |
|
| 0 | 91 | | scenesMemoryScore.Add(parcelSceneValue.sceneData.sceneNumber, parcelSceneValue.metricsCounter.currentCou |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | object drawCalls = null; |
| 0 | 95 | | object totalMemoryReserved = null; |
| 0 | 96 | | object totalMemoryUsage = null; |
| 0 | 97 | | object totalGCAlloc = null; |
| | 98 | |
|
| 0 | 99 | | if (trackProfileRecords) |
| | 100 | | { |
| 0 | 101 | | drawCalls = (int)profilerRecordsService.DrawCalls; |
| 0 | 102 | | totalMemoryReserved = profilerRecordsService.ReservedMemory; |
| 0 | 103 | | totalMemoryUsage = profilerRecordsService.UsedMemory; |
| 0 | 104 | | totalGCAlloc = totalAllocSample; |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | var playerCount = otherPlayers.Count(); |
| 0 | 108 | | var loadRadius = generalSettings.scenesLoadRadius; |
| | 109 | |
|
| 0 | 110 | | (int gltfloading, int gltffailed, int gltfcancelled, int gltfloaded) = PerformanceAnalytics.GLTFTracker.GetD |
| 0 | 111 | | (int abloading, int abfailed, int abcancelled, int abloaded) = PerformanceAnalytics.ABTracker.GetData(); |
| 0 | 112 | | var gltfTextures = PerformanceAnalytics.GLTFTextureTracker.Get(); |
| 0 | 113 | | var abTextures = PerformanceAnalytics.ABTextureTracker.Get(); |
| 0 | 114 | | var promiseTextures = PerformanceAnalytics.PromiseTextureTracker.Get(); |
| 0 | 115 | | var queuedMessages = PerformanceAnalytics.MessagesEnqueuedTracker.Get(); |
| 0 | 116 | | var processedMessages = PerformanceAnalytics.MessagesProcessedTracker.Get(); |
| | 117 | |
|
| 0 | 118 | | bool usingFPSCap = Settings.i.qualitySettings.Data.fpsCap; |
| | 119 | |
|
| 0 | 120 | | int hiccupsInThousandFrames = tracker.HiccupsCountInBuffer; |
| 0 | 121 | | float hiccupsTime = tracker.HiccupsSum; |
| | 122 | |
|
| 0 | 123 | | float totalTime = tracker.TotalSeconds; |
| | 124 | |
|
| 0 | 125 | | WebInterface.PerformanceReportPayload performanceReportPayload = new WebInterface.PerformanceReportPayload |
| | 126 | | { |
| | 127 | | samples = encodedSamples, |
| | 128 | | fpsIsCapped = usingFPSCap, |
| | 129 | | hiccupsInThousandFrames = hiccupsInThousandFrames, |
| | 130 | | hiccupsTime = hiccupsTime, |
| | 131 | | totalTime = totalTime, |
| | 132 | | gltfInProgress = gltfloading, |
| | 133 | | gltfFailed = gltffailed, |
| | 134 | | gltfCancelled = gltfcancelled, |
| | 135 | | gltfLoaded = gltfloaded, |
| | 136 | | abInProgress = abloading, |
| | 137 | | abFailed = abfailed, |
| | 138 | | abCancelled = abcancelled, |
| | 139 | | abLoaded = abloaded, |
| | 140 | | gltfTexturesLoaded = gltfTextures, |
| | 141 | | abTexturesLoaded = abTextures, |
| | 142 | | promiseTexturesLoaded = promiseTextures, |
| | 143 | | enqueuedMessages = queuedMessages, |
| | 144 | | processedMessages = processedMessages, |
| | 145 | | playerCount = playerCount, |
| | 146 | | loadRadius = (int)loadRadius, |
| | 147 | | sceneScores = scenesMemoryScore, |
| | 148 | | drawCalls = drawCalls, |
| | 149 | | memoryReserved = totalMemoryReserved, |
| | 150 | | memoryUsage = totalMemoryUsage, |
| | 151 | | totalGCAlloc = totalGCAlloc |
| | 152 | | }; |
| | 153 | |
|
| 0 | 154 | | string result = JsonConvert.SerializeObject(performanceReportPayload); |
| 0 | 155 | | WebInterface.SendPerformanceReport(result); |
| 0 | 156 | | PerformanceAnalytics.ResetAll(); |
| 0 | 157 | | } |
| | 158 | | } |
| | 159 | | } |