5#include "Containers/Map.h"
8#include "Delegates/Delegate.h"
9#include "HAL/PlatformTime.h"
10#include "Misc/Build.h"
11#include "Misc/DateTime.h"
15 #define DO_TIMEGUARD 0
18#ifndef DO_LIGHTWEIGHT_TIMEGUARD
25DECLARE_DELEGATE_RetVal(
const FString, FTimerNameDelegate);
52 FORCEINLINE FTimeGuard(FTimerNameDelegate InNameDelegate,
const float InTargetMS = 0.0)
53 : Name(
nullptr), ObjectName(NAME_None)
55 NameDelegate = (bEnabled && IsInGameThread()) ? InNameDelegate :
nullptr;
57 if (NameDelegate.IsBound())
59 TargetTimeMS = (InTargetMS > 0) ? InTargetMS : FrameTimeThresholdMS;
60 StartTime = FPlatformTime::Seconds();
64 FORCEINLINE FTimeGuard(TCHAR
const* InName, FName InObjectName = NAME_None,
const float InTargetMS = 0.0)
65 : Name(
nullptr), ObjectName(NAME_None)
67 Name = (bEnabled && IsInGameThread()) ? InName :
nullptr;
71 ObjectName = InObjectName;
72 TargetTimeMS = (InTargetMS > 0) ? InTargetMS : FrameTimeThresholdMS;
73 StartTime = FPlatformTime::Seconds();
78
79
80 FORCEINLINE ~FTimeGuard()
84 double delta = (FPlatformTime::Seconds() - StartTime) * 1000;
86 if (delta > TargetTimeMS)
88 if ( ObjectName != NAME_None )
90 ReportHitch( *FString::Printf(TEXT(
"%s %s"), Name, *ObjectName.ToString()), delta,
true );
94 ReportHitch(Name, delta,
false);
98 else if (NameDelegate.IsBound())
100 double delta = (FPlatformTime::Seconds() - StartTime) * 1000;
102 if (delta > TargetTimeMS)
104 FString Temp = NameDelegate.Execute();
105 ReportHitch(*Temp, delta,
true);
111 static void ClearData();
112 static void SetEnabled(
bool InEnable);
113 static void SetFrameTimeThresholdMS(
float InTimeMS);
114 static void GetData(TMap<
const TCHAR*, FGuardInfo>& Dest);
119 static void ReportHitch(
const TCHAR* InName,
float TimeMS,
bool VolatileName );
120 static TMap<
const TCHAR*, FGuardInfo> HitchData;
121 static TSet<
const TCHAR *> VolatileNames;
122 static FCriticalSection ReportMutex;
123 static bool bEnabled;
124 static float FrameTimeThresholdMS;
131 FTimerNameDelegate NameDelegate;
136UE_DEPRECATED(4.21,
"FLightweightTimeGuard has been renamed to FTimeGuard.")
137typedef FTimeGuard FLightweightTimeGuard;
139#define SCOPE_TIME_GUARD(name)
140 FTimeGuard ANONYMOUS_VARIABLE(TimeGuard)(name);
142#define SCOPE_TIME_GUARD_MS(name, timeMs)
143 FTimeGuard ANONYMOUS_VARIABLE(TimeGuard)(name, NAME_None, timeMs);
145#define SCOPE_TIME_GUARD_NAMED(name, fname)
146 FTimeGuard ANONYMOUS_VARIABLE(TimeGuard)(name, fname);
148#define SCOPE_TIME_GUARD_NAMED_MS(name, fname, timems)
149 FTimeGuard ANONYMOUS_VARIABLE(TimeGuard)(name, fname, timems);
154#define SCOPE_TIME_GUARD_DELEGATE(inDelegate)
155 FTimeGuard ANONYMOUS_VARIABLE(TimeGuard)(inDelegate);
157#define SCOPE_TIME_GUARD_DELEGATE_MS(inDelegate, timems)
158 FTimeGuard ANONYMOUS_VARIABLE(TimeGuard)(inDelegate, timems);
160#define CLEAR_TIME_GUARDS FTimeGuard::ClearData
162#define ENABLE_TIME_GUARDS(bEnabled) FTimeGuard::SetEnabled(bEnabled)
166#define SCOPE_TIME_GUARD(name)
167#define SCOPE_TIME_GUARD_MS(name, timeMs)
168#define SCOPE_TIME_GUARD_NAMED(name, fname)
169#define SCOPE_TIME_GUARD_NAMED_MS(name, fname, timeMs)
170#define CLEAR_TIME_GUARDS()
171#define ENABLE_TIME_GUARDS(bEnabled)
179 #define LIGHTWEIGHT_TIME_GUARD_BEGIN( Name, ThresholdMS )
180 float PREPROCESSOR_JOIN(__TimeGuard_ThresholdMS_, Name) = ThresholdMS;
181 uint64 PREPROCESSOR_JOIN(__TimeGuard_StartCycles_, Name) = ( ThresholdMS > 0.0f
) ? FPlatformTime::Cycles64() : 0
;
183 #define LIGHTWEIGHT_TIME_GUARD_END( Name, NameStringCode )
184 if ( PREPROCESSOR_JOIN(__TimeGuard_ThresholdMS_, Name) > 0.0f
)
186 float PREPROCESSOR_JOIN(__TimeGuard_MSElapsed_,Name) = FPlatformTime::ToMilliseconds64( FPlatformTime::Cycles64() - PREPROCESSOR_JOIN(__TimeGuard_StartCycles_,Name) );
187 if ( PREPROCESSOR_JOIN(__TimeGuard_MSElapsed_,Name) > PREPROCESSOR_JOIN(__TimeGuard_ThresholdMS_, Name) )
189 FString ReportName = NameStringCode;
190 UE_LOG(LogCore, Warning, TEXT("LIGHTWEIGHT_TIME_GUARD: %s - %s took %.2fms!"), TEXT(#Name), *ReportName, PREPROCESSOR_JOIN(__TimeGuard_MSElapsed_,Name));
194 #define LIGHTWEIGHT_TIME_GUARD_BEGIN( Name, ThresholdMS )
195 #define LIGHTWEIGHT_TIME_GUARD_END( Name, NameStringCode )
#define DO_LIGHTWEIGHT_TIMEGUARD