UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StatsHierarchical.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
7#include "CoreTypes.h"
8#include "HAL/PlatformCrt.h"
9#include "HAL/PlatformTime.h"
11#include "Logging/MessageLog.h"
12#include "Misc/Build.h"
14#include "UObject/NameTypes.h"
15
16class FMessageLog;
18
27{
28public:
29
30 // Default constructor
32
33 // Returns the name of this element
34 CORE_API FName GetFName() const;
35
36 // Returns the name of this element as a FString
37 CORE_API FString GetName() const;
38
39 /*
40 Returns the invocation path of this element.
41 The path is a period-separated string of all of the nested
42 profiling scopes.
43 For example: main.MyClass::Method.Algo::Sort
44 */
45 CORE_API FString GetPath() const;
46
47 // Returns the number of invocations collected into this element
48 // @param bInclusive If true the invocations will contain the invocations used by child elements
49 CORE_API uint32 Num(bool bInclusive = false) const;
50
51 // Returns the total number of cycles recorded
52 // @param bInclusive If true the cycles will contain the cycles used by child elements
53 CORE_API uint32 TotalCycles(bool bInclusive = true) const;
54
55 // Returns the number of maximum cycles for this element (and children)
56 CORE_API uint32 MaxCycles(bool bInclusive = true) const;
57
58 // Returns the total number of seconds recorded
59 // @param bInclusive If true the time will contain the cycles used by child elements
60 CORE_API double TotalSeconds(bool bInclusive = true) const;
61
62 // Returns the average number of seconds recorded (total / num)
63 // @param bInclusive If true the time will contain the cycles used by child elements
64 CORE_API double AverageSeconds(bool bInclusive = true) const;
65
66 // Returns the contribution between 0.0 and 1.0 within the parent element.
67 // 1.0 means that 100% of the time of the parent element is spent in this child.
68 // @param bAgainstMaximum If true the ratio is expressed against the largest time in the tree
69 // @param bInclusive If true the time will contain the cycles used by child elements
70 CORE_API double Contribution(bool bAgainstMaximum = false, bool bInclusive = true) const;
71
72 // Returns all child elements
74
75protected:
76
78 FString Path;
81
82 // derived data (computed by UpdatePoseMeasurement)
88
89 // children of the tree
91
92 // returns the child or nullptr based on a given path
94
96
97 friend class FStatsHierarchical;
99};
100
101#if STATS
102
103// Used to declare a hierarchical counter. The information about all of the counters can
104// be retrieved by FStatsHierarhical::GetLastMeasurements.
105// Note: You need to call FStatsHierarhical::BeginMeasurements && FStatsHierarhical::EndMeasurements
106// for this to have an effect.
107#define DECLARE_SCOPE_HIERARCHICAL_COUNTER(CounterName) \
108 FStatsHierarchical::FScope PREPROCESSOR_JOIN(StatsHierarchicalScope, __LINE__)(#CounterName);
109#define DECLARE_SCOPE_HIERARCHICAL_COUNTER_FUNC() \
110 FStatsHierarchical::FScope PREPROCESSOR_JOIN(StatsHierarchicalScope, __LINE__)(__FUNCTION__);
111
112#else
113
114#define DECLARE_SCOPE_HIERARCHICAL_COUNTER(CounterName)
115#define DECLARE_SCOPE_HIERARCHICAL_COUNTER_FUNC()
116
117#endif
118
137{
138public:
139
140 // Helper class to create a local scope for profiling.
141 // Calls the static methods on FStatsHierarchical.
142 // Users should use the DECLARE_SCOPE_HIERARCHICAL_COUNTER macro instead.
143 struct FScope
144 {
146 {
147#if STATS
148 FStatsHierarchical::BeginMeasurement(InLabel);
149#endif
150 }
151
153 {
154#if STATS
155 FStatsHierarchical::EndMeasurement();
156#endif
157 }
158 };
159
160 // Enabled measurements / profiling
161 static CORE_API void BeginMeasurements();
162
163 // Returns true if measurements are enabled
164 static CORE_API bool IsEnabled();
165
172
173 // Returns the last recorded profiling tree
175
176 // Prints the results into a provided log
177 static CORE_API void DumpMeasurements(FMessageLog& Log, bool bSortByDuration = true);
178
179 // Returns the name to use for untracked time
181
182private:
183
184 // A single entry for profiling.
185 // This data is used internally only and should not
186 // be used directly.
187 struct FHierarchicalStatEntry
188 {
189 FHierarchicalStatEntry(const ANSICHAR * InLabel, uint32 InCycles)
190 :Label(InLabel)
191 , Cycles(InCycles)
192 {
193 }
194
195 FHierarchicalStatEntry()
196 :Label(nullptr)
197 , Cycles(0)
198 {
199 }
200
201 const ANSICHAR * Label;
202 uint32 Cycles;
203
204 friend class FStatsHierarchical;
205 };
206
211 static CORE_API void BeginMeasurement(const ANSICHAR * Label);
212
213 // Ends the last measurement
214 static CORE_API void EndMeasurement();
215
216 static CORE_API bool bEnabled;
218
220};
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Num
Definition MetalRHIPrivate.h:234
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition MessageLog.h:21
Definition NameTypes.h:617
Definition StatsHierarchical.h:137
static CORE_API bool IsEnabled()
Definition StatsHierarchical.cpp:290
static CORE_API FStatsTreeElement EndMeasurements(FStatsTreeElement MeasurementsToMerge=FStatsTreeElement(), bool bAddUntrackedElements=true)
Definition StatsHierarchical.cpp:303
static CORE_API void BeginMeasurements()
Definition StatsHierarchical.cpp:280
static CORE_API FStatsTreeElement GetLastMeasurements()
Definition StatsHierarchical.cpp:465
static CORE_API FName GetUntrackedTimeName()
Definition StatsHierarchical.cpp:524
static CORE_API void DumpMeasurements(FMessageLog &Log, bool bSortByDuration=true)
Definition StatsHierarchical.cpp:476
Definition StatsHierarchical.h:27
CORE_API double AverageSeconds(bool bInclusive=true) const
Definition StatsHierarchical.cpp:88
CORE_API const TArray< TSharedPtr< FStatsTreeElement > > & GetChildren() const
Definition StatsHierarchical.cpp:103
double RatioAgainstMaximumInclusive
Definition StatsHierarchical.h:86
CORE_API void UpdatePostMeasurement(double InCyclesPerTimerToRemove=0)
Definition StatsHierarchical.cpp:141
CORE_API FStatsTreeElement()
Definition StatsHierarchical.cpp:9
uint32 Cycles
Definition StatsHierarchical.h:80
FName Name
Definition StatsHierarchical.h:77
double RatioAgainstMaximumExclusive
Definition StatsHierarchical.h:87
CORE_API uint32 MaxCycles(bool bInclusive=true) const
Definition StatsHierarchical.cpp:67
CORE_API FStatsTreeElement * FindChild(const FString &InPath)
Definition StatsHierarchical.cpp:108
CORE_API double TotalSeconds(bool bInclusive=true) const
Definition StatsHierarchical.cpp:83
FString Path
Definition StatsHierarchical.h:78
double RatioAgainstTotalExclusive
Definition StatsHierarchical.h:85
friend class FStatsHierarchicalClient
Definition StatsHierarchical.h:98
CORE_API FString GetPath() const
Definition StatsHierarchical.cpp:32
CORE_API FString GetName() const
Definition StatsHierarchical.cpp:27
CORE_API double Contribution(bool bAgainstMaximum=false, bool bInclusive=true) const
Definition StatsHierarchical.cpp:94
TArray< TSharedPtr< FStatsTreeElement > > Children
Definition StatsHierarchical.h:90
uint32 Invocations
Definition StatsHierarchical.h:79
CORE_API uint32 TotalCycles(bool bInclusive=true) const
Definition StatsHierarchical.cpp:54
uint32 CyclesOfChildren
Definition StatsHierarchical.h:83
double RatioAgainstTotalInclusive
Definition StatsHierarchical.h:84
CORE_API FName GetFName() const
Definition StatsHierarchical.cpp:22
Definition Array.h:670
Definition StatsHierarchical.h:144
FScope(const ANSICHAR *InLabel)
Definition StatsHierarchical.h:145
~FScope()
Definition StatsHierarchical.h:152