From a6050e6f27b8f99423d96a22f934cc7b77e6cc7b Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Fri, 6 Jan 2023 09:18:11 +0100 Subject: [PATCH] Add managed memory allocations profiling with dotnet7 --- Source/Editor/Windows/Profiler/Memory.cs | 2 +- Source/Editor/Windows/Profiler/ProfilerMode.cs | 5 +++++ Source/Editor/Windows/Profiler/ProfilerWindow.cs | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Source/Editor/Windows/Profiler/Memory.cs b/Source/Editor/Windows/Profiler/Memory.cs index 5d719a1b9..475320181 100644 --- a/Source/Editor/Windows/Profiler/Memory.cs +++ b/Source/Editor/Windows/Profiler/Memory.cs @@ -61,7 +61,7 @@ namespace FlaxEditor.Windows.Profiler { // Count memory allocated during last frame int nativeMemoryAllocation = 0; - int managedMemoryAllocation = 0; + int managedMemoryAllocation = sharedData.ManagedMemoryAllocation; var events = sharedData.GetEventsCPU(); var length = events?.Length ?? 0; for (int i = 0; i < length; i++) diff --git a/Source/Editor/Windows/Profiler/ProfilerMode.cs b/Source/Editor/Windows/Profiler/ProfilerMode.cs index 6d8b81cba..c385f24dc 100644 --- a/Source/Editor/Windows/Profiler/ProfilerMode.cs +++ b/Source/Editor/Windows/Profiler/ProfilerMode.cs @@ -24,6 +24,11 @@ namespace FlaxEditor.Windows.Profiler /// public ProfilingTools.MainStats Stats; + /// + /// The additional managed memory allocation size during this update. Given value is in bytes. + /// + public int ManagedMemoryAllocation; + /// /// Gets the collected CPU events by the profiler from local or remote session. /// diff --git a/Source/Editor/Windows/Profiler/ProfilerWindow.cs b/Source/Editor/Windows/Profiler/ProfilerWindow.cs index a40c38ea1..ac30b57e6 100644 --- a/Source/Editor/Windows/Profiler/ProfilerWindow.cs +++ b/Source/Editor/Windows/Profiler/ProfilerWindow.cs @@ -24,6 +24,8 @@ namespace FlaxEditor.Windows.Profiler private int _frameIndex = -1; private int _framesCount; private bool _showOnlyLastUpdateEvents = true; + private long _lastManagedMemory = 0; + private long _lastManagedMemoryProfiler = 0; /// /// Gets or sets a value indicating whether live events recording is enabled. @@ -143,6 +145,7 @@ namespace FlaxEditor.Windows.Profiler { _frameIndex = -1; _framesCount = 0; + _lastManagedMemory = 0; for (int i = 0; i < _tabs.ChildrenCount; i++) { if (_tabs.Children[i] is ProfilerMode mode) @@ -218,8 +221,16 @@ namespace FlaxEditor.Windows.Profiler { FlaxEngine.Profiler.BeginEvent("ProfilerWindow.OnUpdate"); + // Get memory allocations during last frame + long managedMemory = GC.GetAllocatedBytesForCurrentThread(); + if (_lastManagedMemory == 0) + _lastManagedMemory = managedMemory; + var managedAllocs = managedMemory - _lastManagedMemory - _lastManagedMemoryProfiler; + _lastManagedMemory = managedMemory; + ProfilerMode.SharedUpdateData sharedData = new ProfilerMode.SharedUpdateData(); sharedData.Begin(); + sharedData.ManagedMemoryAllocation = (int)managedAllocs; for (int i = 0; i < _tabs.ChildrenCount; i++) { if (_tabs.Children[i] is ProfilerMode mode) @@ -242,6 +253,10 @@ namespace FlaxEditor.Windows.Profiler _framesCount = Mathf.Min(_framesCount + 1, ProfilerMode.MaxSamples); UpdateButtons(); + // Get memory allocations within profiler window update to exclude from stats + managedMemory = GC.GetAllocatedBytesForCurrentThread(); + _lastManagedMemoryProfiler = managedMemory - _lastManagedMemory; + FlaxEngine.Profiler.EndEvent(); } }