Skip to content

Commit 5429203

Browse files
committed
Added basic profiling counters
1 parent ba2588d commit 5429203

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

Packages/com.unity.inputsystem/InputSystem/Editor/Internal/ProfilerModule.cs renamed to Packages/com.unity.inputsystem/InputSystem/Editor/Internal/InputSystemProfilerModule.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ void ReloadData()
7474
var selectedFrameIndex = System.Convert.ToInt32(ProfilerWindow.selectedFrameIndex);
7575

7676
var eventCount = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndex,
77-
InputStatistics.Category.Name, InputStatistics.kEventCountName);
77+
InputStatistics.Category.Name, InputStatistics.EventCountName);
7878
var eventSizeBytes = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndex,
79-
InputStatistics.Category.Name, InputStatistics.kEventSizeName);
79+
InputStatistics.Category.Name, InputStatistics.EventSizeName);
8080
var averageLatency = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndex,
81-
InputStatistics.Category.Name, InputStatistics.kAverageLatencyName);
81+
InputStatistics.Category.Name, InputStatistics.AverageLatencyName);
8282
var maxLatency = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndex,
83-
InputStatistics.Category.Name, InputStatistics.kMaxLatencyName);
83+
InputStatistics.Category.Name, InputStatistics.MaxLatencyName);
8484
var eventProcessingTime = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndex,
85-
InputStatistics.Category.Name, InputStatistics.kEventProcessingTimeName);
85+
InputStatistics.Category.Name, InputStatistics.EventProcessingTimeName);
8686

87-
m_EventCountLabel.text = $"{InputStatistics.kEventCountName}: {eventCount}";
88-
m_EventSizeLabel.text = $"{InputStatistics.kEventSizeName}: {eventSizeBytes}";
89-
m_AverageLatencyLabel.text = $"{InputStatistics.kAverageLatencyName}: {averageLatency}";
90-
m_MaxLatencyLabel.text = $"{InputStatistics.kMaxLatencyName}: {maxLatency}";
91-
m_EventProcessingTimeLabel.text = $"{InputStatistics.kEventProcessingTimeName}: {eventProcessingTime}";
87+
m_EventCountLabel.text = $"{InputStatistics.EventCountName}: {eventCount}";
88+
m_EventSizeLabel.text = $"{InputStatistics.EventSizeName}: {eventSizeBytes}";
89+
m_AverageLatencyLabel.text = $"{InputStatistics.AverageLatencyName}: {averageLatency}";
90+
m_MaxLatencyLabel.text = $"{InputStatistics.MaxLatencyName}: {maxLatency}";
91+
m_EventProcessingTimeLabel.text = $"{InputStatistics.EventProcessingTimeName}: {eventProcessingTime}";
9292
}
9393

9494
void OnSelectedFrameIndexChanged(long selectedFrameIndex)
@@ -99,11 +99,11 @@ void OnSelectedFrameIndexChanged(long selectedFrameIndex)
9999

100100
private static readonly ProfilerCounterDescriptor[] Counters = new ProfilerCounterDescriptor[]
101101
{
102-
new (InputStatistics.kEventCountName, InputStatistics.Category),
103-
new (InputStatistics.kEventSizeName, InputStatistics.Category),
104-
new (InputStatistics.kAverageLatencyName, InputStatistics.Category),
105-
new (InputStatistics.kMaxLatencyName, InputStatistics.Category),
106-
new (InputStatistics.kEventProcessingTimeName, InputStatistics.Category),
102+
new (InputStatistics.EventCountName, InputStatistics.Category),
103+
new (InputStatistics.EventSizeName, InputStatistics.Category),
104+
new (InputStatistics.AverageLatencyName, InputStatistics.Category),
105+
new (InputStatistics.MaxLatencyName, InputStatistics.Category),
106+
new (InputStatistics.EventProcessingTimeName, InputStatistics.Category),
107107
};
108108

109109
public InputSystemProfilerModule()
File renamed without changes.

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,7 +3204,10 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
32043204
}
32053205

32063206
var processingStartTime = Stopwatch.GetTimestamp();
3207+
var totalEventCount = 0;
3208+
var totalEventSizeBytes = 0;
32073209
var totalEventLag = 0.0;
3210+
var maxEventLag = 0.0;
32083211

32093212
#if UNITY_EDITOR
32103213
var isPlaying = gameIsPlaying;
@@ -3465,9 +3468,14 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
34653468

34663469
// Update metrics.
34673470
if (currentEventTimeInternal <= currentTime)
3468-
totalEventLag += currentTime - currentEventTimeInternal;
3469-
++m_Metrics.totalEventCount;
3470-
m_Metrics.totalEventBytes += (int)currentEventReadPtr->sizeInBytes;
3471+
{
3472+
var lag = currentTime - currentEventTimeInternal;
3473+
totalEventLag += lag;
3474+
if (lag > maxEventLag)
3475+
maxEventLag = lag;
3476+
}
3477+
++totalEventCount;
3478+
totalEventSizeBytes += (int)currentEventReadPtr->sizeInBytes;
34713479

34723480
// Process.
34733481
switch (currentEventType)
@@ -3621,12 +3629,23 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
36213629
break;
36223630
}
36233631

3624-
m_Metrics.totalEventProcessingTime +=
3632+
ResetCurrentProcessedEventBytesForDevices();
3633+
3634+
// Update metrics (exposed via analytics and debugger)
3635+
var eventProcessingTime =
36253636
((double)(Stopwatch.GetTimestamp() - processingStartTime)) / Stopwatch.Frequency;
3637+
m_Metrics.totalEventCount += totalEventCount;
3638+
m_Metrics.totalEventBytes += totalEventSizeBytes;
3639+
m_Metrics.totalEventProcessingTime += eventProcessingTime;
36263640
m_Metrics.totalEventLagTime += totalEventLag;
3627-
3628-
ResetCurrentProcessedEventBytesForDevices();
3629-
3641+
3642+
// Profiler counters
3643+
InputStatistics.EventCount.Value += totalEventCount;
3644+
InputStatistics.EventSize.Value += totalEventSizeBytes;
3645+
InputStatistics.AverageLatency.Value += ((totalEventLag / totalEventCount) * 1e9);
3646+
InputStatistics.MaxLatency.Value += (maxEventLag * 1e9);
3647+
InputStatistics.EventProcessingTime.Value += eventProcessingTime * 1e9; // TODO Possible to replace Stopwatch with marker somehow?
3648+
36303649
m_InputEventStream.Close(ref eventBuffer);
36313650
}
36323651
catch (Exception)

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,49 @@ namespace UnityEngine.InputSystem
55
/// <summary>
66
/// Input Statistics for Unity Profiler integration.
77
/// </summary>
8-
internal class InputStatistics
8+
internal static class InputStatistics
99
{
1010
public static readonly ProfilerCategory Category = ProfilerCategory.Input;
1111

12-
public const string kEventCountName = "Input Event Count";
13-
public const string kEventSizeName = "Input Event Size";
14-
public const string kAverageLatencyName = "Average Latency";
15-
public const string kMaxLatencyName = "Max Latency";
16-
public const string kEventProcessingTimeName = "Event Processing Time";
12+
public const string EventCountName = "Input Event Count";
13+
public const string EventSizeName = "Input Event Size";
14+
public const string AverageLatencyName = "Average Latency";
15+
public const string MaxLatencyName = "Max Latency";
16+
public const string EventProcessingTimeName = "Event Processing Time";
1717

1818
/// <summary>
1919
/// Counter reflecting the number of input events.
2020
/// </summary>
2121
public static readonly ProfilerCounterValue<int> EventCount = new ProfilerCounterValue<int>(
22-
Category, kEventCountName, ProfilerMarkerDataUnit.Count,
22+
Category, EventCountName, ProfilerMarkerDataUnit.Count,
2323
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
2424

2525
/// <summary>
2626
/// Counter reflecting the accumulated input event size in bytes.
2727
/// </summary>
2828
public static readonly ProfilerCounterValue<int> EventSize = new ProfilerCounterValue<int>(
29-
Category, kEventSizeName, ProfilerMarkerDataUnit.Bytes,
29+
Category, EventSizeName, ProfilerMarkerDataUnit.Bytes,
3030
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
3131

3232
/// <summary>
3333
/// Counter value reflecting the average input latency.
3434
/// </summary>
35-
public static readonly ProfilerCounterValue<float> AverageLatency = new ProfilerCounterValue<float>(
36-
Category, kAverageLatencyName, ProfilerMarkerDataUnit.TimeNanoseconds,
35+
public static readonly ProfilerCounterValue<double> AverageLatency = new ProfilerCounterValue<double>(
36+
Category, AverageLatencyName, ProfilerMarkerDataUnit.TimeNanoseconds,
3737
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
3838

3939
/// <summary>
4040
/// Counter value reflecting the maximum input latency.
4141
/// </summary>
42-
public static readonly ProfilerCounterValue<float> MaxLatency = new ProfilerCounterValue<float>(
43-
Category, kMaxLatencyName, ProfilerMarkerDataUnit.TimeNanoseconds,
42+
public static readonly ProfilerCounterValue<double> MaxLatency = new ProfilerCounterValue<double>(
43+
Category, MaxLatencyName, ProfilerMarkerDataUnit.TimeNanoseconds,
4444
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
4545

4646
/// <summary>
4747
/// Counter value reflecting the accumulated event processing time (Update) during a rendering frame.
4848
/// </summary>
4949
public static readonly ProfilerCounterValue<double> EventProcessingTime = new ProfilerCounterValue<double>(
50-
Category, kEventProcessingTimeName, ProfilerMarkerDataUnit.TimeNanoseconds,
51-
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush
52-
);
50+
Category, EventProcessingTimeName, ProfilerMarkerDataUnit.TimeNanoseconds,
51+
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
5352
}
5453
}

0 commit comments

Comments
 (0)