| | 1 | | using Unity.Profiling; |
| | 2 | |
|
| | 3 | | namespace MainScripts.DCL.WorldRuntime.Debugging.Performance |
| | 4 | | { |
| | 5 | | public class ProfilerRecordsService : IProfilerRecordsService |
| | 6 | | { |
| | 7 | | private const int CAPACITY = 15; // Amounts of frames used for calculating average FPS |
| | 8 | |
|
| | 9 | | private ProfilerRecorder mainThreadTimeRecorder; |
| | 10 | |
|
| | 11 | | private ProfilerRecorder drawCallsRecorder; |
| | 12 | | private ProfilerRecorder reservedMemoryRecorder; |
| | 13 | | private ProfilerRecorder usedMemoryRecorder; |
| | 14 | | private ProfilerRecorder gcAllocatedInFrameRecorder; |
| | 15 | |
|
| | 16 | | private bool isRecordingAdditionalProfilers; |
| | 17 | |
|
| 0 | 18 | | public float LastFrameTimeInMS => mainThreadTimeRecorder.LastValue * 1e-6f; // [sec] |
| 0 | 19 | | public float LastFPS => 1 / (mainThreadTimeRecorder.LastValue * 1e-9f); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Average frame time and FPS. In-game use only due to overhead (not for external analytics) |
| | 23 | | /// </summary> |
| | 24 | | public (float FrameTime, float FPS) AverageData |
| | 25 | | { |
| | 26 | | get |
| | 27 | | { |
| 0 | 28 | | float frameTime = GetRecorderFrameAverage(mainThreadTimeRecorder) * 1e-6f; // [ms] |
| 0 | 29 | | return (FrameTime: frameTime, FPS: 1000 / frameTime); |
| | 30 | | } |
| | 31 | | } |
| | 32 | |
|
| | 33 | | // Additional recordings |
| 0 | 34 | | public long GcAllocatedInFrame => gcAllocatedInFrameRecorder.LastValue; |
| 0 | 35 | | public long UsedMemory => usedMemoryRecorder.LastValue; |
| 0 | 36 | | public long ReservedMemory => reservedMemoryRecorder.LastValue; |
| 0 | 37 | | public long DrawCalls => drawCallsRecorder.LastValue; |
| | 38 | |
|
| 425 | 39 | | public ProfilerRecordsService() |
| | 40 | | { |
| 425 | 41 | | mainThreadTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "Main Thread", CAPACITY); |
| 425 | 42 | | } |
| | 43 | |
|
| 425 | 44 | | public void Initialize() { } |
| | 45 | |
|
| | 46 | | public void Dispose() |
| | 47 | | { |
| 425 | 48 | | mainThreadTimeRecorder.Dispose(); |
| | 49 | |
|
| 425 | 50 | | drawCallsRecorder.Dispose(); |
| 425 | 51 | | reservedMemoryRecorder.Dispose(); |
| 425 | 52 | | usedMemoryRecorder.Dispose(); |
| 425 | 53 | | gcAllocatedInFrameRecorder.Dispose(); |
| 425 | 54 | | } |
| | 55 | |
|
| | 56 | | public void RecordAdditionalProfilerMetrics() |
| | 57 | | { |
| 0 | 58 | | if (isRecordingAdditionalProfilers) return; |
| | 59 | |
|
| 0 | 60 | | drawCallsRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Render, "Draw Calls Count"); |
| 0 | 61 | | reservedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Reserved Memory"); |
| 0 | 62 | | usedMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "Total Used Memory"); |
| 0 | 63 | | gcAllocatedInFrameRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Allocated In Frame"); |
| | 64 | |
|
| 0 | 65 | | isRecordingAdditionalProfilers = true; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void StartRecordGCAllocatedInFrame() |
| | 69 | | { |
| 0 | 70 | | if (isRecordingAdditionalProfilers) |
| 0 | 71 | | return; |
| | 72 | |
|
| 0 | 73 | | gcAllocatedInFrameRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Allocated In Frame"); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public void StopRecordGCAllocatedInFrame() |
| | 77 | | { |
| 0 | 78 | | if (isRecordingAdditionalProfilers) |
| 0 | 79 | | return; |
| | 80 | |
|
| 0 | 81 | | gcAllocatedInFrameRecorder.Dispose(); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | private static float GetRecorderFrameAverage(ProfilerRecorder recorder) |
| | 85 | | { |
| 0 | 86 | | int samplesCount = recorder.Capacity; |
| | 87 | |
|
| 0 | 88 | | if (samplesCount == 0) |
| 0 | 89 | | return 0; |
| | 90 | |
|
| 0 | 91 | | float r = 0; |
| | 92 | |
|
| | 93 | | unsafe |
| | 94 | | { |
| 0 | 95 | | ProfilerRecorderSample* samples = stackalloc ProfilerRecorderSample[samplesCount]; |
| 0 | 96 | | recorder.CopyTo(samples, samplesCount); |
| | 97 | |
|
| 0 | 98 | | for (var i = 0; i < samplesCount; ++i) |
| 0 | 99 | | r += samples[i].Value; |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | return r / samplesCount; |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |