Skip to content

Commit 1402022

Browse files
authored
CHANGE: Make sure all callbacks are instrumented with ProfilerMarkers instead of Profiler.Begin/EndSample. (#1992)
* Make sure all callbacks are instrumented with ProfilerMarkers instead of Profiler.Begin/EndSample. * Fix spacing and string consistency. * Match modifiers of local code.
1 parent d7b1b86 commit 1402022

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ internal unsafe class InputActionState : IInputStateChangeMonitor, ICloneable, I
127127
private static readonly ProfilerMarker k_InputInitialActionStateCheckMarker = new ProfilerMarker("InitialActionStateCheck");
128128
private static readonly ProfilerMarker k_InputActionResolveConflictMarker = new ProfilerMarker("InputActionResolveConflict");
129129
private static readonly ProfilerMarker k_InputActionCallbackMarker = new ProfilerMarker("InputActionCallback");
130+
private static readonly ProfilerMarker k_InputOnActionChangeMarker = new ProfilerMarker("InpustSystem.onActionChange");
131+
private static readonly ProfilerMarker k_InputOnDeviceChangeMarker = new ProfilerMarker("InpustSystem.onDeviceChange");
130132

131133
/// <summary>
132134
/// Initialize execution state with given resolved binding information.
@@ -2548,7 +2550,7 @@ private void CallActionListeners(int actionIndex, InputActionMap actionMap, Inpu
25482550
return;
25492551
}
25502552

2551-
DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionChange, action, change, "InputSystem.onActionChange");
2553+
DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionChange, action, change, k_InputOnActionChangeMarker, "InputSystem.onActionChange");
25522554
}
25532555

25542556
// Run callbacks (if any) directly on action.
@@ -4336,7 +4338,7 @@ internal static void NotifyListenersOfActionChange(InputActionChange change, obj
43364338
Debug.Assert(actionOrMapOrAsset is InputAction || (actionOrMapOrAsset as InputActionMap)?.m_SingletonAction == null,
43374339
"Must not send notifications for changes made to hidden action maps of singleton actions");
43384340

4339-
DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionChange, actionOrMapOrAsset, change, "onActionChange");
4341+
DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionChange, actionOrMapOrAsset, change, k_InputOnActionChangeMarker, "InputSystem.onActionChange");
43404342
if (change == InputActionChange.BoundControlsChanged)
43414343
DelegateHelpers.InvokeCallbacksSafe(ref s_GlobalState.onActionControlsChanged, actionOrMapOrAsset, "onActionControlsChange");
43424344
}

Packages/com.unity.inputsystem/InputSystem/InputManager.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ internal partial class InputManager
7171
static readonly ProfilerMarker k_InputRestoreDevicesAfterReloadMarker = new ProfilerMarker("InputManager.RestoreDevicesAfterDomainReload");
7272
static readonly ProfilerMarker k_InputRegisterCustomTypesMarker = new ProfilerMarker("InputManager.RegisterCustomTypes");
7373

74+
static readonly ProfilerMarker k_InputOnBeforeUpdateMarker = new ProfilerMarker("InputSystem.onBeforeUpdate");
75+
static readonly ProfilerMarker k_InputOnAfterUpdateMarker = new ProfilerMarker("InputSystem.onAfterUpdate");
76+
static readonly ProfilerMarker k_InputOnSettingsChangeMarker = new ProfilerMarker("InputSystem.onSettingsChange");
77+
static readonly ProfilerMarker k_InputOnDeviceSettingsChangeMarker = new ProfilerMarker("InputSystem.onDeviceSettingsChange");
78+
static readonly ProfilerMarker k_InputOnEventMarker = new ProfilerMarker("InputSystem.onEvent");
79+
static readonly ProfilerMarker k_InputOnLayoutChangeMarker = new ProfilerMarker("InputSystem.onLayoutChange");
80+
static readonly ProfilerMarker k_InputOnDeviceChangeMarker = new ProfilerMarker("InpustSystem.onDeviceChange");
81+
static readonly ProfilerMarker k_InputOnActionsChangeMarker = new ProfilerMarker("InpustSystem.onActionsChange");
82+
83+
7484
public InputMetrics metrics
7585
{
7686
get
@@ -600,7 +610,7 @@ private void PerformLayoutPostRegistration(InternedString layoutName, InlinedArr
600610

601611
// Let listeners know.
602612
var change = isReplacement ? InputControlLayoutChange.Replaced : InputControlLayoutChange.Added;
603-
DelegateHelpers.InvokeCallbacksSafe(ref m_LayoutChangeListeners, layoutName.ToString(), change, "InputSystem.onLayoutChange");
613+
DelegateHelpers.InvokeCallbacksSafe(ref m_LayoutChangeListeners, layoutName.ToString(), change, k_InputOnLayoutChangeMarker, "InputSystem.onLayoutChange");
604614
}
605615

606616
public void RegisterPrecompiledLayout<TDevice>(string metadata)
@@ -868,7 +878,7 @@ public void RemoveControlLayout(string name)
868878
//// remove those layouts, too
869879

870880
// Let listeners know.
871-
DelegateHelpers.InvokeCallbacksSafe(ref m_LayoutChangeListeners, name, InputControlLayoutChange.Removed, "InputSystem.onLayoutChange");
881+
DelegateHelpers.InvokeCallbacksSafe(ref m_LayoutChangeListeners, name, InputControlLayoutChange.Removed, k_InputOnLayoutChangeMarker, "InputSystem.onLayoutChange");
872882
}
873883

874884
public InputControlLayout TryLoadControlLayout(Type type)
@@ -1121,7 +1131,7 @@ private void NotifyUsageChanged(InputDevice device)
11211131
InputActionState.OnDeviceChange(device, InputDeviceChange.UsageChanged);
11221132

11231133
// Notify listeners.
1124-
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.UsageChanged, "InputSystem.onDeviceChange");
1134+
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.UsageChanged, k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
11251135

11261136
////REVIEW: This was for the XRController leftHand and rightHand getters but these do lookups dynamically now; remove?
11271137
// Usage may affect current device so update.
@@ -1287,7 +1297,7 @@ public void AddDevice(InputDevice device)
12871297
device.MakeCurrent();
12881298

12891299
// Notify listeners.
1290-
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.Added, "InputSystem.onDeviceChange");
1300+
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.Added, k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
12911301

12921302
// Request device to send us an initial state update.
12931303
if (device.enabled)
@@ -1442,7 +1452,7 @@ public void RemoveDevice(InputDevice device, bool keepOnListOfAvailableDevices =
14421452
device.NotifyRemoved();
14431453

14441454
// Let listeners know.
1445-
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.Removed, "InputSystem.onDeviceChange");
1455+
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.Removed, k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
14461456

14471457
// Try setting next device of same type as current
14481458
InputSystem.GetDevice(device.GetType())?.MakeCurrent();
@@ -1466,7 +1476,7 @@ public unsafe void ResetDevice(InputDevice device, bool alsoResetDontResetContro
14661476
// Trigger reset notification.
14671477
var change = isHardReset ? InputDeviceChange.HardReset : InputDeviceChange.SoftReset;
14681478
InputActionState.OnDeviceChange(device, change);
1469-
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, change, "onDeviceChange");
1479+
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, change, k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
14701480

14711481
// If the device implements its own reset, let it handle it.
14721482
if (!alsoResetDontResetControls && device is ICustomDeviceReset customReset)
@@ -1754,7 +1764,7 @@ public void EnableOrDisableDevice(InputDevice device, bool enable, DeviceDisable
17541764

17551765
// Let listeners know.
17561766
var deviceChange = enable ? InputDeviceChange.Enabled : InputDeviceChange.Disabled;
1757-
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, deviceChange, "InputSystem.onDeviceChange");
1767+
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, deviceChange, k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
17581768
}
17591769

17601770
private unsafe void QueueEvent(InputEvent* eventPtr)
@@ -2500,7 +2510,7 @@ private void OnNativeDeviceDiscovered(int deviceId, string deviceDescriptor)
25002510
AddDevice(device);
25012511

25022512
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners, device, InputDeviceChange.Reconnected,
2503-
"InputSystem.onDeviceChange");
2513+
k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
25042514
}
25052515
else
25062516
{
@@ -2709,7 +2719,7 @@ private void OnBeforeUpdate(InputUpdateType updateType)
27092719
}
27102720
}
27112721

2712-
DelegateHelpers.InvokeCallbacksSafe(ref m_BeforeUpdateListeners, "onBeforeUpdate");
2722+
DelegateHelpers.InvokeCallbacksSafe(ref m_BeforeUpdateListeners, k_InputOnBeforeUpdateMarker, "InputSystem.onBeforeUpdate");
27132723
}
27142724

27152725
/// <summary>
@@ -2824,14 +2834,14 @@ internal void ApplySettings()
28242834

28252835
// Let listeners know.
28262836
DelegateHelpers.InvokeCallbacksSafe(ref m_SettingsChangedListeners,
2827-
"InputSystem.onSettingsChange");
2837+
k_InputOnSettingsChangeMarker, "InputSystem.onSettingsChange");
28282838
}
28292839

28302840
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
28312841
internal void ApplyActions()
28322842
{
28332843
// Let listeners know.
2834-
DelegateHelpers.InvokeCallbacksSafe(ref m_ActionsChangedListeners, "InputSystem.onActionsChange");
2844+
DelegateHelpers.InvokeCallbacksSafe(ref m_ActionsChangedListeners, k_InputOnActionsChangeMarker, "InputSystem.onActionsChange");
28352845
}
28362846

28372847
#endif
@@ -3448,7 +3458,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
34483458
if (m_EventListeners.length > 0)
34493459
{
34503460
DelegateHelpers.InvokeCallbacksSafe(ref m_EventListeners,
3451-
new InputEventPtr(currentEventReadPtr), device, "InputSystem.onEvent");
3461+
new InputEventPtr(currentEventReadPtr), device, k_InputOnEventMarker, "InputSystem.onEvent");
34523462

34533463
// If a listener marks the event as handled, we don't process it further.
34543464
if (currentEventReadPtr->handled)
@@ -3590,7 +3600,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
35903600
ArrayHelpers.AppendWithCapacity(ref m_DisconnectedDevices,
35913601
ref m_DisconnectedDevicesCount, device);
35923602
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners,
3593-
device, InputDeviceChange.Disconnected, "InputSystem.onDeviceChange");
3603+
device, InputDeviceChange.Disconnected, k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
35943604
}
35953605

35963606
break;
@@ -3600,7 +3610,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
36003610
device.NotifyConfigurationChanged();
36013611
InputActionState.OnDeviceChange(device, InputDeviceChange.ConfigurationChanged);
36023612
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceChangeListeners,
3603-
device, InputDeviceChange.ConfigurationChanged, "InputSystem.onDeviceChange");
3613+
device, InputDeviceChange.ConfigurationChanged, k_InputOnDeviceChangeMarker, "InputSystem.onDeviceChange");
36043614
break;
36053615

36063616
case DeviceResetEvent.Type:
@@ -3716,7 +3726,7 @@ private void InvokeAfterUpdateCallback(InputUpdateType updateType)
37163726
return;
37173727

37183728
DelegateHelpers.InvokeCallbacksSafe(ref m_AfterUpdateListeners,
3719-
"InputSystem.onAfterUpdate");
3729+
k_InputOnAfterUpdateMarker, "InputSystem.onAfterUpdate");
37203730
}
37213731

37223732
private bool m_ShouldMakeCurrentlyUpdatingDeviceCurrent;
@@ -3897,7 +3907,7 @@ internal unsafe bool UpdateState(InputDevice device, InputUpdateType updateType,
38973907

38983908
// Notify listeners.
38993909
DelegateHelpers.InvokeCallbacksSafe(ref m_DeviceStateChangeListeners,
3900-
device, eventPtr, "InputSystem.onDeviceStateChange");
3910+
device, eventPtr, k_InputOnDeviceSettingsChangeMarker, "InputSystem.onDeviceStateChange");
39013911

39023912
// Now that we've committed the new state to memory, if any of the change
39033913
// monitors fired, let the associated actions know.

Packages/com.unity.inputsystem/InputSystem/Utilities/DelegateHelpers.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Unity.Profiling;
23

34
namespace UnityEngine.InputSystem.Utilities
45
{
@@ -7,11 +8,11 @@ internal static class DelegateHelpers
78
// InvokeCallbacksSafe protects both against the callback getting removed while being called
89
// and against exceptions being thrown by the callback.
910

10-
public static void InvokeCallbacksSafe(ref CallbackArray<Action> callbacks, string callbackName, object context = null)
11+
public static void InvokeCallbacksSafe(ref CallbackArray<Action> callbacks, ProfilerMarker marker, string callbackName, object context = null)
1112
{
1213
if (callbacks.length == 0)
1314
return;
14-
Profiling.Profiler.BeginSample(callbackName);
15+
marker.Begin();
1516
callbacks.LockForChanges();
1617
for (var i = 0; i < callbacks.length; ++i)
1718
{
@@ -29,7 +30,7 @@ public static void InvokeCallbacksSafe(ref CallbackArray<Action> callbacks, stri
2930
}
3031
}
3132
callbacks.UnlockForChanges();
32-
Profiling.Profiler.EndSample();
33+
marker.End();
3334
}
3435

3536
public static void InvokeCallbacksSafe<TValue>(ref CallbackArray<Action<TValue>> callbacks, TValue argument, string callbackName, object context = null)
@@ -57,11 +58,11 @@ public static void InvokeCallbacksSafe<TValue>(ref CallbackArray<Action<TValue>>
5758
Profiling.Profiler.EndSample();
5859
}
5960

60-
public static void InvokeCallbacksSafe<TValue1, TValue2>(ref CallbackArray<Action<TValue1, TValue2>> callbacks, TValue1 argument1, TValue2 argument2, string callbackName, object context = null)
61+
public static void InvokeCallbacksSafe<TValue1, TValue2>(ref CallbackArray<Action<TValue1, TValue2>> callbacks, TValue1 argument1, TValue2 argument2, ProfilerMarker marker, string callbackName, object context = null)
6162
{
6263
if (callbacks.length == 0)
6364
return;
64-
Profiling.Profiler.BeginSample(callbackName);
65+
marker.Begin();
6566
callbacks.LockForChanges();
6667
for (var i = 0; i < callbacks.length; ++i)
6768
{
@@ -79,7 +80,7 @@ public static void InvokeCallbacksSafe<TValue1, TValue2>(ref CallbackArray<Actio
7980
}
8081
}
8182
callbacks.UnlockForChanges();
82-
Profiling.Profiler.EndSample();
83+
marker.End();
8384
}
8485

8586
public static bool InvokeCallbacksSafe_AnyCallbackReturnsTrue<TValue1, TValue2>(ref CallbackArray<Func<TValue1, TValue2, bool>> callbacks,

0 commit comments

Comments
 (0)