Class Profiler
- Namespace
- Stride.Core.Diagnostics
- Assembly
- Stride.Core.dll
High level CPU Profiler. For usage see remarks.
public static class Profiler
- Inheritance
-
Profiler
Remarks
This class is a lightweight profiler that can log detailed KPI (Key Performance Indicators) of an application.
To use it, simply enclose in a using
code the section of code you want to profile:
public static readonly ProfilingKey GameInitialization = new ProfilingKey("Game", "Initialization");
// This will log a 'Begin' profiling event.
using (var profile = Profiler.Begin(GameInitialization))
{
// Long running code here...
// You can log 'Mark' profiling event
profile.Mark("CriticalPart");
// Adds an attribute that will be logged in End event
profile.SetAttribute("ModelCount", modelCount);
} // here a 'End' profiling event will be issued.
By default, the profiler is not enabled, so there is a minimum performance impact leaving it in the code (it has the costs of a lock and a dictionary lookup). It doesn't measure anything and doesn't produce any KPI.
To enable a particular profiler (before using Begin(ProfilingKey, string) method):
Profiler.Enable(GameInitialization);
To enable all profilers, use Profiler.Enable()
method.
When the profiler is enabled, it is logged using the logging system through the standard Logger infrastructure. The logger module name used is "Profile." concatenates with the name of the profile.
Note also that when profiling, it is possible to attach some property values (counters, indicators...etc.) to a profiler state. This property values will be displayed along the standard profiler state. You can use SetAttribute(string, object) to attach a property value to a ProfilingState.
Fields
GpuTimestampFrequencyRatio
public static double GpuTimestampFrequencyRatio
Field Value
Methods
AppendTime(StringBuilder, long, long)
Append the provided time properly formated at the end of the string.
tickFrequency
is used to convert the ticks into time.
If tickFrequency
is 0 then Frequency is used to perform the calculation.
public static void AppendTime(StringBuilder builder, long accumulatedTicks, long tickFrequency = 0)
Parameters
builder
StringBuilderaccumulatedTicks
longtickFrequency
long
Begin(ProfilingKey, string)
Creates a profiler with the specified key. The returned object must be disposed at the end of the section being profiled. See remarks.
public static ProfilingState Begin(ProfilingKey profilingKey, string text = null)
Parameters
profilingKey
ProfilingKeyThe profile key.
text
stringThe text to log with the profile.
Returns
- ProfilingState
A profiler state.
Remarks
It is recommended to call this method with using (var profile = Profiler.Profile(...))
in order to make sure that the Dispose() method will be called on the
ProfilingState returned object.
Begin(ProfilingKey, string, ProfilingCustomValue, ProfilingCustomValue?, ProfilingCustomValue?, ProfilingCustomValue?)
Creates a profiler with the specified key. The returned object must be disposed at the end of the section being profiled. See remarks.
public static ProfilingState Begin(ProfilingKey profilingKey, string textFormat, ProfilingCustomValue value0, ProfilingCustomValue? value1 = null, ProfilingCustomValue? value2 = null, ProfilingCustomValue? value3 = null)
Parameters
profilingKey
ProfilingKeyThe profile key.
textFormat
stringThe text to format.
value0
ProfilingCustomValueFirst value (can be int, float, long or double).
value1
ProfilingCustomValue?Second value (can be int, float, long or double).
value2
ProfilingCustomValue?Third value (can be int, float, long or double).
value3
ProfilingCustomValue?Fourth value (can be int, float, long or double).
Returns
- ProfilingState
A profiler state.
Remarks
It is recommended to call this method with using (var profile = Profiler.Profile(...))
in order to make sure that the Dispose() method will be called on the
ProfilingState returned object.
Begin(ProfilingKey, string, params object[])
Creates a profiler with the specified key. The returned object must be disposed at the end of the section being profiled. See remarks.
public static ProfilingState Begin(ProfilingKey profilingKey, string textFormat, params object[] textFormatArguments)
Parameters
profilingKey
ProfilingKeyThe profile key.
textFormat
stringThe text to format.
textFormatArguments
object[]The text format arguments.
Returns
- ProfilingState
A profiler state.
Remarks
It is recommended to call this method with using (var profile = Profiler.Profile(...))
in order to make sure that the Dispose() method will be called on the
ProfilingState returned object.
Disable(ProfilingKey)
Disables the specified profiler.
public static void Disable(ProfilingKey profilingKey)
Parameters
profilingKey
ProfilingKeyThe profile key.
DisableAll()
Disable all profilers.
public static void DisableAll()
Enable(ProfilingKey)
Enables the specified profiler.
public static void Enable(ProfilingKey profilingKey)
Parameters
profilingKey
ProfilingKeyThe profile key.
EnableAll()
Enables all profilers.
public static void EnableAll()
GetEvents(ProfilingEventType, bool)
Retrieve the selected type of events and returns the number of elapsed frames.
public static FastList<ProfilingEvent> GetEvents(ProfilingEventType eventType, bool clearOtherEventTypes = true)
Parameters
eventType
ProfilingEventTypeThe type of events to retrieve
clearOtherEventTypes
boolif true, also clears the event types to avoid event over-accumulation.
Returns
- FastList<ProfilingEvent>
The profiling events.
IsEnabled(ProfilingKey)
Enables the specified profiler.
public static bool IsEnabled(ProfilingKey profilingKey)
Parameters
profilingKey
ProfilingKeyThe profile key.
Returns
New(ProfilingKey)
Creates a profiler with the specified name. The returned object must be disposed at the end of the section being profiled. See remarks.
public static ProfilingState New(ProfilingKey profilingKey)
Parameters
profilingKey
ProfilingKeyThe profile key.
Returns
- ProfilingState
A profiler state.
Remarks
It is recommended to call this method with using (var profile = Profiler.Profile(...))
in order to make sure that the Dispose() method will be called on the
ProfilingState returned object.
ProcessEvent(ref ProfilingEvent, ProfilingEventType)
public static void ProcessEvent(ref ProfilingEvent profilingEvent, ProfilingEventType eventType)
Parameters
profilingEvent
ProfilingEventeventType
ProfilingEventType
Reset()
Resets the id counter to zero and disable all registered profiles.
public static void Reset()