Profiler threading support, flame graph
Changed the profiler into a node based system for better data access, more overhead than the simple struct+depth info but can hold more detail and less post processing of data Profiler now also profiles threads Added some test profile tags The profiler window now also has a FlameGraph
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using ImGuiNET;
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime;
|
||||
using System;
|
||||
|
||||
namespace Nerfed.Editor.Systems
|
||||
{
|
||||
@@ -11,7 +10,9 @@ namespace Nerfed.Editor.Systems
|
||||
const ImGuiTreeNodeFlags treeNodeFlags = ImGuiTreeNodeFlags.SpanAllColumns;
|
||||
const ImGuiTreeNodeFlags treeNodeLeafFlags = ImGuiTreeNodeFlags.SpanAllColumns | ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen;
|
||||
|
||||
int frame = 0;
|
||||
private int selectedFrame = 0;
|
||||
private int previousSelectedFrame = -1;
|
||||
private IOrderedEnumerable<KeyValuePair<string, (double ms, uint calls)>> orderedCombinedData = null;
|
||||
|
||||
public EditorProfilerWindow(World world) : base(world)
|
||||
{
|
||||
@@ -19,7 +20,7 @@ namespace Nerfed.Editor.Systems
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
if (Profiler.frames.Count <= 0)
|
||||
if (Profiler.Frames.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -27,71 +28,117 @@ namespace Nerfed.Editor.Systems
|
||||
ImGui.Begin("Profiler");
|
||||
|
||||
ImGui.BeginChild("Toolbar", new System.Numerics.Vector2(0, 0), ImGuiChildFlags.AutoResizeY);
|
||||
if (ImGui.RadioButton("Recording", Profiler.recording))
|
||||
if (ImGui.RadioButton("Recording", Profiler.IsRecording))
|
||||
{
|
||||
Profiler.recording = !Profiler.recording;
|
||||
Profiler.SetActive(!Profiler.IsRecording);
|
||||
}
|
||||
|
||||
ImGui.SameLine();
|
||||
|
||||
if (Profiler.recording)
|
||||
if (Profiler.IsRecording)
|
||||
{
|
||||
frame = Profiler.frames.Count - 1;
|
||||
// Select last frame when recording to see latest frame data.
|
||||
selectedFrame = Profiler.Frames.Count - 1;
|
||||
}
|
||||
if (ImGui.SliderInt(string.Empty, ref frame, 0, Profiler.frames.Count - 1))
|
||||
if (ImGui.SliderInt(string.Empty, ref selectedFrame, 0, Profiler.Frames.Count - 1))
|
||||
{
|
||||
Profiler.recording = false;
|
||||
// Stop recording when browsing frames.
|
||||
Profiler.SetActive(false);
|
||||
}
|
||||
|
||||
Profiler.FrameData frameData = Profiler.frames.ElementAt(frame);
|
||||
double ms = frameData.ElapsedMilliseconds();
|
||||
Profiler.Frame frame = Profiler.Frames.ElementAt(selectedFrame);
|
||||
double ms = frame.ElapsedMilliseconds();
|
||||
double s = 1000;
|
||||
ImGui.Text($"Frame: {frameData.frame} ({ms:0.000} ms | {(s / ms):0} fps)");
|
||||
ImGui.Text($"Frame: {frame.FrameCount} ({ms:0.000} ms | {(s / ms):0} fps)");
|
||||
ImGui.EndChild();
|
||||
|
||||
if (!Profiler.IsRecording) {
|
||||
if (previousSelectedFrame != selectedFrame)
|
||||
{
|
||||
previousSelectedFrame = selectedFrame;
|
||||
orderedCombinedData = CalculateCombinedData(frame);
|
||||
}
|
||||
|
||||
DrawFlameGraph(frame);
|
||||
|
||||
DrawHierachy(frame);
|
||||
|
||||
ImGui.SameLine();
|
||||
|
||||
DrawCombined(orderedCombinedData);
|
||||
}
|
||||
|
||||
ImGui.End();
|
||||
}
|
||||
|
||||
private static void DrawHierachy(Profiler.Frame frame)
|
||||
{
|
||||
if(frame == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui.BeginChild("Hierachy", new System.Numerics.Vector2(150, 0), ImGuiChildFlags.ResizeX);
|
||||
|
||||
if (ImGui.BeginTable("ProfilerData", 2, tableFlags, new System.Numerics.Vector2(0, 0)))
|
||||
if (ImGui.BeginTable("ProfilerData", 3, tableFlags, new System.Numerics.Vector2(0, 0)))
|
||||
{
|
||||
ImGui.TableSetupColumn("name", ImGuiTableColumnFlags.WidthStretch, 0.8f, 0);
|
||||
ImGui.TableSetupColumn("thread", ImGuiTableColumnFlags.WidthStretch, 0.2f, 1);
|
||||
ImGui.TableSetupColumn("ms", ImGuiTableColumnFlags.WidthStretch, 0.2f, 1);
|
||||
ImGui.TableSetupScrollFreeze(0, 1); // Make row always visible
|
||||
ImGui.TableHeadersRow();
|
||||
|
||||
foreach (Profiler.ProfileRecord record in frameData.records)
|
||||
foreach (Profiler.ScopeNode node in frame.RootNodes)
|
||||
{
|
||||
ImGui.TableNextRow();
|
||||
ImGui.TableNextColumn();
|
||||
string indentation = new string(' ', record.depth); // Indentation based on depth
|
||||
ImGui.Text($"{indentation}{record.label}");
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{record.ElapsedMilliseconds():0.000}");
|
||||
DrawHierachyNode(node);
|
||||
}
|
||||
|
||||
ImGui.EndTable();
|
||||
}
|
||||
|
||||
ImGui.EndChild();
|
||||
}
|
||||
|
||||
ImGui.SameLine();
|
||||
private static void DrawHierachyNode(Profiler.ScopeNode node)
|
||||
{
|
||||
ImGui.TableNextRow();
|
||||
ImGui.TableNextColumn();
|
||||
|
||||
bool isOpen = false;
|
||||
bool isLeaf = node.Children.Count == 0;
|
||||
|
||||
if (isLeaf) {
|
||||
ImGui.TreeNodeEx(node.Label, treeNodeLeafFlags);
|
||||
}
|
||||
else
|
||||
{
|
||||
isOpen = ImGui.TreeNodeEx(node.Label, treeNodeFlags);
|
||||
}
|
||||
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{node.ManagedThreadId}");
|
||||
ImGui.TableNextColumn();
|
||||
ImGui.Text($"{node.ElapsedMilliseconds():0.000}");
|
||||
|
||||
if (isOpen)
|
||||
{
|
||||
for (int i = 0; i < node.Children.Count; i++)
|
||||
{
|
||||
DrawHierachyNode(node.Children[i]);
|
||||
}
|
||||
ImGui.TreePop();
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawCombined(in IOrderedEnumerable<KeyValuePair<string, (double ms, uint calls)>> orderedCombinedData)
|
||||
{
|
||||
if(orderedCombinedData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui.BeginChild("Combined", new System.Numerics.Vector2(0, 0));
|
||||
|
||||
// Gather combined data.
|
||||
Dictionary<string, (double ms, uint calls)> combinedRecordData = new Dictionary<string, (double ms, uint calls)>(128);
|
||||
foreach (Profiler.ProfileRecord record in frameData.records)
|
||||
{
|
||||
if (combinedRecordData.TryGetValue(record.label, out (double ms, uint calls) combined))
|
||||
{
|
||||
combinedRecordData[record.label] = (combined.ms + record.ElapsedMilliseconds(), combined.calls + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
combinedRecordData.Add(record.label, (record.ElapsedMilliseconds(), 1));
|
||||
}
|
||||
}
|
||||
IOrderedEnumerable<KeyValuePair<string, (double ms, uint calls)>> orderedCombinedData = combinedRecordData.OrderByDescending(x => x.Value.ms);
|
||||
|
||||
if (ImGui.BeginTable("ProfilerCombinedData", 3, tableFlags, new System.Numerics.Vector2(0, 0)))
|
||||
{
|
||||
ImGui.TableSetupColumn("name", ImGuiTableColumnFlags.WidthStretch, 0.6f, 0);
|
||||
@@ -115,12 +162,43 @@ namespace Nerfed.Editor.Systems
|
||||
}
|
||||
|
||||
ImGui.EndChild();
|
||||
ImGui.End();
|
||||
}
|
||||
|
||||
private void Draw()
|
||||
private static IOrderedEnumerable<KeyValuePair<string, (double ms, uint calls)>> CalculateCombinedData(Profiler.Frame frame)
|
||||
{
|
||||
Dictionary<string, (double ms, uint calls)> combinedRecordData = new Dictionary<string, (double ms, uint calls)>(128);
|
||||
foreach (Profiler.ScopeNode node in frame.RootNodes)
|
||||
{
|
||||
CalculateCombinedData(node, in combinedRecordData);
|
||||
}
|
||||
return combinedRecordData.OrderByDescending(x => x.Value.ms);
|
||||
}
|
||||
|
||||
private static void CalculateCombinedData(Profiler.ScopeNode node, in Dictionary<string, (double ms, uint calls)> combinedRecordData)
|
||||
{
|
||||
if (combinedRecordData.TryGetValue(node.Label, out (double ms, uint calls) combined))
|
||||
{
|
||||
combinedRecordData[node.Label] = (combined.ms + node.ElapsedMilliseconds(), combined.calls + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
combinedRecordData.Add(node.Label, (node.ElapsedMilliseconds(), 1));
|
||||
}
|
||||
|
||||
for (int i = 0; i < node.Children.Count; i++)
|
||||
{
|
||||
CalculateCombinedData(node.Children[i], combinedRecordData);
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawFlameGraph(Profiler.Frame frame)
|
||||
{
|
||||
if (frame == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ProfilerVisualizer.RenderFlameGraph(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user