Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add AsyncProfiler #360

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- [#360] Added `AsyncProfiler` to help profile code running in Tasks
- Also added profiler scopes for `IRenderFilter.Create`/`IRenderFilterNode.Refresh`.
- [#361] Added `IRenderFilterNode.OnFrameGroup`

### Fixed
Expand Down
89 changes: 89 additions & 0 deletions Editor/AsyncProfiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Threading;
using UnityEngine.Profiling;
using Object = UnityEngine.Object;

namespace nadena.dev.ndmf
{
public static class AsyncProfiler
{
private class ProfilerFrame
{
public ProfilerFrame Parent;
public ProfilerFrame Root;
public int Depth;

public string Context;
public Object Object;
}

private static readonly AsyncLocal<ProfilerFrame> _currentFrame = new(OnFrameChange);

private static void OnFrameChange(AsyncLocalValueChangedArgs<ProfilerFrame> obj)
{
var currentFrame = obj.PreviousValue;
var newFrame = obj.CurrentValue;

while (currentFrame != null && (newFrame == null || newFrame.Root != currentFrame.Root ||
newFrame.Depth <= currentFrame.Depth))
{
Profiler.EndSample();
currentFrame = currentFrame.Parent;
}

EnterFrameRecursive(newFrame, currentFrame?.Depth ?? -1);
}

private static void EnterFrameRecursive(ProfilerFrame newFrame, int currentFrameDepth)
{
if (newFrame == null || newFrame.Depth <= currentFrameDepth) return;

if (newFrame.Parent != null) EnterFrameRecursive(newFrame.Parent, currentFrameDepth);

Profiler.BeginSample(newFrame.Context, newFrame.Object);
}

public static IDisposable PushProfilerContext(string context, Object obj = null)
{
var currentFrame = _currentFrame.Value;

var newFrame = new ProfilerFrame
{
Parent = currentFrame,
Root = currentFrame?.Root,
Depth = currentFrame?.Depth + 1 ?? 0,
Context = context,
Object = obj
};

if (newFrame.Root == null) newFrame.Root = newFrame;

_currentFrame.Value = newFrame;

return new PopFrame(newFrame);
}

public static void PopProfilerContext()
{
_currentFrame.Value = _currentFrame.Value?.Parent;
}

private class PopFrame : IDisposable
{
private readonly ProfilerFrame _targetFrame;

public PopFrame(ProfilerFrame targetFrame)
{
_targetFrame = targetFrame;
}

public void Dispose()
{
var currentFrame = _currentFrame.Value;
if (currentFrame.Root != _targetFrame.Root || currentFrame.Depth < _targetFrame.Depth) return;

_currentFrame.Value = _targetFrame;
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/AsyncProfiler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Editor/PreviewSystem/Rendering/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public static async Task<NodeController> Create(
string trace
)
{
AsyncProfiler.PushProfilerContext("NodeController.Create[" + filter + "]", group.Renderers[0].gameObject);
var context =
new ComputeContext("NodeController " + trace + " for " + filter + " on " +
group.Renderers[0].gameObject.name);
Expand Down Expand Up @@ -149,6 +150,7 @@ public async Task<NodeController> Refresh(
string trace
)
{
AsyncProfiler.PushProfilerContext("NodeController.Refresh[" + _filter + "]", _group.Renderers[0].gameObject);
var registry = ObjectRegistry.Merge(null, proxies.Select(p => p.Item3)
.Append(ObjectRegistry));
var context = new ComputeContext("NodeController (refresh) for " + _filter + " on " +
Expand Down
Loading