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