UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StatsMisc.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "CoreTypes.h"
7#include "HAL/PlatformTime.h"
10#include "Misc/Build.h"
12
18{
19public:
22 : StartTime(bInEnabled ? FPlatformTime::Seconds() : 0.0)
23 , Seconds(InSeconds)
24 , bEnabled(bInEnabled)
25 , RecursionDepth(nullptr)
26 {}
29 : StartTime(FPlatformTime::Seconds())
30 , Seconds(InSeconds)
31 , bEnabled(*InRecursionDepth == 0)
32 , RecursionDepth(InRecursionDepth)
33 {
34 (*RecursionDepth)++;
35 }
38 {
39 if (bEnabled)
40 {
41 Seconds += FPlatformTime::Seconds() - StartTime;
42 }
43
44 if (RecursionDepth)
45 {
46 (*RecursionDepth)--;
47 }
48 }
49private:
51 double StartTime;
53 double& Seconds;
55 bool bEnabled;
57 int32* RecursionDepth;
58};
59
61#define SCOPE_SECONDS_COUNTER_BASE(Seconds) \
62 FSimpleScopeSecondsCounter SecondsCount_##Seconds(Seconds);
63
64#define SCOPE_SECONDS_COUNTER_RECURSION_SAFE_BASE(Seconds) \
65 static int32 SecondsCount_##Seconds##_RecursionCounter = 0; \
66 FSimpleScopeSecondsCounter SecondsCount_##Seconds(Seconds, &SecondsCount_##Seconds##_RecursionCounter);
67
68#if STATS
69#define SCOPE_SECONDS_COUNTER(Seconds) SCOPE_SECONDS_COUNTER_BASE(Seconds)
70#define SCOPE_SECONDS_COUNTER_RECURSION_SAFE(Seconds) SCOPE_SECONDS_COUNTER_RECURSION_SAFE_BASE(Seconds)
71#else
72#define SCOPE_SECONDS_COUNTER(Seconds)
73#define SCOPE_SECONDS_COUNTER_RECURSION_SAFE(Seconds)
74#endif
75
76
84{
85#if STATS
87 : Time(0.0)
88 , ScopeCounter(0)
89 {
90 }
91
92 double GetTime() const { return Time; }
93 void ClearTime() { check(ScopeCounter == 0); Time = 0.0; }
94private:
95 double Time;
97
98 friend class FSecondsCounterScope;
99#else //STATS
100public:
101 static double GetTime() { return 0.0; }
102 static void ClearTime() {}
103#endif //STATS
104};
105
110{
111#if STATS
112public:
114 : Data(InData)
115 , StartTime(-1.f)
116 {
117 if (Data.ScopeCounter == 0)
118 {
119 StartTime = FPlatformTime::Seconds();
120 }
121 ++InData.ScopeCounter;
122 }
123
125 {
126 --Data.ScopeCounter;
127 if (Data.ScopeCounter == 0)
128 {
129 checkf(StartTime >= 0.f, TEXT("Counter is corrupt! Thinks it started before epoch"));
130 Data.Time += (FPlatformTime::Seconds() - StartTime);
131 }
132 }
133
134private:
136 double StartTime;
137#else //STATS
138public:
140#endif //STATS
141};
142
143
146
182
198
199#define SCOPE_LOG_TIME(Name,CumulativePtr) \
200 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(true, Name, CumulativePtr);
201
202#define SCOPE_LOG_TIME_IN_SECONDS(Name,CumulativePtr) \
203 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(true, Name, CumulativePtr, FScopeLogTime::ScopeLog_Seconds);
204
205#define SCOPE_LOG_TIME_FUNC() \
206 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(true, __FUNCTION__);
207
208#define SCOPE_LOG_TIME_FUNC_WITH_GLOBAL(CumulativePtr) \
209 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(true, __FUNCTION__,CumulativePtr);
210
211#define CONDITIONAL_SCOPE_LOG_TIME(Condition, Name,CumulativePtr) \
212 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(Condition, Name, CumulativePtr);
213
214#define CONDITIONAL_SCOPE_LOG_TIME_IN_SECONDS(Condition, Name,CumulativePtr) \
215 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(Condition, Name, CumulativePtr, FScopeLogTime::ScopeLog_Seconds);
216
217#define CONDITIONAL_SCOPE_LOG_TIME_FUNC(Condition) \
218 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(Condition, __FUNCTION__);
219
220#define CONDITIONAL_SCOPE_LOG_TIME_FUNC_WITH_GLOBAL(Condition, CumulativePtr) \
221 FConditionalScopeLogTime PREPROCESSOR_JOIN(ScopeLogTime,__LINE__)(Condition, __FUNCTION__,CumulativePtr);
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::WIDECHAR WIDECHAR
A wide character. Normally a signed type.
Definition Platform.h:1133
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
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
return true
Definition ExternalRpcRegistry.cpp:601
TKeyValuePair< double, uint32 > FTotalTimeAndCount
Definition StatsMisc.h:145
Definition StatsMisc.h:110
FSecondsCounterScope(FSecondsCounterData &InData)
Definition StatsMisc.h:139
Definition StatsMisc.h:18
FSimpleScopeSecondsCounter(double &InSeconds, bool bInEnabled=true)
Definition StatsMisc.h:21
~FSimpleScopeSecondsCounter()
Definition StatsMisc.h:37
FSimpleScopeSecondsCounter(double &InSeconds, int32 *InRecursionDepth)
Definition StatsMisc.h:28
Definition AndroidPlatformTime.h:18
static double Seconds()
Definition AndroidPlatformTime.h:20
Definition StatsMisc.h:151
EScopeLogTimeUnits
Definition StatsMisc.h:153
@ ScopeLog_Milliseconds
Definition StatsMisc.h:155
@ ScopeLog_Seconds
Definition StatsMisc.h:156
@ ScopeLog_DontLog
Definition StatsMisc.h:154
CORE_API ~FConditionalScopeLogTime()
Definition StatsMisc.cpp:22
EScopeLogTimeUnits Units
Definition StatsMisc.h:180
const double StartTime
Definition StatsMisc.h:177
FString GetDisplayUnitsString() const
Definition StatsMisc.cpp:53
FTotalTimeAndCount * Cumulative
Definition StatsMisc.h:179
double GetDisplayScopedTime(double InScopedTime) const
Definition StatsMisc.cpp:43
const FString Name
Definition StatsMisc.h:178
Definition StatsMisc.h:187
FScopeLogTime(const WIDECHAR *InName, FTotalTimeAndCount *InCumulative=nullptr, EScopeLogTimeUnits InUnits=ScopeLog_Milliseconds)
Definition StatsMisc.h:188
FScopeLogTime(const ANSICHAR *InName, FTotalTimeAndCount *InCumulative=nullptr, EScopeLogTimeUnits InUnits=ScopeLog_Milliseconds)
Definition StatsMisc.h:193
Definition StatsMisc.h:84
static double GetTime()
Definition StatsMisc.h:101
static void ClearTime()
Definition StatsMisc.h:102
Definition UnrealTemplate.h:462