Skip to content

Commit a4285dc

Browse files
authored
CHANGE: Use ProfilerMarker in place of Profiler based on Unity version and presence of com.unity.profiling.core package (#1970)
* Change instances of Profiler.BeginSample with ProfilerMarkers where possible to enable capturing of data from ProfilerRecorders. * Update to the minimum editor version for the com.unity.profiling package. * Wrap ifdef logic into a profiler class to avoid sprinkling code with them. * revert change to analyze config. * Remove unused imports. Update changelog to better reflect changes. * Use m_Name instead of name. * Update member names to reflect coding conventions. * Use the Input ProfilerCategory for the profiler Markers.
1 parent 0e5d403 commit a4285dc

15 files changed

Lines changed: 167 additions & 79 deletions

File tree

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ however, it has to be formatted properly to pass verification tests.
1010

1111
## [Unreleased] - yyyy-mm-dd
1212

13+
### Changed
14+
- Use `ProfilerMarker` instead of `Profiler.BeginSample` and `Profiler.EndSample` when appropriate to enable recording of profiling data.
15+
1316
## [1.10.0] - 2024-07-24
1417

1518
### Fixed

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionState.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
using Unity.Collections.LowLevel.Unsafe;
77
using UnityEngine.InputSystem.Controls;
88
using UnityEngine.InputSystem.LowLevel;
9+
using UnityEngine.InputSystem.Profiler;
910
using UnityEngine.InputSystem.Utilities;
10-
using UnityEngine.Profiling;
11+
12+
using ProfilerMarker = Unity.Profiling.ProfilerMarker;
1113

1214
////TODO: now that we can bind to controls by display name, we need to re-resolve controls when those change (e.g. when the keyboard layout changes)
1315

@@ -122,6 +124,9 @@ internal unsafe class InputActionState : IInputStateChangeMonitor, ICloneable, I
122124
private InputEventPtr m_CurrentlyProcessingThisEvent;
123125
private Action m_OnBeforeUpdateDelegate;
124126
private Action m_OnAfterUpdateDelegate;
127+
private static readonly InputProfilerMarker k_InputInitialActionStateCheckMarker = new InputProfilerMarker("InitialActionStateCheck");
128+
private static readonly InputProfilerMarker k_InputActionResolveConflictMarker = new InputProfilerMarker("InputActionResolveConflict");
129+
private static readonly InputProfilerMarker k_InputActionCallbackMarker = new InputProfilerMarker("InputActionCallback");
125130

126131
/// <summary>
127132
/// Initialize execution state with given resolved binding information.
@@ -1257,7 +1262,7 @@ private void OnBeforeInitialUpdate()
12571262
// Remove us from the callback as the processing we're doing here is a one-time thing.
12581263
UnhookOnBeforeUpdate();
12591264

1260-
Profiler.BeginSample("InitialActionStateCheck");
1265+
k_InputInitialActionStateCheckMarker.Begin();
12611266

12621267
// Use current time as time of control state change.
12631268
var time = InputState.currentTime;
@@ -1315,7 +1320,7 @@ private void OnBeforeInitialUpdate()
13151320
}
13161321
manager.FireStateChangeNotifications();
13171322

1318-
Profiler.EndSample();
1323+
k_InputInitialActionStateCheckMarker.End();
13191324
}
13201325

13211326
// Called from InputManager when one of our state change monitors has fired.
@@ -1657,7 +1662,7 @@ private bool IsConflictingInput(ref TriggerState trigger, int actionIndex)
16571662
// Anything below here we want to avoid executing whenever we can.
16581663
Debug.Assert(actionState->mayNeedConflictResolution);
16591664

1660-
Profiler.BeginSample("InputActionResolveConflict");
1665+
k_InputActionResolveConflictMarker.Begin();
16611666

16621667
// We take a local copy of this value, so we can change it to use the starting control of composites
16631668
// for simpler conflict resolution (so composites always use the same value), but still report the actually
@@ -1691,7 +1696,7 @@ private bool IsConflictingInput(ref TriggerState trigger, int actionIndex)
16911696
if (actionStateControlIndex == kInvalidIndex)
16921697
{
16931698
actionState->magnitude = trigger.magnitude;
1694-
Profiler.EndSample();
1699+
k_InputActionResolveConflictMarker.End();
16951700
return false;
16961701
}
16971702

@@ -1720,7 +1725,7 @@ private bool IsConflictingInput(ref TriggerState trigger, int actionIndex)
17201725

17211726
// Keep recorded magnitude in action state up to date.
17221727
actionState->magnitude = trigger.magnitude;
1723-
Profiler.EndSample();
1728+
k_InputActionResolveConflictMarker.End();
17241729
return false;
17251730
}
17261731

@@ -1734,7 +1739,7 @@ private bool IsConflictingInput(ref TriggerState trigger, int actionIndex)
17341739
// actuation as we didn't have the highest actuation anyway.
17351740
if (!isControlCurrentlyDrivingTheAction)
17361741
{
1737-
Profiler.EndSample();
1742+
k_InputActionResolveConflictMarker.End();
17381743
////REVIEW: should we *count* actuations instead? (problem is that then we have to reliably determine when a control
17391744
//// first actuates; the current solution will occasionally run conflict resolution when it doesn't have to
17401745
//// but won't require the extra bookkeeping)
@@ -1749,7 +1754,7 @@ private bool IsConflictingInput(ref TriggerState trigger, int actionIndex)
17491754
{
17501755
// Keep recorded magnitude in action state up to date.
17511756
actionState->magnitude = trigger.magnitude;
1752-
Profiler.EndSample();
1757+
k_InputActionResolveConflictMarker.End();
17531758
return false;
17541759
}
17551760

@@ -1868,12 +1873,12 @@ private bool IsConflictingInput(ref TriggerState trigger, int actionIndex)
18681873
actionState->bindingIndex = bindingWithHighestActuation;
18691874
actionState->magnitude = highestActuationLevel;
18701875

1871-
Profiler.EndSample();
1876+
k_InputActionResolveConflictMarker.End();
18721877
return false;
18731878
}
18741879
}
18751880

1876-
Profiler.EndSample();
1881+
k_InputActionResolveConflictMarker.End();
18771882

18781883
// If we're not really effecting any change on the action, ignore the control state change.
18791884
// NOTE: We may be looking at a control here that points in a completely direction, for example, even
@@ -2520,7 +2525,7 @@ private void CallActionListeners(int actionIndex, InputActionMap actionMap, Inpu
25202525
m_ActionIndex = actionIndex,
25212526
};
25222527

2523-
Profiler.BeginSample("InputActionCallback");
2528+
k_InputActionCallbackMarker.Begin();
25242529

25252530
// Global callback goes first.
25262531
var action = context.action;
@@ -2552,7 +2557,7 @@ private void CallActionListeners(int actionIndex, InputActionMap actionMap, Inpu
25522557
// Run callbacks (if any) on action map.
25532558
DelegateHelpers.InvokeCallbacksSafe(ref callbacksOnMap, context, callbackName, actionMap);
25542559

2555-
Profiler.EndSample();
2560+
k_InputActionCallbackMarker.End();
25562561
}
25572562

25582563
private object GetActionOrNoneString(ref TriggerState trigger)

Packages/com.unity.inputsystem/InputSystem/Devices/Touchscreen.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using UnityEngine.InputSystem.Layouts;
77
using UnityEngine.InputSystem.LowLevel;
88
using UnityEngine.InputSystem.Utilities;
9-
using UnityEngine.Profiling;
9+
using UnityEngine.InputSystem.Profiler;
1010

1111
////TODO: property that tells whether a Touchscreen is multi-touch capable
1212

@@ -521,6 +521,10 @@ public class Touchscreen : Pointer, IInputStateCallbackReceiver, IEventMerger, I
521521
/// </remarks>
522522
public ReadOnlyArray<TouchControl> touches { get; protected set; }
523523

524+
525+
static readonly InputProfilerMarker k_TouchscreenUpdateMarker = new InputProfilerMarker("Touchscreen.OnNextUpdate");
526+
static readonly InputProfilerMarker k_TouchAllocateMarker = new InputProfilerMarker("TouchAllocate");
527+
524528
protected TouchControl[] touchControlArray
525529
{
526530
get => touches.m_Array;
@@ -607,7 +611,7 @@ protected override void FinishSetup()
607611

608612
protected new unsafe void OnNextUpdate()
609613
{
610-
Profiler.BeginSample("Touchscreen.OnNextUpdate");
614+
k_TouchscreenUpdateMarker.Begin();
611615

612616
////TODO: early out and skip crawling through touches if we didn't change state in the last update
613617
//// (also obsoletes the need for the if() check below)
@@ -634,7 +638,7 @@ protected override void FinishSetup()
634638
if (primaryTouchState->tapCount > 0 && InputState.currentTime >= primaryTouchState->startTime + s_TapTime + s_TapDelayTime)
635639
InputState.Change(primaryTouch.tapCount, (byte)0);
636640

637-
Profiler.EndSample();
641+
k_TouchscreenUpdateMarker.End();
638642
}
639643

640644
/// <summary>
@@ -657,7 +661,7 @@ protected override void FinishSetup()
657661
return;
658662
}
659663

660-
Profiler.BeginSample("TouchAllocate");
664+
k_TouchAllocateMarker.Begin();
661665

662666
// For performance reasons, we read memory here directly rather than going through
663667
// ReadValue() of the individual TouchControl children. This means that Touchscreen,
@@ -820,15 +824,15 @@ protected override void FinishSetup()
820824
InputState.Change(touches[i], ref newTouchState, eventPtr: eventPtr);
821825
}
822826

823-
Profiler.EndSample();
827+
k_TouchAllocateMarker.End();
824828
return;
825829
}
826830
}
827831

828832
// Couldn't find an entry. Either it was a touch that we previously ran out of available
829833
// entries for or it's an event sent out of sequence. Ignore the touch to be consistent.
830834

831-
Profiler.EndSample();
835+
k_TouchAllocateMarker.End();
832836
return;
833837
}
834838

@@ -863,7 +867,7 @@ protected override void FinishSetup()
863867

864868
InputState.Change(touches[i], ref newTouchState, eventPtr: eventPtr);
865869

866-
Profiler.EndSample();
870+
k_TouchAllocateMarker.End();
867871
return;
868872
}
869873
}
@@ -873,7 +877,7 @@ protected override void FinishSetup()
873877
// NOTE: Getting here means we're having fewer touch entries than the number of concurrent touches supported
874878
// by the backend (or someone is simply sending us nonsense data).
875879

876-
Profiler.EndSample();
880+
k_TouchAllocateMarker.End();
877881
}
878882

879883
void IInputStateCallbackReceiver.OnNextUpdate()

Packages/com.unity.inputsystem/InputSystem/Editor/Internal/InputControlTreeView.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Linq;
55
using UnityEditor.IMGUI.Controls;
66
using UnityEngine.InputSystem.LowLevel;
7-
using UnityEngine.Profiling;
7+
using UnityEngine.InputSystem.Profiler;
88

99
////TODO: make control values editable (create state events from UI and pump them into the system)
1010

@@ -23,6 +23,8 @@ internal class InputControlTreeView : TreeView
2323
public byte[][] multipleStateBuffers;
2424
public bool showDifferentOnly;
2525

26+
static readonly InputProfilerMarker k_InputBuildControlTreeMarker = new InputProfilerMarker("BuildControlTree");
27+
2628
public static InputControlTreeView Create(InputControl rootControl, int numValueColumns, ref TreeViewState treeState, ref MultiColumnHeaderState headerState)
2729
{
2830
if (treeState == null)
@@ -136,14 +138,14 @@ private InputControlTreeView(InputControl root, TreeViewState state, MultiColumn
136138

137139
protected override TreeViewItem BuildRoot()
138140
{
139-
Profiler.BeginSample("BuildControlTree");
141+
k_InputBuildControlTreeMarker.Begin();
140142

141143
var id = 1;
142144

143145
// Build tree from control down the control hierarchy.
144146
var rootItem = BuildControlTreeRecursive(m_RootControl, 0, ref id);
145147

146-
Profiler.EndSample();
148+
k_InputBuildControlTreeMarker.End();
147149

148150
// Wrap root control in invisible item required by TreeView.
149151
return new TreeViewItem

Packages/com.unity.inputsystem/InputSystem/Editor/Internal/InputEventTreeView.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using UnityEditor.IMGUI.Controls;
55
using UnityEngine.InputSystem.LowLevel;
66
using UnityEditor;
7-
using UnityEngine.Profiling;
7+
using UnityEngine.InputSystem.Profiler;
88

99
////FIXME: this performs horribly; the constant rebuilding on every single event makes the debug view super slow when device is noisy
1010

@@ -23,6 +23,7 @@ internal class InputEventTreeView : TreeView
2323
{
2424
private readonly InputEventTrace m_EventTrace;
2525
private readonly InputControl m_RootControl;
26+
private static readonly InputProfilerMarker k_InputEventTreeBuildRootMarker = new InputProfilerMarker("InputEventTreeView.BuildRoot");
2627

2728
private enum ColumnId
2829
{
@@ -177,7 +178,7 @@ private void PopUpStateWindow(InputEventPtr eventPtr)
177178

178179
protected override TreeViewItem BuildRoot()
179180
{
180-
Profiler.BeginSample("InputEventTreeView.BuildRoot");
181+
k_InputEventTreeBuildRootMarker.Begin();
181182

182183
var root = new TreeViewItem
183184
{
@@ -217,7 +218,7 @@ protected override TreeViewItem BuildRoot()
217218
root.children.Reverse();
218219
}
219220

220-
Profiler.EndSample();
221+
k_InputEventTreeBuildRootMarker.End();
221222
return root;
222223
}
223224

Packages/com.unity.inputsystem/InputSystem/Events/InputEventTrace.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Unity.Collections;
77
using Unity.Collections.LowLevel.Unsafe;
88
using UnityEngine.InputSystem.Layouts;
9-
using UnityEngine.Profiling;
9+
using UnityEngine.InputSystem.Profiler;
1010

1111
namespace UnityEngine.InputSystem.LowLevel
1212
{
@@ -27,6 +27,7 @@ namespace UnityEngine.InputSystem.LowLevel
2727
public sealed unsafe class InputEventTrace : IDisposable, IEnumerable<InputEventPtr>
2828
{
2929
private const int kDefaultBufferSize = 1024 * 1024;
30+
private static readonly InputProfilerMarker k_InputEvenTraceMarker = new InputProfilerMarker("InputEventTrace");
3031

3132
/// <summary>
3233
/// If <see name="recordFrameMarkers"/> is enabled, an <see cref="InputEvent"/> with this <see cref="FourCC"/>
@@ -811,7 +812,7 @@ private void OnInputEvent(InputEventPtr inputEvent, InputDevice device)
811812
if (bytesNeeded > m_MaxEventBufferSize)
812813
return;
813814

814-
Profiler.BeginSample("InputEventTrace");
815+
k_InputEvenTraceMarker.Begin();
815816

816817
if (m_EventBufferTail == default)
817818
{
@@ -837,7 +838,7 @@ private void OnInputEvent(InputEventPtr inputEvent, InputDevice device)
837838

838839
if (newBufferSize < bytesNeeded)
839840
{
840-
Profiler.EndSample();
841+
k_InputEvenTraceMarker.End();
841842
return;
842843
}
843844

@@ -936,7 +937,7 @@ private void OnInputEvent(InputEventPtr inputEvent, InputDevice device)
936937
DelegateHelpers.InvokeCallbacksSafe(ref m_EventListeners, new InputEventPtr((InputEvent*)buffer),
937938
"InputEventTrace.onEvent");
938939

939-
Profiler.EndSample();
940+
k_InputEvenTraceMarker.End();
940941
}
941942

942943
private class Enumerator : IEnumerator<InputEventPtr>

0 commit comments

Comments
 (0)